• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.AlertDialog;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.PermissionChecker;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.hardware.usb.IUsbManager;
28 import android.hardware.usb.UsbAccessory;
29 import android.hardware.usb.UsbDevice;
30 import android.hardware.usb.UsbManager;
31 import android.os.Bundle;
32 import android.os.IBinder;
33 import android.os.ServiceManager;
34 import android.os.UserHandle;
35 import android.util.Log;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.Window;
39 import android.view.WindowManager;
40 import android.widget.CheckBox;
41 import android.widget.CompoundButton;
42 import android.widget.TextView;
43 
44 import com.android.internal.app.AlertActivity;
45 import com.android.internal.app.AlertController;
46 import com.android.systemui.R;
47 
48 public class UsbConfirmActivity extends AlertActivity
49         implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
50 
51     private static final String TAG = "UsbConfirmActivity";
52 
53     private CheckBox mAlwaysUse;
54     private TextView mClearDefaultHint;
55     private UsbDevice mDevice;
56     private UsbAccessory mAccessory;
57     private ResolveInfo mResolveInfo;
58     private boolean mPermissionGranted;
59     private UsbDisconnectedReceiver mDisconnectedReceiver;
60 
61     @Override
onCreate(Bundle icicle)62     public void onCreate(Bundle icicle) {
63         getWindow().addSystemFlags(
64                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
65 
66         super.onCreate(icicle);
67 
68         Intent intent = getIntent();
69         mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
70         mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
71         mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo");
72         String packageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE);
73 
74         PackageManager packageManager = getPackageManager();
75         String appName = mResolveInfo.loadLabel(packageManager).toString();
76 
77         final AlertController.AlertParams ap = mAlertParams;
78         ap.mTitle = appName;
79         boolean useRecordWarning = false;
80         if (mDevice == null) {
81             ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName,
82                     mAccessory.getDescription());
83             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
84         } else {
85             int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
86             boolean hasRecordPermission =
87                     PermissionChecker.checkPermissionForPreflight(
88                             this, android.Manifest.permission.RECORD_AUDIO, -1, uid,
89                             packageName)
90                             == android.content.pm.PackageManager.PERMISSION_GRANTED;
91             boolean isAudioCaptureDevice = mDevice.getHasAudioCapture();
92             useRecordWarning = isAudioCaptureDevice && !hasRecordPermission;
93 
94             int strID = useRecordWarning
95                     ? R.string.usb_device_confirm_prompt_warn
96                     : R.string.usb_device_confirm_prompt;
97 
98             ap.mMessage = getString(strID, appName, mDevice.getProductName());
99             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
100         }
101         ap.mPositiveButtonText = getString(android.R.string.ok);
102         ap.mNegativeButtonText = getString(android.R.string.cancel);
103         ap.mPositiveButtonListener = this;
104         ap.mNegativeButtonListener = this;
105 
106         // add "always use" checkbox
107         if (!useRecordWarning) {
108             LayoutInflater inflater = (LayoutInflater) getSystemService(
109                     Context.LAYOUT_INFLATER_SERVICE);
110             ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
111             mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
112             if (mDevice == null) {
113                 mAlwaysUse.setText(getString(R.string.always_use_accessory, appName,
114                         mAccessory.getDescription()));
115             } else {
116                 mAlwaysUse.setText(getString(R.string.always_use_device, appName,
117                         mDevice.getProductName()));
118             }
119             mAlwaysUse.setOnCheckedChangeListener(this);
120             mClearDefaultHint = (TextView) ap.mView.findViewById(
121                     com.android.internal.R.id.clearDefaultHint);
122             mClearDefaultHint.setVisibility(View.GONE);
123         }
124         setupAlert();
125 
126     }
127 
128     @Override
onDestroy()129     protected void onDestroy() {
130         if (mDisconnectedReceiver != null) {
131             unregisterReceiver(mDisconnectedReceiver);
132         }
133         super.onDestroy();
134     }
135 
onClick(DialogInterface dialog, int which)136     public void onClick(DialogInterface dialog, int which) {
137         if (which == AlertDialog.BUTTON_POSITIVE) {
138             try {
139                 IBinder b = ServiceManager.getService(USB_SERVICE);
140                 IUsbManager service = IUsbManager.Stub.asInterface(b);
141                 final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
142                 final int userId = UserHandle.myUserId();
143                 boolean alwaysUse = mAlwaysUse != null ? mAlwaysUse.isChecked() : false;
144                 Intent intent = null;
145 
146                 if (mDevice != null) {
147                     intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
148                     intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
149 
150                     // grant permission for the device
151                     service.grantDevicePermission(mDevice, uid);
152                     // set or clear default setting
153                     if (alwaysUse) {
154                         service.setDevicePackage(
155                                 mDevice, mResolveInfo.activityInfo.packageName, userId);
156                     } else {
157                         service.setDevicePackage(mDevice, null, userId);
158                     }
159                 } else if (mAccessory != null) {
160                     intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
161                     intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
162 
163                     // grant permission for the accessory
164                     service.grantAccessoryPermission(mAccessory, uid);
165                     // set or clear default setting
166                     if (alwaysUse) {
167                         service.setAccessoryPackage(
168                                 mAccessory, mResolveInfo.activityInfo.packageName, userId);
169                     } else {
170                         service.setAccessoryPackage(mAccessory, null, userId);
171                     }
172                 }
173 
174                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
175                 intent.setComponent(
176                     new ComponentName(mResolveInfo.activityInfo.packageName,
177                             mResolveInfo.activityInfo.name));
178                 startActivityAsUser(intent, new UserHandle(userId));
179             } catch (Exception e) {
180                 Log.e(TAG, "Unable to start activity", e);
181             }
182         }
183         finish();
184     }
185 
onCheckedChanged(CompoundButton buttonView, boolean isChecked)186     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
187         if (mClearDefaultHint == null) return;
188 
189         if(isChecked) {
190             mClearDefaultHint.setVisibility(View.VISIBLE);
191         } else {
192             mClearDefaultHint.setVisibility(View.GONE);
193         }
194     }
195 }
196