<lambda>null1 package com.android.systemui.qs.tiles
2 
3 import android.app.AlarmManager
4 import android.app.AlarmManager.AlarmClockInfo
5 import android.content.Intent
6 import android.os.Handler
7 import android.os.Looper
8 import android.provider.AlarmClock
9 import android.service.quicksettings.Tile
10 import android.text.TextUtils
11 import android.text.format.DateFormat
12 import androidx.annotation.VisibleForTesting
13 import com.android.internal.jank.InteractionJankMonitor
14 import com.android.internal.logging.MetricsLogger
15 import com.android.systemui.animation.Expandable
16 import com.android.systemui.dagger.qualifiers.Background
17 import com.android.systemui.dagger.qualifiers.Main
18 import com.android.systemui.plugins.ActivityStarter
19 import com.android.systemui.plugins.FalsingManager
20 import com.android.systemui.plugins.qs.QSTile
21 import com.android.systemui.plugins.statusbar.StatusBarStateController
22 import com.android.systemui.qs.QSHost
23 import com.android.systemui.qs.QsEventLogger
24 import com.android.systemui.qs.logging.QSLogger
25 import com.android.systemui.qs.tileimpl.QSTileImpl
26 import com.android.systemui.res.R
27 import com.android.systemui.settings.UserTracker
28 import com.android.systemui.statusbar.policy.NextAlarmController
29 import java.util.Locale
30 import javax.inject.Inject
31 
32 class AlarmTile
33 @Inject
34 constructor(
35     host: QSHost,
36     uiEventLogger: QsEventLogger,
37     @Background backgroundLooper: Looper,
38     @Main mainHandler: Handler,
39     falsingManager: FalsingManager,
40     metricsLogger: MetricsLogger,
41     statusBarStateController: StatusBarStateController,
42     activityStarter: ActivityStarter,
43     qsLogger: QSLogger,
44     private val userTracker: UserTracker,
45     nextAlarmController: NextAlarmController
46 ) : QSTileImpl<QSTile.State>(
47     host,
48     uiEventLogger,
49     backgroundLooper,
50     mainHandler,
51     falsingManager,
52     metricsLogger,
53     statusBarStateController,
54     activityStarter,
55     qsLogger
56 ) {
57 
58     private var lastAlarmInfo: AlarmManager.AlarmClockInfo? = null
59     private val icon = ResourceIcon.get(R.drawable.ic_alarm)
60     @VisibleForTesting internal val defaultIntent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
61     private val callback = NextAlarmController.NextAlarmChangeCallback { nextAlarm ->
62         lastAlarmInfo = nextAlarm
63         refreshState()
64     }
65 
66     init {
67         nextAlarmController.observe(this, callback)
68     }
69 
70     override fun newTileState(): QSTile.State {
71         return QSTile.State().apply {
72             handlesLongClick = false
73         }
74     }
75 
76     override fun handleClick(expandable: Expandable?) {
77         val animationController =
78             expandable?.activityTransitionController(
79                 InteractionJankMonitor.CUJ_SHADE_APP_LAUNCH_FROM_QS_TILE
80             )
81         val pendingIntent = lastAlarmInfo?.showIntent
82         if (pendingIntent != null) {
83             mActivityStarter.postStartActivityDismissingKeyguard(pendingIntent, animationController)
84         } else {
85             mActivityStarter.postStartActivityDismissingKeyguard(defaultIntent, 0,
86                     animationController)
87         }
88     }
89 
90     override fun handleUpdateState(state: QSTile.State, arg: Any?) {
91         state.icon = icon
92         state.label = tileLabel
93         lastAlarmInfo?.let {
94             state.secondaryLabel = formatNextAlarm(it)
95             state.state = Tile.STATE_ACTIVE
96         } ?: run {
97             state.secondaryLabel = mContext.getString(R.string.qs_alarm_tile_no_alarm)
98             state.state = Tile.STATE_INACTIVE
99         }
100         state.contentDescription = TextUtils.concat(state.label, ", ", state.secondaryLabel)
101     }
102 
103     override fun getTileLabel(): CharSequence {
104         return mContext.getString(R.string.status_bar_alarm)
105     }
106 
107     private fun formatNextAlarm(info: AlarmClockInfo): String {
108         val skeleton = if (use24HourFormat()) "EHm" else "Ehma"
109         val pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton)
110         return DateFormat.format(pattern, info.triggerTime).toString()
111     }
112 
113     private fun use24HourFormat(): Boolean {
114         return DateFormat.is24HourFormat(mContext, userTracker.userId)
115     }
116 
117     override fun getMetricsCategory(): Int {
118         return 0
119     }
120 
121     override fun getLongClickIntent(): Intent? {
122         return null
123     }
124 
125     companion object {
126         const val TILE_SPEC = "alarm"
127     }
128 }
129