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 static com.android.internal.app.IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE;
20 
21 import android.content.ActivityNotFoundException;
22 import android.content.Intent;
23 import android.content.pm.ResolveInfo;
24 import android.hardware.usb.IUsbManager;
25 import android.hardware.usb.UsbAccessory;
26 import android.hardware.usb.UsbDevice;
27 import android.hardware.usb.UsbManager;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.os.Parcelable;
31 import android.os.RemoteException;
32 import android.os.ServiceManager;
33 import android.os.UserHandle;
34 import android.util.Log;
35 import android.widget.CheckBox;
36 
37 import com.android.internal.app.ResolverActivity;
38 import com.android.internal.app.chooser.TargetInfo;
39 import com.android.systemui.R;
40 
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 
44 /* Activity for choosing an application for a USB device or accessory */
45 public class UsbResolverActivity extends ResolverActivity {
46     public static final String TAG = "UsbResolverActivity";
47     public static final String EXTRA_RESOLVE_INFOS = "rlist";
48     public static final String EXTRA_RESOLVE_INFO = "rinfo";
49 
50     private UsbDevice mDevice;
51     private UsbAccessory mAccessory;
52     private UsbDisconnectedReceiver mDisconnectedReceiver;
53 
54     /** Resolve info that switches user profiles */
55     private ResolveInfo mForwardResolveInfo;
56 
57     /** The intent that should be started when the profile is switched */
58     private Intent mOtherProfileIntent;
59 
60     @Override
onCreate(Bundle savedInstanceState)61     protected void onCreate(Bundle savedInstanceState) {
62         Intent intent = getIntent();
63         Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
64         if (!(targetParcelable instanceof Intent)) {
65             Log.w("UsbResolverActivity", "Target is not an intent: " + targetParcelable);
66             finish();
67             return;
68         }
69         Intent target = (Intent)targetParcelable;
70         ArrayList<ResolveInfo> rList = new ArrayList<>(
71                 intent.getParcelableArrayListExtra(EXTRA_RESOLVE_INFOS));
72 
73         // The rList contains the apps for all profiles of this users. Separate those. We currently
74         // only support two profiles, i.e. one forward resolve info.
75         ArrayList<ResolveInfo> rListOtherProfile = new ArrayList<>();
76         mForwardResolveInfo = null;
77         for (Iterator<ResolveInfo> iterator = rList.iterator(); iterator.hasNext();) {
78             ResolveInfo ri = iterator.next();
79 
80             if (ri.getComponentInfo().name.equals(FORWARD_INTENT_TO_MANAGED_PROFILE)) {
81                 mForwardResolveInfo = ri;
82             } else if (UserHandle.getUserId(ri.activityInfo.applicationInfo.uid)
83                     != UserHandle.myUserId()) {
84                 iterator.remove();
85                 rListOtherProfile.add(ri);
86             }
87         }
88 
89         mDevice = (UsbDevice)target.getParcelableExtra(UsbManager.EXTRA_DEVICE);
90         boolean hasAudioCapture = false;
91         if (mDevice != null) {
92             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
93             hasAudioCapture = mDevice.getHasAudioCapture();
94         } else {
95             mAccessory = (UsbAccessory)target.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
96             if (mAccessory == null) {
97                 Log.e(TAG, "no device or accessory");
98                 finish();
99                 return;
100             }
101             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
102         }
103 
104         // Create intent that will be used when switching to other profile. Emulate the behavior of
105         // UsbProfileGroupSettingsManager#resolveActivity
106         if (mForwardResolveInfo != null) {
107             if (rListOtherProfile.size() > 1) {
108                 mOtherProfileIntent = new Intent(intent);
109                 mOtherProfileIntent.putParcelableArrayListExtra(EXTRA_RESOLVE_INFOS,
110                         rListOtherProfile);
111             } else {
112                 mOtherProfileIntent = new Intent(this, UsbConfirmActivity.class);
113                 mOtherProfileIntent.putExtra(EXTRA_RESOLVE_INFO, rListOtherProfile.get(0));
114 
115                 if (mDevice != null) {
116                     mOtherProfileIntent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
117                 }
118 
119                 if (mAccessory != null) {
120                     mOtherProfileIntent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
121                 }
122             }
123         }
124         getIntent().putExtra(
125                 ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, hasAudioCapture);
126 
127         CharSequence title = getResources().getText(com.android.internal.R.string.chooseUsbActivity);
128         super.onCreate(savedInstanceState, target, title, null, rList, true);
129 
130         CheckBox alwaysUse = (CheckBox)findViewById(com.android.internal.R.id.alwaysUse);
131         if (alwaysUse != null) {
132             if (mDevice == null) {
133                 alwaysUse.setText(R.string.always_use_accessory);
134             } else {
135                 alwaysUse.setText(R.string.always_use_device);
136             }
137         }
138     }
139 
140     @Override
onDestroy()141     protected void onDestroy() {
142         if (mDisconnectedReceiver != null) {
143             unregisterReceiver(mDisconnectedReceiver);
144         }
145         super.onDestroy();
146     }
147 
148     @Override
onTargetSelected(TargetInfo target, boolean alwaysCheck)149     protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
150         final ResolveInfo ri = target.getResolveInfo();
151         if (ri == mForwardResolveInfo) {
152             startActivityAsUser(mOtherProfileIntent, null,
153                     UserHandle.of(mForwardResolveInfo.targetUserId));
154             return true;
155         }
156 
157         try {
158             IBinder b = ServiceManager.getService(USB_SERVICE);
159             IUsbManager service = IUsbManager.Stub.asInterface(b);
160             final int uid = ri.activityInfo.applicationInfo.uid;
161             final int userId = UserHandle.myUserId();
162 
163             if (mDevice != null) {
164                 // grant permission for the device
165                 service.grantDevicePermission(mDevice, uid);
166                 // set or clear default setting
167                 if (alwaysCheck) {
168                     service.setDevicePackage(mDevice, ri.activityInfo.packageName, userId);
169                 } else {
170                     service.setDevicePackage(mDevice, null, userId);
171                 }
172             } else if (mAccessory != null) {
173                 // grant permission for the accessory
174                 service.grantAccessoryPermission(mAccessory, uid);
175                 // set or clear default setting
176                 if (alwaysCheck) {
177                     service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName, userId);
178                 } else {
179                     service.setAccessoryPackage(mAccessory, null, userId);
180                 }
181             }
182 
183             try {
184                 target.startAsUser(this, null, UserHandle.of(userId));
185             } catch (ActivityNotFoundException e) {
186                 Log.e(TAG, "startActivity failed", e);
187             }
188         } catch (RemoteException e) {
189             Log.e(TAG, "onIntentSelected failed", e);
190         }
191         return true;
192     }
193 
194     @Override
shouldShowTabs()195     protected boolean shouldShowTabs() {
196         return false;
197     }
198 }
199