diff options
author | Fedor917 <cryscript@gmail.com> | 2016-11-10 16:02:46 +0700 |
---|---|---|
committer | Fedor917 <cryscript@gmail.com> | 2016-11-10 16:02:46 +0700 |
commit | 7f87c3ffd0dfea3ee5e4ef8b74c8425fba14e713 (patch) | |
tree | 0d4ad7b22e926fc499c7702a26d15a428c1c952a /pocketmode/src | |
parent | 01dca9c333a1da30424142d4aae939b40b7e4a30 (diff) |
Add pocket mode based on Oneplus 3
Diffstat (limited to 'pocketmode/src')
3 files changed, 183 insertions, 0 deletions
diff --git a/pocketmode/src/com/cyanogenmod/pocketmode/BootCompletedReceiver.java b/pocketmode/src/com/cyanogenmod/pocketmode/BootCompletedReceiver.java new file mode 100644 index 0000000..04a5e72 --- /dev/null +++ b/pocketmode/src/com/cyanogenmod/pocketmode/BootCompletedReceiver.java @@ -0,0 +1,33 @@ +/* + * 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 com.cyanogenmod.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/com/cyanogenmod/pocketmode/PocketModeService.java b/pocketmode/src/com/cyanogenmod/pocketmode/PocketModeService.java new file mode 100644 index 0000000..a5025ee --- /dev/null +++ b/pocketmode/src/com/cyanogenmod/pocketmode/PocketModeService.java @@ -0,0 +1,82 @@ +/* + * 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 com.cyanogenmod.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(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"); + super.onDestroy(); + this.unregisterReceiver(mScreenStateReceiver); + mProximitySensor.disable(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void onDisplayOn() { + if (DEBUG) Log.d(TAG, "Display on"); + 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_SCREEN_ON)) { + onDisplayOn(); + } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + onDisplayOff(); + } + } + }; +} diff --git a/pocketmode/src/com/cyanogenmod/pocketmode/ProximitySensor.java b/pocketmode/src/com/cyanogenmod/pocketmode/ProximitySensor.java new file mode 100644 index 0000000..9ff1f06 --- /dev/null +++ b/pocketmode/src/com/cyanogenmod/pocketmode/ProximitySensor.java @@ -0,0 +1,68 @@ +/* + * 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 com.cyanogenmod.pocketmode; + +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.cyanogenmod.internal.util.FileUtils; + +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:fpc_fpc1020/proximity_state"; + + private SensorManager mSensorManager; + private Sensor mSensor; + private Context mContext; + + public ProximitySensor(Context context) { + mContext = context; + 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"); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + protected void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + mSensorManager.unregisterListener(this, mSensor); + } +} |