Fix turn screen on prefs

This commit is contained in:
Vitaliy 2020-05-29 15:36:14 +03:00
parent 6d160244ac
commit af13433571
5 changed files with 34 additions and 45 deletions

View file

@ -11,6 +11,7 @@
Thx - Hardy Thx - Hardy
--> -->
<string name="default_screen_timeout">Default screen timeout</string>
<string name="screen_timeout_descr">If the \"%1$s\" option is enabled, the activity time will depend on it.</string> <string name="screen_timeout_descr">If the \"%1$s\" option is enabled, the activity time will depend on it.</string>
<string name="keep_screen_off">Keep screen off</string> <string name="keep_screen_off">Keep screen off</string>
<string name="keep_screen_on">Keep screen on</string> <string name="keep_screen_on">Keep screen on</string>

View file

@ -66,13 +66,11 @@ import net.osmand.map.MapTileDownloader.IMapDownloaderCallback;
import net.osmand.plus.AppInitializer; import net.osmand.plus.AppInitializer;
import net.osmand.plus.AppInitializer.AppInitializeListener; import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.AppInitializer.InitEvents; import net.osmand.plus.AppInitializer.InitEvents;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.HuaweiDrmHelper; import net.osmand.plus.HuaweiDrmHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.MapMarkersHelper.MapMarkerChangedListener; import net.osmand.plus.MapMarkersHelper.MapMarkerChangedListener;
import net.osmand.plus.OnDismissDialogFragmentListener; import net.osmand.plus.OnDismissDialogFragmentListener;
import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener;
import net.osmand.plus.OsmAndConstants; import net.osmand.plus.OsmAndConstants;
import net.osmand.plus.OsmAndLocationSimulation; import net.osmand.plus.OsmAndLocationSimulation;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -89,6 +87,7 @@ import net.osmand.plus.chooseplan.OsmLiveCancelledDialog;
import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType; import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.dialogs.CrashBottomSheetDialogFragment; import net.osmand.plus.dialogs.CrashBottomSheetDialogFragment;
import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment; import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment;
import net.osmand.plus.dialogs.SendAnalyticsBottomSheetDialogFragment; import net.osmand.plus.dialogs.SendAnalyticsBottomSheetDialogFragment;
import net.osmand.plus.dialogs.WhatsNewDialogFragment; import net.osmand.plus.dialogs.WhatsNewDialogFragment;
@ -101,7 +100,6 @@ import net.osmand.plus.firstusage.FirstUsageWizardFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.DiscountHelper; import net.osmand.plus.helpers.DiscountHelper;
import net.osmand.plus.helpers.ImportHelper; import net.osmand.plus.helpers.ImportHelper;
import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.helpers.IntentHelper; import net.osmand.plus.helpers.IntentHelper;
import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.LockHelper.LockUIAdapter; import net.osmand.plus.helpers.LockHelper.LockUIAdapter;
@ -134,6 +132,8 @@ import net.osmand.plus.routing.TransportRoutingHelper.TransportRouteCalculationP
import net.osmand.plus.search.QuickSearchDialogFragment; import net.osmand.plus.search.QuickSearchDialogFragment;
import net.osmand.plus.search.QuickSearchDialogFragment.QuickSearchTab; import net.osmand.plus.search.QuickSearchDialogFragment.QuickSearchTab;
import net.osmand.plus.search.QuickSearchDialogFragment.QuickSearchType; import net.osmand.plus.search.QuickSearchDialogFragment.QuickSearchType;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.fragments.BaseSettingsFragment; import net.osmand.plus.settings.fragments.BaseSettingsFragment;
import net.osmand.plus.settings.fragments.BaseSettingsFragment.SettingsScreenType; import net.osmand.plus.settings.fragments.BaseSettingsFragment.SettingsScreenType;
@ -1968,9 +1968,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
} }
public void changeKeyguardFlags() { public void changeKeyguardFlags() {
boolean turnScreenOnEnabled = getRoutingHelper().isFollowingMode(); boolean enabled = settings.TURN_SCREEN_ON_TIME_INT.get() >= 0;
int turnScreenOnTime = settings.TURN_SCREEN_ON_TIME_INT.get();
boolean enabled = turnScreenOnEnabled && (turnScreenOnTime > 0 || turnScreenOnTime == -1);
boolean keepScreenOn = !settings.USE_SYSTEM_SCREEN_TIMEOUT.get(); boolean keepScreenOn = !settings.USE_SYSTEM_SCREEN_TIMEOUT.get();
changeKeyguardFlags(enabled, keepScreenOn); changeKeyguardFlags(enabled, keepScreenOn);
} }

View file

