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