<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.pyamsoft.pydroid.ui.version;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.pyamsoft.pydroid.BuildConfigChecker;
import com.pyamsoft.pydroid.ui.PYDroidInjector;
import com.pyamsoft.pydroid.ui.ads.AdvertisementActivity;
import com.pyamsoft.pydroid.util.AppUtil;
import com.pyamsoft.pydroid.version.VersionCheckPresenter;
import com.pyamsoft.pydroid.version.VersionCheckProvider;
import timber.log.Timber;

public abstract class VersionCheckActivity extends AdvertisementActivity
    implements VersionCheckProvider {

  @NonNull private static final String VERSION_CHECKED = "version_check_completed";
  @SuppressWarnings("WeakerAccess") VersionCheckPresenter presenter;
  boolean versionChecked;

  @CheckResult private boolean isVersionCheckEnabled() {
    // Always enabled for release builds
    return !BuildConfigChecker.getInstance().isDebugMode() || shouldCheckVersion();
  }

  @CheckResult protected boolean shouldCheckVersion() {
    return true;
  }

  @CallSuper @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    versionChecked =
        savedInstanceState != null && savedInstanceState.getBoolean(VERSION_CHECKED, false);

    PYDroidInjector.get().provideComponent().provideVersionCheckComponent().inject(this);
  }

  @CallSuper @Override protected void onStart() {
    super.onStart();
    presenter.bindView(null);

    if (isVersionCheckEnabled() && !versionChecked) {
      presenter.checkForUpdates(getPackageName(), getCurrentApplicationVersion(),
          new VersionCheckPresenter.UpdateCheckCallback() {
            @Override public void onVersionCheckFinished() {
              Timber.d("License check finished, mark");
              versionChecked = true;
            }

            @Override
            public void onUpdatedVersionFound(int oldVersionCode, int updatedVersionCode) {
              Timber.d("Updated version found. %d => %d", oldVersionCode, updatedVersionCode);
              AppUtil.guaranteeSingleDialogFragment(VersionCheckActivity.this,
                  VersionUpgradeDialog.newInstance(provideApplicationName(), oldVersionCode,
                      updatedVersionCode), VersionUpgradeDialog.TAG);
            }
          });
    }
  }

  @CallSuper @Override protected void onStop() {
    super.onStop();
    presenter.unbindView();
  }

  @Override protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(VERSION_CHECKED, versionChecked);
    super.onSaveInstanceState(outState);
  }
}<commit_msg>Find bugs<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.pyamsoft.pydroid.ui.version;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.pyamsoft.pydroid.BuildConfigChecker;
import com.pyamsoft.pydroid.ui.PYDroidInjector;
import com.pyamsoft.pydroid.ui.ads.AdvertisementActivity;
import com.pyamsoft.pydroid.util.AppUtil;
import com.pyamsoft.pydroid.version.VersionCheckPresenter;
import com.pyamsoft.pydroid.version.VersionCheckProvider;
import timber.log.Timber;

public abstract class VersionCheckActivity extends AdvertisementActivity
    implements VersionCheckProvider {

  @NonNull private static final String VERSION_CHECKED = "version_check_completed";
  @SuppressWarnings("WeakerAccess") VersionCheckPresenter presenter;
  boolean versionChecked;

  @CheckResult private boolean isVersionCheckEnabled() {
    // Always enabled for release builds
    return !BuildConfigChecker.getInstance().isDebugMode() || shouldCheckVersion();
  }

  @CheckResult protected boolean shouldCheckVersion() {
    return true;
  }

  @CallSuper @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    versionChecked =
        savedInstanceState != null && savedInstanceState.getBoolean(VERSION_CHECKED, false);

    PYDroidInjector.get().provideComponent().provideVersionCheckComponent().inject(this);
  }

  @CallSuper @Override protected void onStart() {
    super.onStart();
    presenter.bindView(null);

    if (!versionChecked && isVersionCheckEnabled()) {
      presenter.checkForUpdates(getPackageName(), getCurrentApplicationVersion(),
          new VersionCheckPresenter.UpdateCheckCallback() {
            @Override public void onVersionCheckFinished() {
              Timber.d("License check finished, mark");
              versionChecked = true;
            }

            @Override
            public void onUpdatedVersionFound(int oldVersionCode, int updatedVersionCode) {
              Timber.d("Updated version found. %d => %d", oldVersionCode, updatedVersionCode);
              AppUtil.guaranteeSingleDialogFragment(VersionCheckActivity.this,
                  VersionUpgradeDialog.newInstance(provideApplicationName(), oldVersionCode,
                      updatedVersionCode), VersionUpgradeDialog.TAG);
            }
          });
    }
  }

  @CallSuper @Override protected void onStop() {
    super.onStop();
    presenter.unbindView();
  }

  @Override protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(VERSION_CHECKED, versionChecked);
    super.onSaveInstanceState(outState);
  }
}
{'fraction_non_alphanumeric': 0.08743990384615384, 'fraction_numerical': 0.002403846153846154, 'mean_word_length': 4.050834597875569, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6275885', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1702, 'n_words': 437}