Fix turn screen on

This commit is contained in:
crimean 2019-07-06 17:29:24 +03:00
parent e9ae109ef8
commit 8e15031d9d
10 changed files with 94 additions and 103 deletions

View file

@ -3049,7 +3049,6 @@ Repræsenterer område: %1$s x %2$s</string>
<string name="public_transport_type">Transporttype</string>
<string name="searching_gps">Søger efter GPS</string>
<string name="coordinates_widget">Koordinat modul</string>
<string name="app_profile_custom_nav_subtitle">" "</string>
<string name="rate_dialog_descr">Brug 30 sekunder, udveksle erfaringer og bedøm vores arbejde på Google Play.</string>
<string name="button_rate">Bedøm</string>
<string name="shared_string_privacy_policy">Privatlivspolitik</string>

View file

@ -3131,7 +3131,6 @@ Abgedeckte Fläche: %1$s x %2$s</string>
<string name="routing_attr_allow_expert_description">Extrem schwere Routen, mit gefährlichen Hindernissen und schwieriger Umgebung.</string>
<string name="routing_attr_allow_classic_only_name">Nur klassische Routen zulassen</string>
<string name="routing_attr_difficulty_preference_name">Bevorzugte Schwierigkeit</string>
<string name="app_profile_custom_nav_subtitle">" "</string>
<string name="routing_attr_piste_type_downhill_name">Ski Alpin/Abfahrt</string>
<string name="routing_attr_piste_type_downhill_description">Pisten für Ski Alpin oder Abfahrt und Zugang zu den Skiliften.</string>
<string name="process_downloading_service">OsmAnd Download-Service</string>

View file

@ -3043,7 +3043,6 @@
<string name="public_transport_type">Transporttype</string>
<string name="searching_gps">Søker etter GPS</string>
<string name="coordinates_widget">Koordinat-miniprogram</string>
<string name="app_profile_custom_nav_subtitle">" "</string>
<string name="rate_dialog_descr">Ta 30 sekunder til å vurdere programmet og dele din tilbakemelding på Google Play.</string>
<string name="button_rate">Vurder</string>
<string name="shared_string_privacy_policy">Personvernspraksis</string>

View file

@ -3123,7 +3123,6 @@ Pôr do Sol: %2$s</string>
<string name="shared_string_color_magenta">Magenta</string>
<string name="shared_string_icon">Ícone</string>
<string name="collected_data">Dados coletados</string>
<string name="app_profile_custom_nav_subtitle">.</string>
<string name="press_again_to_change_the_map_orientation">Toque novamente para alterar a orientação do mapa</string>
<string name="shared_string_min_speed">Velocidade mínima</string>
<string name="shared_string_max_speed">Velocidade máxima</string>

View file

@ -3044,7 +3044,6 @@
<string name="public_transport_type">Tipo de transporte</string>
<string name="searching_gps">Procurando GPS</string>
<string name="coordinates_widget">Widget de coordenadas</string>
<string name="app_profile_custom_nav_subtitle">" "</string>
<string name="rate_dialog_descr">Por favor, dê-nos 30 segundos, compartilhe comentários e classifique o nosso trabalho no Google Play.</string>
<string name="button_rate">Avaliar</string>
<string name="shared_string_privacy_policy">Política de privacidade</string>

View file

@ -15,8 +15,6 @@
<string name="turn_screen_on_time_descr">Set the time for which the screen will turn on.</string>
<string name="turn_screen_on_sensor">Use proximity sensor</string>
<string name="turn_screen_on_sensor_descr">Wave your hand over the top of the screen to turn on the screen while navigating.</string>
<string name="app_profile_custom_nav_subtitle"></string>
<string name="rate_dialog_descr">Please give us 30 seconds, share feedback and rate our work on Google Play.</string>
<string name="app_mode_offroad">Offroad</string>
<string name="edit_profile_setup_title">Setup Profile</string>
<string name="edit_profile_setup_subtitle">Profile keeps its own settings</string>

View file

@ -1887,17 +1887,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
}
public void changeKeyguardFlags(final boolean enable) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
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);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
}
});
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);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
}
@Override

View file

