From 470c614f6251eb6fa09a65cd4cb30a6881292874 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Sun, 25 Jul 2021 02:06:42 +0200 Subject: [PATCH] sdm710-common: Add PocketMode app --- device.mk | 4 + pocketmode/Android.mk | 16 ++++ pocketmode/AndroidManifest.xml | 45 ++++++++++ pocketmode/proguard.flags | 3 + .../pocketmode/BootCompletedReceiver.java | 36 ++++++++ .../pocketmode/PocketModeService.java | 84 +++++++++++++++++++ .../lineageos/pocketmode/ProximitySensor.java | 77 +++++++++++++++++ 7 files changed, 265 insertions(+) create mode 100644 pocketmode/Android.mk create mode 100644 pocketmode/AndroidManifest.xml create mode 100644 pocketmode/proguard.flags create mode 100644 pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java create mode 100644 pocketmode/src/org/lineageos/pocketmode/PocketModeService.java create mode 100644 pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java diff --git a/device.mk b/device.mk index 79c02f3..3ceeaf0 100644 --- a/device.mk +++ b/device.mk @@ -281,6 +281,10 @@ PRODUCT_COPY_FILES += \ frameworks/native/data/etc/android.software.sip.voip.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.software.sip.voip.xml \ frameworks/native/data/etc/handheld_core_hardware.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/handheld_core_hardware.xml +# PocketMode +PRODUCT_PACKAGES += \ + LenovoPocketMode + # Power PRODUCT_PACKAGES += \ android.hardware.power-service.lenovo diff --git a/pocketmode/Android.mk b/pocketmode/Android.mk new file mode 100644 index 0000000..3ff3004 --- /dev/null +++ b/pocketmode/Android.mk @@ -0,0 +1,16 @@ +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := LenovoPocketMode +LOCAL_CERTIFICATE := platform +LOCAL_PRIVATE_PLATFORM_APIS := true +LOCAL_PRIVILEGED_MODULE := true + +LOCAL_PROGUARD_FLAG_FILES := proguard.flags + +include $(BUILD_PACKAGE) diff --git a/pocketmode/AndroidManifest.xml b/pocketmode/AndroidManifest.xml new file mode 100644 index 0000000..1df9ca1 --- /dev/null +++ b/pocketmode/AndroidManifest.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/pocketmode/proguard.flags b/pocketmode/proguard.flags new file mode 100644 index 0000000..2087239 --- /dev/null +++ b/pocketmode/proguard.flags @@ -0,0 +1,3 @@ +-keep class org.lineageos.pocketmode.* { + *; +} diff --git a/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java b/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java new file mode 100644 index 0000000..9d70d85 --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016 The CyanogenMod Project + * 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.pocketmode; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.UserHandle; +import android.util.Log; + +public class BootCompletedReceiver extends BroadcastReceiver { + + private static final String TAG = "LenovoPocketMode"; + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "Starting"); + context.startServiceAsUser(new Intent(context, PocketModeService.class), + UserHandle.CURRENT); + } +} diff --git a/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java b/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java new file mode 100644 index 0000000..cf75bfb --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2016 The CyanogenMod Project + * 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.pocketmode; + +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.util.Log; + +public class PocketModeService extends Service { + private static final String TAG = "PocketModeService"; + private static final boolean DEBUG = false; + + private ProximitySensor mProximitySensor; + + @Override + public void onCreate() { + if (DEBUG) Log.d(TAG, "Creating service"); + mProximitySensor = new ProximitySensor(this); + + IntentFilter screenStateFilter = new IntentFilter(); + screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); + screenStateFilter.addAction(Intent.ACTION_USER_PRESENT); + registerReceiver(mScreenStateReceiver, screenStateFilter); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (DEBUG) Log.d(TAG, "Starting service"); + return START_STICKY; + } + + @Override + public void onDestroy() { + if (DEBUG) Log.d(TAG, "Destroying service"); + this.unregisterReceiver(mScreenStateReceiver); + mProximitySensor.disable(); + super.onDestroy(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void onDeviceUnlocked() { + if (DEBUG) Log.d(TAG, "Device unlocked"); + mProximitySensor.disable(); + } + + private void onDisplayOff() { + if (DEBUG) Log.d(TAG, "Display off"); + mProximitySensor.enable(); + } + + private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { + onDeviceUnlocked(); + } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + onDisplayOff(); + } + } + }; +} diff --git a/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java b/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java new file mode 100644 index 0000000..51f9cf4 --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/ProximitySensor.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2016 The CyanogenMod Project + * 2017-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.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.os.FileUtils; +import android.util.Log; + +import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class ProximitySensor implements SensorEventListener { + private static final String TAG = "PocketModeProximity"; + private static final boolean DEBUG = false; + + private ExecutorService mExecutorService; + private Context mContext; + private Sensor mSensor; + private SensorManager mSensorManager; + + public ProximitySensor(Context context) { + mContext = context; + mSensorManager = mContext.getSystemService(SensorManager.class); + mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); + mExecutorService = Executors.newSingleThreadExecutor(); + } + + private Future submit(Runnable runnable) { + return mExecutorService.submit(runnable); + } + + @Override + public void onSensorChanged(SensorEvent event) { + /* Empty */ + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + protected void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + submit(() -> { + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL); + }); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + submit(() -> { + mSensorManager.unregisterListener(this, mSensor); + }); + } +}