91 lines
2.7 KiB
Java
91 lines
2.7 KiB
Java
/*
|
|
* Copyright (C) 2015 The CyanogenMod Project
|
|
* 2017-2018 The LineageOS Project
|
|
* 2020-2022 Paranoid Android
|
|
*
|
|
* 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 net.typeblog.lunatic.Services;
|
|
|
|
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;
|
|
|
|
import net.typeblog.lunatic.Manager.AnimationManager;
|
|
|
|
public class ChargingService extends Service {
|
|
|
|
private static final String TAG = "SpotlightChargingService";
|
|
private static final boolean DEBUG = true;
|
|
private AnimationManager mAnimationManager;
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
if (DEBUG) Log.d(TAG, "Creating service");
|
|
|
|
mAnimationManager = new AnimationManager(this);
|
|
|
|
IntentFilter powerMonitor = new IntentFilter();
|
|
powerMonitor.addAction(Intent.ACTION_POWER_CONNECTED);
|
|
powerMonitor.addAction(Intent.ACTION_POWER_DISCONNECTED);
|
|
registerReceiver(mPowerMonitor, powerMonitor);
|
|
}
|
|
|
|
@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");
|
|
|
|
onPowerDisconnected();
|
|
|
|
super.onDestroy();
|
|
this.unregisterReceiver(mPowerMonitor);
|
|
}
|
|
|
|
@Override
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
private void onPowerConnected() {
|
|
if (DEBUG) Log.d(TAG, "Power connected");
|
|
mAnimationManager.playCharging();
|
|
}
|
|
|
|
private void onPowerDisconnected() {
|
|
if (DEBUG) Log.d(TAG, "Power disconnected");
|
|
mAnimationManager.stopCharging();
|
|
}
|
|
|
|
private BroadcastReceiver mPowerMonitor = new BroadcastReceiver() {
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
|
|
onPowerConnected();
|
|
} else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
|
|
onPowerDisconnected();
|
|
}
|
|
}
|
|
};
|
|
}
|