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