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;
34import android.widget.CheckBox;
35
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    protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) {
96        try {
97            IBinder b = ServiceManager.getService(USB_SERVICE);
98            IUsbManager service = IUsbManager.Stub.asInterface(b);
99            int uid = ri.activityInfo.applicationInfo.uid;
100
101            if (mDevice != null) {
102                // grant permission for the device
103                service.grantDevicePermission(mDevice, uid);
104                // set or clear default setting
105                if (alwaysCheck) {
106                    service.setDevicePackage(mDevice, ri.activityInfo.packageName);
107                } else {
108                    service.setDevicePackage(mDevice, null);
109                }
110            } else if (mAccessory != null) {
111                // grant permission for the accessory
112                service.grantAccessoryPermission(mAccessory, uid);
113                // set or clear default setting
114                if (alwaysCheck) {
115                    service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName);
116                } else {
117                    service.setAccessoryPackage(mAccessory, null);
118                }
119            }
120
121            try {
122                startActivity(intent);
123            } catch (ActivityNotFoundException e) {
124                Log.e(TAG, "startActivity failed", e);
125            }
126        } catch (RemoteException e) {
127            Log.e(TAG, "onIntentSelected failed", e);
128        }
129    }
130}
131