land: Send doze pulse intent on tilt detected

Based on oneplus3's implementation.

Change-Id: Ibc4bd629f8280d63f84d23981e01cc861992c17f
This commit is contained in:
Zhao Wei Liew 2016-12-22 11:21:49 +08:00
parent c753cb1b98
commit 9ef01e64c7
9 changed files with 338 additions and 0 deletions

17
doze/Android.mk Normal file
View file

@ -0,0 +1,17 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := CMDoze
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))

38
doze/AndroidManifest.xml Normal file
View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015-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.
-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.cyanogenmod.doze"
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<protected-broadcast android:name="com.android.systemui.doze.pulse" />
<application
android:label="@string/app_name">
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".DozeService" />
</application>
</manifest>

3
doze/proguard.flags Normal file
View file

@ -0,0 +1,3 @@
-keep class org.cyanogenmod.doze.* {
*;
}

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name" translatable="false">CMDoze</string>
</resources>

View file

@ -0,0 +1,33 @@
/**
* Copyright (c) 2015-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.cyanogenmod.doze;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootCompletedReceiver extends BroadcastReceiver {
private static final boolean DEBUG = false;
private static final String TAG = BootCompletedReceiver.class.getSimpleName();
@Override
public void onReceive(final Context context, Intent intent) {
if (DEBUG) Log.d(TAG, "Starting service");
context.startService(new Intent(context, DozeService.class));
}
}

View file

@ -0,0 +1,79 @@
/**
* Copyright (c) 2015-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.cyanogenmod.doze;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.UserHandle;
import android.util.Log;
public abstract class CMSensor {
private static final boolean DEBUG = false;
private static final String TAG = CMSensor.class.getSimpleName();
private static final String DOZE_INTENT = "com.android.systemui.doze.pulse";
private static final int SENSOR_WAKELOCK_DURATION = 200;
private static final int BATCH_LATENCY_IN_MS = 100;
private Context mContext;
private SensorManager mSensorManager;
private Sensor mSensor;
private SensorEventListener mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
onSensorEvent(event);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do nothing
}
};
CMSensor(Context context, int type) {
mContext = context;
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(type);
}
void launchDozePulse() {
Log.d(TAG, "Launch doze pulse");
mContext.sendBroadcastAsUser(new Intent(DOZE_INTENT),
new UserHandle(UserHandle.USER_CURRENT));
}
void enable() {
if (DEBUG) Log.d(TAG, "Enabling");
mSensorManager.registerListener(mSensorEventListener, mSensor,
SensorManager.SENSOR_DELAY_NORMAL, BATCH_LATENCY_IN_MS * 1000);
}
void disable() {
if (DEBUG) Log.d(TAG, "Disabling");
mSensorManager.unregisterListener(mSensorEventListener);
}
void onSensorEvent(SensorEvent event) {
if (DEBUG) Log.d(TAG, "Got sensor event: " + event.values[0]);
}
}

View file

@ -0,0 +1,91 @@
/**
* Copyright (c) 2015-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.cyanogenmod.doze;
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.provider.Settings;
import android.util.Log;
public class DozeService extends Service {
private static final boolean DEBUG = false;
private static final String TAG = DozeService.class.getSimpleName();
private Context mContext;
private TiltSensor mTiltSensor;
private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
onDisplayOff();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
onDisplayOn();
}
}
};
@Override
public void onCreate() {
if (DEBUG) Log.d(TAG, "Creating service");
super.onCreate();
mContext = this;
mTiltSensor = new TiltSensor(mContext);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (DEBUG) Log.d(TAG, "Starting service");
IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
mContext.registerReceiver(mScreenStateReceiver, screenStateFilter);
return START_STICKY;
}
@Override
public void onDestroy() {
if (DEBUG) Log.d(TAG, "Destroying service");
super.onDestroy();
mTiltSensor.disable();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private boolean isDozeEnabled() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.DOZE_ENABLED, 1) != 0;
}
private void onDisplayOn() {
if (DEBUG) Log.d(TAG, "Display on");
mTiltSensor.disable();
}
private void onDisplayOff() {
if (DEBUG) Log.d(TAG, "Display off");
if (isDozeEnabled()) {
mTiltSensor.enable();
}
}
}

View file

@ -0,0 +1,54 @@
/**
* Copyright (c) 2015-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.cyanogenmod.doze;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.os.SystemClock;
import android.util.Log;
public class TiltSensor extends CMSensor {
private static final int MIN_PULSE_INTERVAL_MS = 2500;
private long mEntryTimestamp;
TiltSensor(Context context) {
super(context, Sensor.TYPE_TILT_DETECTOR);
}
@Override
void enable() {
super.enable();
mEntryTimestamp = SystemClock.elapsedRealtime();
}
@Override
void onSensorEvent(SensorEvent event) {
super.onSensorEvent(event);
if (SystemClock.elapsedRealtime() - mEntryTimestamp < MIN_PULSE_INTERVAL_MS) {
return;
}
mEntryTimestamp = SystemClock.elapsedRealtime();
if (event.values[0] == 1) {
launchDozePulse();
}
}
}

View file

@ -11,6 +11,10 @@ PRODUCT_AAPT_PREF_CONFIG := xhdpi
# A list of dpis to select prebuilt apk, in precedence order.
PRODUCT_AAPT_PREBUILT_DPI := hdpi
# Ambient display
PRODUCT_PACKAGES += \
CMDoze
# Dalvik
PRODUCT_PROPERTY_OVERRIDES += \
dalvik.vm.heapgrowthlimit=192m \