Move keepScreenOn and rotateScreen

This commit is contained in:
serso 2016-03-27 20:28:53 +02:00
parent 59ce9105ac
commit f123646400
4 changed files with 65 additions and 84 deletions

View File

@ -12,10 +12,7 @@ import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.KeyEvent; import android.view.*;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -31,9 +28,13 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; 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.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 @Nonnull
protected final Tabs tabs; protected final Tabs tabs;
@ -69,6 +70,7 @@ public class BaseActivity extends AppCompatActivity {
private Preferences.Gui.Mode mode = Preferences.Gui.Mode.engineer; private Preferences.Gui.Mode mode = Preferences.Gui.Mode.engineer;
@Nonnull @Nonnull
private Language language = Languages.SYSTEM_LANGUAGE; private Language language = Languages.SYSTEM_LANGUAGE;
private boolean paused = true;
public BaseActivity(@StringRes int titleId) { public BaseActivity(@StringRes int titleId) {
this(R.layout.activity_tabs, 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 @Nonnull
public Preferences.Gui.Mode getActivityMode() { public Preferences.Gui.Mode getActivityMode() {
return mode; return mode;
@ -154,6 +151,11 @@ public class BaseActivity extends AppCompatActivity {
languages.updateContextLocale(this, false); languages.updateContextLocale(this, false);
createView(); createView();
updateOrientation();
updateKeepScreenOn();
preferences.registerOnSharedPreferenceChangeListener(this);
} }
private void createView() { private void createView() {
@ -236,13 +238,27 @@ public class BaseActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
paused = false;
if (!restartIfThemeChanged()) { if (!restartIfThemeChanged()) {
restartIfLanguageChanged(); 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 @Override
protected void onPause() { protected void onPause() {
paused = true;
tabs.onPause(); tabs.onPause();
super.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);
}
}
} }

View File

@ -27,7 +27,10 @@ import android.os.Bundle;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar; 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 android.widget.FrameLayout;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -39,18 +42,11 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; public class CalculatorActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {
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 {
@Inject @Inject
PreferredPreferences preferredPreferences; PreferredPreferences preferredPreferences;
@Inject @Inject
SharedPreferences preferences;
@Inject
Keyboard keyboard; Keyboard keyboard;
@Inject @Inject
PartialKeyboardUi partialKeyboardUi; PartialKeyboardUi partialKeyboardUi;
@ -101,9 +97,6 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
startupHelper.onMainActivityOpened(this); startupHelper.onMainActivityOpened(this);
} }
updateOrientation();
preferences.registerOnSharedPreferenceChangeListener(this);
preferredPreferences.check(this, false); preferredPreferences.check(this, false);
} }
@ -133,16 +126,7 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
launcher.setActivity(this); launcher.setActivity(this);
if (restartIfModeChanged()) { restartIfModeChanged();
return;
}
final Window window = getWindow();
if (keepScreenOn.getPreference(preferences)) {
window.addFlags(FLAG_KEEP_SCREEN_ON);
} else {
window.clearFlags(FLAG_KEEP_SCREEN_ON);
}
} }
@Override @Override
@ -153,7 +137,6 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
@Override @Override
protected void onDestroy() { protected void onDestroy() {
preferences.unregisterOnSharedPreferenceChangeListener(this);
if (partialKeyboard != null) { if (partialKeyboard != null) {
partialKeyboardUi.onDestroyView(); partialKeyboardUi.onDestroyView();
} }
@ -162,21 +145,10 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nonnull String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, @Nonnull String key) {
super.onSharedPreferenceChanged(preferences, key);
if (Preferences.Gui.useBackAsPrevious.isSameKey(key)) { if (Preferences.Gui.useBackAsPrevious.isSameKey(key)) {
useBackAsPrevious = Preferences.Gui.useBackAsPrevious.getPreference(preferences); 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 @Override

View File

@ -8,11 +8,9 @@ import android.support.annotation.NonNull;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.annotation.XmlRes; import android.support.annotation.XmlRes;
import android.util.SparseArray; import android.util.SparseArray;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.checkout.ActivityCheckout; import org.solovyev.android.checkout.ActivityCheckout;
@ -45,15 +43,12 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc
} }
ActivityCheckout checkout; ActivityCheckout checkout;
private boolean paused = true;
@Inject @Inject
Billing billing; Billing billing;
@Inject @Inject
Products products; Products products;
@Inject @Inject
SharedPreferences preferences;
@Inject
Languages languages; Languages languages;
public PreferencesActivity() { public PreferencesActivity() {
@ -78,7 +73,6 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
preferences.registerOnSharedPreferenceChangeListener(this);
final Intent intent = getIntent(); final Intent intent = getIntent();
final int preferenceTitle = intent.getIntExtra(EXTRA_PREFERENCE_TITLE, 0); final int preferenceTitle = intent.getIntExtra(EXTRA_PREFERENCE_TITLE, 0);
@ -103,33 +97,9 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc
component.inject(this); 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 @Override
protected void onDestroy() { protected void onDestroy() {
checkout.stop(); checkout.stop();
preferences.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy(); super.onDestroy();
} }

View File

@ -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 { private class WizardPagerAdapter extends FragmentStatePagerAdapter {
@Nonnull @Nonnull
private final ListWizardFlow flow; private final ListWizardFlow flow;