1 /*
2  * Copyright (C) 2022 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.usb;
18 
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import android.annotation.IntDef;
22 import android.content.res.Resources;
23 import android.util.Log;
24 
25 import com.android.systemui.res.R;
26 
27 import java.lang.annotation.Retention;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * USB Audio devices warning dialog messages help class.
33  */
34 public class UsbAudioWarningDialogMessage {
35     private static final String TAG = "UsbAudioWarningDialogMessage";
36 
37     @Retention(SOURCE)
38     @IntDef({TYPE_PERMISSION, TYPE_CONFIRM})
39     public @interface DialogType {}
40     public static final int TYPE_PERMISSION = 0;
41     public static final int TYPE_CONFIRM = 1;
42 
43     private int mDialogType;
44     private UsbDialogHelper mDialogHelper;
45 
46     @Inject
UsbAudioWarningDialogMessage()47     public UsbAudioWarningDialogMessage() {
48     }
49 
50     /**
51      * Initialize USB audio warning dialog message type and helper class.
52      * @param type Dialog type for Activity.
53      * @param usbDialogHelper Helper class for getting USB permission and confirm dialogs
54      */
init(@ialogType int type, UsbDialogHelper usbDialogHelper)55     public void init(@DialogType int type, UsbDialogHelper usbDialogHelper) {
56         mDialogType = type;
57         mDialogHelper = usbDialogHelper;
58     }
59 
hasRecordPermission()60     boolean hasRecordPermission() {
61         return mDialogHelper.packageHasAudioRecordingPermission();
62     }
63 
isUsbAudioDevice()64     boolean isUsbAudioDevice() {
65         return mDialogHelper.isUsbDevice() && (mDialogHelper.deviceHasAudioCapture()
66                 || (mDialogHelper.deviceHasAudioPlayback()));
67     }
68 
hasAudioPlayback()69     boolean hasAudioPlayback() {
70         return mDialogHelper.deviceHasAudioPlayback();
71     }
72 
hasAudioCapture()73     boolean hasAudioCapture() {
74         return mDialogHelper.deviceHasAudioCapture();
75     }
76 
77     /**
78      * According to USB audio warning dialog matrix table to return warning message id.
79      * @return string resId for USB audio warning dialog message, otherwise {ID_NULL}.
80      * See usb_audio.md for USB audio Permission and Confirmation warning dialog resource
81      * string id matrix table.
82      */
getMessageId()83     public int getMessageId() {
84         if (!mDialogHelper.isUsbDevice()) {
85             return getUsbAccessoryPromptId();
86         }
87 
88         if (hasRecordPermission() && isUsbAudioDevice()) {
89             // case# 1, 2, 3
90             return R.string.usb_audio_device_prompt;
91         } else if (!hasRecordPermission() && isUsbAudioDevice() && hasAudioPlayback()
92                 && !hasAudioCapture()) {
93             // case# 5
94             return R.string.usb_audio_device_prompt;
95         }
96 
97         if (!hasRecordPermission() && isUsbAudioDevice() && hasAudioCapture()) {
98             // case# 6,7
99             return R.string.usb_audio_device_prompt_warn;
100         }
101 
102         Log.w(TAG, "Only shows title with empty content description!");
103         return Resources.ID_NULL;
104     }
105 
106     /**
107      * Gets prompt dialog title.
108      * @return string id for USB prompt dialog title.
109      */
getPromptTitleId()110     public int getPromptTitleId() {
111         return (mDialogType == TYPE_PERMISSION)
112                 ? R.string.usb_audio_device_permission_prompt_title
113                 : R.string.usb_audio_device_confirm_prompt_title;
114     }
115 
116     /**
117      * Gets USB Accessory prompt message id.
118      * @return string id for USB Accessory prompt message.
119      */
getUsbAccessoryPromptId()120     public int getUsbAccessoryPromptId() {
121         return (mDialogType == TYPE_PERMISSION)
122                 ? R.string.usb_accessory_permission_prompt : R.string.usb_accessory_confirm_prompt;
123     }
124 }
125