diff --git a/MotoActions/AndroidManifest.xml b/MotoActions/AndroidManifest.xml index 13f3036..3e0f13b 100644 --- a/MotoActions/AndroidManifest.xml +++ b/MotoActions/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -14,10 +15,6 @@ - - mScreenStateNotifiers = new LinkedList(); - private final List mUpdatedStateNotifiers = - new LinkedList(); + private final List mScreenStateNotifiers = new LinkedList<>(); + private final List mUpdatedStateNotifiers = new LinkedList<>(); public MotoActionsService(Context context) { super("MotoActionService"); - mContext = context; Log.d(TAG, "Starting"); MotoActionsSettings motoActionsSettings = new MotoActionsSettings(context, this); - mSensorHelper = new SensorHelper(context); - mScreenReceiver = new ScreenReceiver(context, this); + SensorHelper sensorHelper = new SensorHelper(context); + new ScreenReceiver(context, this); - mDozePulseAction = new DozePulseAction(context); + DozePulseAction mDozePulseAction = new DozePulseAction(context); mScreenStateNotifiers.add(mDozePulseAction); // Actionable sensors get screen on/off notifications - mScreenStateNotifiers.add(new GlanceSensor(motoActionsSettings, mSensorHelper, mDozePulseAction)); - mScreenStateNotifiers.add(new ProximitySensor(motoActionsSettings, mSensorHelper, mDozePulseAction)); + mScreenStateNotifiers.add(new GlanceSensor(motoActionsSettings, sensorHelper, mDozePulseAction)); + mScreenStateNotifiers.add(new ProximitySensor(motoActionsSettings, sensorHelper, mDozePulseAction)); // Other actions that are always enabled - mUpdatedStateNotifiers.add(new CameraActivationSensor(motoActionsSettings, mSensorHelper)); - mUpdatedStateNotifiers.add(new ChopChopSensor(motoActionsSettings, mSensorHelper)); - mUpdatedStateNotifiers.add(new ProximitySilencer(motoActionsSettings, context, mSensorHelper)); - mUpdatedStateNotifiers.add(new FlipToMute(motoActionsSettings, context, mSensorHelper)); - mUpdatedStateNotifiers.add(new LiftToSilence(motoActionsSettings, context, mSensorHelper)); + mUpdatedStateNotifiers.add(new CameraActivationSensor(motoActionsSettings, sensorHelper)); + mUpdatedStateNotifiers.add(new ChopChopSensor(motoActionsSettings, sensorHelper)); + mUpdatedStateNotifiers.add(new ProximitySilencer(motoActionsSettings, context, sensorHelper)); + mUpdatedStateNotifiers.add(new FlipToMute(motoActionsSettings, context, sensorHelper)); + mUpdatedStateNotifiers.add(new LiftToSilence(motoActionsSettings, context, sensorHelper)); mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MotoActionsWakeLock"); + String tag = context.getPackageName() + ":ServiceWakeLock"; + mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, tag); updateState(); } diff --git a/MotoActions/src/com/moto/actions/MotoActionsSettings.java b/MotoActions/src/com/moto/actions/MotoActionsSettings.java index ca7cccf..af40fba 100644 --- a/MotoActions/src/com/moto/actions/MotoActionsSettings.java +++ b/MotoActions/src/com/moto/actions/MotoActionsSettings.java @@ -30,7 +30,7 @@ import com.moto.actions.actions.UpdatedStateNotifier; import com.moto.actions.actions.CameraActivationAction; import com.moto.actions.actions.TorchAction; -public class MotoActionsSettings { +public class MotoActionsSettings implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = "MotoActions"; private static final String GESTURE_CAMERA_ACTION_KEY = "gesture_camera_action"; @@ -55,7 +55,7 @@ public class MotoActionsSettings { public MotoActionsSettings(Context context, UpdatedStateNotifier updatedStateNotifier) { SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); loadPreferences(sharedPrefs); - sharedPrefs.registerOnSharedPreferenceChangeListener(mPrefListener); + sharedPrefs.registerOnSharedPreferenceChangeListener(this); mContext = context; mUpdatedStateNotifier = updatedStateNotifier; } @@ -114,8 +114,6 @@ public class MotoActionsSettings { mLiftToSilenceEnabled = sharedPreferences.getBoolean(GESTURE_LIFT_TO_SILENCE_KEY, false); } - private SharedPreferences.OnSharedPreferenceChangeListener mPrefListener = - new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { boolean updated = true; @@ -146,5 +144,4 @@ public class MotoActionsSettings { mUpdatedStateNotifier.updateState(); } } - }; } diff --git a/MotoActions/src/com/moto/actions/SensorAction.java b/MotoActions/src/com/moto/actions/SensorAction.java index bb0a21e..11a5df2 100644 --- a/MotoActions/src/com/moto/actions/SensorAction.java +++ b/MotoActions/src/com/moto/actions/SensorAction.java @@ -18,5 +18,5 @@ package com.moto.actions; public interface SensorAction { - public void action(); + void action(); } diff --git a/MotoActions/src/com/moto/actions/SensorHelper.java b/MotoActions/src/com/moto/actions/SensorHelper.java index 2a9bfff..6312ffe 100644 --- a/MotoActions/src/com/moto/actions/SensorHelper.java +++ b/MotoActions/src/com/moto/actions/SensorHelper.java @@ -26,7 +26,6 @@ import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEventListener; import android.hardware.SensorManager; -import android.util.Log; public class SensorHelper { private static final String TAG = "MotoActions"; @@ -45,7 +44,7 @@ public class SensorHelper { public SensorHelper(Context context) { mContext = context; - mSensorManager = (SensorManager) mContext .getSystemService(Context.SENSOR_SERVICE); + mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); dumpSensorsList(); } diff --git a/MotoActions/src/com/moto/actions/actions/CameraActivationAction.java b/MotoActions/src/com/moto/actions/actions/CameraActivationAction.java index f8ddc09..f77e27c 100644 --- a/MotoActions/src/com/moto/actions/actions/CameraActivationAction.java +++ b/MotoActions/src/com/moto/actions/actions/CameraActivationAction.java @@ -17,8 +17,6 @@ package com.moto.actions.actions; -import java.util.List; - import android.app.KeyguardManager; import android.content.ComponentName; import android.content.Context; @@ -28,12 +26,14 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.PowerManager; import android.os.PowerManager.WakeLock; +import android.os.VibrationEffect; import android.os.Vibrator; import android.provider.MediaStore; -import android.util.Log; import com.moto.actions.SensorAction; +import java.util.List; + public class CameraActivationAction implements SensorAction { private static final String TAG = "MotoActions"; @@ -55,7 +55,7 @@ public class CameraActivationAction implements SensorAction { public void action() { vibrate(); turnScreenOn(); - if (mKeyguardManager.inKeyguardRestrictedInputMode()) { + if (mKeyguardManager.isKeyguardLocked()) { launchSecureCamera(); } else { launchCamera(); @@ -63,13 +63,16 @@ public class CameraActivationAction implements SensorAction { } private void vibrate() { - Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); - v.vibrate(500); + Vibrator vib = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); + if (vib == null) return; + VibrationEffect effect = VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE); + vib.vibrate(effect); } private void turnScreenOn() { - PowerManager.WakeLock wl = mPowerManager.newWakeLock( - PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG); + String tag = mContext.getPackageName() + ":CameraWakeLock"; + WakeLock wl = mPowerManager.newWakeLock( + PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, tag); wl.acquire(TURN_SCREEN_ON_WAKE_LOCK_MS); } diff --git a/MotoActions/src/com/moto/actions/actions/CameraActivationSensor.java b/MotoActions/src/com/moto/actions/actions/CameraActivationSensor.java index a70975e..1c219ae 100644 --- a/MotoActions/src/com/moto/actions/actions/CameraActivationSensor.java +++ b/MotoActions/src/com/moto/actions/actions/CameraActivationSensor.java @@ -17,12 +17,9 @@ package com.moto.actions.actions; -import java.util.List; - import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; -import android.hardware.SensorManager; import android.util.Log; import com.moto.actions.MotoActionsSettings; @@ -31,20 +28,14 @@ import com.moto.actions.SensorHelper; public class CameraActivationSensor implements SensorEventListener, UpdatedStateNotifier { private static final String TAG = "MotoActions-CameraSensor"; - private static final int TURN_SCREEN_ON_WAKE_LOCK_MS = 500; - private final MotoActionsSettings mMotoActionsSettings; - private final SensorHelper mSensorHelper; - - private final Sensor mSensor; private boolean mIsEnabled; public CameraActivationSensor(MotoActionsSettings motoActionsSettings, SensorHelper sensorHelper) { mMotoActionsSettings = motoActionsSettings; - mSensorHelper = sensorHelper; - mSensor = sensorHelper.getCameraActivationSensor(); - mSensorHelper.registerListener(mSensor, this); + Sensor sensor = sensorHelper.getCameraActivationSensor(); + sensorHelper.registerListener(sensor, this); } @Override diff --git a/MotoActions/src/com/moto/actions/actions/ChopChopSensor.java b/MotoActions/src/com/moto/actions/actions/ChopChopSensor.java index f5bc3e3..fc9f12e 100644 --- a/MotoActions/src/com/moto/actions/actions/ChopChopSensor.java +++ b/MotoActions/src/com/moto/actions/actions/ChopChopSensor.java @@ -17,12 +17,9 @@ package com.moto.actions.actions; -import java.util.List; - import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; -import android.hardware.SensorManager; import android.util.Log; import com.moto.actions.MotoActionsSettings; @@ -31,8 +28,6 @@ import com.moto.actions.SensorHelper; public class ChopChopSensor implements SensorEventListener, UpdatedStateNotifier { private static final String TAG = "MotoActions-ChopChopSensor"; - private static final int TURN_SCREEN_ON_WAKE_LOCK_MS = 500; - private final MotoActionsSettings mMotoActionsSettings; private final SensorHelper mSensorHelper; private final Sensor mSensor; diff --git a/MotoActions/src/com/moto/actions/actions/FlipToMute.java b/MotoActions/src/com/moto/actions/actions/FlipToMute.java index 8676ad1..a7c753f 100644 --- a/MotoActions/src/com/moto/actions/actions/FlipToMute.java +++ b/MotoActions/src/com/moto/actions/actions/FlipToMute.java @@ -25,6 +25,7 @@ import android.content.IntentFilter; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; +import android.os.VibrationEffect; import android.os.Vibrator; import android.util.Log; @@ -57,7 +58,9 @@ public class FlipToMute implements UpdatedStateNotifier { mStow = sensorHelper.getStowSensor(); mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + if (mNotificationManager != null) { mFilter = mNotificationManager.getCurrentInterruptionFilter(); + } mReceiver = new Receiver(); } @@ -123,8 +126,10 @@ public class FlipToMute implements UpdatedStateNotifier { } private void vibrate() { - Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); - v.vibrate(250); + Vibrator vib = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); + if (vib == null) return; + VibrationEffect effect = VibrationEffect.createOneShot(250, VibrationEffect.DEFAULT_AMPLITUDE); + vib.vibrate(effect); } public class Receiver extends BroadcastReceiver { @@ -132,6 +137,7 @@ public class FlipToMute implements UpdatedStateNotifier { @Override public void onReceive(Context context, Intent intent) { if (!mIsFlatDown && !mIsStowed) { + if (mNotificationManager == null) return; mFilter = mNotificationManager.getCurrentInterruptionFilter(); Log.d(TAG, "Interrupt filter: Backup"); } diff --git a/MotoActions/src/com/moto/actions/actions/ProximitySilencer.java b/MotoActions/src/com/moto/actions/actions/ProximitySilencer.java index 70a7813..71c2cfa 100644 --- a/MotoActions/src/com/moto/actions/actions/ProximitySilencer.java +++ b/MotoActions/src/com/moto/actions/actions/ProximitySilencer.java @@ -72,22 +72,18 @@ public class ProximitySilencer extends PhoneStateListener implements SensorEvent long now = System.currentTimeMillis(); if (isNear){ - if (mIsRinging && (now - mRingStartedMs >= SILENCE_DELAY_MS)){ - mCoveredRinging = true; - } else { - mCoveredRinging = false; - } + mCoveredRinging = mIsRinging && (now - mRingStartedMs >= SILENCE_DELAY_MS); return; } - if (!isNear && mIsRinging) { + if (mIsRinging) { Log.d(TAG, "event: " + event.values[0] + ", " + " covered " + Boolean.toString(mCoveredRinging)); if (mCoveredRinging) { Log.d(TAG, "Silencing ringer"); mTelecomManager.silenceRinger(); } else { Log.d(TAG, "Ignoring silence gesture: " + now + " is too close to " + - mRingStartedMs + ", delay=" + SILENCE_DELAY_MS + " or covered " + Boolean.toString(mCoveredRinging)); + mRingStartedMs + ", delay=" + SILENCE_DELAY_MS); } mCoveredRinging = false; } diff --git a/MotoActions/src/com/moto/actions/actions/TorchAction.java b/MotoActions/src/com/moto/actions/actions/TorchAction.java index 064d9f7..58680ba 100644 --- a/MotoActions/src/com/moto/actions/actions/TorchAction.java +++ b/MotoActions/src/com/moto/actions/actions/TorchAction.java @@ -18,19 +18,18 @@ package com.moto.actions.actions; import android.content.Context; -import android.hardware.camera2.CameraManager; -import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraAccessException; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraManager; +import android.os.VibrationEffect; import android.os.Vibrator; -import android.util.Log; +import android.support.annotation.NonNull; import com.moto.actions.SensorAction; public class TorchAction implements SensorAction { private static final String TAG = "MotoActions"; - private static final int TURN_SCREEN_ON_WAKE_LOCK_MS = 500; - private CameraManager mCameraManager; private final Vibrator mVibrator; private String mRearCameraId; @@ -51,17 +50,20 @@ public class TorchAction implements SensorAction { } } } catch (CameraAccessException e) { + // Noop } } @Override public void action() { - mVibrator.vibrate(250); + VibrationEffect vibrationEffect = VibrationEffect.createOneShot(250, VibrationEffect.DEFAULT_AMPLITUDE); + mVibrator.vibrate(vibrationEffect); if (mRearCameraId != null) { try { mCameraManager.setTorchMode(mRearCameraId, !mTorchEnabled); mTorchEnabled = !mTorchEnabled; } catch (CameraAccessException e) { + // Noop } } } @@ -69,14 +71,14 @@ public class TorchAction implements SensorAction { private class MyTorchCallback extends CameraManager.TorchCallback { @Override - public void onTorchModeChanged(String cameraId, boolean enabled) { + public void onTorchModeChanged(@NonNull String cameraId, boolean enabled) { if (!cameraId.equals(mRearCameraId)) return; mTorchEnabled = enabled; } @Override - public void onTorchModeUnavailable(String cameraId) { + public void onTorchModeUnavailable(@NonNull String cameraId) { if (!cameraId.equals(mRearCameraId)) return; mTorchEnabled = false; diff --git a/MotoActions/src/com/moto/actions/actions/UpdatedStateNotifier.java b/MotoActions/src/com/moto/actions/actions/UpdatedStateNotifier.java index f260771..3cc346e 100644 --- a/MotoActions/src/com/moto/actions/actions/UpdatedStateNotifier.java +++ b/MotoActions/src/com/moto/actions/actions/UpdatedStateNotifier.java @@ -18,5 +18,5 @@ package com.moto.actions.actions; public interface UpdatedStateNotifier { - public void updateState(); + void updateState(); } diff --git a/MotoActions/src/com/moto/actions/doze/DozePulseAction.java b/MotoActions/src/com/moto/actions/doze/DozePulseAction.java index 6760951..8ac4ab0 100644 --- a/MotoActions/src/com/moto/actions/doze/DozePulseAction.java +++ b/MotoActions/src/com/moto/actions/doze/DozePulseAction.java @@ -52,7 +52,7 @@ public class DozePulseAction implements SensorAction, ScreenStateNotifier { } } - public synchronized boolean mayDoze() { + private synchronized boolean mayDoze() { long now = System.currentTimeMillis(); if (now - mLastDoze > DELAY_BETWEEN_DOZES_IN_MS) { Log.d(TAG, "Allowing doze"); diff --git a/MotoActions/src/com/moto/actions/doze/ScreenReceiver.java b/MotoActions/src/com/moto/actions/doze/ScreenReceiver.java index 4281342..b9aa691 100644 --- a/MotoActions/src/com/moto/actions/doze/ScreenReceiver.java +++ b/MotoActions/src/com/moto/actions/doze/ScreenReceiver.java @@ -39,6 +39,8 @@ public class ScreenReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { + if (intent.getAction() == null) return; + if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { mNotifier.screenTurnedOff(); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { diff --git a/MotoActions/src/com/moto/actions/doze/ScreenStateNotifier.java b/MotoActions/src/com/moto/actions/doze/ScreenStateNotifier.java index c87b813..438fff2 100644 --- a/MotoActions/src/com/moto/actions/doze/ScreenStateNotifier.java +++ b/MotoActions/src/com/moto/actions/doze/ScreenStateNotifier.java @@ -18,6 +18,6 @@ package com.moto.actions.doze; public interface ScreenStateNotifier { - public void screenTurnedOn(); - public void screenTurnedOff(); + void screenTurnedOn(); + void screenTurnedOff(); }