UsbConfirmActivity.java revision 8f6dce44e34504b891b53ff8ea524268b9095a8d
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.app.Activity;
20import android.app.AlertDialog;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.hardware.usb.IUsbManager;
29import android.hardware.usb.UsbDevice;
30import android.hardware.usb.UsbAccessory;
31import android.hardware.usb.UsbManager;
32import android.os.Bundle;
33import android.os.IBinder;
34import android.os.RemoteException;
35import android.os.ServiceManager;
36import android.util.Log;
37import android.view.LayoutInflater;
38import android.view.View;
39import android.widget.CheckBox;
40import android.widget.CompoundButton;
41import android.widget.TextView;
42
43import com.android.internal.app.AlertActivity;
44import com.android.internal.app.AlertController;
45
46import com.android.systemui.R;
47
48public class UsbConfirmActivity extends AlertActivity
49        implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
50
51    private static final String TAG = "UsbConfirmActivity";
52
53    private CheckBox mAlwaysUse;
54    private TextView mClearDefaultHint;
55    private UsbDevice mDevice;
56    private UsbAccessory mAccessory;
57    private ResolveInfo mResolveInfo;
58    private boolean mPermissionGranted;
59    private UsbDisconnectedReceiver mDisconnectedReceiver;
60
61    @Override
62    public void onCreate(Bundle icicle) {
63        super.onCreate(icicle);
64
65       Intent intent = getIntent();
66        mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
67        mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
68        mResolveInfo = (ResolveInfo)intent.getParcelableExtra("rinfo");
69
70        PackageManager packageManager = getPackageManager();
71        String appName = mResolveInfo.loadLabel(packageManager).toString();
72
73        final AlertController.AlertParams ap = mAlertParams;
74        ap.mIcon = mResolveInfo.loadIcon(packageManager);
75        ap.mTitle = appName;
76        if (mDevice == null) {
77            ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName);
78            mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
79        } else {
80            ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName);
81            mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
82        }
83        ap.mPositiveButtonText = getString(com.android.internal.R.string.ok);
84        ap.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
85        ap.mPositiveButtonListener = this;
86        ap.mNegativeButtonListener = this;
87
88        // add "always use" checkbox
89        LayoutInflater inflater = (LayoutInflater)getSystemService(
90                Context.LAYOUT_INFLATER_SERVICE);
91        ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
92        mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
93        mAlwaysUse.setText(com.android.internal.R.string.alwaysUse);
94        mAlwaysUse.setOnCheckedChangeListener(this);
95        mClearDefaultHint = (TextView)ap.mView.findViewById(
96                                                    com.android.internal.R.id.clearDefaultHint);
97        mClearDefaultHint.setVisibility(View.GONE);
98
99        setupAlert();
100
101    }
102
103    @Override
104    protected void onDestroy() {
105        if (mDisconnectedReceiver != null) {
106            unregisterReceiver(mDisconnectedReceiver);
107        }
108        super.onDestroy();
109    }
110
111    public void onClick(DialogInterface dialog, int which) {
112        if (which == AlertDialog.BUTTON_POSITIVE) {
113            try {
114                IBinder b = ServiceManager.getService(USB_SERVICE);
115                IUsbManager service = IUsbManager.Stub.asInterface(b);
116                int uid = mResolveInfo.activityInfo.applicationInfo.uid;
117                boolean alwaysUse = mAlwaysUse.isChecked();
118                Intent intent = null;
119
120                if (mDevice != null) {
121                    intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
122                    intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
123
124                    // grant permission for the device
125                    service.grantDevicePermission(mDevice, uid);
126                    // set or clear default setting
127                    if (alwaysUse) {
128                        service.setDevicePackage(mDevice, mResolveInfo.activityInfo.packageName);
129                    } else {
130                        service.setDevicePackage(mDevice, null);
131                    }
132                } else if (mAccessory != null) {
133                    intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
134                    intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
135
136                    // grant permission for the accessory
137                    service.grantAccessoryPermission(mAccessory, uid);
138                    // set or clear default setting
139                    if (alwaysUse) {
140                        service.setAccessoryPackage(mAccessory,
141                                mResolveInfo.activityInfo.packageName);
142                    } else {
143                        service.setAccessoryPackage(mAccessory, null);
144                    }
145                }
146
147                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
148                intent.setComponent(
149                    new ComponentName(mResolveInfo.activityInfo.packageName,
150                            mResolveInfo.activityInfo.name));
151                startActivity(intent);
152            } catch (Exception e) {
153                Log.e(TAG, "Unable to start activity", e);
154            }
155        }
156        finish();
157    }
158
159    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
160        if (mClearDefaultHint == null) return;
161
162        if(isChecked) {
163            mClearDefaultHint.setVisibility(View.VISIBLE);
164        } else {
165            mClearDefaultHint.setVisibility(View.GONE);
166        }
167    }
168}
169