From c3d328b6be521feff20f9d10f755716d4d639cc7 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Fri, 10 May 2019 21:25:31 +0200 Subject: msm8996-common: pocketmode: Allow control over PocketMode service * Once the service is running, proximity sensor is constantly active when the display is turned off, resulting into a residual increase in battery consumption. Add a toggle so that users can decide whether they accept that and prefer to prevent accidental wake-ups triggered by the fingerprint sensor. * Keep the receiver that listens for the screen status registered only if the fingerprint wake-up feature is enabled at the same time as the accidental wake-up prevention feature. * Set PocketMode as a required module of ConfigPanel, to make sure it is only shipped on devices building the latter. * The configpanel part is integrated in b07a633bdeda835867aa3dc5a033529d7bd712dc Change-Id: Icfa23d2aef971e368476b6f1f7612493c2b69a20 --- .../pocketmode/BootCompletedReceiver.java | 34 ------------------- .../lineageos/pocketmode/PocketModeService.java | 36 +++++++++++++++++--- .../org/lineageos/pocketmode/ProximitySensor.java | 11 +++--- .../src/org/lineageos/pocketmode/Startup.java | 39 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 44 deletions(-) delete mode 100644 pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java create mode 100644 pocketmode/src/org/lineageos/pocketmode/Startup.java (limited to 'pocketmode/src') diff --git a/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java b/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java deleted file mode 100644 index b967946..0000000 --- a/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2016 The CyanogenMod Project - * Copyright (c) 2018 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.pocketmode; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -public class BootCompletedReceiver extends BroadcastReceiver { - - private static final String TAG = "ZukPocketMode"; - - @Override - public void onReceive(final Context context, Intent intent) { - Log.d(TAG, "Starting"); - context.startService(new Intent(context, PocketModeService.class)); - } -} diff --git a/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java b/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java index 42658b9..3af4654 100644 --- a/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java +++ b/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2016 The CyanogenMod Project - * Copyright (c) 2018 The LineageOS Project + * 2018-2019 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,10 +25,18 @@ import android.content.IntentFilter; import android.os.IBinder; import android.util.Log; +import java.util.ArrayList; +import java.util.List; + public class PocketModeService extends Service { private static final String TAG = "PocketModeService"; private static final boolean DEBUG = false; + private static final String CUST_INTENT = "org.lineageos.settings.device.CUST_UPDATE"; + private static final String CUST_INTENT_EXTRA = "pocketmode_service"; + + private static List receivers = new ArrayList(); + private ProximitySensor mProximitySensor; @Override @@ -36,9 +44,8 @@ public class PocketModeService extends Service { if (DEBUG) Log.d(TAG, "Creating service"); mProximitySensor = new ProximitySensor(this); - IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); - screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); - registerReceiver(mScreenStateReceiver, screenStateFilter); + IntentFilter custFilter = new IntentFilter(CUST_INTENT); + registerReceiver(mUpdateReceiver, custFilter); } @Override @@ -51,7 +58,10 @@ public class PocketModeService extends Service { public void onDestroy() { if (DEBUG) Log.d(TAG, "Destroying service"); super.onDestroy(); - this.unregisterReceiver(mScreenStateReceiver); + if (receivers.contains(mScreenStateReceiver)) { + this.unregisterReceiver(mScreenStateReceiver); + } + this.unregisterReceiver(mUpdateReceiver); mProximitySensor.disable(); } @@ -80,4 +90,20 @@ public class PocketModeService extends Service { } } }; + + private BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getBooleanExtra(CUST_INTENT_EXTRA, false)) { + IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); + screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); + registerReceiver(mScreenStateReceiver, screenStateFilter); + receivers.add(mScreenStateReceiver); + } else if (receivers.contains(mScreenStateReceiver)) { + unregisterReceiver(mScreenStateReceiver); + receivers.remove(mScreenStateReceiver); + mProximitySensor.disable(); + } + } + }; } diff --git a/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java b/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java index c1af4e8..4f1402c 100644 --- a/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java +++ b/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2016 The CyanogenMod Project - * Copyright (c) 2018 The LineageOS Project + * 2018-2019 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. @@ -31,7 +31,7 @@ public class ProximitySensor implements SensorEventListener { private static final boolean DEBUG = false; private static final String TAG = "PocketModeProximity"; - private static final String FPC_FILE = "/sys/devices/soc/soc:fpc1020/proximity_state"; + private static final String FP_PROX_NODE = "/sys/devices/soc/soc:fpc1020/proximity_state"; private SensorManager mSensorManager; private Sensor mSensor; @@ -39,15 +39,16 @@ public class ProximitySensor implements SensorEventListener { public ProximitySensor(Context context) { mContext = context; - mSensorManager = mContext.getSystemService(SensorManager.class); + mSensorManager = (SensorManager) + mContext.getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); } @Override public void onSensorChanged(SensorEvent event) { boolean isNear = event.values[0] < mSensor.getMaximumRange(); - if (FileUtils.isFileWritable(FPC_FILE)) { - FileUtils.writeLine(FPC_FILE, isNear ? "1" : "0"); + if (FileUtils.isFileWritable(FP_PROX_NODE)) { + FileUtils.writeLine(FP_PROX_NODE, isNear ? "1" : "0"); } } diff --git a/pocketmode/src/org/lineageos/pocketmode/Startup.java b/pocketmode/src/org/lineageos/pocketmode/Startup.java new file mode 100644 index 0000000..39b3371 --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/Startup.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2016 The CyanogenMod Project + * 2018-2019 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.pocketmode; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.UserHandle; +import android.util.Log; + +public class Startup extends BroadcastReceiver { + + private static final String TAG = "ZukPocketMode"; + + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + if (lineageos.content.Intent.ACTION_INITIALIZE_LINEAGE_HARDWARE.equals(action)) { + Log.d(TAG, "Starting"); + context.startServiceAsUser(new Intent(context, PocketModeService.class), + UserHandle.CURRENT); + } + } +} -- cgit v1.2.3