From cbead067b70d3cd13d584ae3d080bd7bbcbfbd0a Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 29 May 2020 22:38:02 +0300 Subject: [PATCH 1/3] Release wakeLock with screen timeout pref --- .../osmand/plus/activities/MapActivity.java | 2 +- .../net/osmand/plus/helpers/LockHelper.java | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index c39309b99e..c47ed016ac 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1977,7 +1977,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (enable) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); - setKeepScreenOn(true); + setKeepScreenOn(forceKeepScreenOn); } else { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); setKeepScreenOn(forceKeepScreenOn); diff --git a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java index 477e2928ad..a7dcf6b011 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java @@ -10,20 +10,26 @@ import android.hardware.SensorManager; import android.os.Handler; import android.os.PowerManager; import android.os.PowerManager.WakeLock; +import android.provider.Settings; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.routing.VoiceRouter.VoiceMessageListener; import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings.CommonPreference; +import org.apache.commons.logging.Log; + import java.util.List; public class LockHelper implements SensorEventListener { + private static final Log LOG = PlatformUtil.getLog(LockHelper.class); + private static final int SENSOR_SENSITIVITY = 4; @Nullable @@ -138,14 +144,29 @@ public class LockHelper implements SensorEventListener { } private void unlockEvent() { - int unlockTime = turnScreenOnTime.get(); - if (unlockTime > 0 && !useSystemScreenTimeout.get()) { + int unlockTime = getUnlockTime(); + if (unlockTime > 0) { timedUnlock(unlockTime * 1000L); } else { timedUnlock(0); } } + private int getUnlockTime() { + int unlockTime = turnScreenOnTime.get(); + if (useSystemScreenTimeout.get()) { + try { + int screenOffTimeout = Settings.System.getInt(app.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 0); + if (screenOffTimeout > 0) { + unlockTime = screenOffTimeout / 1000; + } + } catch (Exception e) { + LOG.error(e.getMessage(), e); + } + } + return unlockTime; + } + @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { From b7820fdbcd650706e62953e4112ad801f4627a7d Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 29 May 2020 20:16:13 +0300 Subject: [PATCH 2/3] Disable power button pref in UI --- OsmAnd/res/xml/turn_screen_on.xml | 1 + .../osmand/plus/settings/fragments/TurnScreenOnFragment.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/OsmAnd/res/xml/turn_screen_on.xml b/OsmAnd/res/xml/turn_screen_on.xml index 94b0242ba8..e7a5508d01 100644 --- a/OsmAnd/res/xml/turn_screen_on.xml +++ b/OsmAnd/res/xml/turn_screen_on.xml @@ -58,6 +58,7 @@ diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java index 96631cbfd3..28deeb65af 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java @@ -97,11 +97,12 @@ public class TurnScreenOnFragment extends BaseSettingsFragment implements OnPref private void setupTurnScreenOnPowerButtonPref() { ApplicationMode appMode = getSelectedAppMode(); - boolean enabled = settings.TURN_SCREEN_ON_TIME_INT.getModeValue(appMode) == 0 || settings.USE_SYSTEM_SCREEN_TIMEOUT.getModeValue(appMode); + boolean enabled = settings.TURN_SCREEN_ON_TIME_INT.get() == 0 || settings.USE_SYSTEM_SCREEN_TIMEOUT.get(); SwitchPreferenceEx turnScreenOnPowerButton = (SwitchPreferenceEx) findPreference(settings.TURN_SCREEN_ON_POWER_BUTTON.getId()); turnScreenOnPowerButton.setEnabled(enabled); turnScreenOnPowerButton.setDescription(R.string.turn_screen_on_power_button_descr); turnScreenOnPowerButton.setIcon(getPersistentPrefIcon(R.drawable.ic_action_power_button)); + turnScreenOnPowerButton.setChecked(enabled && settings.TURN_SCREEN_ON_POWER_BUTTON.getModeValue(appMode)); } @Override From 12a4bd1056c9f123a129fa0ff6878d396601f1a5 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 29 May 2020 22:47:24 +0300 Subject: [PATCH 3/3] Remove unnecessary code --- OsmAnd/src/net/osmand/plus/activities/MapActivity.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index c47ed016ac..e19af7c3e5 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1977,11 +1977,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (enable) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); - setKeepScreenOn(forceKeepScreenOn); } else { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); - setKeepScreenOn(forceKeepScreenOn); } + setKeepScreenOn(forceKeepScreenOn); } @Override