1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.statusbar.policy 18 19 import android.app.Notification 20 import android.app.NotificationChannel 21 import android.app.NotificationManager 22 import android.app.PendingIntent 23 import android.content.Context 24 import android.content.Intent 25 import android.net.Uri 26 import com.android.systemui.dagger.qualifiers.Background 27 import com.android.systemui.res.R 28 import com.android.systemui.util.concurrency.DelayableExecutor 29 import javax.inject.Inject 30 31 /** 32 * Listens for important battery states and sends non-dismissible system notifications if there is a 33 * problem 34 */ 35 class BatteryStateNotifier @Inject constructor( 36 val controller: BatteryController, 37 val noMan: NotificationManager, 38 @Background val delayableExecutor: DelayableExecutor, 39 val context: Context 40 ) : BatteryController.BatteryStateChangeCallback { 41 var stateUnknown = false 42 startListeningnull43 fun startListening() { 44 controller.addCallback(this) 45 } 46 stopListeningnull47 fun stopListening() { 48 controller.removeCallback(this) 49 } 50 onBatteryUnknownStateChangednull51 override fun onBatteryUnknownStateChanged(isUnknown: Boolean) { 52 stateUnknown = isUnknown 53 if (stateUnknown) { 54 val channel = NotificationChannel("battery_status", "Battery status", 55 NotificationManager.IMPORTANCE_DEFAULT) 56 noMan.createNotificationChannel(channel) 57 58 val intent = Intent(Intent.ACTION_VIEW, 59 Uri.parse(context.getString(R.string.config_batteryStateUnknownUrl))) 60 val pi = PendingIntent.getActivity(context, 0, intent, 61 PendingIntent.FLAG_IMMUTABLE) 62 63 val builder = Notification.Builder(context, channel.id) 64 .setAutoCancel(false) 65 .setContentTitle( 66 context.getString(R.string.battery_state_unknown_notification_title)) 67 .setContentText( 68 context.getString(R.string.battery_state_unknown_notification_text)) 69 .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb) 70 .setContentIntent(pi) 71 .setAutoCancel(true) 72 .setOngoing(true) 73 74 noMan.notify(TAG, ID, builder.build()) 75 } else { 76 scheduleNotificationCancel() 77 } 78 } 79 scheduleNotificationCancelnull80 private fun scheduleNotificationCancel() { 81 val r = { 82 if (!stateUnknown) { 83 noMan.cancel(ID) 84 } 85 } 86 delayableExecutor.executeDelayed(r, DELAY_MILLIS) 87 } 88 } 89 90 private const val TAG = "BatteryStateNotifier" 91 private const val ID = 666 92 private const val DELAY_MILLIS: Long = 4 * 60 * 60 * 1000 93