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

View file

@ -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 {

View file

@ -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

View file

@ -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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,12 +1,16 @@
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 {
@ -19,11 +23,22 @@ public class AndroidUtils {
);
}
public static Paint createPaintWithGreyScale(){
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();
}
}

View file

@ -1,42 +1,36 @@
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 {
import java.util.ArrayList;
public class LockHelper implements LockObservable {
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 ArrayList<OnLockListener> onLockListeners;
private boolean functionEnable = false;
private final static String TAG = "LockHelperTag";
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);
mDeviceAdmin = new ComponentName(app, DeviceAdminRecv.class);
mDevicePolicyManager = (DevicePolicyManager) app.getSystemService(Context.DEVICE_POLICY_SERVICE);
lockRunnable = new LockRunnable();
keyguardLock = ((KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE))
.newKeyguardLock(TAG);
onLockListeners = new ArrayList<>();
}
private void releaseWakeLocks() {
@ -46,18 +40,11 @@ public class LockHelper {
}
}
private class LockRunnable implements Runnable {
@Override
public void run() {
lock();
}
}
public void lock() {
if (readyToLock()) {
notifyOnLock();
releaseWakeLocks();
keyguardLock.reenableKeyguard();
if (functionEnable) {
if (functionEnable && !AndroidUtils.isScreenLocked(app)) {
mDevicePolicyManager.lockNow();
}
}
@ -65,11 +52,12 @@ public class LockHelper {
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");
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();
keyguardLock.disableKeyguard();
}
}
@ -82,15 +70,11 @@ public class LockHelper {
private boolean readyToLock() {
return mDevicePolicyManager != null
&& mDeviceAdmin != null
&& mDevicePolicyManager.isAdminActive(mDeviceAdmin)
&& ContextCompat.checkSelfPermission(context, Manifest.permission.DISABLE_KEYGUARD)
== PackageManager.PERMISSION_GRANTED;
&& mDevicePolicyManager.isAdminActive(mDeviceAdmin);
}
private boolean readyToUnlock() {
return wakeLock==null
&& ContextCompat.checkSelfPermission(context, Manifest.permission.DISABLE_KEYGUARD)
== PackageManager.PERMISSION_GRANTED;
return wakeLock == null;
}
public void disableFunction() {
@ -100,4 +84,43 @@ public class LockHelper {
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();
}
}
}

View file

@ -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,7 +24,7 @@ import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
public class OsmAndAidlHelper implements Observable {
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";
@ -130,6 +130,25 @@ public class OsmAndAidlHelper implements Observable {
}
}
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 reconnectOsmand() {
PluginSettings.OsmandVersion versionToConnect = settings.getOsmandVersion();
if (versionToConnect != null) {

View file

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

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;
public interface Observable {
public interface MessageObservable {
void addListener(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;
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,10 +17,14 @@ public class UnlockMessageListener implements OnMessageListener {
settings = app.getSettings();
lockHelper = app.getLockHelper();
osmAndAidlHelper = app.getOsmAndAidlHelper();
}
@Override
public void onMessageReceive() {
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();
@ -27,4 +32,5 @@ public class UnlockMessageListener implements OnMessageListener {
}
}
}
}
}

View file

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