@ -33,6 +33,8 @@ public class LockHelper implements SensorEventListener {
private OsmandApplication app; private OsmandApplication app;
private CommonPreference<Integer> turnScreenOnTime; private CommonPreference<Integer> turnScreenOnTime;
private CommonPreference<Boolean> turnScreenOnSensor; private CommonPreference<Boolean> turnScreenOnSensor;
private CommonPreference<Boolean> useSystemScreenTimeout;
private CommonPreference<Boolean> turnScreenOnPowerButton;
private CommonPreference<Boolean> turnScreenOnNavigationInstructions; private CommonPreference<Boolean> turnScreenOnNavigationInstructions;
@Nullable @Nullable
@ -53,6 +55,8 @@ public class LockHelper implements SensorEventListener {
OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
turnScreenOnTime = settings.TURN_SCREEN_ON_TIME_INT; turnScreenOnTime = settings.TURN_SCREEN_ON_TIME_INT;
turnScreenOnSensor = settings.TURN_SCREEN_ON_SENSOR; turnScreenOnSensor = settings.TURN_SCREEN_ON_SENSOR;
useSystemScreenTimeout = settings.USE_SYSTEM_SCREEN_TIMEOUT;
turnScreenOnPowerButton = settings.TURN_SCREEN_ON_POWER_BUTTON;
turnScreenOnNavigationInstructions = settings.TURN_SCREEN_ON_NAVIGATION_INSTRUCTIONS; turnScreenOnNavigationInstructions = settings.TURN_SCREEN_ON_NAVIGATION_INSTRUCTIONS;
lockRunnable = new Runnable() { lockRunnable = new Runnable() {
@ -75,6 +79,8 @@ public class LockHelper implements SensorEventListener {
OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
turnScreenOnTime = settings.TURN_SCREEN_ON_TIME_INT; turnScreenOnTime = settings.TURN_SCREEN_ON_TIME_INT;
turnScreenOnSensor = settings.TURN_SCREEN_ON_SENSOR; turnScreenOnSensor = settings.TURN_SCREEN_ON_SENSOR;
useSystemScreenTimeout = settings.USE_SYSTEM_SCREEN_TIMEOUT;
turnScreenOnPowerButton = settings.TURN_SCREEN_ON_POWER_BUTTON;
turnScreenOnNavigationInstructions = settings.TURN_SCREEN_ON_NAVIGATION_INSTRUCTIONS; turnScreenOnNavigationInstructions = settings.TURN_SCREEN_ON_NAVIGATION_INSTRUCTIONS;
} }
}; };
@ -107,13 +113,11 @@ public class LockHelper implements SensorEventListener {
private void lock() { private void lock() {
releaseWakeLocks(); releaseWakeLocks();
int unlockTime = turnScreenOnTime.get(); int unlockTime = turnScreenOnTime.get();
if (lockUIAdapter != null && isFollowingMode() && unlockTime != -1) { if (lockUIAdapter != null) {
if (!(useSystemScreenTimeout.get() && turnScreenOnPowerButton.get()) && unlockTime != 0) {
lockUIAdapter.lock(); lockUIAdapter.lock();
} }
} }
private boolean isFollowingMode() {
return app.getRoutingHelper().isFollowingMode();
} }
private void timedUnlock(final long millis) { private void timedUnlock(final long millis) {
@ -135,10 +139,10 @@ public class LockHelper implements SensorEventListener {
private void unlockEvent() { private void unlockEvent() {
int unlockTime = turnScreenOnTime.get(); int unlockTime = turnScreenOnTime.get();
if (unlockTime > 0) { if (unlockTime > 0 && !useSystemScreenTimeout.get()) {
timedUnlock(unlockTime * 1000L); timedUnlock(unlockTime * 1000L);
} else if (unlockTime == -1) { } else {
timedUnlock(-1); timedUnlock(0);
} }
} }
@ -156,14 +160,6 @@ public class LockHelper implements SensorEventListener {
} }
} }
private void switchSensorOn() {
switchSensor(true);
}
private void switchSensorOff() {
switchSensor(false);
}
private void switchSensor(boolean on) { private void switchSensor(boolean on) {
SensorManager sensorManager = (SensorManager) app.getSystemService(Context.SENSOR_SERVICE); SensorManager sensorManager = (SensorManager) app.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) { if (sensorManager != null) {
@ -176,23 +172,18 @@ public class LockHelper implements SensorEventListener {
} }
} }
private boolean isSensorEnabled() {
return turnScreenOnSensor.get() && isFollowingMode();
}
public void onStart(@NonNull Activity activity) { public void onStart(@NonNull Activity activity) {
switchSensorOff(); switchSensor(false);
} }
public void onStop(@NonNull Activity activity) { public void onStop(@NonNull Activity activity) {
lock(); lock();
if (!activity.isFinishing() && isSensorEnabled()) { if (!activity.isFinishing() && turnScreenOnSensor.get()) {
switchSensorOn(); switchSensor(true);
} }
} }
public void setLockUIAdapter(@Nullable LockUIAdapter adapter) { public void setLockUIAdapter(@Nullable LockUIAdapter adapter) {
lockUIAdapter = adapter; lockUIAdapter = adapter;
} }
} }

