fix locking/unlocking

This commit is contained in:
Nazar 2019-06-26 17:10:12 +03:00
parent 9807fe212d
commit d7dd44fea3
20 changed files with 417 additions and 260 deletions

View file

@ -707,4 +707,7 @@ interface IOsmAndAidlInterface {
boolean setCustomization(in CustomizationInfoParams params); boolean setCustomization(in CustomizationInfoParams params);
long registerForVoiceRouterMessages(in ANavigationVoiceRouterMessageParams params, IOsmAndAidlCallback callback); long registerForVoiceRouterMessages(in ANavigationVoiceRouterMessageParams params, IOsmAndAidlCallback callback);
boolean isMapActivityActive();
boolean changeMapActivityKeyguardFlags(in boolean enable);
} }

View file

@ -203,6 +203,7 @@ public class OsmandAidlApi {
private Map<Long, VoiceRouter.VoiceMessageListener> voiceRouterMessageCallbacks= new ConcurrentHashMap<>(); private Map<Long, VoiceRouter.VoiceMessageListener> voiceRouterMessageCallbacks= new ConcurrentHashMap<>();
private AMapPointUpdateListener aMapPointUpdateListener; private AMapPointUpdateListener aMapPointUpdateListener;
private AMapKeyguardFlagsUpdateListener aMapKeyguardFlagsUpdateListener;
private boolean mapActivityActive = false; private boolean mapActivityActive = false;
@ -237,6 +238,7 @@ public class OsmandAidlApi {
initOsmandTelegram(); initOsmandTelegram();
app.getAppCustomization().addListener(mapActivity); app.getAppCustomization().addListener(mapActivity);
aMapPointUpdateListener = mapActivity; aMapPointUpdateListener = mapActivity;
aMapKeyguardFlagsUpdateListener = mapActivity;
} }
public void onDestroyMapActivity(MapActivity mapActivity) { public void onDestroyMapActivity(MapActivity mapActivity) {
@ -1900,7 +1902,7 @@ public class OsmandAidlApi {
navUpdateCallbacks.remove(id); navUpdateCallbacks.remove(id);
} }
public void registerForVoiceRouterMessages(long id){ public void registerForVoiceRouterMessages(long id) {
VoiceRouter.VoiceMessageListener listener = new VoiceRouter.VoiceMessageListener() { VoiceRouter.VoiceMessageListener listener = new VoiceRouter.VoiceMessageListener() {
@Override @Override
public void onVoiceMessage() { public void onVoiceMessage() {
@ -1921,11 +1923,19 @@ public class OsmandAidlApi {
app.getRoutingHelper().getVoiceRouter().addVoiceMessageListener(listener); app.getRoutingHelper().getVoiceRouter().addVoiceMessageListener(listener);
} }
public void unregisterFromVoiceRouterMessages(long id){ public void unregisterFromVoiceRouterMessages(long id) {
app.getRoutingHelper().getVoiceRouter().removeVoiceMessageListener(voiceRouterMessageCallbacks.get(id)); app.getRoutingHelper().getVoiceRouter().removeVoiceMessageListener(voiceRouterMessageCallbacks.get(id));
voiceRouterMessageCallbacks.remove(id); voiceRouterMessageCallbacks.remove(id);
} }
public boolean changeMapActivityKeyguardFlags(boolean enable) {
if (aMapKeyguardFlagsUpdateListener != null) {
aMapKeyguardFlagsUpdateListener.changeKeyguardFlags(enable);
return true;
}
return false;
}
public Map<String, ContextMenuButtonsParams> getContextMenuButtonsParams() { public Map<String, ContextMenuButtonsParams> getContextMenuButtonsParams() {
return contextMenuButtonsParams; return contextMenuButtonsParams;
} }
@ -2261,4 +2271,7 @@ public class OsmandAidlApi {
void onAMapPointUpdated(AMapPoint point, String layerId); void onAMapPointUpdated(AMapPoint point, String layerId);
} }
public interface AMapKeyguardFlagsUpdateListener {
void changeKeyguardFlags(boolean enable);
}
} }

View file

@ -1163,6 +1163,31 @@ public class OsmandAidlService extends Service implements AidlCallbackListener {
return UNKNOWN_API_ERROR; 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 { public static class AidlCallbackParams {

View file

@ -36,6 +36,7 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewStub; import android.view.ViewStub;
import android.view.Window; import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -47,6 +48,7 @@ import net.osmand.SecondSplashScreenFragment;
import net.osmand.StateChangedListener; import net.osmand.StateChangedListener;
import net.osmand.ValueHolder; import net.osmand.ValueHolder;
import net.osmand.access.MapAccessibilityActions; import net.osmand.access.MapAccessibilityActions;
import net.osmand.aidl.OsmandAidlApi;
import net.osmand.aidl.OsmandAidlApi.AMapPointUpdateListener; import net.osmand.aidl.OsmandAidlApi.AMapPointUpdateListener;
import net.osmand.aidl.map.ALatLon; import net.osmand.aidl.map.ALatLon;
import net.osmand.aidl.maplayer.point.AMapPoint; import net.osmand.aidl.maplayer.point.AMapPoint;
@ -157,7 +159,7 @@ import java.util.regex.Pattern;
public class MapActivity extends OsmandActionBarActivity implements DownloadEvents, public class MapActivity extends OsmandActionBarActivity implements DownloadEvents,
OnRequestPermissionsResultCallback, IRouteInformationListener, AMapPointUpdateListener, 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"; 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; 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()); 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 { private class ScreenOffReceiver extends BroadcastReceiver {
@Override @Override

View file

@ -2,7 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.osmand.turnScreenOn"> package="net.osmand.turnScreenOn">
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<application <application

View file

@ -708,4 +708,7 @@ interface IOsmAndAidlInterface {
boolean setCustomization(in CustomizationInfoParams params); boolean setCustomization(in CustomizationInfoParams params);
long registerForVoiceRouterMessages(in ANavigationVoiceRouterMessageParams params, IOsmAndAidlCallback callback); long registerForVoiceRouterMessages(in ANavigationVoiceRouterMessageParams params, IOsmAndAidlCallback callback);
boolean isMapActivityActive();
boolean changeMapActivityKeyguardFlags(in boolean enable);
} }

View file

@ -28,6 +28,8 @@ import net.osmand.turnScreenOn.helpers.LockHelper;
import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper; import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper;
import net.osmand.turnScreenOn.helpers.RadioGroupWrapper; import net.osmand.turnScreenOn.helpers.RadioGroupWrapper;
import net.osmand.turnScreenOn.helpers.SensorHelper; import net.osmand.turnScreenOn.helpers.SensorHelper;
import net.osmand.turnScreenOn.listener.LockHelperEventListener;
import net.osmand.turnScreenOn.listener.OnLockListener;
import net.osmand.turnScreenOn.listener.UnlockMessageListener; import net.osmand.turnScreenOn.listener.UnlockMessageListener;
import net.osmand.turnScreenOn.listener.OnMessageListener; import net.osmand.turnScreenOn.listener.OnMessageListener;
@ -42,6 +44,7 @@ public class MainActivity extends AppCompatActivity {
private SensorHelper sensorHelper; private SensorHelper sensorHelper;
private LockHelper lockHelper; private LockHelper lockHelper;
private OnMessageListener unlockMessageListener; private OnMessageListener unlockMessageListener;
private OnLockListener lockHelperEventListener;
private List<PluginSettings.OsmandVersion> availableOsmandVersions; private List<PluginSettings.OsmandVersion> availableOsmandVersions;
@ -73,6 +76,7 @@ public class MainActivity extends AppCompatActivity {
sensorHelper = app.getSensorHelper(); sensorHelper = app.getSensorHelper();
lockHelper = app.getLockHelper(); lockHelper = app.getLockHelper();
unlockMessageListener = new UnlockMessageListener(app); unlockMessageListener = new UnlockMessageListener(app);
lockHelperEventListener = new LockHelperEventListener(app);
} }
public void startProcess() { public void startProcess() {
@ -183,7 +187,7 @@ public class MainActivity extends AppCompatActivity {
closeDialog(); closeDialog();
refreshUI(); refreshUI();
} }
}, 5); }, 100);
} }
}; };
@ -272,6 +276,7 @@ public class MainActivity extends AppCompatActivity {
osmAndAidlHelper.registerForVoiceRouterMessages(); osmAndAidlHelper.registerForVoiceRouterMessages();
osmAndAidlHelper.addListener(unlockMessageListener); osmAndAidlHelper.addListener(unlockMessageListener);
sensorHelper.addListener(unlockMessageListener); sensorHelper.addListener(unlockMessageListener);
lockHelper.addLockListener(lockHelperEventListener);
tvPluginStateDescription.setText(getString(R.string.enabled)); tvPluginStateDescription.setText(getString(R.string.enabled));
tvPluginStateDescription.setTextColor(getResources().getColor(R.color.black)); tvPluginStateDescription.setTextColor(getResources().getColor(R.color.black));
@ -291,6 +296,8 @@ public class MainActivity extends AppCompatActivity {
osmAndAidlHelper.unregisterFromVoiceRouterMessages(); osmAndAidlHelper.unregisterFromVoiceRouterMessages();
osmAndAidlHelper.removeListener(unlockMessageListener); osmAndAidlHelper.removeListener(unlockMessageListener);
sensorHelper.removeListener(unlockMessageListener); sensorHelper.removeListener(unlockMessageListener);
lockHelper.removeLockListener(lockHelperEventListener);
lockHelper.disableFunction(); lockHelper.disableFunction();
tvPluginStateDescription.setText(getString(R.string.disabled)); tvPluginStateDescription.setText(getString(R.string.disabled));

View file

@ -7,8 +7,6 @@ import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import net.osmand.turnScreenOn.app.TurnScreenApp; import net.osmand.turnScreenOn.app.TurnScreenApp;

View file

@ -3,7 +3,6 @@ package net.osmand.turnScreenOn.app;
import android.app.Application; import android.app.Application;
import net.osmand.turnScreenOn.PluginSettings; import net.osmand.turnScreenOn.PluginSettings;
import net.osmand.turnScreenOn.helpers.AndroidUtils;
import net.osmand.turnScreenOn.helpers.LockHelper; import net.osmand.turnScreenOn.helpers.LockHelper;
import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper; import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper;
import net.osmand.turnScreenOn.helpers.SensorHelper; import net.osmand.turnScreenOn.helpers.SensorHelper;

View file

@ -1,29 +1,44 @@
package net.osmand.turnScreenOn.helpers; package net.osmand.turnScreenOn.helpers;
import android.app.KeyguardManager;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.ColorMatrix; import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter; import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Build;
import android.os.PowerManager;
import android.util.TypedValue; import android.util.TypedValue;
import static android.content.Context.POWER_SERVICE;
import static android.util.TypedValue.COMPLEX_UNIT_DIP; import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class AndroidUtils { public class AndroidUtils {
public static int dpToPx(Context ctx, float dp) { public static int dpToPx(Context ctx, float dp) {
Resources r = ctx.getResources(); Resources r = ctx.getResources();
return (int) TypedValue.applyDimension( return (int) TypedValue.applyDimension(
COMPLEX_UNIT_DIP, COMPLEX_UNIT_DIP,
dp, dp,
r.getDisplayMetrics() r.getDisplayMetrics()
); );
} }
public static Paint createPaintWithGreyScale(){ public static Paint createPaintWithGreyScale() {
Paint pGreyScale = new Paint(); Paint pGreyScale = new Paint();
ColorMatrix cm = new ColorMatrix(); ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0); cm.setSaturation(0);
pGreyScale.setColorFilter(new ColorMatrixColorFilter(cm)); pGreyScale.setColorFilter(new ColorMatrixColorFilter(cm));
return pGreyScale; 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();
}
} }

View file

@ -1,103 +1,126 @@
package net.osmand.turnScreenOn.helpers; package net.osmand.turnScreenOn.helpers;
import android.Manifest;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager; import android.app.admin.DevicePolicyManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import net.osmand.turnScreenOn.app.TurnScreenApp; import net.osmand.turnScreenOn.app.TurnScreenApp;
import net.osmand.turnScreenOn.listener.LockObservable;
import net.osmand.turnScreenOn.listener.OnLockListener;
import net.osmand.turnScreenOn.receiver.DeviceAdminRecv; import net.osmand.turnScreenOn.receiver.DeviceAdminRecv;
public class LockHelper { import java.util.ArrayList;
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;
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<OnLockListener> onLockListeners;
private final static String TAG = "LockHelperTag"; private boolean functionEnable = false;
public LockHelper(TurnScreenApp app) { public LockHelper(TurnScreenApp app) {
this.app = app; this.app = app;
this.context = app; uiHandler = new Handler();
uiHandler = new Handler(); mDeviceAdmin = new ComponentName(app, DeviceAdminRecv.class);
mDeviceAdmin = new ComponentName(context, DeviceAdminRecv.class); mDevicePolicyManager = (DevicePolicyManager) app.getSystemService(Context.DEVICE_POLICY_SERVICE);
mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); lockRunnable = new LockRunnable();
lockRunnable = new LockRunnable(); onLockListeners = new ArrayList<>();
keyguardLock = ((KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE)) }
.newKeyguardLock(TAG);
}
private void releaseWakeLocks() { private void releaseWakeLocks() {
if (wakeLock != null) { if (wakeLock != null) {
wakeLock.release(); wakeLock.release();
wakeLock = null; wakeLock = null;
} }
} }
private class LockRunnable implements Runnable { public void lock() {
@Override if (readyToLock()) {
public void run() { notifyOnLock();
lock(); releaseWakeLocks();
} if (functionEnable && !AndroidUtils.isScreenLocked(app)) {
} mDevicePolicyManager.lockNow();
}
}
}
public void lock() { public void unlock() {
if (readyToLock()) { if (readyToUnlock()) {
releaseWakeLocks(); notifyOnUnlock();
keyguardLock.reenableKeyguard(); PowerManager pm = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
if (functionEnable) { wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
mDevicePolicyManager.lockNow(); | PowerManager.ACQUIRE_CAUSES_WAKEUP
} | PowerManager.ON_AFTER_RELEASE, "tso:wakelocktag");
} wakeLock.acquire();
} }
}
public void unlock() { public void timedUnlock(long millis) {
if (readyToUnlock()) { uiHandler.removeCallbacks(lockRunnable);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); unlock();
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK uiHandler.postDelayed(lockRunnable, millis);
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "tso:wakelocktag"); }
wakeLock.acquire();
keyguardLock.disableKeyguard();
}
}
public void timedUnlock(long millis) { private boolean readyToLock() {
uiHandler.removeCallbacks(lockRunnable); return mDevicePolicyManager != null
unlock(); && mDeviceAdmin != null
uiHandler.postDelayed(lockRunnable, millis); && mDevicePolicyManager.isAdminActive(mDeviceAdmin);
} }
private boolean readyToLock() { private boolean readyToUnlock() {
return mDevicePolicyManager != null return wakeLock == null;
&& mDeviceAdmin != null }
&& mDevicePolicyManager.isAdminActive(mDeviceAdmin)
&& ContextCompat.checkSelfPermission(context, Manifest.permission.DISABLE_KEYGUARD)
== PackageManager.PERMISSION_GRANTED;
}
private boolean readyToUnlock() { public void disableFunction() {
return wakeLock==null functionEnable = false;
&& ContextCompat.checkSelfPermission(context, Manifest.permission.DISABLE_KEYGUARD) }
== PackageManager.PERMISSION_GRANTED;
}
public void disableFunction() { public void enableFunction() {
functionEnable = false; functionEnable = true;
} }
public void enableFunction() { @Override
functionEnable = true; 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();
}
}
} }

