From a95471f8705b3885a9dabde85b537a4917b0e986 Mon Sep 17 00:00:00 2001 From: Michael W Date: Fri, 11 Feb 2022 15:38:28 +0100 Subject: [PATCH] MotoActions: General cleanup * Remove unused classes, methods, interfaces, imports * Reorder imports * Add final where applicable * Fix line lengths * Use local variables where possible * Replace deprecated Vibrator.vibrate(long) with the successor * Simplify conditions where they are always met * Rearrange tags and remove unused schemas in resources * Merge ScreenReceiver into the service -> a separate, instantiated but never called-into class makes one think it's unused Change-Id: I05cff7ecaf424c166fedceb1f4f8963164c6085a --- MotoActions/AndroidManifest.xml | 9 +- MotoActions/res/color/chop.xml | 11 +- MotoActions/res/color/flip.xml | 9 +- MotoActions/res/color/pick.xml | 9 +- MotoActions/res/color/pick2.xml | 9 +- MotoActions/res/color/pick3.xml | 9 +- MotoActions/res/color/quick.xml | 9 +- MotoActions/res/drawable/ic_hand.xml | 11 +- MotoActions/res/drawable/ic_pickup.xml | 11 +- MotoActions/res/drawable/ic_pocket.xml | 7 +- MotoActions/res/drawable/ic_settings_chop.xml | 8 +- MotoActions/res/drawable/ic_settings_flip.xml | 8 +- MotoActions/res/drawable/ic_settings_pick.xml | 8 +- .../res/drawable/ic_settings_twist.xml | 8 +- MotoActions/res/drawable/ic_settings_wave.xml | 8 +- .../device/ActionsPreferenceActivity.java | 8 +- .../device/ActionsPreferenceFragment.java | 14 +-- .../device/BootCompletedReceiver.java | 28 +---- .../settings/device/MotoActionsService.java | 104 ++++++++++-------- .../settings/device/MotoActionsSettings.java | 73 ++++++------ .../settings/device/SensorAction.java | 4 +- .../settings/device/SensorHelper.java | 29 ++--- .../settings/device/ServiceWrapper.java | 66 ----------- .../actions/CameraActivationAction.java | 25 +++-- .../actions/CameraActivationSensor.java | 26 ++--- .../device/actions/ChopChopSensor.java | 16 +-- .../settings/device/actions/FlipToMute.java | 26 ++--- .../device/actions/LiftToSilence.java | 19 ++-- .../device/actions/ProximitySilencer.java | 33 +++--- .../settings/device/actions/TorchAction.java | 28 ++--- .../device/actions/UpdatedStateNotifier.java | 4 +- .../settings/device/doze/DozePulseAction.java | 2 +- .../settings/device/doze/FlatUpSensor.java | 12 +- .../settings/device/doze/ScreenReceiver.java | 46 -------- .../device/doze/ScreenStateNotifier.java | 7 +- 35 files changed, 301 insertions(+), 403 deletions(-) delete mode 100644 MotoActions/src/org/lineageos/settings/device/ServiceWrapper.java delete mode 100644 MotoActions/src/org/lineageos/settings/device/doze/ScreenReceiver.java diff --git a/MotoActions/AndroidManifest.xml b/MotoActions/AndroidManifest.xml index bb8b82f..5d206d9 100644 --- a/MotoActions/AndroidManifest.xml +++ b/MotoActions/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -27,13 +28,9 @@ - - - - - + - \ No newline at end of file + diff --git a/MotoActions/res/color/flip.xml b/MotoActions/res/color/flip.xml index 2c494b3..8b94f5d 100644 --- a/MotoActions/res/color/flip.xml +++ b/MotoActions/res/color/flip.xml @@ -1,6 +1,11 @@ - + diff --git a/MotoActions/res/color/pick.xml b/MotoActions/res/color/pick.xml index 07fd2a5..09f4d02 100644 --- a/MotoActions/res/color/pick.xml +++ b/MotoActions/res/color/pick.xml @@ -1,6 +1,11 @@ - + diff --git a/MotoActions/res/color/pick2.xml b/MotoActions/res/color/pick2.xml index 97b189f..f02c511 100644 --- a/MotoActions/res/color/pick2.xml +++ b/MotoActions/res/color/pick2.xml @@ -1,6 +1,11 @@ - + diff --git a/MotoActions/res/color/pick3.xml b/MotoActions/res/color/pick3.xml index aa00b39..5ac80a6 100644 --- a/MotoActions/res/color/pick3.xml +++ b/MotoActions/res/color/pick3.xml @@ -1,6 +1,11 @@ - + diff --git a/MotoActions/res/color/quick.xml b/MotoActions/res/color/quick.xml index 215cf11..f5944c4 100644 --- a/MotoActions/res/color/quick.xml +++ b/MotoActions/res/color/quick.xml @@ -1,6 +1,11 @@ - + diff --git a/MotoActions/res/drawable/ic_hand.xml b/MotoActions/res/drawable/ic_hand.xml index 6d379fc..1e79721 100644 --- a/MotoActions/res/drawable/ic_hand.xml +++ b/MotoActions/res/drawable/ic_hand.xml @@ -1,9 +1,8 @@ - + android:width="24dp" + android:height="24dp" + android:tint="?android:attr/colorControlNormal" + android:viewportWidth="24" + android:viewportHeight="24"> diff --git a/MotoActions/res/drawable/ic_pickup.xml b/MotoActions/res/drawable/ic_pickup.xml index 9a12665..0f68d5b 100644 --- a/MotoActions/res/drawable/ic_pickup.xml +++ b/MotoActions/res/drawable/ic_pickup.xml @@ -1,9 +1,8 @@ - + android:width="24dp" + android:height="24dp" + android:tint="?android:attr/colorControlNormal" + android:viewportWidth="24" + android:viewportHeight="24"> diff --git a/MotoActions/res/drawable/ic_pocket.xml b/MotoActions/res/drawable/ic_pocket.xml index 44ae6e2..b65d441 100644 --- a/MotoActions/res/drawable/ic_pocket.xml +++ b/MotoActions/res/drawable/ic_pocket.xml @@ -1,9 +1,8 @@ - + android:viewportHeight="24"> diff --git a/MotoActions/res/drawable/ic_settings_chop.xml b/MotoActions/res/drawable/ic_settings_chop.xml index 8e2926b..f2df1c1 100644 --- a/MotoActions/res/drawable/ic_settings_chop.xml +++ b/MotoActions/res/drawable/ic_settings_chop.xml @@ -1,6 +1,10 @@ - + diff --git a/MotoActions/res/drawable/ic_settings_flip.xml b/MotoActions/res/drawable/ic_settings_flip.xml index 7b07736..360fe7c 100644 --- a/MotoActions/res/drawable/ic_settings_flip.xml +++ b/MotoActions/res/drawable/ic_settings_flip.xml @@ -1,6 +1,10 @@ - + diff --git a/MotoActions/res/drawable/ic_settings_pick.xml b/MotoActions/res/drawable/ic_settings_pick.xml index adfdece..df299d3 100644 --- a/MotoActions/res/drawable/ic_settings_pick.xml +++ b/MotoActions/res/drawable/ic_settings_pick.xml @@ -1,6 +1,10 @@ - + diff --git a/MotoActions/res/drawable/ic_settings_twist.xml b/MotoActions/res/drawable/ic_settings_twist.xml index 50675ca..f04e77f 100644 --- a/MotoActions/res/drawable/ic_settings_twist.xml +++ b/MotoActions/res/drawable/ic_settings_twist.xml @@ -1,6 +1,10 @@ - + diff --git a/MotoActions/res/drawable/ic_settings_wave.xml b/MotoActions/res/drawable/ic_settings_wave.xml index 5c559a4..8cee58f 100644 --- a/MotoActions/res/drawable/ic_settings_wave.xml +++ b/MotoActions/res/drawable/ic_settings_wave.xml @@ -1,6 +1,10 @@ - + diff --git a/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceActivity.java b/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceActivity.java index d53cda9..2bb86ef 100644 --- a/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceActivity.java +++ b/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceActivity.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2016 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,9 @@ public class ActionsPreferenceActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, new ActionsPreferenceFragment()).commit(); + getFragmentManager() + .beginTransaction() + .replace(android.R.id.content, new ActionsPreferenceFragment()) + .commit(); } } diff --git a/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceFragment.java b/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceFragment.java index 38cf260..7ee570c 100644 --- a/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceFragment.java +++ b/MotoActions/src/org/lineageos/settings/device/ActionsPreferenceFragment.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2015-2016 The CyanogenMod Project - * Copyright (C) 2017 The LineageOS Project + * Copyright (C) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,21 +19,17 @@ package org.lineageos.settings.device; import android.app.ActionBar; import android.os.Bundle; -import androidx.preference.Preference; +import android.view.MenuItem; + import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceFragment; -import androidx.preference.SwitchPreference; -import android.text.TextUtils; -import android.view.MenuItem; import org.lineageos.settings.device.actions.CameraActivationSensor; -import java.io.File; - public class ActionsPreferenceFragment extends PreferenceFragment { - private final String KEY_ACTIONS_CATEGORY = "actions_key"; - private final String KEY_GESTURE_CAMERA_ACTION = "gesture_camera_action"; + private static final String KEY_ACTIONS_CATEGORY = "actions_key"; + private static final String KEY_GESTURE_CAMERA_ACTION = "gesture_camera_action"; @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { diff --git a/MotoActions/src/org/lineageos/settings/device/BootCompletedReceiver.java b/MotoActions/src/org/lineageos/settings/device/BootCompletedReceiver.java index 198bdba..dc8610f 100644 --- a/MotoActions/src/org/lineageos/settings/device/BootCompletedReceiver.java +++ b/MotoActions/src/org/lineageos/settings/device/BootCompletedReceiver.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,36 +18,18 @@ package org.lineageos.settings.device; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; +import android.os.UserHandle; import android.util.Log; -import org.lineageos.settings.device.ServiceWrapper.LocalBinder; - public class BootCompletedReceiver extends BroadcastReceiver { - static final String TAG = "MotoActions"; - private ServiceWrapper mServiceWrapper; + private static final String TAG = "MotoActions"; @Override public void onReceive(final Context context, Intent intent) { Log.i(TAG, "Booting"); - context.startService(new Intent(context, ServiceWrapper.class)); + context.startServiceAsUser(new Intent(context, MotoActionsService.class), + UserHandle.CURRENT); } - - private ServiceConnection serviceConnection = new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName className, IBinder service) { - LocalBinder binder = (LocalBinder) service; - mServiceWrapper = binder.getService(); - mServiceWrapper.start(); - } - - @Override - public void onServiceDisconnected(ComponentName className) { - mServiceWrapper = null; - } - }; } diff --git a/MotoActions/src/org/lineageos/settings/device/MotoActionsService.java b/MotoActions/src/org/lineageos/settings/device/MotoActionsService.java index e6e3ec4..9507387 100644 --- a/MotoActions/src/org/lineageos/settings/device/MotoActionsService.java +++ b/MotoActions/src/org/lineageos/settings/device/MotoActionsService.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,83 +17,84 @@ package org.lineageos.settings.device; -import android.app.IntentService; +import android.app.Service; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.os.IBinder; import android.os.PowerManager; -import android.provider.Settings; import android.util.Log; -import java.util.List; -import java.util.LinkedList; - -import org.lineageos.settings.device.actions.UpdatedStateNotifier; import org.lineageos.settings.device.actions.CameraActivationSensor; import org.lineageos.settings.device.actions.ChopChopSensor; import org.lineageos.settings.device.actions.FlipToMute; import org.lineageos.settings.device.actions.LiftToSilence; import org.lineageos.settings.device.actions.ProximitySilencer; - +import org.lineageos.settings.device.actions.UpdatedStateNotifier; import org.lineageos.settings.device.doze.DozePulseAction; import org.lineageos.settings.device.doze.FlatUpSensor; -import org.lineageos.settings.device.doze.ScreenReceiver; import org.lineageos.settings.device.doze.ScreenStateNotifier; import org.lineageos.settings.device.doze.StowSensor; -public class MotoActionsService extends IntentService implements ScreenStateNotifier, +import java.util.LinkedList; +import java.util.List; + +public class MotoActionsService extends Service implements ScreenStateNotifier, UpdatedStateNotifier { private static final String TAG = "MotoActions"; - private final Context mContext; + private final List mScreenStateNotifiers = new LinkedList<>(); + private final List mUpdatedStateNotifiers = new LinkedList<>(); - private final DozePulseAction mDozePulseAction; - private final PowerManager mPowerManager; - private final PowerManager.WakeLock mWakeLock; - private final ScreenReceiver mScreenReceiver; - private final SensorHelper mSensorHelper; - - private final List mScreenStateNotifiers = new LinkedList(); - private final List mUpdatedStateNotifiers = - new LinkedList(); - - public MotoActionsService(Context context) { - super("MotoActionservice"); - mContext = context; + private PowerManager mPowerManager; + private PowerManager.WakeLock mWakeLock; + public void onCreate() { Log.d(TAG, "Starting"); - MotoActionsSettings MotoActionsSettings = new MotoActionsSettings(context, this); - mSensorHelper = new SensorHelper(context); - mScreenReceiver = new ScreenReceiver(context, this); + MotoActionsSettings actionsSettings = new MotoActionsSettings(this, this); + SensorHelper sensorHelper = new SensorHelper(this); + DozePulseAction dozePulseAction = new DozePulseAction(this); - mDozePulseAction = new DozePulseAction(context); - mScreenStateNotifiers.add(mDozePulseAction); + mScreenStateNotifiers.add(dozePulseAction); // Actionable sensors get screen on/off notifications - mScreenStateNotifiers.add(new StowSensor(MotoActionsSettings, mSensorHelper, mDozePulseAction)); - mScreenStateNotifiers.add(new FlatUpSensor(MotoActionsSettings, mSensorHelper, mDozePulseAction)); + mScreenStateNotifiers.add(new StowSensor(actionsSettings, sensorHelper, dozePulseAction)); + mScreenStateNotifiers.add(new FlatUpSensor(actionsSettings, sensorHelper, dozePulseAction)); // 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)); - - mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MotoActionsWakeLock"); + mUpdatedStateNotifiers.add(new CameraActivationSensor(actionsSettings, sensorHelper)); + mUpdatedStateNotifiers.add(new ChopChopSensor(actionsSettings, sensorHelper)); + mUpdatedStateNotifiers.add(new ProximitySilencer(actionsSettings, this, sensorHelper)); + mUpdatedStateNotifiers.add(new FlipToMute(actionsSettings, this, sensorHelper)); + mUpdatedStateNotifiers.add(new LiftToSilence(actionsSettings, this, sensorHelper)); + + mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); + mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + TAG + ":WakeLock"); + + IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); + filter.addAction(Intent.ACTION_SCREEN_OFF); + registerReceiver(mScreenStateReceiver, filter); + updateState(); } + public int onStartCommand(Intent intent, int flags, int startId) { + return START_STICKY; + } + @Override - protected void onHandleIntent(Intent intent) { + public IBinder onBind(Intent intent) { + return null; } @Override public void screenTurnedOn() { - if (!mWakeLock.isHeld()) { - mWakeLock.acquire(); - } + if (!mWakeLock.isHeld()) { + mWakeLock.acquire(); + } for (ScreenStateNotifier screenStateNotifier : mScreenStateNotifiers) { screenStateNotifier.screenTurnedOn(); } @@ -101,9 +102,9 @@ public class MotoActionsService extends IntentService implements ScreenStateNoti @Override public void screenTurnedOff() { - if (mWakeLock.isHeld()) { - mWakeLock.release(); - } + if (mWakeLock.isHeld()) { + mWakeLock.release(); + } for (ScreenStateNotifier screenStateNotifier : mScreenStateNotifiers) { screenStateNotifier.screenTurnedOff(); } @@ -119,4 +120,15 @@ public class MotoActionsService extends IntentService implements ScreenStateNoti notifier.updateState(); } } + + private final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + screenTurnedOff(); + } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { + screenTurnedOn(); + } + } + }; } diff --git a/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java b/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java index 50ceb61..d1e2e50 100644 --- a/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java +++ b/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java @@ -17,23 +17,22 @@ package org.lineageos.settings.device; +import static android.provider.Settings.Secure.DOZE_ALWAYS_ON; +import static android.provider.Settings.Secure.DOZE_ENABLED; + import android.content.Context; import android.content.SharedPreferences; import android.hardware.display.AmbientDisplayConfiguration; import android.os.UserHandle; -import android.preference.PreferenceManager; import android.provider.Settings; -import org.lineageos.settings.device.actions.UpdatedStateNotifier; +import androidx.preference.PreferenceManager; + import org.lineageos.settings.device.actions.CameraActivationAction; import org.lineageos.settings.device.actions.TorchAction; - -import static android.provider.Settings.Secure.DOZE_ALWAYS_ON; -import static android.provider.Settings.Secure.DOZE_ENABLED; +import org.lineageos.settings.device.actions.UpdatedStateNotifier; public class MotoActionsSettings { - private static final String TAG = "MotoActions"; - private static final String GESTURE_CAMERA_ACTION_KEY = "gesture_camera_action"; private static final String GESTURE_CHOP_CHOP_KEY = "gesture_chop_chop"; private static final String GESTURE_IR_SILENCER_KEY = "gesture_ir_silencer"; @@ -149,35 +148,35 @@ public class MotoActionsSettings { mLiftToSilenceEnabled = sharedPreferences.getBoolean(GESTURE_LIFT_TO_SILENCE_KEY, false); } - private SharedPreferences.OnSharedPreferenceChangeListener mPrefListener = + private final SharedPreferences.OnSharedPreferenceChangeListener mPrefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - boolean updated = true; - - if (GESTURE_CAMERA_ACTION_KEY.equals(key)) { - mCameraGestureEnabled = sharedPreferences.getBoolean(GESTURE_CAMERA_ACTION_KEY, true); - } else if (GESTURE_CHOP_CHOP_KEY.equals(key)) { - mChopChopEnabled = sharedPreferences.getBoolean(GESTURE_CHOP_CHOP_KEY, true); - } else if (GESTURE_IR_WAKEUP_KEY.equals(key)) { - mIrWakeUpEnabled = sharedPreferences.getBoolean(GESTURE_IR_WAKEUP_KEY, true); - } else if (GESTURE_PICK_UP_KEY.equals(key)) { - mPickUpGestureEnabled = sharedPreferences.getBoolean(GESTURE_PICK_UP_KEY, true); - } else if (GESTURE_POCKET_KEY.equals(key)) { - mPocketGestureEnabled = sharedPreferences.getBoolean(GESTURE_POCKET_KEY, true); - } else if (GESTURE_IR_SILENCER_KEY.equals(key)) { - mIrSilencerEnabled = sharedPreferences.getBoolean(GESTURE_IR_SILENCER_KEY, false); - } else if (GESTURE_FLIP_TO_MUTE_KEY.equals(key)) { - mFlipToMuteEnabled = sharedPreferences.getBoolean(GESTURE_FLIP_TO_MUTE_KEY, false); - } else if (GESTURE_LIFT_TO_SILENCE_KEY.equals(key)) { - mLiftToSilenceEnabled = sharedPreferences.getBoolean(GESTURE_LIFT_TO_SILENCE_KEY, false); - } else { - updated = false; - } - - if (updated) { - mUpdatedStateNotifier.updateState(); - } - } - }; + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) { + boolean updated = true; + + if (GESTURE_CAMERA_ACTION_KEY.equals(key)) { + mCameraGestureEnabled = sharedPrefs.getBoolean(GESTURE_CAMERA_ACTION_KEY, true); + } else if (GESTURE_CHOP_CHOP_KEY.equals(key)) { + mChopChopEnabled = sharedPrefs.getBoolean(GESTURE_CHOP_CHOP_KEY, true); + } else if (GESTURE_IR_WAKEUP_KEY.equals(key)) { + mIrWakeUpEnabled = sharedPrefs.getBoolean(GESTURE_IR_WAKEUP_KEY, true); + } else if (GESTURE_PICK_UP_KEY.equals(key)) { + mPickUpGestureEnabled = sharedPrefs.getBoolean(GESTURE_PICK_UP_KEY, true); + } else if (GESTURE_POCKET_KEY.equals(key)) { + mPocketGestureEnabled = sharedPrefs.getBoolean(GESTURE_POCKET_KEY, true); + } else if (GESTURE_IR_SILENCER_KEY.equals(key)) { + mIrSilencerEnabled = sharedPrefs.getBoolean(GESTURE_IR_SILENCER_KEY, false); + } else if (GESTURE_FLIP_TO_MUTE_KEY.equals(key)) { + mFlipToMuteEnabled = sharedPrefs.getBoolean(GESTURE_FLIP_TO_MUTE_KEY, false); + } else if (GESTURE_LIFT_TO_SILENCE_KEY.equals(key)) { + mLiftToSilenceEnabled = sharedPrefs.getBoolean(GESTURE_LIFT_TO_SILENCE_KEY, false); + } else { + updated = false; + } + + if (updated) { + mUpdatedStateNotifier.updateState(); + } + } + }; } diff --git a/MotoActions/src/org/lineageos/settings/device/SensorAction.java b/MotoActions/src/org/lineageos/settings/device/SensorAction.java index 4bc72c4..ed57c95 100644 --- a/MotoActions/src/org/lineageos/settings/device/SensorAction.java +++ b/MotoActions/src/org/lineageos/settings/device/SensorAction.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,5 +18,5 @@ package org.lineageos.settings.device; public interface SensorAction { - public void action(); + void action(); } diff --git a/MotoActions/src/org/lineageos/settings/device/SensorHelper.java b/MotoActions/src/org/lineageos/settings/device/SensorHelper.java index 9ba0d99..a4d2a33 100644 --- a/MotoActions/src/org/lineageos/settings/device/SensorHelper.java +++ b/MotoActions/src/org/lineageos/settings/device/SensorHelper.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017-2020 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,18 +17,17 @@ package org.lineageos.settings.device; -import java.util.List; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; - import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEventListener; import android.hardware.SensorManager; -import android.hardware.TriggerEventListener; import android.util.Log; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.util.List; + 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(); } @@ -57,7 +56,8 @@ public class SensorHelper { List sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor sensor : sensorList) { writer.write("sensor " + sensor.getType() + " = " + sensor.getName() - + " max batch: " + sensor.getFifoMaxEventCount() + " isWakeUp: " + sensor.isWakeUpSensor() + "\n"); + + " max batch: " + sensor.getFifoMaxEventCount() + " isWakeUp: " + + sensor.isWakeUpSensor() + "\n"); } writer.close(); } catch (IOException e) { @@ -99,15 +99,4 @@ public class SensorHelper { public void unregisterListener(SensorEventListener listener) { mSensorManager.unregisterListener(listener); } - - /* TriggerSensor */ - public void requestTriggerSensor(Sensor sensor, TriggerEventListener listener) { - if (!mSensorManager.requestTriggerSensor(listener, sensor)) { - Log.e(TAG, "Failed to requestTriggerSensor for sensor " + sensor); - } - } - - public void cancelTriggerSensor(Sensor sensor, TriggerEventListener listener) { - mSensorManager.cancelTriggerSensor(listener, sensor); - } } diff --git a/MotoActions/src/org/lineageos/settings/device/ServiceWrapper.java b/MotoActions/src/org/lineageos/settings/device/ServiceWrapper.java deleted file mode 100644 index 6deb8bf..0000000 --- a/MotoActions/src/org/lineageos/settings/device/ServiceWrapper.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.lineageos.settings.device; - -import android.content.Intent; -import android.os.Binder; -import android.os.Bundle; -import android.os.IBinder; -import android.util.Log; - -public class ServiceWrapper extends android.app.Service { - static final String TAG = "MotoActions-ServiceWrapper"; - - private final IBinder mBinder = new LocalBinder(); - private MotoActionsService mMotoActionsService; - - public interface ServiceCallback { - void sendResults(int resultCode, Bundle b); - } - - public class LocalBinder extends Binder { - ServiceWrapper getService() { - // Return this instance of the service so clients can call public - // methods - return ServiceWrapper.this; - } - } - - @Override - public void onCreate() { - Log.i(TAG, "onCreate"); - super.onCreate(); - mMotoActionsService = new MotoActionsService(this); - } - - @Override - public IBinder onBind(Intent intent) { - Log.i(TAG, "onBind"); - return null; - } - - public void setCallback(ServiceCallback callback) { - } - - public void start() { - Log.i(TAG, "start"); - } - - public void stop() { - } -} diff --git a/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationAction.java b/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationAction.java index 3bd276c..60a943a 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationAction.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationAction.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ package org.lineageos.settings.device.actions; -import java.util.List; - import android.app.KeyguardManager; import android.content.ComponentName; import android.content.Context; @@ -27,15 +25,16 @@ import android.content.pm.ActivityInfo; 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 org.lineageos.settings.device.SensorAction; +import java.util.List; + public class CameraActivationAction implements SensorAction { - private static final String TAG = "MotoActions"; + private static final String TAG = "CameraActivationAction"; private static final int TURN_SCREEN_ON_WAKE_LOCK_MS = 500; @@ -43,12 +42,16 @@ public class CameraActivationAction implements SensorAction { private final KeyguardManager mKeyguardManager; private final PackageManager mPackageManager; private final PowerManager mPowerManager; + private final PowerManager.WakeLock mScreenWakelock; public CameraActivationAction(Context context) { mContext = context; mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mPackageManager = context.getPackageManager(); + mScreenWakelock = mPowerManager.newWakeLock( + PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, + TAG + ":Wakelock"); } @Override @@ -64,13 +67,11 @@ public class CameraActivationAction implements SensorAction { private void vibrate() { Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); - v.vibrate(500); + v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); } private void turnScreenOn() { - PowerManager.WakeLock wl = mPowerManager.newWakeLock( - PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG); - wl.acquire(TURN_SCREEN_ON_WAKE_LOCK_MS); + mScreenWakelock.acquire(TURN_SCREEN_ON_WAKE_LOCK_MS); } private void launchCamera() { @@ -112,13 +113,13 @@ public class CameraActivationAction implements SensorAction { } private ActivityInfo getBestActivityInfo(Intent intent, ActivityInfo match) { - List activities = mPackageManager.queryIntentActivities(intent, 0); + List activities = mPackageManager.queryIntentActivities(intent, 0); ActivityInfo best = null; if (activities.size() > 0) { best = activities.get(0).activityInfo; if (match != null) { String packageName = match.applicationInfo.packageName; - for (int i = activities.size()-1; i >= 0; i--) { + for (int i = activities.size() - 1; i >= 0; i--) { ActivityInfo activityInfo = activities.get(i).activityInfo; if (packageName.equals(activityInfo.applicationInfo.packageName)) { best = activityInfo; diff --git a/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationSensor.java b/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationSensor.java index 2d8062b..847777c 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationSensor.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/CameraActivationSensor.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017-2020 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,13 +17,10 @@ package org.lineageos.settings.device.actions; -import java.util.List; - import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; -import android.hardware.SensorManager; import android.util.Log; import org.lineageos.settings.device.MotoActionsSettings; @@ -32,20 +29,15 @@ import org.lineageos.settings.device.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); + public CameraActivationSensor(MotoActionsSettings motoActionsSettings, + SensorHelper sensorHelper) { + mMotoActionsSettings = motoActionsSettings; + Sensor sensor = sensorHelper.getCameraActivationSensor(); + sensorHelper.registerListener(sensor, this); } @Override @@ -53,7 +45,7 @@ public class CameraActivationSensor implements SensorEventListener, UpdatedState if (mMotoActionsSettings.isCameraGestureEnabled() && !mIsEnabled) { Log.d(TAG, "Enabling"); mIsEnabled = true; - } else if (! mMotoActionsSettings.isCameraGestureEnabled() && mIsEnabled) { + } else if (!mMotoActionsSettings.isCameraGestureEnabled() && mIsEnabled) { Log.d(TAG, "Disabling"); mIsEnabled = false; } @@ -62,7 +54,9 @@ public class CameraActivationSensor implements SensorEventListener, UpdatedState @Override public void onSensorChanged(SensorEvent event) { Log.d(TAG, "activate camera"); - if (mIsEnabled) mMotoActionsSettings.cameraAction(); + if (mIsEnabled) { + mMotoActionsSettings.cameraAction(); + } } @Override diff --git a/MotoActions/src/org/lineageos/settings/device/actions/ChopChopSensor.java b/MotoActions/src/org/lineageos/settings/device/actions/ChopChopSensor.java index e67eeda..e309905 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/ChopChopSensor.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/ChopChopSensor.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015-2016 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,9 @@ package org.lineageos.settings.device.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 org.lineageos.settings.device.MotoActionsSettings; @@ -31,8 +28,6 @@ import org.lineageos.settings.device.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; @@ -41,8 +36,8 @@ public class ChopChopSensor implements SensorEventListener, UpdatedStateNotifier private boolean mIsEnabled; private boolean mProxIsCovered; - public ChopChopSensor(MotoActionsSettings MotoActionsSettings, SensorHelper sensorHelper) { - mMotoActionsSettings = MotoActionsSettings; + public ChopChopSensor(MotoActionsSettings motoActionsSettings, SensorHelper sensorHelper) { + mMotoActionsSettings = motoActionsSettings; mSensorHelper = sensorHelper; mSensor = sensorHelper.getChopChopSensor(); mProx = sensorHelper.getProximitySensor(); @@ -55,7 +50,7 @@ public class ChopChopSensor implements SensorEventListener, UpdatedStateNotifier mSensorHelper.registerListener(mSensor, this); mSensorHelper.registerListener(mProx, mProxListener); mIsEnabled = true; - } else if (! mMotoActionsSettings.isChopChopGestureEnabled() && mIsEnabled) { + } else if (!mMotoActionsSettings.isChopChopGestureEnabled() && mIsEnabled) { Log.d(TAG, "Disabling"); mSensorHelper.unregisterListener(this); mSensorHelper.unregisterListener(mProxListener); @@ -77,7 +72,7 @@ public class ChopChopSensor implements SensorEventListener, UpdatedStateNotifier public void onAccuracyChanged(Sensor sensor, int accuracy) { } - private SensorEventListener mProxListener = new SensorEventListener() { + private final SensorEventListener mProxListener = new SensorEventListener() { @Override public synchronized void onSensorChanged(SensorEvent event) { mProxIsCovered = event.values[0] < mProx.getMaximumRange(); @@ -87,5 +82,4 @@ public class ChopChopSensor implements SensorEventListener, UpdatedStateNotifier public void onAccuracyChanged(Sensor mSensor, int accuracy) { } }; - } diff --git a/MotoActions/src/org/lineageos/settings/device/actions/FlipToMute.java b/MotoActions/src/org/lineageos/settings/device/actions/FlipToMute.java index 3cfd994..e1cda5a 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/FlipToMute.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/FlipToMute.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2016 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,23 +38,22 @@ public class FlipToMute implements UpdatedStateNotifier { private final SensorHelper mSensorHelper; private final Sensor mFlatDown; private final Sensor mStow; + private final Receiver mReceiver; private boolean mIsEnabled; private boolean mIsFlatDown; private boolean mIsStowed; private int mFilter; - private Context mContext; - private Receiver mReceiver; + private final Context mContext; - public FlipToMute(MotoActionsSettings MotoActionsSettings, Context context, - SensorHelper sensorHelper) { - mMotoActionsSettings = MotoActionsSettings; + public FlipToMute(MotoActionsSettings motoActionsSettings, Context context, + SensorHelper sensorHelper) { + mMotoActionsSettings = motoActionsSettings; mContext = context; mSensorHelper = sensorHelper; mFlatDown = sensorHelper.getFlatDownSensor(); mStow = sensorHelper.getStowSensor(); - mNotificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + mNotificationManager = context.getSystemService(NotificationManager.class); mFilter = mNotificationManager.getCurrentInterruptionFilter(); mReceiver = new Receiver(); } @@ -66,7 +65,7 @@ public class FlipToMute implements UpdatedStateNotifier { mSensorHelper.registerListener(mFlatDown, mFlatDownListener); mSensorHelper.registerListener(mStow, mStowListener); mContext.registerReceiver(mReceiver, - new IntentFilter(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)); + new IntentFilter(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)); mIsEnabled = true; } else if (!mMotoActionsSettings.isFlipToMuteEnabled() && mIsEnabled) { Log.d(TAG, "Disabling"); @@ -77,7 +76,7 @@ public class FlipToMute implements UpdatedStateNotifier { } } - private SensorEventListener mFlatDownListener = new SensorEventListener() { + private final SensorEventListener mFlatDownListener = new SensorEventListener() { @Override public synchronized void onSensorChanged(SensorEvent event) { mIsFlatDown = (event.values[0] != 0); @@ -89,7 +88,7 @@ public class FlipToMute implements UpdatedStateNotifier { } }; - private SensorEventListener mStowListener = new SensorEventListener() { + private final SensorEventListener mStowListener = new SensorEventListener() { @Override public synchronized void onSensorChanged(SensorEvent event) { mIsStowed = (event.values[0] != 0); @@ -102,11 +101,11 @@ public class FlipToMute implements UpdatedStateNotifier { }; private void sensorChange() { - Log.d(TAG, "event: " + mIsFlatDown + " mIsStowed=" + mIsStowed); if (mIsFlatDown && mIsStowed) { - mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY); + mNotificationManager.setInterruptionFilter( + NotificationManager.INTERRUPTION_FILTER_PRIORITY); Log.d(TAG, "Interrupt filter: Allow priority"); } else if (!mIsFlatDown) { mNotificationManager.setInterruptionFilter(mFilter); @@ -115,7 +114,6 @@ public class FlipToMute implements UpdatedStateNotifier { } public class Receiver extends BroadcastReceiver { - @Override public void onReceive(Context context, Intent intent) { if (!mIsFlatDown && !mIsStowed) { diff --git a/MotoActions/src/org/lineageos/settings/device/actions/LiftToSilence.java b/MotoActions/src/org/lineageos/settings/device/actions/LiftToSilence.java index 5175489..d1e020f 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/LiftToSilence.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/LiftToSilence.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2016 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,15 +21,16 @@ import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; -import android.telephony.PhoneStateListener; import android.telecom.TelecomManager; +import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import org.lineageos.settings.device.MotoActionsSettings; import org.lineageos.settings.device.SensorHelper; -public class LiftToSilence extends PhoneStateListener implements SensorEventListener, UpdatedStateNotifier { +public class LiftToSilence extends PhoneStateListener implements SensorEventListener, + UpdatedStateNotifier { private static final String TAG = "MotoActions-LiftToSilence"; private final MotoActionsSettings mMotoActionsSettings; @@ -44,14 +45,14 @@ public class LiftToSilence extends PhoneStateListener implements SensorEventList private boolean mIsStowed; private boolean mLastFlatUp; - public LiftToSilence(MotoActionsSettings MotoActionsSettings, Context context, - SensorHelper sensorHelper) { - mMotoActionsSettings = MotoActionsSettings; + public LiftToSilence(MotoActionsSettings motoActionsSettings, Context context, + SensorHelper sensorHelper) { + mMotoActionsSettings = motoActionsSettings; mSensorHelper = sensorHelper; mFlatUpSensor = sensorHelper.getFlatUpSensor(); mStowSensor = sensorHelper.getStowSensor(); mTelecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); - mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + mTelephonyManager = context.getSystemService(TelephonyManager.class); } @Override @@ -84,7 +85,7 @@ public class LiftToSilence extends PhoneStateListener implements SensorEventList boolean thisFlatUp = (event.values[0] != 0); Log.d(TAG, "event: " + thisFlatUp + " mLastFlatUp=" + mLastFlatUp + " mIsStowed=" + - mIsStowed); + mIsStowed); if (mLastFlatUp && !thisFlatUp && !mIsStowed) { mTelecomManager.silenceRinger(); @@ -96,7 +97,7 @@ public class LiftToSilence extends PhoneStateListener implements SensorEventList public void onAccuracyChanged(Sensor mSensor, int accuracy) { } - private SensorEventListener mStowListener = new SensorEventListener() { + private final SensorEventListener mStowListener = new SensorEventListener() { @Override public synchronized void onSensorChanged(SensorEvent event) { mIsStowed = (event.values[0] != 0); diff --git a/MotoActions/src/org/lineageos/settings/device/actions/ProximitySilencer.java b/MotoActions/src/org/lineageos/settings/device/actions/ProximitySilencer.java index d97016e..ce6f6f8 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/ProximitySilencer.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/ProximitySilencer.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,21 +17,22 @@ package org.lineageos.settings.device.actions; +import static android.telephony.TelephonyManager.CALL_STATE_RINGING; + import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; -import android.telephony.PhoneStateListener; import android.telecom.TelecomManager; +import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import org.lineageos.settings.device.MotoActionsSettings; import org.lineageos.settings.device.SensorHelper; -import static android.telephony.TelephonyManager.*; - -public class ProximitySilencer extends PhoneStateListener implements SensorEventListener, UpdatedStateNotifier { +public class ProximitySilencer extends PhoneStateListener implements SensorEventListener, + UpdatedStateNotifier { private static final String TAG = "MotoActions-ProximitySilencer"; private static final int SILENCE_DELAY_MS = 500; @@ -45,12 +46,12 @@ public class ProximitySilencer extends PhoneStateListener implements SensorEvent private long mRingStartedMs; private boolean mCoveredRinging; - public ProximitySilencer(MotoActionsSettings MotoActionsSettings, Context context, - SensorHelper sensorHelper) { + public ProximitySilencer(MotoActionsSettings motoActionsSettings, Context context, + SensorHelper sensorHelper) { mTelecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); - mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + mTelephonyManager = context.getSystemService(TelephonyManager.class); - mMotoActionsSettings = MotoActionsSettings; + mMotoActionsSettings = motoActionsSettings; mSensorHelper = sensorHelper; mSensor = sensorHelper.getProximitySensor(); mCoveredRinging = false; @@ -71,23 +72,19 @@ public class ProximitySilencer extends PhoneStateListener implements SensorEvent boolean isNear = event.values[0] < mSensor.getMaximumRange(); long now = System.currentTimeMillis(); - if (isNear){ - if (mIsRinging && (now - mRingStartedMs >= SILENCE_DELAY_MS)){ - mCoveredRinging = true; - } else { - mCoveredRinging = false; - } + if (isNear) { + mCoveredRinging = mIsRinging && (now - mRingStartedMs >= SILENCE_DELAY_MS); return; } - if (!isNear && mIsRinging) { - Log.d(TAG, "event: " + event.values[0] + ", " + " covered " + Boolean.toString(mCoveredRinging)); + if (mIsRinging) { + Log.d(TAG, "event: " + event.values[0] + ", " + " covered " + 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/org/lineageos/settings/device/actions/TorchAction.java b/MotoActions/src/org/lineageos/settings/device/actions/TorchAction.java index d1d824a..0c221f0 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/TorchAction.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/TorchAction.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,23 +18,21 @@ package org.lineageos.settings.device.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 org.lineageos.settings.device.SensorAction; public class TorchAction implements SensorAction { - private static final String TAG = "MotoActions"; - - private static final int TURN_SCREEN_ON_WAKE_LOCK_MS = 500; + private static boolean mTorchEnabled; - private CameraManager mCameraManager; + private final CameraManager mCameraManager; private final Vibrator mVibrator; + private String mRearCameraId; - private static boolean mTorchEnabled; public TorchAction(Context mContext) { mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE); @@ -50,18 +48,18 @@ public class TorchAction implements SensorAction { break; } } - } catch (CameraAccessException e) { + } catch (CameraAccessException ignored) { } } @Override public void action() { - mVibrator.vibrate(250); + mVibrator.vibrate(VibrationEffect.createOneShot(250, VibrationEffect.DEFAULT_AMPLITUDE)); if (mRearCameraId != null) { try { mCameraManager.setTorchMode(mRearCameraId, !mTorchEnabled); mTorchEnabled = !mTorchEnabled; - } catch (CameraAccessException e) { + } catch (CameraAccessException ignored) { } } } @@ -70,15 +68,17 @@ public class TorchAction implements SensorAction { @Override public void onTorchModeChanged(String cameraId, boolean enabled) { - if (!cameraId.equals(mRearCameraId)) + if (!cameraId.equals(mRearCameraId)) { return; + } mTorchEnabled = enabled; } @Override public void onTorchModeUnavailable(String cameraId) { - if (!cameraId.equals(mRearCameraId)) + if (!cameraId.equals(mRearCameraId)) { return; + } mTorchEnabled = false; } } diff --git a/MotoActions/src/org/lineageos/settings/device/actions/UpdatedStateNotifier.java b/MotoActions/src/org/lineageos/settings/device/actions/UpdatedStateNotifier.java index 224a57b..ac7e5c8 100644 --- a/MotoActions/src/org/lineageos/settings/device/actions/UpdatedStateNotifier.java +++ b/MotoActions/src/org/lineageos/settings/device/actions/UpdatedStateNotifier.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,5 +18,5 @@ package org.lineageos.settings.device.actions; public interface UpdatedStateNotifier { - public void updateState(); + void updateState(); } diff --git a/MotoActions/src/org/lineageos/settings/device/doze/DozePulseAction.java b/MotoActions/src/org/lineageos/settings/device/doze/DozePulseAction.java index 267ebce..319b6cd 100644 --- a/MotoActions/src/org/lineageos/settings/device/doze/DozePulseAction.java +++ b/MotoActions/src/org/lineageos/settings/device/doze/DozePulseAction.java @@ -47,7 +47,7 @@ public class DozePulseAction implements SensorAction, ScreenStateNotifier { } public void action() { - if (mayDoze()) { + if (mayDoze()) { Log.d(TAG, "Sending doze.pulse intent"); Intent pulseIntent = new Intent("com.android.systemui.doze.pulse"); mContext.sendBroadcastAsUser(pulseIntent, UserHandle.CURRENT); diff --git a/MotoActions/src/org/lineageos/settings/device/doze/FlatUpSensor.java b/MotoActions/src/org/lineageos/settings/device/doze/FlatUpSensor.java index 2a50cc3..70300f2 100644 --- a/MotoActions/src/org/lineageos/settings/device/doze/FlatUpSensor.java +++ b/MotoActions/src/org/lineageos/settings/device/doze/FlatUpSensor.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (C) 2017 The LineageOS Project + * Copyright (C) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,7 +40,7 @@ public class FlatUpSensor implements ScreenStateNotifier { private boolean mLastFlatUp; public FlatUpSensor(MotoActionsSettings MotoActionsSettings, SensorHelper sensorHelper, - SensorAction action) { + SensorAction action) { mMotoActionsSettings = MotoActionsSettings; mSensorHelper = sensorHelper; mSensorAction = action; @@ -69,15 +69,15 @@ public class FlatUpSensor implements ScreenStateNotifier { } } - private SensorEventListener mFlatUpListener = new SensorEventListener() { + private final SensorEventListener mFlatUpListener = new SensorEventListener() { @Override public synchronized void onSensorChanged(SensorEvent event) { boolean thisFlatUp = (event.values[0] != 0); Log.d(TAG, "event: " + thisFlatUp + " mLastFlatUp=" + mLastFlatUp + " mIsStowed=" + - mIsStowed); + mIsStowed); - if (mLastFlatUp && ! thisFlatUp && ! mIsStowed) { + if (mLastFlatUp && !thisFlatUp && !mIsStowed) { mSensorAction.action(); } mLastFlatUp = thisFlatUp; @@ -88,7 +88,7 @@ public class FlatUpSensor implements ScreenStateNotifier { } }; - private SensorEventListener mStowListener = new SensorEventListener() { + private final SensorEventListener mStowListener = new SensorEventListener() { @Override public synchronized void onSensorChanged(SensorEvent event) { mIsStowed = (event.values[0] != 0); diff --git a/MotoActions/src/org/lineageos/settings/device/doze/ScreenReceiver.java b/MotoActions/src/org/lineageos/settings/device/doze/ScreenReceiver.java deleted file mode 100644 index e8d8024..0000000 --- a/MotoActions/src/org/lineageos/settings/device/doze/ScreenReceiver.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.lineageos.settings.device.doze; - -import java.util.List; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; - -public class ScreenReceiver extends BroadcastReceiver { - private final ScreenStateNotifier mNotifier; - - public ScreenReceiver(Context context, ScreenStateNotifier notifier) { - mNotifier = notifier; - - IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); - filter.addAction(Intent.ACTION_SCREEN_OFF); - context.registerReceiver(this, filter); - } - - @Override - public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { - mNotifier.screenTurnedOff(); - } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { - mNotifier.screenTurnedOn(); - } - } -} diff --git a/MotoActions/src/org/lineageos/settings/device/doze/ScreenStateNotifier.java b/MotoActions/src/org/lineageos/settings/device/doze/ScreenStateNotifier.java index 7b9ab59..4d1e511 100644 --- a/MotoActions/src/org/lineageos/settings/device/doze/ScreenStateNotifier.java +++ b/MotoActions/src/org/lineageos/settings/device/doze/ScreenStateNotifier.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 2017-2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.lineageos.settings.device.doze; public interface ScreenStateNotifier { - public void screenTurnedOn(); - public void screenTurnedOff(); + void screenTurnedOn(); + + void screenTurnedOff(); } -- 2.20.1