Actions: Robustify
* Use not deprecated APIs where possible * Added some NPE safety checks * General cleaning of unsed code
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
<original-package android:name="com.cyanogenmod.settings.device" />
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
|
||||
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.moto.actions;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import android.preference.PreferenceActivity;
|
||||
|
||||
public class ActionsPreferenceActivity extends PreferenceActivity {
|
||||
@@ -25,9 +24,12 @@ public class ActionsPreferenceActivity extends PreferenceActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (savedInstanceState == null){
|
||||
|
||||
if (getActionBar() != null) {
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(android.R.id.content, new ActionsPreferenceFragment()).commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,6 @@ import android.os.Bundle;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
|
||||
public class ActionsPreferenceFragment extends PreferenceFragment {
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
|
||||
@@ -27,7 +27,6 @@ import android.content.ServiceConnection;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import com.moto.actions.util.FileUtils;
|
||||
import com.moto.actions.actions.Constants;
|
||||
import com.moto.actions.ServiceWrapper.LocalBinder;
|
||||
|
||||
@@ -41,6 +40,10 @@ public class BootCompletedReceiver extends BroadcastReceiver {
|
||||
public void onReceive(final Context context, Intent intent) {
|
||||
Log.i(TAG, "Booting");
|
||||
|
||||
if (intent.getAction() != null && !intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Restore nodes to saved preference values
|
||||
for (String pref : Constants.sPrefKeys) {
|
||||
Constants.writePreference(context, pref);
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.moto.actions;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import android.preference.PreferenceActivity;
|
||||
|
||||
public class DozeSettingsActivity extends PreferenceActivity {
|
||||
@@ -25,9 +24,12 @@ public class DozeSettingsActivity extends PreferenceActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (savedInstanceState == null){
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(android.R.id.content, new DozeSettingsFragment()).commit();
|
||||
if (getActionBar() != null) {
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
getActionBar().setTitle(R.string.ambient_display_title);
|
||||
}
|
||||
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(android.R.id.content, new DozeSettingsFragment()).commit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package com.moto.actions;
|
||||
|
||||
import android.app.ActionBar;
|
||||
import android.os.Bundle;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.provider.Settings;
|
||||
@@ -41,14 +40,6 @@ public class DozeSettingsFragment extends PreferenceFragment {
|
||||
private String KEY_GESTURE_HAND_WAVE = "gesture_hand_wave";
|
||||
private String KEY_GESTURE_PICK_UP = "gesture_pick_up";
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
ActionBar actionbar = getActivity().getActionBar();
|
||||
actionbar.setDisplayHomeAsUpEnabled(true);
|
||||
actionbar.setTitle(R.string.ambient_display_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
@@ -25,9 +25,13 @@ public class FPGestureSettingsActivity extends PreferenceActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (savedInstanceState == null){
|
||||
|
||||
if (getActionBar() != null) {
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
getActionBar().setTitle(R.string.fingerprint_gestures_title);
|
||||
}
|
||||
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(android.R.id.content, new FPGestureSettingsFragment()).commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,14 +47,6 @@ public class FPGestureSettingsFragment extends PreferenceFragment {
|
||||
private TextView mSwitchBarText;
|
||||
private Switch mFPGestureSwitch;
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
ActionBar actionbar = getActivity().getActionBar();
|
||||
actionbar.setDisplayHomeAsUpEnabled(true);
|
||||
actionbar.setTitle(R.string.fingerprint_gestures_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
@@ -25,9 +25,12 @@ public class GestureSettingsActivity extends PreferenceActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (savedInstanceState == null){
|
||||
|
||||
if (getActionBar() != null) {
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(android.R.id.content, new GestureSettingsFragment()).commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,6 @@ import android.support.v14.preference.PreferenceFragment;
|
||||
|
||||
public class GestureSettingsFragment extends PreferenceFragment {
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
addPreferencesFromResource(R.xml.actions_panel);
|
||||
|
||||
@@ -21,7 +21,6 @@ import android.app.IntentService;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
@@ -44,44 +43,38 @@ public class MotoActionsService extends IntentService implements ScreenStateNoti
|
||||
UpdatedStateNotifier {
|
||||
private static final String TAG = "MotoActions";
|
||||
|
||||
private final Context mContext;
|
||||
|
||||
private final DozePulseAction mDozePulseAction;
|
||||
private final PowerManager mPowerManager;
|
||||
private final PowerManager.WakeLock mWakeLock;
|
||||
private final ScreenReceiver mScreenReceiver;
|
||||
private final SensorHelper mSensorHelper;
|
||||
|
||||
private final List<ScreenStateNotifier> mScreenStateNotifiers = new LinkedList<ScreenStateNotifier>();
|
||||
private final List<UpdatedStateNotifier> mUpdatedStateNotifiers =
|
||||
new LinkedList<UpdatedStateNotifier>();
|
||||
private final List<ScreenStateNotifier> mScreenStateNotifiers = new LinkedList<>();
|
||||
private final List<UpdatedStateNotifier> 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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@
|
||||
package com.moto.actions;
|
||||
|
||||
public interface SensorAction {
|
||||
public void action();
|
||||
void action();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -18,5 +18,5 @@
|
||||
package com.moto.actions.actions;
|
||||
|
||||
public interface UpdatedStateNotifier {
|
||||
public void updateState();
|
||||
void updateState();
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -18,6 +18,6 @@
|
||||
package com.moto.actions.doze;
|
||||
|
||||
public interface ScreenStateNotifier {
|
||||
public void screenTurnedOn();
|
||||
public void screenTurnedOff();
|
||||
void screenTurnedOn();
|
||||
void screenTurnedOff();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user