View file

@ -15,7 +15,7 @@ import net.osmand.aidl.navigation.ANavigationVoiceRouterMessageParams;
import net.osmand.aidl.search.SearchResult; import net.osmand.aidl.search.SearchResult;
import net.osmand.turnScreenOn.PluginSettings; import net.osmand.turnScreenOn.PluginSettings;
import net.osmand.turnScreenOn.app.TurnScreenApp; 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.listener.OnMessageListener;
import net.osmand.turnScreenOn.log.PlatformUtil; import net.osmand.turnScreenOn.log.PlatformUtil;
@ -24,170 +24,189 @@ import org.apache.commons.logging.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class OsmAndAidlHelper implements Observable { public class OsmAndAidlHelper implements MessageObservable {
private static final Log LOG = PlatformUtil.getLog(OsmAndAidlHelper.class); 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<OnMessageListener> messageListeners; private List<OnMessageListener> messageListeners;
private TurnScreenApp app; private TurnScreenApp app;
private PluginSettings settings; private PluginSettings settings;
private String connectedOsmandVersionPath; private String connectedOsmandVersionPath;
private long osmandUpdatesCallbackId = -1; private long osmandUpdatesCallbackId = -1;
private boolean attemptedRegister = false; private boolean attemptedRegister = false;
private boolean isRegistered = false; private boolean isRegistered = false;
private IOsmAndAidlInterface mIOsmAndAidlInterface; private IOsmAndAidlInterface mIOsmAndAidlInterface;
private IOsmAndAidlCallback mIOsmAndAidlCallbackInterface = new IOsmAndAidlCallback.Stub() { private IOsmAndAidlCallback mIOsmAndAidlCallbackInterface = new IOsmAndAidlCallback.Stub() {
@Override @Override
public void onSearchComplete(List<SearchResult> resultSet) throws RemoteException { public void onSearchComplete(List<SearchResult> resultSet) throws RemoteException {
} }
@Override @Override
public void onUpdate() throws RemoteException { public void onUpdate() throws RemoteException {
} }
@Override @Override
public void onAppInitialized() throws RemoteException { public void onAppInitialized() throws RemoteException {
} }
@Override @Override
public void onGpxBitmapCreated(AGpxBitmap bitmap) throws RemoteException { public void onGpxBitmapCreated(AGpxBitmap bitmap) throws RemoteException {
} }
@Override @Override
public void updateNavigationInfo(ADirectionInfo directionInfo) throws RemoteException { public void updateNavigationInfo(ADirectionInfo directionInfo) throws RemoteException {
} }
@Override @Override
public void onContextMenuButtonClicked(int buttonId, String pointId, String layerId) throws RemoteException { public void onContextMenuButtonClicked(int buttonId, String pointId, String layerId) throws RemoteException {
} }
@Override @Override
public void onVoiceRouterNotify() throws RemoteException { public void onVoiceRouterNotify() throws RemoteException {
notifyListeners(); notifyListeners();
} }
}; };
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service); mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service);
if (mIOsmAndAidlInterface != null && attemptedRegister) { if (mIOsmAndAidlInterface != null && attemptedRegister) {
attemptedRegister = false; attemptedRegister = false;
registerForVoiceRouterMessages(); registerForVoiceRouterMessages();
} }
} }
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
mIOsmAndAidlInterface = null; mIOsmAndAidlInterface = null;
isRegistered = false; isRegistered = false;
} }
}; };
public OsmAndAidlHelper(TurnScreenApp app) { public OsmAndAidlHelper(TurnScreenApp app) {
this.app = app; this.app = app;
settings = app.getSettings(); settings = app.getSettings();
messageListeners = new ArrayList<>(); messageListeners = new ArrayList<>();
} }
public void registerForVoiceRouterMessages() { public void registerForVoiceRouterMessages() {
try { try {
if (mIOsmAndAidlInterface != null && !isRegistered) { if (mIOsmAndAidlInterface != null && !isRegistered) {
ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams();
params.setSubscribeToUpdates(true); params.setSubscribeToUpdates(true);
params.setCallbackId(osmandUpdatesCallbackId); params.setCallbackId(osmandUpdatesCallbackId);
osmandUpdatesCallbackId = mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface); osmandUpdatesCallbackId = mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface);
isRegistered = true; isRegistered = true;
} else { } else {
attemptedRegister = true; attemptedRegister = true;
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
} }
public void unregisterFromVoiceRouterMessages() { public void unregisterFromVoiceRouterMessages() {
try { try {
if (mIOsmAndAidlInterface != null && isRegistered) { if (mIOsmAndAidlInterface != null && isRegistered) {
ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams();
params.setSubscribeToUpdates(false); params.setSubscribeToUpdates(false);
params.setCallbackId(osmandUpdatesCallbackId); params.setCallbackId(osmandUpdatesCallbackId);
mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface); mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallbackInterface);
isRegistered = false; isRegistered = false;
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
} }
public void reconnectOsmand() { public boolean isMapActivityActive() {
PluginSettings.OsmandVersion versionToConnect = settings.getOsmandVersion(); try {
if (versionToConnect != null) { if (mIOsmAndAidlInterface != null) {
String newOsmandVersionPath = versionToConnect.getPath(); return mIOsmAndAidlInterface.isMapActivityActive();
if (connectedOsmandVersionPath == null }
|| !connectedOsmandVersionPath.equals(newOsmandVersionPath) } catch (RemoteException e) {
|| mIOsmAndAidlInterface == null) { }
cleanupResources(); return false;
connectOsmand(); }
}
connectedOsmandVersionPath = newOsmandVersionPath;
}
}
public void connectOsmand() { public void changeMapActivityKeyguardFlags(boolean newState) {
bindService(settings.getOsmandVersion().getPath()); try {
} if (mIOsmAndAidlInterface != null) {
mIOsmAndAidlInterface.changeMapActivityKeyguardFlags(newState);
}
} catch (RemoteException e) {
}
}
private void bindService(String packageName) { public void reconnectOsmand() {
if (mIOsmAndAidlInterface == null) { PluginSettings.OsmandVersion versionToConnect = settings.getOsmandVersion();
Intent intent = new Intent(OSMAND_AIDL_SERVICE_PATH); if (versionToConnect != null) {
intent.setPackage(packageName); String newOsmandVersionPath = versionToConnect.getPath();
app.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); if (connectedOsmandVersionPath == null
} || !connectedOsmandVersionPath.equals(newOsmandVersionPath)
} || mIOsmAndAidlInterface == null) {
cleanupResources();
connectOsmand();
}
connectedOsmandVersionPath = newOsmandVersionPath;
}
}
private void cleanupResources() { public void connectOsmand() {
try { bindService(settings.getOsmandVersion().getPath());
if (mIOsmAndAidlInterface != null) { }
mIOsmAndAidlInterface = null;
unregisterFromVoiceRouterMessages();
isRegistered = false;
app.unbindService(mConnection);
}
} catch (Throwable e) {
}
}
@Override private void bindService(String packageName) {
public void addListener(OnMessageListener listener) { if (mIOsmAndAidlInterface == null) {
if (listener != null && !messageListeners.contains(listener)) { Intent intent = new Intent(OSMAND_AIDL_SERVICE_PATH);
messageListeners.add(listener); intent.setPackage(packageName);
} app.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
} }
}
@Override private void cleanupResources() {
public void removeListener(OnMessageListener listener) { try {
if (listener != null) { if (mIOsmAndAidlInterface != null) {
messageListeners.remove(listener); mIOsmAndAidlInterface = null;
} unregisterFromVoiceRouterMessages();
} isRegistered = false;
app.unbindService(mConnection);
}
} catch (Throwable e) {
}
}
@Override @Override
public void notifyListeners() { public void addListener(OnMessageListener listener) {
if (messageListeners != null) { if (listener != null && !messageListeners.contains(listener)) {
for (OnMessageListener listener : messageListeners) { messageListeners.add(listener);
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();
}
}
}
} }

View file

@ -5,17 +5,16 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
import android.hardware.SensorEventListener; import android.hardware.SensorEventListener;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.util.Log;
import net.osmand.turnScreenOn.app.TurnScreenApp; 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.listener.OnMessageListener;
import net.osmand.turnScreenOn.log.PlatformUtil; import net.osmand.turnScreenOn.log.PlatformUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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 org.apache.commons.logging.Log LOG = PlatformUtil.getLog(SensorHelper.class);
private static final int SENSOR_SENSITIVITY = 4; private static final int SENSOR_SENSITIVITY = 4;

View file

@ -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);
}
}

