diff --git a/common.mk b/common.mk index 4048fa1..c5ad4b6 100644 --- a/common.mk +++ b/common.mk @@ -107,6 +107,10 @@ PRODUCT_PACKAGES += \ Tag \ com.android.nfc_extras +# Pocket mode +PRODUCT_PACKAGES += \ + OnePlusPocketMode + # Power PRODUCT_PACKAGES += \ power.qcom diff --git a/pocketmode/Android.mk b/pocketmode/Android.mk new file mode 100644 index 0000000..ec579f9 --- /dev/null +++ b/pocketmode/Android.mk @@ -0,0 +1,18 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := OnePlusPocketMode +LOCAL_CERTIFICATE := platform +LOCAL_PRIVILEGED_MODULE := true + +LOCAL_STATIC_JAVA_LIBRARIES := \ + org.lineageos.platform.internal + +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..9fdf751 --- /dev/null +++ b/pocketmode/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + 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..2ce11b9 --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/BootCompletedReceiver.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2016 The CyanogenMod 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 = "OnePlusPocketMode"; + + @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 new file mode 100644 index 0000000..2b8002a --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2016 The CyanogenMod 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 PocketSensor mPocketSensor; + + @Override + public void onCreate() { + if (DEBUG) Log.d(TAG, "Creating service"); + mPocketSensor = new PocketSensor(this); + + IntentFilter screenStateFilter = new IntentFilter(); + screenStateFilter.addAction(Intent.ACTION_SCREEN_ON); + screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); + 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); + mPocketSensor.disable(); + super.onDestroy(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { + if (DEBUG) Log.d(TAG, "Display on"); + mPocketSensor.disable(); + } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + if (DEBUG) Log.d(TAG, "Display off"); + mPocketSensor.enable(); + } + } + }; +} diff --git a/pocketmode/src/org/lineageos/pocketmode/PocketSensor.java b/pocketmode/src/org/lineageos/pocketmode/PocketSensor.java new file mode 100644 index 0000000..eb961b3 --- /dev/null +++ b/pocketmode/src/org/lineageos/pocketmode/PocketSensor.java @@ -0,0 +1,89 @@ +/* + * 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.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.text.TextUtils; +import android.util.Log; + +import org.lineageos.internal.util.FileUtils; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class PocketSensor implements SensorEventListener { + private static final boolean DEBUG = false; + private static final String TAG = "PocketSensor"; + + private static final String GOODIX_FILE = + "/sys/devices/platform/soc/soc:goodix_fp/proximity_state"; + + private ExecutorService mExecutorService; + private SensorManager mSensorManager; + private Sensor mSensor; + private Context mContext; + + public PocketSensor(Context context) { + mContext = context; + mSensorManager = mContext.getSystemService(SensorManager.class); + mExecutorService = Executors.newSingleThreadExecutor(); + + for (Sensor sensor : mSensorManager.getSensorList(Sensor.TYPE_ALL)) { + if (TextUtils.equals(sensor.getStringType(), "oneplus.sensor.pocket")) { + mSensor = sensor; + break; + } + } + } + + @Override + public void onSensorChanged(SensorEvent event) { + setFPProximityState(event.values[0] == 1.0); + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + private void setFPProximityState(boolean isNear) { + if (!FileUtils.writeLine(GOODIX_FILE, isNear ? "1" : "0")) { + Log.e(TAG, "Proximity state file " + GOODIX_FILE + " is not writable!"); + } + } + + void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + mExecutorService.submit(() -> { + mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); + }); + } + + void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + mExecutorService.submit(() -> { + mSensorManager.unregisterListener(this, mSensor); + // Ensure FP is left enabled + setFPProximityState(/* isNear */ false); + }); + } +} diff --git a/rootdir/etc/init.qcom.rc b/rootdir/etc/init.qcom.rc index fe3e177..92a247b 100644 --- a/rootdir/etc/init.qcom.rc +++ b/rootdir/etc/init.qcom.rc @@ -6,6 +6,9 @@ on init mount none /system/lib/modules/wlan.ko /vendor/lib/modules/qca_cld3_wlan.ko bind on boot + # Fingerprint + chown system system /sys/devices/platform/soc/soc:goodix_fp/proximity_state + # Touchpanel chmod 0660 /proc/touchpanel/double_swipe_enable chmod 0660 /proc/touchpanel/down_arrow_enable diff --git a/sepolicy/private/file.te b/sepolicy/private/file.te index 869cbd1..668f5a5 100644 --- a/sepolicy/private/file.te +++ b/sepolicy/private/file.te @@ -5,3 +5,4 @@ type op1_file, file_type; type op2_file, file_type; type persist_file, file_type; type proc_touchpanel, fs_type; +type sysfs_fpc_proximity, sysfs_type, fs_type; diff --git a/sepolicy/private/file_contexts b/sepolicy/private/file_contexts index 3a24358..8364aa9 100644 --- a/sepolicy/private/file_contexts +++ b/sepolicy/private/file_contexts @@ -6,6 +6,9 @@ /op2(/.*)? u:object_r:op2_file:s0 /persist(/.*)? u:object_r:persist_file:s0 +# Files in sysfs +/sys/devices/platform/soc/soc:goodix_fp/proximity_state u:object_r:sysfs_fpc_proximity:s0 + # Lights /system/bin/hw/android\.hardware\.light@2\.0-service\.oneplus_sdm845 u:object_r:hal_light_sdm845_exec:s0 diff --git a/sepolicy/private/system_app.te b/sepolicy/private/system_app.te new file mode 100644 index 0000000..faff4e8 --- /dev/null +++ b/sepolicy/private/system_app.te @@ -0,0 +1 @@ +allow system_app sysfs_fpc_proximity:file w_file_perms;