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 android.content.ActivityNotFoundException;
20import android.content.Intent;
21import android.content.pm.ResolveInfo;
22import android.hardware.usb.IUsbManager;
23import android.hardware.usb.UsbAccessory;
24import android.hardware.usb.UsbDevice;
25import android.hardware.usb.UsbManager;
26import android.os.Bundle;
27import android.os.IBinder;
28import android.os.Parcelable;
29import android.os.RemoteException;
30import android.os.ServiceManager;
31import android.os.UserHandle;
32import android.util.Log;
33import android.widget.CheckBox;
34
35import com.android.internal.app.ResolverActivity;
36import com.android.systemui.R;
37
38import java.util.ArrayList;
39
40/* Activity for choosing an application for a USB device or accessory */
41public 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
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
88    protected void onDestroy() {
89        if (mDisconnectedReceiver != null) {
90            unregisterReceiver(mDisconnectedReceiver);
91        }
92        super.onDestroy();
93    }
94
95    @Override
96    protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
97        final ResolveInfo ri = target.getResolveInfo();
98        try {
99            IBinder b = ServiceManager.getService(USB_SERVICE);
100            IUsbManager service = IUsbManager.Stub.asInterface(b);
101            final int uid = ri.activityInfo.applicationInfo.uid;
102            final int userId = UserHandle.myUserId();
103
104            if (mDevice != null) {
105                // grant permission for the device
106                service.grantDevicePermission(mDevice, uid);
107                // set or clear default setting
108                if (alwaysCheck) {
109                    service.setDevicePackage(mDevice, ri.activityInfo.packageName, userId);
110                } else {
111                    service.setDevicePackage(mDevice, null, userId);
112                }
113            } else if (mAccessory != null) {
114                // grant permission for the accessory
115                service.grantAccessoryPermission(mAccessory, uid);
116                // set or clear default setting
117                if (alwaysCheck) {
118                    service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName, userId);
119                } else {
120                    service.setAccessoryPackage(mAccessory, null, userId);
121                }
122            }
123
124            try {
125                target.startAsUser(this, null, new UserHandle(userId));
126            } catch (ActivityNotFoundException e) {
127                Log.e(TAG, "startActivity failed", e);
128            }
129        } catch (RemoteException e) {
130            Log.e(TAG, "onIntentSelected failed", e);
131        }
132        return true;
133    }
134}
135