blob: 1e69fc5ce1fcb9d6ed0472a455bd41e872b73208 [file] [log] [blame]
Mike Lockwood3a68b832011-03-08 10:08:59 -05001/*
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
Mike Lockwood3a68b832011-03-08 10:08:59 -050019import android.app.AlertDialog;
20import android.app.PendingIntent;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.hardware.usb.IUsbManager;
Mike Lockwood3a68b832011-03-08 10:08:59 -050027import android.hardware.usb.UsbAccessory;
John Spurlockde84f0e2013-06-12 12:41:00 -040028import android.hardware.usb.UsbDevice;
Mike Lockwood3a68b832011-03-08 10:08:59 -050029import android.hardware.usb.UsbManager;
30import android.os.Bundle;
31import android.os.IBinder;
32import android.os.RemoteException;
33import android.os.ServiceManager;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070034import android.os.UserHandle;
Mike Lockwood3a68b832011-03-08 10:08:59 -050035import 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;
Mike Lockwood3a68b832011-03-08 10:08:59 -050044import com.android.systemui.R;
45
46public class UsbPermissionActivity extends AlertActivity
47 implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
48
49 private static final String TAG = "UsbPermissionActivity";
50
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -040051 private CheckBox mAlwaysUse;
Mike Lockwood3a68b832011-03-08 10:08:59 -050052 private TextView mClearDefaultHint;
53 private UsbDevice mDevice;
54 private UsbAccessory mAccessory;
55 private PendingIntent mPendingIntent;
56 private String mPackageName;
57 private int mUid;
58 private boolean mPermissionGranted;
Mike Lockwoodd5913572011-03-08 22:47:08 -050059 private UsbDisconnectedReceiver mDisconnectedReceiver;
Mike Lockwood3a68b832011-03-08 10:08:59 -050060
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 mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT);
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070069 mUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
Mike Lockwood3a68b832011-03-08 10:08:59 -050070 mPackageName = intent.getStringExtra("package");
71
72 PackageManager packageManager = getPackageManager();
73 ApplicationInfo aInfo;
74 try {
75 aInfo = packageManager.getApplicationInfo(mPackageName, 0);
76 } catch (PackageManager.NameNotFoundException e) {
77 Log.e(TAG, "unable to look up package name", e);
78 finish();
79 return;
80 }
81 String appName = aInfo.loadLabel(packageManager).toString();
82
83 final AlertController.AlertParams ap = mAlertParams;
84 ap.mIcon = aInfo.loadIcon(packageManager);
85 ap.mTitle = appName;
86 if (mDevice == null) {
87 ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName);
Mike Lockwoodd5913572011-03-08 22:47:08 -050088 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
Mike Lockwood3a68b832011-03-08 10:08:59 -050089 } else {
90 ap.mMessage = getString(R.string.usb_device_permission_prompt, appName);
Mike Lockwoodd5913572011-03-08 22:47:08 -050091 mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
Mike Lockwood3a68b832011-03-08 10:08:59 -050092 }
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040093 ap.mPositiveButtonText = getString(android.R.string.ok);
94 ap.mNegativeButtonText = getString(android.R.string.cancel);
Mike Lockwood3a68b832011-03-08 10:08:59 -050095 ap.mPositiveButtonListener = this;
96 ap.mNegativeButtonListener = this;
97
98 // add "always use" checkbox
99 LayoutInflater inflater = (LayoutInflater)getSystemService(
100 Context.LAYOUT_INFLATER_SERVICE);
101 ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400102 mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
Mike Lockwoodad5f83e2011-03-15 16:04:12 -0400103 if (mDevice == null) {
104 mAlwaysUse.setText(R.string.always_use_accessory);
105 } else {
106 mAlwaysUse.setText(R.string.always_use_device);
107 }
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400108 mAlwaysUse.setOnCheckedChangeListener(this);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500109 mClearDefaultHint = (TextView)ap.mView.findViewById(
110 com.android.internal.R.id.clearDefaultHint);
111 mClearDefaultHint.setVisibility(View.GONE);
112
113 setupAlert();
114
115 }
116
117 @Override
118 public void onDestroy() {
119 IBinder b = ServiceManager.getService(USB_SERVICE);
120 IUsbManager service = IUsbManager.Stub.asInterface(b);
121
122 // send response via pending intent
123 Intent intent = new Intent();
124 try {
125 if (mDevice != null) {
126 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
127 if (mPermissionGranted) {
128 service.grantDevicePermission(mDevice, mUid);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400129 if (mAlwaysUse.isChecked()) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700130 final int userId = UserHandle.getUserId(mUid);
131 service.setDevicePackage(mDevice, mPackageName, userId);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500132 }
133 }
134 }
135 if (mAccessory != null) {
136 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
137 if (mPermissionGranted) {
138 service.grantAccessoryPermission(mAccessory, mUid);
Mike Lockwoodbce6f8f2011-03-13 17:26:52 -0400139 if (mAlwaysUse.isChecked()) {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -0700140 final int userId = UserHandle.getUserId(mUid);
141 service.setAccessoryPackage(mAccessory, mPackageName, userId);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500142 }
143 }
144 }
145 intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
146 mPendingIntent.send(this, 0, intent);
147 } catch (PendingIntent.CanceledException e) {
148 Log.w(TAG, "PendingIntent was cancelled");
149 } catch (RemoteException e) {
150 Log.e(TAG, "IUsbService connection failed", e);
151 }
152
Mike Lockwoodd5913572011-03-08 22:47:08 -0500153 if (mDisconnectedReceiver != null) {
154 unregisterReceiver(mDisconnectedReceiver);
155 }
Mike Lockwood3a68b832011-03-08 10:08:59 -0500156 super.onDestroy();
157 }
158
159 public void onClick(DialogInterface dialog, int which) {
160 if (which == AlertDialog.BUTTON_POSITIVE) {
161 mPermissionGranted = true;
162 }
163 finish();
164 }
165
166 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
167 if (mClearDefaultHint == null) return;
168
169 if(isChecked) {
170 mClearDefaultHint.setVisibility(View.VISIBLE);
171 } else {
172 mClearDefaultHint.setVisibility(View.GONE);
173 }
174 }
175}