• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.systemui.usb;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.PendingIntent;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.hardware.usb.IUsbManager;
28 import android.hardware.usb.UsbDevice;
29 import android.hardware.usb.UsbAccessory;
30 import android.hardware.usb.UsbManager;
31 import android.os.Bundle;
32 import android.os.IBinder;
33 import android.os.RemoteException;
34 import android.os.ServiceManager;
35 import android.util.Log;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.widget.CheckBox;
39 import android.widget.CompoundButton;
40 import android.widget.TextView;
41 
42 import com.android.internal.app.AlertActivity;
43 import com.android.internal.app.AlertController;
44 
45 import com.android.systemui.R;
46 
47 public class UsbPermissionActivity extends AlertActivity
48         implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
49 
50     private static final String TAG = "UsbPermissionActivity";
51 
52     private CheckBox mAlwaysUse;
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
onCreate(Bundle icicle)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(android.R.string.ok);
95         ap.mNegativeButtonText = getString(android.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         mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
104         if (mDevice == null) {
105             mAlwaysUse.setText(R.string.always_use_accessory);
106         } else {
107             mAlwaysUse.setText(R.string.always_use_device);
108         }
109         mAlwaysUse.setOnCheckedChangeListener(this);
110         mClearDefaultHint = (TextView)ap.mView.findViewById(
111                                                     com.android.internal.R.id.clearDefaultHint);
112         mClearDefaultHint.setVisibility(View.GONE);
113 
114         setupAlert();
115 
116     }
117 
118     @Override
onDestroy()119     public void onDestroy() {
120         IBinder b = ServiceManager.getService(USB_SERVICE);
121         IUsbManager service = IUsbManager.Stub.asInterface(b);
122 
123         // send response via pending intent
124         Intent intent = new Intent();
125         try {
126             if (mDevice != null) {
127                 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
128                 if (mPermissionGranted) {
129                     service.grantDevicePermission(mDevice, mUid);
130                     if (mAlwaysUse.isChecked()) {
131                         service.setDevicePackage(mDevice, mPackageName);
132                     }
133                 }
134             }
135             if (mAccessory != null) {
136                 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
137                 if (mPermissionGranted) {
138                     service.grantAccessoryPermission(mAccessory, mUid);
139                     if (mAlwaysUse.isChecked()) {
140                         service.setAccessoryPackage(mAccessory, mPackageName);
141                     }
142                 }
143             }
144             intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
145             mPendingIntent.send(this, 0, intent);
146         } catch (PendingIntent.CanceledException e) {
147             Log.w(TAG, "PendingIntent was cancelled");
148         } catch (RemoteException e) {
149             Log.e(TAG, "IUsbService connection failed", e);
150         }
151 
152         if (mDisconnectedReceiver != null) {
153             unregisterReceiver(mDisconnectedReceiver);
154         }
155         super.onDestroy();
156     }
157 
onClick(DialogInterface dialog, int which)158     public void onClick(DialogInterface dialog, int which) {
159         if (which == AlertDialog.BUTTON_POSITIVE) {
160             mPermissionGranted = true;
161         }
162         finish();
163     }
164 
onCheckedChanged(CompoundButton buttonView, boolean isChecked)165     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
166         if (mClearDefaultHint == null) return;
167 
168         if(isChecked) {
169             mClearDefaultHint.setVisibility(View.VISIBLE);
170         } else {
171             mClearDefaultHint.setVisibility(View.GONE);
172         }
173     }
174 }
175