@ -429,10 +429,6 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
return true;
}
super.onPreferenceChange(preference, newValue);
if (id.equals(settings.TURN_SCREEN_ON_TIME_INT.getId())) {
boolean isRoutingListenerEnabled = Integer.valueOf(newValue.toString()) > 0;
getMyApplication().getLockHelper().setVoiceRouterListener(isRoutingListenerEnabled);
}
return true;
}

View file

@ -8,27 +8,32 @@ import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.routing.VoiceRouter;
import net.osmand.plus.OsmandSettings.CommonPreference;
import net.osmand.plus.routing.VoiceRouter.VoiceMessageListener;
public class LockHelper implements SensorEventListener {
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(LockHelper.class);
private static final int SENSOR_SENSITIVITY = 4;
private final SensorManager mSensorManager;
private final Sensor mProximity;
private PowerManager.WakeLock wakeLock = null;
@Nullable
private WakeLock wakeLock = null;
private Handler uiHandler;
private OsmandApplication app;
private LockRunnable lockRunnable;
private LockUIAdapter lockUIAdapter;
private OsmandSettings settings;
private CommonPreference<Integer> turnScreenOnTime;
private CommonPreference<Boolean> turnScreenOnSensor;
private VoiceRouter.VoiceMessageListener voiceMessageListener;
@Nullable
private LockUIAdapter lockUIAdapter;
private Runnable lockRunnable;
private VoiceMessageListener voiceMessageListener;
public interface LockUIAdapter {
@ -37,28 +42,26 @@ public class LockHelper implements SensorEventListener {
void unlock();
}
private class LockRunnable implements Runnable {
@Override
public void run() {
lock();
}
}
public LockHelper(final OsmandApplication app) {
this.app = app;
uiHandler = new Handler();
lockRunnable = new LockRunnable();
settings = app.getSettings();
OsmandSettings settings = app.getSettings();
turnScreenOnTime = settings.TURN_SCREEN_ON_TIME_INT;
turnScreenOnSensor = settings.TURN_SCREEN_ON_SENSOR;
mSensorManager = (SensorManager) app.getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
voiceMessageListener = new VoiceRouter.VoiceMessageListener() {
lockRunnable = new Runnable() {
@Override
public void run() {
lock();
}
};
voiceMessageListener = new VoiceMessageListener() {
@Override
public void onVoiceMessage() {
unlockEvent();
}
};
app.getRoutingHelper().getVoiceRouter().addVoiceMessageListener(voiceMessageListener);
}
private void releaseWakeLocks() {
@ -77,9 +80,11 @@ public class LockHelper implements SensorEventListener {
}
PowerManager pm = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "tso:wakelocktag");
wakeLock.acquire(timeInMills);
if (pm != null) {
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "tso:wakelocktag");
wakeLock.acquire(timeInMills);
}
}
private void lock() {
@ -89,15 +94,19 @@ public class LockHelper implements SensorEventListener {
}
}
private void timedUnlock(long millis) {
private void timedUnlock(final long millis) {
uiHandler.removeCallbacks(lockRunnable);
unlock(millis);
uiHandler.post(new Runnable() {
@Override
public void run() {
unlock(millis);
}
});
uiHandler.postDelayed(lockRunnable, millis);
}
public void unlockEvent() {
int unlockTime = app.getSettings().TURN_SCREEN_ON_TIME_INT.get();
private void unlockEvent() {
int unlockTime = turnScreenOnTime.get();
if (unlockTime > 0) {
timedUnlock(unlockTime * 1000L);
}
@ -117,46 +126,43 @@ public class LockHelper implements SensorEventListener {
}
}
public void setSensor(boolean enable) {
if (enable) {
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
} else {
mSensorManager.unregisterListener(this);
private void switchSensorOn() {
switchSensor(true);
}
private void switchSensorOff() {
switchSensor(false);
}
private void switchSensor(boolean on) {
SensorManager sensorManager = (SensorManager) app.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (on) {
sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
} else {
sensorManager.unregisterListener(this);
}
}
}
public void setVoiceRouterListener(boolean enable) {
VoiceRouter vr = app.getRoutingHelper().getVoiceRouter();
if (enable) {
vr.addVoiceMessageListener(voiceMessageListener);
} else {
vr.removeVoiceMessageListener(voiceMessageListener);
}
private boolean isSensorEnabled() {
return turnScreenOnSensor.get() && app.getRoutingHelper().isFollowingMode();
}
public void refreshSettings() {
boolean isVRListenerEnabled = settings.TURN_SCREEN_ON_TIME_INT.get() > 0;
setVoiceRouterListener(isVRListenerEnabled);
boolean isSensorEnabled = settings.TURN_SCREEN_ON_SENSOR.get()
&& app.getRoutingHelper().isFollowingMode();
setSensor(isSensorEnabled);
}
public void onStart(Activity a) {
public void onStart(@NonNull Activity activity) {
if (wakeLock == null) {
setSensor(false);
setVoiceRouterListener(false);
}
}
public void onStop(Activity a) {
if(!a.isFinishing()) {
refreshSettings();
switchSensorOff();
}
}
public void setLockUIAdapter(LockUIAdapter adapter) {
public void onStop(@NonNull Activity activity) {
if (!activity.isFinishing() && isSensorEnabled()) {
switchSensorOn();
}
}
public void setLockUIAdapter(@Nullable LockUIAdapter adapter) {
lockUIAdapter = adapter;
}

View file

@ -3,7 +3,10 @@ package net.osmand.plus.routing;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -80,13 +83,12 @@ public class VoiceRouter {
void onVoiceMessage();
}
private ConcurrentHashMap<WeakReference<VoiceMessageListener>, Integer> voiceMessageListeners;
private List<WeakReference<VoiceMessageListener>> voiceMessageListeners = new ArrayList<>();
VoiceRouter(RoutingHelper router, final OsmandSettings settings) {
this.router = router;
this.settings = settings;
mute = settings.VOICE_MUTE.get();
voiceMessageListeners = new ConcurrentHashMap<>();
}
public void setPlayer(CommandPlayer player) {
@ -932,37 +934,36 @@ public class VoiceRouter {
}
public void addVoiceMessageListener(VoiceMessageListener voiceMessageListener) {
voiceMessageListeners = updateVoiceMessageListeners(new ConcurrentHashMap<>(voiceMessageListeners),
voiceMessageListener, true);
voiceMessageListeners = updateVoiceMessageListeners(new ArrayList<>(voiceMessageListeners), voiceMessageListener, true);
}
public void removeVoiceMessageListener(VoiceMessageListener voiceMessageListener) {
voiceMessageListeners = updateVoiceMessageListeners(new ConcurrentHashMap<>(voiceMessageListeners),
voiceMessageListener, false);
voiceMessageListeners = updateVoiceMessageListeners(new ArrayList<>(voiceMessageListeners), voiceMessageListener, false);
}
public void notifyOnVoiceMessage() {
for (WeakReference<VoiceMessageListener> weakReferenceWrapper : voiceMessageListeners.keySet()) {
VoiceMessageListener lnt = weakReferenceWrapper.get();
private void notifyOnVoiceMessage() {
List<WeakReference<VoiceMessageListener>> voiceMessageListeners = new ArrayList<>(this.voiceMessageListeners);
for (WeakReference<VoiceMessageListener> weakReference : voiceMessageListeners) {
VoiceMessageListener lnt = weakReference.get();
if (lnt != null) {
lnt.onVoiceMessage();
}
}
}
private ConcurrentHashMap<WeakReference<VoiceMessageListener>, Integer> updateVoiceMessageListeners(
ConcurrentHashMap<WeakReference<VoiceMessageListener>, Integer> copy,
VoiceMessageListener listener, boolean isNewListener) {
for (WeakReference<VoiceMessageListener> wr : copy.keySet()) {
VoiceMessageListener l = wr.get();
if (l == null || l.equals(listener)) {
copy.remove(wr);
private List<WeakReference<VoiceMessageListener>> updateVoiceMessageListeners(List<WeakReference<VoiceMessageListener>> voiceMessageListeners,
VoiceMessageListener listener, boolean isNewListener) {
Iterator<WeakReference<VoiceMessageListener>> it = voiceMessageListeners.iterator();
while (it.hasNext()) {
WeakReference<VoiceMessageListener> ref = it.next();
VoiceMessageListener l = ref.get();
if (l == null || l == listener) {
it.remove();
}
}
if (isNewListener) {
copy.put(new WeakReference<>(listener), 0);
voiceMessageListeners.add(new WeakReference<>(listener));
}
return copy;
return voiceMessageListeners;
}
}