1 /*
2  * Copyright (C) 2011 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 android.content.res.Resources;
20 import android.os.Bundle;
21 
22 import javax.inject.Inject;
23 
24 /**
25  * Dialog shown to confirm the package to start when a USB device or accessory is attached and there
26  * is only one package that claims to handle this USB device or accessory.
27  */
28 public class UsbConfirmActivity extends UsbDialogActivity {
29 
30     private UsbAudioWarningDialogMessage mUsbConfirmMessageHandler;
31 
32     @Inject
UsbConfirmActivity(UsbAudioWarningDialogMessage usbAudioWarningDialogMessage)33     public UsbConfirmActivity(UsbAudioWarningDialogMessage usbAudioWarningDialogMessage) {
34         mUsbConfirmMessageHandler = usbAudioWarningDialogMessage;
35     }
36 
37     @Override
onCreate(Bundle icicle)38     public void onCreate(Bundle icicle) {
39         super.onCreate(icicle);
40         mUsbConfirmMessageHandler.init(UsbAudioWarningDialogMessage.TYPE_CONFIRM, mDialogHelper);
41     }
42 
43     @Override
onResume()44     protected void onResume() {
45         super.onResume();
46         // Only show the "always use" checkbox if there is no USB/Record warning
47         final boolean useRecordWarning = mDialogHelper.isUsbDevice()
48                 && (mDialogHelper.deviceHasAudioCapture()
49                 && !mDialogHelper.packageHasAudioRecordingPermission());
50 
51         final int titleId = mUsbConfirmMessageHandler.getPromptTitleId();
52         final String title = getString(titleId, mDialogHelper.getAppName(),
53                 mDialogHelper.getDeviceDescription());
54         final int messageId = mUsbConfirmMessageHandler.getMessageId();
55         String message = (messageId != Resources.ID_NULL)
56                 ? getString(messageId, mDialogHelper.getAppName(),
57                 mDialogHelper.getDeviceDescription()) : null;
58         setAlertParams(title, message);
59         if (!useRecordWarning) {
60             addAlwaysUseCheckbox();
61         }
62         setupAlert();
63     }
64 
65     @Override
onConfirm()66     void onConfirm() {
67         mDialogHelper.grantUidAccessPermission();
68         if (isAlwaysUseChecked()) {
69             mDialogHelper.setDefaultPackage();
70         } else {
71             mDialogHelper.clearDefaultPackage();
72         }
73         mDialogHelper.confirmDialogStartActivity();
74         finish();
75     }
76 }
77