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.controls.ui
18 
19 import android.app.AlertDialog
20 import android.app.PendingIntent
21 import android.content.DialogInterface
22 import android.content.Intent
23 import android.content.pm.PackageManager
24 import android.service.controls.Control
25 import android.view.View
26 import android.view.WindowManager
27 
28 import com.android.systemui.R
29 
30 class StatusBehavior : Behavior {
31     lateinit var cvh: ControlViewHolder
32 
33     override fun initialize(cvh: ControlViewHolder) {
34         this.cvh = cvh
35     }
36 
37     override fun bind(cws: ControlWithState, colorOffset: Int) {
38         val status = cws.control?.status ?: Control.STATUS_UNKNOWN
39         val msg = when (status) {
40             Control.STATUS_ERROR -> R.string.controls_error_generic
41             Control.STATUS_DISABLED -> R.string.controls_error_timeout
42             Control.STATUS_NOT_FOUND -> {
43                 cvh.layout.setOnClickListener(View.OnClickListener() {
44                     showNotFoundDialog(cvh, cws)
45                 })
46                 cvh.layout.setOnLongClickListener(View.OnLongClickListener() {
47                     showNotFoundDialog(cvh, cws)
48                     true
49                 })
50                 R.string.controls_error_removed
51             }
52             else -> {
53                 cvh.isLoading = true
54                 com.android.internal.R.string.loading
55             }
56         }
57         cvh.setStatusText(cvh.context.getString(msg))
58         cvh.applyRenderInfo(false, colorOffset)
59     }
60 
61     private fun showNotFoundDialog(cvh: ControlViewHolder, cws: ControlWithState) {
62         val pm = cvh.context.getPackageManager()
63         val ai = pm.getApplicationInfo(cws.componentName.packageName, PackageManager.GET_META_DATA)
64         val appLabel = pm.getApplicationLabel(ai)
65         val builder = AlertDialog.Builder(
66             cvh.context,
67             android.R.style.Theme_DeviceDefault_Dialog_Alert
68         ).apply {
69             val res = cvh.context.resources
70             setTitle(res.getString(R.string.controls_error_removed_title))
71             setMessage(res.getString(
72                 R.string.controls_error_removed_message, cvh.title.getText(), appLabel))
73             setPositiveButton(
74                 R.string.controls_open_app,
75                 DialogInterface.OnClickListener { dialog, _ ->
76                     try {
77                         cws.control?.getAppIntent()?.send()
78                         context.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
79                     } catch (e: PendingIntent.CanceledException) {
80                         cvh.setErrorStatus()
81                     }
82                     dialog.dismiss()
83             })
84             setNegativeButton(
85                 android.R.string.cancel,
86                 DialogInterface.OnClickListener { dialog, _ ->
87                     dialog.cancel()
88                 }
89             )
90         }
91         cvh.visibleDialog = builder.create().apply {
92             getWindow().apply {
93                 setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY)
94                 show()
95             }
96         }
97     }
98 }
99