• 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.content.ActivityNotFoundException;
20 import android.content.Intent;
21 import android.content.pm.ResolveInfo;
22 import android.hardware.usb.IUsbManager;
23 import android.hardware.usb.UsbAccessory;
24 import android.hardware.usb.UsbDevice;
25 import android.hardware.usb.UsbManager;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.os.Parcelable;
29 import android.os.RemoteException;
30 import android.os.ServiceManager;
31 import android.os.UserHandle;
32 import android.util.Log;
33 import android.widget.CheckBox;
34 
35 import com.android.internal.app.ResolverActivity;
36 import com.android.systemui.R;
37 
38 import java.util.ArrayList;
39 
40 /* Activity for choosing an application for a USB device or accessory */
41 public class UsbResolverActivity extends ResolverActivity {
42     public static final String TAG = "UsbResolverActivity";
43     public static final String EXTRA_RESOLVE_INFOS = "rlist";
44 
45     private UsbDevice mDevice;
46     private UsbAccessory mAccessory;
47     private UsbDisconnectedReceiver mDisconnectedReceiver;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         Intent intent = getIntent();
52         Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
53         if (!(targetParcelable instanceof Intent)) {
54             Log.w("UsbResolverActivity", "Target is not an intent: " + targetParcelable);
55             finish();
56             return;
57         }
58         Intent target = (Intent)targetParcelable;
59         ArrayList<ResolveInfo> rList = intent.getParcelableArrayListExtra(EXTRA_RESOLVE_INFOS);
60         CharSequence title = getResources().getText(com.android.internal.R.string.chooseUsbActivity);
61         super.onCreate(savedInstanceState, target, title, null, rList,
62                 true /* Set alwaysUseOption to true to enable "always use this app" checkbox. */ );
63 
64         CheckBox alwaysUse = (CheckBox)findViewById(com.android.internal.R.id.alwaysUse);
65         if (alwaysUse != null) {
66             if (mDevice == null) {
67                 alwaysUse.setText(R.string.always_use_accessory);
68             } else {
69                 alwaysUse.setText(R.string.always_use_device);
70             }
71         }
72 
73         mDevice = (UsbDevice)target.getParcelableExtra(UsbManager.EXTRA_DEVICE);
74         if (mDevice != null) {
75             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
76         } else {
77             mAccessory = (UsbAccessory)target.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
78             if (mAccessory == null) {
79                 Log.e(TAG, "no device or accessory");
80                 finish();
81                 return;
82             }
83             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
84         }
85     }
86 
87     @Override
onDestroy()88     protected void onDestroy() {
89         if (mDisconnectedReceiver != null) {
90             unregisterReceiver(mDisconnectedReceiver);
91         }
92         super.onDestroy();
93     }
94 
95     @Override
onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck)96     protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) {
97         try {
98             IBinder b = ServiceManager.getService(USB_SERVICE);
99             IUsbManager service = IUsbManager.Stub.asInterface(b);
100             final int uid = ri.activityInfo.applicationInfo.uid;
101             final int userId = UserHandle.myUserId();
102 
103             if (mDevice != null) {
104                 // grant permission for the device
105                 service.grantDevicePermission(mDevice, uid);
106                 // set or clear default setting
107                 if (alwaysCheck) {
108                     service.setDevicePackage(mDevice, ri.activityInfo.packageName, userId);
109                 } else {
110                     service.setDevicePackage(mDevice, null, userId);
111                 }
112             } else if (mAccessory != null) {
113                 // grant permission for the accessory
114                 service.grantAccessoryPermission(mAccessory, uid);
115                 // set or clear default setting
116                 if (alwaysCheck) {
117                     service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName, userId);
118                 } else {
119                     service.setAccessoryPackage(mAccessory, null, userId);
120                 }
121             }
122 
123             try {
124                 startActivityAsUser(intent, new UserHandle(userId));
125             } catch (ActivityNotFoundException e) {
126                 Log.e(TAG, "startActivity failed", e);
127             }
128         } catch (RemoteException e) {
129             Log.e(TAG, "onIntentSelected failed", e);
130         }
131     }
132 }
133