View file

@ -0,0 +1,8 @@
package net.osmand.turnScreenOn.listener;
public interface LockObservable {
void addLockListener(OnLockListener listener);
void removeLockListener(OnLockListener listener);
void notifyOnLock();
void notifyOnUnlock();
}

View file

@ -1,6 +1,6 @@
package net.osmand.turnScreenOn.listener; package net.osmand.turnScreenOn.listener;
public interface Observable { public interface MessageObservable {
void addListener(OnMessageListener listener); void addListener(OnMessageListener listener);
void removeListener(OnMessageListener listener); void removeListener(OnMessageListener listener);

View file

@ -0,0 +1,6 @@
package net.osmand.turnScreenOn.listener;
public interface OnLockListener {
void onLock();
void onUnlock();
}

View file

@ -1,14 +1,15 @@
package net.osmand.turnScreenOn.listener; package net.osmand.turnScreenOn.listener;
import android.util.Log;
import net.osmand.turnScreenOn.PluginSettings; import net.osmand.turnScreenOn.PluginSettings;
import net.osmand.turnScreenOn.app.TurnScreenApp; import net.osmand.turnScreenOn.app.TurnScreenApp;
import net.osmand.turnScreenOn.helpers.AndroidUtils;
import net.osmand.turnScreenOn.helpers.LockHelper; import net.osmand.turnScreenOn.helpers.LockHelper;
import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper;
public class UnlockMessageListener implements OnMessageListener { public class UnlockMessageListener implements OnMessageListener {
private TurnScreenApp app; private TurnScreenApp app;
private PluginSettings settings; private PluginSettings settings;
private OsmAndAidlHelper osmAndAidlHelper;
private LockHelper lockHelper; private LockHelper lockHelper;
public UnlockMessageListener(TurnScreenApp app) { public UnlockMessageListener(TurnScreenApp app) {
@ -16,14 +17,19 @@ public class UnlockMessageListener implements OnMessageListener {
settings = app.getSettings(); settings = app.getSettings();
lockHelper = app.getLockHelper(); lockHelper = app.getLockHelper();
osmAndAidlHelper = app.getOsmAndAidlHelper();
} }
@Override @Override
public void onMessageReceive() { public void onMessageReceive() {
if (settings != null && lockHelper != null) { boolean isScreenOn = AndroidUtils.isScreenOn(app);
if (settings.isAdminDevicePermissionAvailable()) { boolean isScreenLocked = AndroidUtils.isScreenLocked(app);
PluginSettings.UnlockTime time = settings.getTime(); if ((!isScreenOn || isScreenLocked) && osmAndAidlHelper.isMapActivityActive()) {
lockHelper.timedUnlock(time.getSeconds() * 1000L); if (settings != null && lockHelper != null) {
if (settings.isAdminDevicePermissionAvailable()) {
PluginSettings.UnlockTime time = settings.getTime();
lockHelper.timedUnlock(time.getSeconds() * 1000L);
}
} }
} }
} }

View file

@ -3,22 +3,15 @@ package net.osmand.turnScreenOn.receiver;
import android.app.admin.DeviceAdminReceiver; import android.app.admin.DeviceAdminReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
public class DeviceAdminRecv extends DeviceAdminReceiver { public class DeviceAdminRecv extends DeviceAdminReceiver {
private static final String TAG = "DeviceAdminReceiver"; public void onEnabled(Context context, Intent intent) { }
public void onEnabled(Context context, Intent intent) {
Log.d(TAG, "permission disabled");
}
public CharSequence onDisableRequested(Context context, Intent intent) { public CharSequence onDisableRequested(Context context, Intent intent) {
return null; return null;
} }
public void onDisabled(Context context, Intent intent) { public void onDisabled(Context context, Intent intent) { }
Log.d(TAG, "permission enabled");
}
} }