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