95 lines
3.2 KiB
Java
95 lines
3.2 KiB
Java
|
/*
|
||
|
* Copyright (c) 2015 The CyanogenMod Project
|
||
|
* 2017-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.settings.doze;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.content.Intent;
|
||
|
import android.hardware.Sensor;
|
||
|
import android.hardware.SensorManager;
|
||
|
import android.os.UserHandle;
|
||
|
import android.support.v7.preference.PreferenceManager;
|
||
|
import android.provider.Settings;
|
||
|
import android.util.Log;
|
||
|
|
||
|
import static android.provider.Settings.Secure.DOZE_ENABLED;
|
||
|
|
||
|
public final class Utils {
|
||
|
|
||
|
private static final String TAG = "DozeUtils";
|
||
|
private static final boolean DEBUG = false;
|
||
|
|
||
|
private static final String DOZE_INTENT = "com.android.systemui.doze.pulse";
|
||
|
|
||
|
protected static final String GESTURE_PICK_UP_KEY = "gesture_pick_up";
|
||
|
|
||
|
protected static void startService(Context context) {
|
||
|
if (DEBUG) Log.d(TAG, "Starting service");
|
||
|
context.startServiceAsUser(new Intent(context, DozeService.class),
|
||
|
UserHandle.CURRENT);
|
||
|
}
|
||
|
|
||
|
protected static void stopService(Context context) {
|
||
|
if (DEBUG) Log.d(TAG, "Stopping service");
|
||
|
context.stopServiceAsUser(new Intent(context, DozeService.class),
|
||
|
UserHandle.CURRENT);
|
||
|
}
|
||
|
|
||
|
protected static void checkDozeService(Context context) {
|
||
|
if (isDozeEnabled(context) && isPickUpEnabled(context)) {
|
||
|
startService(context);
|
||
|
} else {
|
||
|
stopService(context);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected static boolean isDozeEnabled(Context context) {
|
||
|
return Settings.Secure.getInt(context.getContentResolver(),
|
||
|
DOZE_ENABLED, 1) != 0;
|
||
|
}
|
||
|
|
||
|
protected static boolean enableDoze(Context context, boolean enable) {
|
||
|
return Settings.Secure.putInt(context.getContentResolver(),
|
||
|
DOZE_ENABLED, enable ? 1 : 0);
|
||
|
}
|
||
|
|
||
|
protected static void launchDozePulse(Context context) {
|
||
|
if (DEBUG) Log.d(TAG, "Launch doze pulse");
|
||
|
context.sendBroadcastAsUser(new Intent(DOZE_INTENT),
|
||
|
new UserHandle(UserHandle.USER_CURRENT));
|
||
|
}
|
||
|
|
||
|
protected static void enablePickUp(Context context, boolean enable) {
|
||
|
PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||
|
.putBoolean(GESTURE_PICK_UP_KEY, enable).apply();
|
||
|
}
|
||
|
|
||
|
protected static boolean isPickUpEnabled(Context context) {
|
||
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
||
|
.getBoolean(GESTURE_PICK_UP_KEY, false);
|
||
|
}
|
||
|
|
||
|
protected static Sensor getSensor(SensorManager sm, String type) {
|
||
|
for (Sensor sensor : sm.getSensorList(Sensor.TYPE_ALL)) {
|
||
|
if (type.equals(sensor.getStringType())) {
|
||
|
return sensor;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|