summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/usb/UsbAudioWarningDialogMessage.java
blob: df845e53a36ba0353c503c0ed6e9462dcf4ec220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.usb;

import static java.lang.annotation.RetentionPolicy.SOURCE;

import android.annotation.IntDef;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;

import com.android.systemui.R;

import java.lang.annotation.Retention;

/**
 * USB Audio devices warning dialog messages help class.
 */
public class UsbAudioWarningDialogMessage {
    private static final String TAG = "UsbAudioWarningDialogMessage";

    @Retention(SOURCE)
    @IntDef({TYPE_PERMISSION, TYPE_CONFIRM})
    public @interface DialogType {}
    public static final int TYPE_PERMISSION = 0;
    public static final int TYPE_CONFIRM = 1;

    private final int mDialogType;
    private UsbDialogHelper mDialogHelper;

    public UsbAudioWarningDialogMessage(Context context, Intent intent, @DialogType int type) {
        mDialogType = type;
        try {
            mDialogHelper = new UsbDialogHelper(context, intent);
        } catch (IllegalStateException e) {
            Log.e(TAG, "Unable to initialize UsbDialogHelper!", e);
        }
    }

    private boolean hasRecordPermission() {
        return mDialogHelper.packageHasAudioRecordingPermission();
    }

    private boolean isUsbAudioDevice() {
        return mDialogHelper.isUsbDevice() && (mDialogHelper.deviceHasAudioCapture()
                || (mDialogHelper.deviceHasAudioPlayback()));
    }

    private boolean hasAudioPlayback() {
        return mDialogHelper.deviceHasAudioPlayback();
    }

    private boolean hasAudioCapture() {
        return mDialogHelper.deviceHasAudioCapture();
    }

    /**
     * According to USB audio warning dialog matrix table to return warning message id.
     * @return string resId for USB audio warning dialog message, otherwise {ID_NULL}.
     * See usb_audio.md for USB audio Permission and Confirmation warning dialog resource
     * string id matrix table.
     */
    public int getMessageId() {
        if (!mDialogHelper.isUsbDevice()) {
            return getUsbAccessoryPromptId();
        }

        if (hasRecordPermission() && isUsbAudioDevice()) {
            // case# 1, 2, 3
            return R.string.usb_audio_device_prompt;
        } else if (!hasRecordPermission() && isUsbAudioDevice() && hasAudioPlayback()
                && !hasAudioCapture()) {
            // case# 5
            return R.string.usb_audio_device_prompt;
        }

        if (!hasRecordPermission() && isUsbAudioDevice() && hasAudioCapture()) {
            // case# 6,7
            return R.string.usb_audio_device_prompt_warn;
        }

        Log.w(TAG, "Only shows title with empty content description!");
        return Resources.ID_NULL;
    }

    /**
     * Gets prompt dialog title.
     * @return string id for USB prompt dialog title.
     */
    public int getPromptTitleId() {
        return (mDialogType == TYPE_PERMISSION)
                ? R.string.usb_audio_device_permission_prompt_title
                : R.string.usb_audio_device_confirm_prompt_title;
    }

    /**
     * Gets USB Accessory prompt message id.
     * @return string id for USB Accessory prompt message.
     */
    public int getUsbAccessoryPromptId() {
        return (mDialogType == TYPE_PERMISSION)
                ? R.string.usb_accessory_permission_prompt : R.string.usb_accessory_confirm_prompt;
    }
}