From f12364640078aa2ca263feb1a08abfe13a9e7624 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 27 Mar 2016 20:28:53 +0200 Subject: [PATCH] Move keepScreenOn and rotateScreen --- .../android/calculator/BaseActivity.java | 68 ++++++++++++++++--- .../calculator/CalculatorActivity.java | 42 ++---------- .../preferences/PreferencesActivity.java | 30 -------- .../calculator/wizard/WizardActivity.java | 9 --- 4 files changed, 65 insertions(+), 84 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java b/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java index f0251f9d..7e49a7b8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java @@ -12,10 +12,7 @@ import android.support.design.widget.FloatingActionButton; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.view.KeyEvent; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; +import android.view.*; import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; @@ -31,9 +28,13 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; +import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; +import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; +import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; import static org.solovyev.android.calculator.App.cast; +import static org.solovyev.android.calculator.Preferences.Gui.keepScreenOn; -public class BaseActivity extends AppCompatActivity { +public class BaseActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @Nonnull protected final Tabs tabs; @@ -69,6 +70,7 @@ public class BaseActivity extends AppCompatActivity { private Preferences.Gui.Mode mode = Preferences.Gui.Mode.engineer; @Nonnull private Language language = Languages.SYSTEM_LANGUAGE; + private boolean paused = true; public BaseActivity(@StringRes int titleId) { this(R.layout.activity_tabs, titleId); @@ -96,11 +98,6 @@ public class BaseActivity extends AppCompatActivity { } } - @Nonnull - public Preferences.Gui.Theme getActivityTheme() { - return theme; - } - @Nonnull public Preferences.Gui.Mode getActivityMode() { return mode; @@ -154,6 +151,11 @@ public class BaseActivity extends AppCompatActivity { languages.updateContextLocale(this, false); createView(); + + updateOrientation(); + updateKeepScreenOn(); + + preferences.registerOnSharedPreferenceChangeListener(this); } private void createView() { @@ -236,13 +238,27 @@ public class BaseActivity extends AppCompatActivity { @Override protected void onResume() { super.onResume(); + paused = false; if (!restartIfThemeChanged()) { restartIfLanguageChanged(); } } + private void updateKeepScreenOn() { + final Window window = getWindow(); + if (window == null) { + return; + } + if (keepScreenOn.getPreference(preferences)) { + window.addFlags(FLAG_KEEP_SCREEN_ON); + } else { + window.clearFlags(FLAG_KEEP_SCREEN_ON); + } + } + @Override protected void onPause() { + paused = true; tabs.onPause(); super.onPause(); } @@ -276,4 +292,36 @@ public class BaseActivity extends AppCompatActivity { } }); } + + @Override + protected void onDestroy() { + super.onDestroy(); + preferences.unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + if (Preferences.Gui.rotateScreen.isSameKey(key)) { + updateOrientation(); + } else if (Preferences.Gui.keepScreenOn.isSameKey(key)) { + updateKeepScreenOn(); + } + + if (paused) { + return; + } + if (Preferences.Gui.theme.isSameKey(key)) { + restartIfThemeChanged(); + } else if (Preferences.Gui.language.isSameKey(key)) { + restartIfLanguageChanged(); + } + } + + private void updateOrientation() { + if (Preferences.Gui.rotateScreen.getPreference(preferences)) { + setRequestedOrientation(SCREEN_ORIENTATION_UNSPECIFIED); + } else { + setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT); + } + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index ed1b3dfa..61df72cf 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -27,7 +27,10 @@ import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.widget.Toolbar; -import android.view.*; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; import android.widget.FrameLayout; import butterknife.Bind; import butterknife.ButterKnife; @@ -39,18 +42,11 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; -import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; -import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; -import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; -import static org.solovyev.android.calculator.Preferences.Gui.keepScreenOn; - -public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener, Toolbar.OnMenuItemClickListener { +public class CalculatorActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener { @Inject PreferredPreferences preferredPreferences; @Inject - SharedPreferences preferences; - @Inject Keyboard keyboard; @Inject PartialKeyboardUi partialKeyboardUi; @@ -101,9 +97,6 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference startupHelper.onMainActivityOpened(this); } - updateOrientation(); - - preferences.registerOnSharedPreferenceChangeListener(this); preferredPreferences.check(this, false); } @@ -133,16 +126,7 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference protected void onResume() { super.onResume(); launcher.setActivity(this); - if (restartIfModeChanged()) { - return; - } - - final Window window = getWindow(); - if (keepScreenOn.getPreference(preferences)) { - window.addFlags(FLAG_KEEP_SCREEN_ON); - } else { - window.clearFlags(FLAG_KEEP_SCREEN_ON); - } + restartIfModeChanged(); } @Override @@ -153,7 +137,6 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference @Override protected void onDestroy() { - preferences.unregisterOnSharedPreferenceChangeListener(this); if (partialKeyboard != null) { partialKeyboardUi.onDestroyView(); } @@ -162,21 +145,10 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference @Override public void onSharedPreferenceChanged(SharedPreferences preferences, @Nonnull String key) { + super.onSharedPreferenceChanged(preferences, key); if (Preferences.Gui.useBackAsPrevious.isSameKey(key)) { useBackAsPrevious = Preferences.Gui.useBackAsPrevious.getPreference(preferences); } - - if (Preferences.Gui.rotateScreen.isSameKey(key)) { - updateOrientation(); - } - } - - private void updateOrientation() { - if (Preferences.Gui.rotateScreen.getPreference(preferences)) { - setRequestedOrientation(SCREEN_ORIENTATION_UNSPECIFIED); - } else { - setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT); - } } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java index aaff957b..ca7312cf 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java @@ -8,11 +8,9 @@ import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.support.annotation.XmlRes; import android.util.SparseArray; - import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.BaseActivity; -import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.checkout.ActivityCheckout; @@ -45,15 +43,12 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc } ActivityCheckout checkout; - private boolean paused = true; @Inject Billing billing; @Inject Products products; @Inject - SharedPreferences preferences; - @Inject Languages languages; public PreferencesActivity() { @@ -78,7 +73,6 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - preferences.registerOnSharedPreferenceChangeListener(this); final Intent intent = getIntent(); final int preferenceTitle = intent.getIntExtra(EXTRA_PREFERENCE_TITLE, 0); @@ -103,33 +97,9 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc component.inject(this); } - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (!paused) { - if (Preferences.Gui.theme.isSameKey(key)) { - restartIfThemeChanged(); - } else if (Preferences.Gui.language.isSameKey(key)) { - restartIfLanguageChanged(); - } - } - } - - @Override - protected void onResume() { - super.onResume(); - paused = false; - } - - @Override - protected void onPause() { - paused = true; - super.onPause(); - } - @Override protected void onDestroy() { checkout.stop(); - preferences.unregisterOnSharedPreferenceChangeListener(this); super.onDestroy(); } diff --git a/app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java b/app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java index 6a3d8a7f..4bd3f474 100644 --- a/app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/wizard/WizardActivity.java @@ -195,15 +195,6 @@ public class WizardActivity extends BaseActivity implements WizardsAware, Shared } } - @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - if (Preferences.Gui.theme.isSameKey(key)) { - restartIfThemeChanged(); - } else if (Preferences.Gui.language.isSameKey(key)) { - restartIfLanguageChanged(); - } - } - private class WizardPagerAdapter extends FragmentStatePagerAdapter { @Nonnull private final ListWizardFlow flow;