diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 90bf2135da..d7231e709f 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -707,4 +707,7 @@ interface IOsmAndAidlInterface { boolean setCustomization(in CustomizationInfoParams params); long registerForVoiceRouterMessages(in ANavigationVoiceRouterMessageParams params, IOsmAndAidlCallback callback); + + boolean isMapActivityActive(); + boolean changeMapActivityKeyguardFlags(in boolean enable); } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 78a57ae82a..4de11447e7 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -203,6 +203,7 @@ public class OsmandAidlApi { private Map voiceRouterMessageCallbacks= new ConcurrentHashMap<>(); private AMapPointUpdateListener aMapPointUpdateListener; + private AMapKeyguardFlagsUpdateListener aMapKeyguardFlagsUpdateListener; private boolean mapActivityActive = false; @@ -237,6 +238,7 @@ public class OsmandAidlApi { initOsmandTelegram(); app.getAppCustomization().addListener(mapActivity); aMapPointUpdateListener = mapActivity; + aMapKeyguardFlagsUpdateListener = mapActivity; } public void onDestroyMapActivity(MapActivity mapActivity) { @@ -1900,7 +1902,7 @@ public class OsmandAidlApi { navUpdateCallbacks.remove(id); } - public void registerForVoiceRouterMessages(long id){ + public void registerForVoiceRouterMessages(long id) { VoiceRouter.VoiceMessageListener listener = new VoiceRouter.VoiceMessageListener() { @Override public void onVoiceMessage() { @@ -1921,10 +1923,18 @@ public class OsmandAidlApi { app.getRoutingHelper().getVoiceRouter().addVoiceMessageListener(listener); } - public void unregisterFromVoiceRouterMessages(long id){ + public void unregisterFromVoiceRouterMessages(long id) { app.getRoutingHelper().getVoiceRouter().removeVoiceMessageListener(voiceRouterMessageCallbacks.get(id)); voiceRouterMessageCallbacks.remove(id); } + + public boolean changeMapActivityKeyguardFlags(boolean enable) { + if (aMapKeyguardFlagsUpdateListener != null) { + aMapKeyguardFlagsUpdateListener.changeKeyguardFlags(enable); + return true; + } + return false; + } public Map getContextMenuButtonsParams() { return contextMenuButtonsParams; @@ -2261,4 +2271,7 @@ public class OsmandAidlApi { void onAMapPointUpdated(AMapPoint point, String layerId); } + public interface AMapKeyguardFlagsUpdateListener { + void changeKeyguardFlags(boolean enable); + } } diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index 262a064a22..a6d43f3bf1 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -1163,6 +1163,31 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { return UNKNOWN_API_ERROR; } } + + @Override + public boolean isMapActivityActive() { + try { + OsmandAidlApi api = getApi("isMapActivityActive"); + return api != null && api.isUpdateAllowed(); + } catch (Exception e) { + handleException(e); + return false; + } + } + + @Override + public boolean changeMapActivityKeyguardFlags(boolean enable) { + try { + OsmandAidlApi api = getApi("changeMapActivityKeyguardFlags"); + if (api != null) { + return api.changeMapActivityKeyguardFlags(enable); + } + return false; + } catch (Exception e) { + handleException(e); + return false; + } + } }; public static class AidlCallbackParams { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 3f56a6fac6..6a3388c575 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -36,6 +36,7 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewStub; import android.view.Window; +import android.view.WindowManager; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -47,6 +48,7 @@ import net.osmand.SecondSplashScreenFragment; import net.osmand.StateChangedListener; import net.osmand.ValueHolder; import net.osmand.access.MapAccessibilityActions; +import net.osmand.aidl.OsmandAidlApi; import net.osmand.aidl.OsmandAidlApi.AMapPointUpdateListener; import net.osmand.aidl.map.ALatLon; import net.osmand.aidl.maplayer.point.AMapPoint; @@ -157,7 +159,7 @@ import java.util.regex.Pattern; public class MapActivity extends OsmandActionBarActivity implements DownloadEvents, OnRequestPermissionsResultCallback, IRouteInformationListener, AMapPointUpdateListener, - MapMarkerChangedListener, OnDismissDialogFragmentListener, OnDrawMapListener, OsmAndAppCustomizationListener { + MapMarkerChangedListener, OnDismissDialogFragmentListener, OnDrawMapListener, OsmAndAppCustomizationListener, OsmandAidlApi.AMapKeyguardFlagsUpdateListener { public static final String INTENT_KEY_PARENT_MAP_ACTIVITY = "intent_parent_map_activity_key"; private static final int SHOW_POSITION_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 1; @@ -1874,6 +1876,21 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven return oldPoint.getLayerId().equals(layerId) && oldPoint.getId().equals(point.getId()); } + @Override + public void changeKeyguardFlags(final boolean enable) { + uiHandler.post(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); + } + } + }); + } + private class ScreenOffReceiver extends BroadcastReceiver { @Override diff --git a/OsmAnd-Plugin-TurnScreenOn/src/androidTest/java/net/osmand/turnScreenOn/ExampleInstrumentedTest.java b/plugins/OsmAnd-TurnScreenOn/src/androidTest/java/net/osmand/turnScreenOn/ExampleInstrumentedTest.java similarity index 100% rename from OsmAnd-Plugin-TurnScreenOn/src/androidTest/java/net/osmand/turnScreenOn/ExampleInstrumentedTest.java rename to plugins/OsmAnd-TurnScreenOn/src/androidTest/java/net/osmand/turnScreenOn/ExampleInstrumentedTest.java diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/AndroidManifest.xml b/plugins/OsmAnd-TurnScreenOn/src/main/AndroidManifest.xml index 0c57ae627f..1046405470 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/AndroidManifest.xml +++ b/plugins/OsmAnd-TurnScreenOn/src/main/AndroidManifest.xml @@ -2,7 +2,6 @@ - availableOsmandVersions; @@ -61,7 +64,7 @@ public class MainActivity extends AppCompatActivity { private ImageView ivSensorIcon; private View bottomSensorCardShadow; private View osmandVersionsPanel; - + private Paint pGreyScale; protected void onCreate(Bundle savedInstanceState) { @@ -73,6 +76,7 @@ public class MainActivity extends AppCompatActivity { sensorHelper = app.getSensorHelper(); lockHelper = app.getLockHelper(); unlockMessageListener = new UnlockMessageListener(app); + lockHelperEventListener = new LockHelperEventListener(app); } public void startProcess() { @@ -183,7 +187,7 @@ public class MainActivity extends AppCompatActivity { closeDialog(); refreshUI(); } - }, 5); + }, 100); } }; @@ -272,7 +276,8 @@ public class MainActivity extends AppCompatActivity { osmAndAidlHelper.registerForVoiceRouterMessages(); osmAndAidlHelper.addListener(unlockMessageListener); sensorHelper.addListener(unlockMessageListener); - + lockHelper.addLockListener(lockHelperEventListener); + tvPluginStateDescription.setText(getString(R.string.enabled)); tvPluginStateDescription.setTextColor(getResources().getColor(R.color.black)); tbPluginToolbar.setBackgroundColor(getResources().getColor(R.color.orange)); @@ -291,6 +296,8 @@ public class MainActivity extends AppCompatActivity { osmAndAidlHelper.unregisterFromVoiceRouterMessages(); osmAndAidlHelper.removeListener(unlockMessageListener); sensorHelper.removeListener(unlockMessageListener); + lockHelper.removeLockListener(lockHelperEventListener); + lockHelper.disableFunction(); tvPluginStateDescription.setText(getString(R.string.disabled)); diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/PluginDescriptionActivity.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/PluginDescriptionActivity.java index 700de8a3ba..99422d86f6 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/PluginDescriptionActivity.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/PluginDescriptionActivity.java @@ -7,8 +7,6 @@ import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; -import android.view.Window; -import android.view.WindowManager; import android.widget.FrameLayout; import net.osmand.turnScreenOn.app.TurnScreenApp; diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/app/TurnScreenApp.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/app/TurnScreenApp.java index 8654d708d8..3798cbc035 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/app/TurnScreenApp.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/app/TurnScreenApp.java @@ -3,7 +3,6 @@ package net.osmand.turnScreenOn.app; import android.app.Application; import net.osmand.turnScreenOn.PluginSettings; -import net.osmand.turnScreenOn.helpers.AndroidUtils; import net.osmand.turnScreenOn.helpers.LockHelper; import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper; import net.osmand.turnScreenOn.helpers.SensorHelper; diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/AndroidUtils.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/AndroidUtils.java index fef986fb95..b3a7cdf210 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/AndroidUtils.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/AndroidUtils.java @@ -1,29 +1,44 @@ package net.osmand.turnScreenOn.helpers; +import android.app.KeyguardManager; import android.content.Context; import android.content.res.Resources; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; +import android.os.Build; +import android.os.PowerManager; import android.util.TypedValue; +import static android.content.Context.POWER_SERVICE; import static android.util.TypedValue.COMPLEX_UNIT_DIP; public class AndroidUtils { - public static int dpToPx(Context ctx, float dp) { - Resources r = ctx.getResources(); - return (int) TypedValue.applyDimension( - COMPLEX_UNIT_DIP, - dp, - r.getDisplayMetrics() - ); - } + public static int dpToPx(Context ctx, float dp) { + Resources r = ctx.getResources(); + return (int) TypedValue.applyDimension( + COMPLEX_UNIT_DIP, + dp, + r.getDisplayMetrics() + ); + } - public static Paint createPaintWithGreyScale(){ - Paint pGreyScale = new Paint(); - ColorMatrix cm = new ColorMatrix(); - cm.setSaturation(0); - pGreyScale.setColorFilter(new ColorMatrixColorFilter(cm)); - return pGreyScale; - } + public static Paint createPaintWithGreyScale() { + Paint pGreyScale = new Paint(); + ColorMatrix cm = new ColorMatrix(); + cm.setSaturation(0); + pGreyScale.setColorFilter(new ColorMatrixColorFilter(cm)); + return pGreyScale; + } + + public static boolean isScreenOn(Context context) { + PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE); + return Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT_WATCH&&powerManager.isInteractive() + || Build.VERSION.SDK_INT< Build.VERSION_CODES.KITKAT_WATCH&&powerManager.isScreenOn(); + } + + public static boolean isScreenLocked(Context context) { + KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); + return keyguardManager.inKeyguardRestrictedInputMode(); + } } diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/LockHelper.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/LockHelper.java index ea1e51f863..393d995d77 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/LockHelper.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/LockHelper.java @@ -1,103 +1,126 @@ package net.osmand.turnScreenOn.helpers; -import android.Manifest; -import android.app.KeyguardManager; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; -import android.content.pm.PackageManager; import android.os.Handler; import android.os.PowerManager; -import android.support.v4.content.ContextCompat; -import android.util.Log; import net.osmand.turnScreenOn.app.TurnScreenApp; +import net.osmand.turnScreenOn.listener.LockObservable; +import net.osmand.turnScreenOn.listener.OnLockListener; import net.osmand.turnScreenOn.receiver.DeviceAdminRecv; -public class LockHelper { - private PowerManager.WakeLock wakeLock = null; - private DevicePolicyManager mDevicePolicyManager; - private ComponentName mDeviceAdmin; - private Handler uiHandler; - private Context context; - private TurnScreenApp app; - private KeyguardManager.KeyguardLock keyguardLock; - private LockRunnable lockRunnable; +import java.util.ArrayList; - private boolean functionEnable = false; +public class LockHelper implements LockObservable { + private PowerManager.WakeLock wakeLock = null; + private DevicePolicyManager mDevicePolicyManager; + private ComponentName mDeviceAdmin; + private Handler uiHandler; + private TurnScreenApp app; + private LockRunnable lockRunnable; + private ArrayList onLockListeners; - private final static String TAG = "LockHelperTag"; + private boolean functionEnable = false; - public LockHelper(TurnScreenApp app) { - this.app = app; - this.context = app; - uiHandler = new Handler(); - mDeviceAdmin = new ComponentName(context, DeviceAdminRecv.class); - mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); - lockRunnable = new LockRunnable(); - keyguardLock = ((KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE)) - .newKeyguardLock(TAG); - } + public LockHelper(TurnScreenApp app) { + this.app = app; + uiHandler = new Handler(); + mDeviceAdmin = new ComponentName(app, DeviceAdminRecv.class); + mDevicePolicyManager = (DevicePolicyManager) app.getSystemService(Context.DEVICE_POLICY_SERVICE); + lockRunnable = new LockRunnable(); + onLockListeners = new ArrayList<>(); + } - private void releaseWakeLocks() { - if (wakeLock != null) { - wakeLock.release(); - wakeLock = null; - } - } + private void releaseWakeLocks() { + if (wakeLock != null) { + wakeLock.release(); + wakeLock = null; + } + } - private class LockRunnable implements Runnable { - @Override - public void run() { - lock(); - } - } + public void lock() { + if (readyToLock()) { + notifyOnLock(); + releaseWakeLocks(); + if (functionEnable && !AndroidUtils.isScreenLocked(app)) { + mDevicePolicyManager.lockNow(); + } + } + } - public void lock() { - if (readyToLock()) { - releaseWakeLocks(); - keyguardLock.reenableKeyguard(); - if (functionEnable) { - mDevicePolicyManager.lockNow(); - } - } - } + public void unlock() { + if (readyToUnlock()) { + notifyOnUnlock(); + PowerManager pm = (PowerManager) app.getSystemService(Context.POWER_SERVICE); + wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK + | PowerManager.ACQUIRE_CAUSES_WAKEUP + | PowerManager.ON_AFTER_RELEASE, "tso:wakelocktag"); + wakeLock.acquire(); + } + } - public void unlock() { - if (readyToUnlock()) { - PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK - | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tso:wakelocktag"); - wakeLock.acquire(); - keyguardLock.disableKeyguard(); - } - } + public void timedUnlock(long millis) { + uiHandler.removeCallbacks(lockRunnable); + unlock(); + uiHandler.postDelayed(lockRunnable, millis); + } - public void timedUnlock(long millis) { - uiHandler.removeCallbacks(lockRunnable); - unlock(); - uiHandler.postDelayed(lockRunnable, millis); - } + private boolean readyToLock() { + return mDevicePolicyManager != null + && mDeviceAdmin != null + && mDevicePolicyManager.isAdminActive(mDeviceAdmin); + } - private boolean readyToLock() { - return mDevicePolicyManager != null - && mDeviceAdmin != null - && mDevicePolicyManager.isAdminActive(mDeviceAdmin) - && ContextCompat.checkSelfPermission(context, Manifest.permission.DISABLE_KEYGUARD) - == PackageManager.PERMISSION_GRANTED; - } + private boolean readyToUnlock() { + return wakeLock == null; + } - private boolean readyToUnlock() { - return wakeLock==null - && ContextCompat.checkSelfPermission(context, Manifest.permission.DISABLE_KEYGUARD) - == PackageManager.PERMISSION_GRANTED; - } + public void disableFunction() { + functionEnable = false; + } - public void disableFunction() { - functionEnable = false; - } + public void enableFunction() { + functionEnable = true; + } - public void enableFunction() { - functionEnable = true; - } + @Override + public void addLockListener(OnLockListener listener) { + if (onLockListeners != null && !onLockListeners.contains(listener)) { + onLockListeners.add(listener); + } + } + + @Override + public void removeLockListener(OnLockListener listener) { + if (onLockListeners != null && onLockListeners.size() > 0) { + onLockListeners.remove(listener); + } + } + + @Override + public void notifyOnLock() { + if (onLockListeners != null) { + for (OnLockListener l : onLockListeners) { + l.onLock(); + } + } + } + + @Override + public void notifyOnUnlock() { + if (onLockListeners != null) { + for (OnLockListener l : onLockListeners) { + l.onUnlock(); + } + } + } + + private class LockRunnable implements Runnable { + @Override + public void run() { + lock(); + } + } } diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/OsmAndAidlHelper.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/OsmAndAidlHelper.java index e1df5a68a6..7babf5b290 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/OsmAndAidlHelper.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/OsmAndAidlHelper.java @@ -15,7 +15,7 @@ import net.osmand.aidl.navigation.ANavigationVoiceRouterMessageParams; import net.osmand.aidl.search.SearchResult; import net.osmand.turnScreenOn.PluginSettings; import net.osmand.turnScreenOn.app.TurnScreenApp; -import net.osmand.turnScreenOn.listener.Observable; +import net.osmand.turnScreenOn.listener.MessageObservable; import net.osmand.turnScreenOn.listener.OnMessageListener; import net.osmand.turnScreenOn.log.PlatformUtil; @@ -24,170 +24,189 @@ import org.apache.commons.logging.Log; import java.util.ArrayList; import java.util.List; -public class OsmAndAidlHelper implements Observable { - private static final Log LOG = PlatformUtil.getLog(OsmAndAidlHelper.class); +public class OsmAndAidlHelper implements MessageObservable { + private static final Log LOG = PlatformUtil.getLog(OsmAndAidlHelper.class); - private final static String OSMAND_AIDL_SERVICE_PATH = "net.osmand.aidl.OsmandAidlService"; + private final static String OSMAND_AIDL_SERVICE_PATH = "net.osmand.aidl.OsmandAidlService"; - private List messageListeners; + private List messageListeners; - private TurnScreenApp app; - private PluginSettings settings; - private String connectedOsmandVersionPath; + private TurnScreenApp app; + private PluginSettings settings; + private String connectedOsmandVersionPath; - private long osmandUpdatesCallbackId = -1; + private long osmandUpdatesCallbackId = -1; - private boolean attemptedRegister = false; - private boolean isRegistered = false; + private boolean attemptedRegister = false; + private boolean isRegistered = false; - private IOsmAndAidlInterface mIOsmAndAidlInterface; - private IOsmAndAidlCallback mIOsmAndAidlCallbackInterface = new IOsmAndAidlCallback.Stub() { + private IOsmAndAidlInterface mIOsmAndAidlInterface; + private IOsmAndAidlCallback mIOsmAndAidlCallbackInterface = new IOsmAndAidlCallback.Stub() { - @Override - public void onSearchComplete(List resultSet) throws RemoteException { + @Override + public void onSearchComplete(List resultSet) throws RemoteException { - } + } - @Override - public void onUpdate() throws RemoteException { + @Override + public void onUpdate() throws RemoteException { - } + } - @Override - public void onAppInitialized() throws RemoteException { + @Override + public void onAppInitialized() throws RemoteException { - } + } - @Override - public void onGpxBitmapCreated(AGpxBitmap bitmap) throws RemoteException { + @Override + public void onGpxBitmapCreated(AGpxBitmap bitmap) throws RemoteException { - } + } - @Override - public void updateNavigationInfo(ADirectionInfo directionInfo) throws RemoteException { + @Override + public void updateNavigationInfo(ADirectionInfo directionInfo) throws RemoteException { - } + } - @Override - public void onContextMenuButtonClicked(int buttonId, String pointId, String layerId) throws RemoteException { + @Override + public void onContextMenuButtonClicked(int buttonId, String pointId, String layerId) throws RemoteException { - } + } - @Override - public void onVoiceRouterNotify() throws RemoteException { - notifyListeners(); - } - }; + @Override + public void onVoiceRouterNotify() throws RemoteException { + notifyListeners(); + } + }; - private ServiceConnection mConnection = new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service); - if (mIOsmAndAidlInterface != null && attemptedRegister) { - attemptedRegister = false; - registerForVoiceRouterMessages(); - } - } + private ServiceConnection mConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service); + if (mIOsmAndAidlInterface != null && attemptedRegister) { + attemptedRegister = false; + registerForVoiceRouterMessages(); + } + } - @Override - public void onServiceDisconnected(ComponentName name) { - mIOsmAndAidlInterface = null; - isRegistered = false; - } - }; + @Override + public void onServiceDisconnected(ComponentName name) { + mIOsmAndAidlInterface = null; + isRegistered = false; + } + }; - public OsmAndAidlHelper(TurnScreenApp app) { - this.app = app; - settings = app.getSettings(); - messageListeners = new ArrayList<>(); - } + public OsmAndAidlHelper(TurnScreenApp app) { + this.app = app; + settings = app.getSettings(); + messageListeners = new ArrayList<>(); + } - public void registerForVoiceRouterMessages() { - try { - if (mIOsmAndAidlInterface != null && !isRegistered) { - ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); - params.setSubscribeToUpdates(true); - params.setCallbackId(osmandUpdatesCallbackId); - osmandUpdatesCallbackId = mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface); - isRegistered = true; - } else { - attemptedRegister = true; - } - } catch (RemoteException e) { - } - } + public void registerForVoiceRouterMessages() { + try { + if (mIOsmAndAidlInterface != null && !isRegistered) { + ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); + params.setSubscribeToUpdates(true); + params.setCallbackId(osmandUpdatesCallbackId); + osmandUpdatesCallbackId = mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface); + isRegistered = true; + } else { + attemptedRegister = true; + } + } catch (RemoteException e) { + } + } - public void unregisterFromVoiceRouterMessages() { - try { - if (mIOsmAndAidlInterface != null && isRegistered) { - ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); - params.setSubscribeToUpdates(false); - params.setCallbackId(osmandUpdatesCallbackId); - mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface); - isRegistered = false; - } - } catch (RemoteException e) { - } - } + public void unregisterFromVoiceRouterMessages() { + try { + if (mIOsmAndAidlInterface != null && isRegistered) { + ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); + params.setSubscribeToUpdates(false); + params.setCallbackId(osmandUpdatesCallbackId); + mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface); + isRegistered = false; + } + } catch (RemoteException e) { + } + } - public void reconnectOsmand() { - PluginSettings.OsmandVersion versionToConnect = settings.getOsmandVersion(); - if (versionToConnect != null) { - String newOsmandVersionPath = versionToConnect.getPath(); - if (connectedOsmandVersionPath == null - || !connectedOsmandVersionPath.equals(newOsmandVersionPath) - || mIOsmAndAidlInterface == null) { - cleanupResources(); - connectOsmand(); - } - connectedOsmandVersionPath = newOsmandVersionPath; - } - } + public boolean isMapActivityActive() { + try { + if (mIOsmAndAidlInterface != null) { + return mIOsmAndAidlInterface.isMapActivityActive(); + } + } catch (RemoteException e) { + } + return false; + } + + public void changeMapActivityKeyguardFlags(boolean newState) { + try { + if (mIOsmAndAidlInterface != null) { + mIOsmAndAidlInterface.changeMapActivityKeyguardFlags(newState); + } + } catch (RemoteException e) { + } + } - public void connectOsmand() { - bindService(settings.getOsmandVersion().getPath()); - } + public void reconnectOsmand() { + PluginSettings.OsmandVersion versionToConnect = settings.getOsmandVersion(); + if (versionToConnect != null) { + String newOsmandVersionPath = versionToConnect.getPath(); + if (connectedOsmandVersionPath == null + || !connectedOsmandVersionPath.equals(newOsmandVersionPath) + || mIOsmAndAidlInterface == null) { + cleanupResources(); + connectOsmand(); + } + connectedOsmandVersionPath = newOsmandVersionPath; + } + } - private void bindService(String packageName) { - if (mIOsmAndAidlInterface == null) { - Intent intent = new Intent(OSMAND_AIDL_SERVICE_PATH); - intent.setPackage(packageName); - app.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - } - } + public void connectOsmand() { + bindService(settings.getOsmandVersion().getPath()); + } - private void cleanupResources() { - try { - if (mIOsmAndAidlInterface != null) { - mIOsmAndAidlInterface = null; - unregisterFromVoiceRouterMessages(); - isRegistered = false; - app.unbindService(mConnection); - } - } catch (Throwable e) { - } - } + private void bindService(String packageName) { + if (mIOsmAndAidlInterface == null) { + Intent intent = new Intent(OSMAND_AIDL_SERVICE_PATH); + intent.setPackage(packageName); + app.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + } + } - @Override - public void addListener(OnMessageListener listener) { - if (listener != null && !messageListeners.contains(listener)) { - messageListeners.add(listener); - } - } + private void cleanupResources() { + try { + if (mIOsmAndAidlInterface != null) { + mIOsmAndAidlInterface = null; + unregisterFromVoiceRouterMessages(); + isRegistered = false; + app.unbindService(mConnection); + } + } catch (Throwable e) { + } + } - @Override - public void removeListener(OnMessageListener listener) { - if (listener != null) { - messageListeners.remove(listener); - } - } + @Override + public void addListener(OnMessageListener listener) { + if (listener != null && !messageListeners.contains(listener)) { + messageListeners.add(listener); + } + } - @Override - public void notifyListeners() { - if (messageListeners != null) { - for (OnMessageListener listener : messageListeners) { - listener.onMessageReceive(); - } - } - } + @Override + public void removeListener(OnMessageListener listener) { + if (listener != null) { + messageListeners.remove(listener); + } + } + + @Override + public void notifyListeners() { + if (messageListeners != null) { + for (OnMessageListener listener : messageListeners) { + listener.onMessageReceive(); + } + } + } } diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/SensorHelper.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/SensorHelper.java index 043e3944b4..ccdddb5bb1 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/SensorHelper.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/helpers/SensorHelper.java @@ -5,17 +5,16 @@ import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; -import android.util.Log; import net.osmand.turnScreenOn.app.TurnScreenApp; -import net.osmand.turnScreenOn.listener.Observable; +import net.osmand.turnScreenOn.listener.MessageObservable; import net.osmand.turnScreenOn.listener.OnMessageListener; import net.osmand.turnScreenOn.log.PlatformUtil; import java.util.ArrayList; import java.util.List; -public class SensorHelper implements SensorEventListener, Observable { +public class SensorHelper implements SensorEventListener, MessageObservable { private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(SensorHelper.class); private static final int SENSOR_SENSITIVITY = 4; diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/LockHelperEventListener.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/LockHelperEventListener.java new file mode 100644 index 0000000000..d331b499c0 --- /dev/null +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/LockHelperEventListener.java @@ -0,0 +1,24 @@ +package net.osmand.turnScreenOn.listener; + +import net.osmand.turnScreenOn.app.TurnScreenApp; +import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper; + +public class LockHelperEventListener implements OnLockListener { + private TurnScreenApp app; + private OsmAndAidlHelper osmAndAidlHelper; + + public LockHelperEventListener(TurnScreenApp app) { + this.app = app; + osmAndAidlHelper = app.getOsmAndAidlHelper(); + } + + @Override + public void onLock() { + osmAndAidlHelper.changeMapActivityKeyguardFlags(false); + } + + @Override + public void onUnlock() { + osmAndAidlHelper.changeMapActivityKeyguardFlags(true); + } +} diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/LockObservable.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/LockObservable.java new file mode 100644 index 0000000000..e5ca3c5924 --- /dev/null +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/LockObservable.java @@ -0,0 +1,8 @@ +package net.osmand.turnScreenOn.listener; + +public interface LockObservable { + void addLockListener(OnLockListener listener); + void removeLockListener(OnLockListener listener); + void notifyOnLock(); + void notifyOnUnlock(); +} diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/Observable.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/MessageObservable.java similarity index 82% rename from plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/Observable.java rename to plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/MessageObservable.java index b45035f873..0c40528829 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/Observable.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/MessageObservable.java @@ -1,6 +1,6 @@ package net.osmand.turnScreenOn.listener; -public interface Observable { +public interface MessageObservable { void addListener(OnMessageListener listener); void removeListener(OnMessageListener listener); diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/OnLockListener.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/OnLockListener.java new file mode 100644 index 0000000000..5748f98aa9 --- /dev/null +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/OnLockListener.java @@ -0,0 +1,6 @@ +package net.osmand.turnScreenOn.listener; + +public interface OnLockListener { + void onLock(); + void onUnlock(); +} \ No newline at end of file diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/UnlockMessageListener.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/UnlockMessageListener.java index c80df14fd5..1fc251c402 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/UnlockMessageListener.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/listener/UnlockMessageListener.java @@ -1,29 +1,35 @@ package net.osmand.turnScreenOn.listener; -import android.util.Log; - import net.osmand.turnScreenOn.PluginSettings; import net.osmand.turnScreenOn.app.TurnScreenApp; +import net.osmand.turnScreenOn.helpers.AndroidUtils; import net.osmand.turnScreenOn.helpers.LockHelper; +import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper; public class UnlockMessageListener implements OnMessageListener { private TurnScreenApp app; private PluginSettings settings; + private OsmAndAidlHelper osmAndAidlHelper; private LockHelper lockHelper; public UnlockMessageListener(TurnScreenApp app) { this.app = app; - + settings = app.getSettings(); lockHelper = app.getLockHelper(); + osmAndAidlHelper = app.getOsmAndAidlHelper(); } @Override public void onMessageReceive() { - if (settings != null && lockHelper != null) { - if (settings.isAdminDevicePermissionAvailable()) { - PluginSettings.UnlockTime time = settings.getTime(); - lockHelper.timedUnlock(time.getSeconds() * 1000L); + boolean isScreenOn = AndroidUtils.isScreenOn(app); + boolean isScreenLocked = AndroidUtils.isScreenLocked(app); + if ((!isScreenOn || isScreenLocked) && osmAndAidlHelper.isMapActivityActive()) { + if (settings != null && lockHelper != null) { + if (settings.isAdminDevicePermissionAvailable()) { + PluginSettings.UnlockTime time = settings.getTime(); + lockHelper.timedUnlock(time.getSeconds() * 1000L); + } } } } diff --git a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/receiver/DeviceAdminRecv.java b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/receiver/DeviceAdminRecv.java index 986ccd40d4..9ecd1b7591 100644 --- a/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/receiver/DeviceAdminRecv.java +++ b/plugins/OsmAnd-TurnScreenOn/src/main/java/net/osmand/turnScreenOn/receiver/DeviceAdminRecv.java @@ -3,22 +3,15 @@ package net.osmand.turnScreenOn.receiver; import android.app.admin.DeviceAdminReceiver; import android.content.Context; import android.content.Intent; -import android.util.Log; public class DeviceAdminRecv extends DeviceAdminReceiver { - private static final String TAG = "DeviceAdminReceiver"; - - public void onEnabled(Context context, Intent intent) { - Log.d(TAG, "permission disabled"); - } + public void onEnabled(Context context, Intent intent) { } public CharSequence onDisableRequested(Context context, Intent intent) { return null; } - public void onDisabled(Context context, Intent intent) { - Log.d(TAG, "permission enabled"); - } + public void onDisabled(Context context, Intent intent) { } } \ No newline at end of file