android_device_oneplus_dre/pocketmode/src/org/lineageos/pocketmode/PocketModeService.java
LuK1337 4ed29836d8 sdm845-common: Import pocketmode app and SELinux rules for it
* Based off android_device_oneplus_msm8998-common pocketmode
  app but without fpc10xx support and using actual OnePlus
  pocketmode sensor.

Change-Id: I73eaf7e00afd08cdad71d323b6821eb8a0160cd2
2018-07-27 20:23:44 +02:00

75 lines
2.4 KiB
Java

/*
* 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();
}
}
};
}