fix locking/unlocking
This commit is contained in:
parent
9807fe212d
commit
d7dd44fea3
20 changed files with 417 additions and 260 deletions
|
@ -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);
|
||||
}
|
|
@ -203,6 +203,7 @@ public class OsmandAidlApi {
|
|||
private Map<Long, VoiceRouter.VoiceMessageListener> 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,11 +1923,19 @@ 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<String, ContextMenuButtonsParams> getContextMenuButtonsParams() {
|
||||
return contextMenuButtonsParams;
|
||||
}
|
||||
|
@ -2261,4 +2271,7 @@ public class OsmandAidlApi {
|
|||
void onAMapPointUpdated(AMapPoint point, String layerId);
|
||||
|
||||
}
|
||||
public interface AMapKeyguardFlagsUpdateListener {
|
||||
void changeKeyguardFlags(boolean enable);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="net.osmand.turnScreenOn">
|
||||
|
||||
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
<application
|
||||
|
|
|
@ -708,4 +708,7 @@ interface IOsmAndAidlInterface {
|
|||
|
||||
boolean setCustomization(in CustomizationInfoParams params);
|
||||
long registerForVoiceRouterMessages(in ANavigationVoiceRouterMessageParams params, IOsmAndAidlCallback callback);
|
||||
|
||||
boolean isMapActivityActive();
|
||||
boolean changeMapActivityKeyguardFlags(in boolean enable);
|
||||
}
|
|
@ -28,6 +28,8 @@ import net.osmand.turnScreenOn.helpers.LockHelper;
|
|||
import net.osmand.turnScreenOn.helpers.OsmAndAidlHelper;
|
||||
import net.osmand.turnScreenOn.helpers.RadioGroupWrapper;
|
||||
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.OnMessageListener;
|
||||
|
||||
|
@ -42,6 +44,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
private SensorHelper sensorHelper;
|
||||
private LockHelper lockHelper;
|
||||
private OnMessageListener unlockMessageListener;
|
||||
private OnLockListener lockHelperEventListener;
|
||||
|
||||
private List<PluginSettings.OsmandVersion> availableOsmandVersions;
|
||||
|
||||
|
@ -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,6 +276,7 @@ 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));
|
||||
|
@ -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));
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<OnLockListener> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<OnMessageListener> messageListeners;
|
||||
private List<OnMessageListener> 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<SearchResult> resultSet) throws RemoteException {
|
||||
@Override
|
||||
public void onSearchComplete(List<SearchResult> 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 connectOsmand() {
|
||||
bindService(settings.getOsmandVersion().getPath());
|
||||
}
|
||||
public void changeMapActivityKeyguardFlags(boolean newState) {
|
||||
try {
|
||||
if (mIOsmAndAidlInterface != null) {
|
||||
mIOsmAndAidlInterface.changeMapActivityKeyguardFlags(newState);
|
||||
}
|
||||
} catch (RemoteException 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);
|
||||
}
|
||||
}
|
||||
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 cleanupResources() {
|
||||
try {
|
||||
if (mIOsmAndAidlInterface != null) {
|
||||
mIOsmAndAidlInterface = null;
|
||||
unregisterFromVoiceRouterMessages();
|
||||
isRegistered = false;
|
||||
app.unbindService(mConnection);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
public void connectOsmand() {
|
||||
bindService(settings.getOsmandVersion().getPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(OnMessageListener listener) {
|
||||
if (listener != null && !messageListeners.contains(listener)) {
|
||||
messageListeners.add(listener);
|
||||
}
|
||||
}
|
||||
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 removeListener(OnMessageListener listener) {
|
||||
if (listener != null) {
|
||||
messageListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
private void cleanupResources() {
|
||||
try {
|
||||
if (mIOsmAndAidlInterface != null) {
|
||||
mIOsmAndAidlInterface = null;
|
||||
unregisterFromVoiceRouterMessages();
|
||||
isRegistered = false;
|
||||
app.unbindService(mConnection);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyListeners() {
|
||||
if (messageListeners != null) {
|
||||
for (OnMessageListener listener : messageListeners) {
|
||||
listener.onMessageReceive();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void addListener(OnMessageListener listener) {
|
||||
if (listener != null && !messageListeners.contains(listener)) {
|
||||
messageListeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package net.osmand.turnScreenOn.listener;
|
||||
|
||||
public interface LockObservable {
|
||||
void addLockListener(OnLockListener listener);
|
||||
void removeLockListener(OnLockListener listener);
|
||||
void notifyOnLock();
|
||||
void notifyOnUnlock();
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package net.osmand.turnScreenOn.listener;
|
||||
|
||||
public interface Observable {
|
||||
public interface MessageObservable {
|
||||
void addListener(OnMessageListener listener);
|
||||
void removeListener(OnMessageListener listener);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package net.osmand.turnScreenOn.listener;
|
||||
|
||||
public interface OnLockListener {
|
||||
void onLock();
|
||||
void onUnlock();
|
||||
}
|
|
@ -1,14 +1,15 @@
|
|||
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) {
|
||||
|
@ -16,14 +17,19 @@ public class UnlockMessageListener implements OnMessageListener {
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) { }
|
||||
|
||||
}
|
Loading…
Reference in a new issue