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