View file

@ -53,8 +53,8 @@ public class WakeTimeBottomSheet extends BasePreferenceBottomSheet {
keepScreenOnEnabled = savedInstanceState.getBoolean(KEEP_SCREEN_ON_ENABLED); keepScreenOnEnabled = savedInstanceState.getBoolean(KEEP_SCREEN_ON_ENABLED);
} else { } else {
int savedValIndex = listPreference.getValueIndex(); int savedValIndex = listPreference.getValueIndex();
keepScreenOnEnabled = savedValIndex == 0; keepScreenOnEnabled = savedValIndex <= 0;
selectedEntryIndex = savedValIndex != 0 ? savedValIndex : 1; selectedEntryIndex = savedValIndex > 0 ? savedValIndex : 1;
} }
items.add(new TitleItem(listPreference.getDialogTitle())); items.add(new TitleItem(listPreference.getDialogTitle()));

View file

@ -1,6 +1,5 @@
package net.osmand.plus.settings.fragments; package net.osmand.plus.settings.fragments;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -32,11 +31,9 @@ public class TurnScreenOnFragment extends BaseSettingsFragment implements OnPref
super.onBindPreferenceViewHolder(preference, holder); super.onBindPreferenceViewHolder(preference, holder);
String prefId = preference.getKey(); String prefId = preference.getKey();
if (settings.TURN_SCREEN_ON_TIME_INT.getId().equals(prefId) && preference instanceof ListPreferenceEx) { if (settings.TURN_SCREEN_ON_TIME_INT.getId().equals(prefId) && preference instanceof ListPreferenceEx) {
Object currentValue = ((ListPreferenceEx) preference).getValue(); TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
ImageView imageView = (ImageView) holder.findViewById(android.R.id.icon); if (summaryView != null && !preference.isEnabled()) {
if (imageView != null && currentValue instanceof Integer) { summaryView.setText(R.string.default_screen_timeout);
boolean enabled = preference.isEnabled() && (Integer) currentValue != 0;
imageView.setEnabled(enabled);
} }
} else if ("turn_screen_on_info".equals(prefId) || "turn_screen_on_options_info".equals(prefId)) { } else if ("turn_screen_on_info".equals(prefId) || "turn_screen_on_options_info".equals(prefId)) {
TextView titleView = (TextView) holder.findViewById(android.R.id.title); TextView titleView = (TextView) holder.findViewById(android.R.id.title);
@ -70,12 +67,11 @@ public class TurnScreenOnFragment extends BaseSettingsFragment implements OnPref
} }
private void setupTurnScreenOnTimePref() { private void setupTurnScreenOnTimePref() {
Integer[] entryValues = new Integer[] {-1, 0, 5, 10, 15, 20, 30, 45, 60}; Integer[] entryValues = new Integer[] {0, 5, 10, 15, 20, 30, 45, 60};
String[] entries = new String[entryValues.length]; String[] entries = new String[entryValues.length];
entries[0] = getString(R.string.shared_string_always); entries[0] = getString(R.string.keep_screen_on);
entries[1] = getString(R.string.shared_string_never); for (int i = 1; i < entryValues.length; i++) {
for (int i = 2; i < entryValues.length; i++) {
entries[i] = entryValues[i] + " " + getString(R.string.int_seconds); entries[i] = entryValues[i] + " " + getString(R.string.int_seconds);
} }
@ -100,9 +96,12 @@ public class TurnScreenOnFragment extends BaseSettingsFragment implements OnPref
} }
private void setupTurnScreenOnPowerButtonPref() { private void setupTurnScreenOnPowerButtonPref() {
ApplicationMode appMode = getSelectedAppMode();
boolean enabled = settings.TURN_SCREEN_ON_TIME_INT.getModeValue(appMode) == 0 || settings.USE_SYSTEM_SCREEN_TIMEOUT.getModeValue(appMode);
SwitchPreferenceEx turnScreenOnPowerButton = (SwitchPreferenceEx) findPreference(settings.TURN_SCREEN_ON_POWER_BUTTON.getId()); SwitchPreferenceEx turnScreenOnPowerButton = (SwitchPreferenceEx) findPreference(settings.TURN_SCREEN_ON_POWER_BUTTON.getId());
turnScreenOnPowerButton.setIcon(getPersistentPrefIcon(R.drawable.ic_action_power_button)); turnScreenOnPowerButton.setEnabled(enabled);
turnScreenOnPowerButton.setDescription(R.string.turn_screen_on_power_button_descr); turnScreenOnPowerButton.setDescription(R.string.turn_screen_on_power_button_descr);
turnScreenOnPowerButton.setIcon(getPersistentPrefIcon(R.drawable.ic_action_power_button));
} }
@Override @Override