Home | History | Annotate | Download | only in usb
      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.AlertDialog;
     20 import android.app.PendingIntent;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.Intent;
     24 import android.content.pm.ApplicationInfo;
     25 import android.content.pm.PackageManager;
     26 import android.hardware.usb.IUsbManager;
     27 import android.hardware.usb.UsbAccessory;
     28 import android.hardware.usb.UsbDevice;
     29 import android.hardware.usb.UsbManager;
     30 import android.os.Bundle;
     31 import android.os.IBinder;
     32 import android.os.RemoteException;
     33 import android.os.ServiceManager;
     34 import android.os.UserHandle;
     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 import com.android.systemui.R;
     45 
     46 public class UsbPermissionActivity extends AlertActivity
     47         implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
     48 
     49     private static final String TAG = "UsbPermissionActivity";
     50 
     51     private CheckBox mAlwaysUse;
     52     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;
     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         mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT);
     69         mUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
     70         mPackageName = intent.getStringExtra(UsbManager.EXTRA_PACKAGE);
     71         boolean canBeDefault = intent.getBooleanExtra(UsbManager.EXTRA_CAN_BE_DEFAULT, false);
     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.mTitle = appName;
     86         if (mDevice == null) {
     87             ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName,
     88                     mAccessory.getDescription());
     89             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
     90         } else {
     91             ap.mMessage = getString(R.string.usb_device_permission_prompt, appName,
     92                     mDevice.getProductName());
     93             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
     94         }
     95         ap.mPositiveButtonText = getString(android.R.string.ok);
     96         ap.mNegativeButtonText = getString(android.R.string.cancel);
     97         ap.mPositiveButtonListener = this;
     98         ap.mNegativeButtonListener = this;
     99 
    100         if (canBeDefault && (mDevice != null || mAccessory != null)) {
    101             // add "open when" checkbox
    102             LayoutInflater inflater = (LayoutInflater) getSystemService(
    103                     Context.LAYOUT_INFLATER_SERVICE);
    104             ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
    105             mAlwaysUse = (CheckBox) ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
    106             if (mDevice == null) {
    107                 mAlwaysUse.setText(getString(R.string.always_use_accessory, appName,
    108                         mAccessory.getDescription()));
    109             } else {
    110                 mAlwaysUse.setText(getString(R.string.always_use_device, appName,
    111                         mDevice.getProductName()));
    112             }
    113             mAlwaysUse.setOnCheckedChangeListener(this);
    114 
    115             mClearDefaultHint = (TextView)ap.mView.findViewById(
    116                     com.android.internal.R.id.clearDefaultHint);
    117             mClearDefaultHint.setVisibility(View.GONE);
    118         }
    119 
    120         setupAlert();
    121     }
    122 
    123     @Override
    124     public void onDestroy() {
    125         IBinder b = ServiceManager.getService(USB_SERVICE);
    126         IUsbManager service = IUsbManager.Stub.asInterface(b);
    127 
    128         // send response via pending intent
    129         Intent intent = new Intent();
    130         try {
    131             if (mDevice != null) {
    132                 intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
    133                 if (mPermissionGranted) {
    134                     service.grantDevicePermission(mDevice, mUid);
    135                     if (mAlwaysUse != null && mAlwaysUse.isChecked()) {
    136                         final int userId = UserHandle.getUserId(mUid);
    137                         service.setDevicePackage(mDevice, mPackageName, userId);
    138                     }
    139                 }
    140             }
    141             if (mAccessory != null) {
    142                 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
    143                 if (mPermissionGranted) {
    144                     service.grantAccessoryPermission(mAccessory, mUid);
    145                     if (mAlwaysUse != null && mAlwaysUse.isChecked()) {
    146                         final int userId = UserHandle.getUserId(mUid);
    147                         service.setAccessoryPackage(mAccessory, mPackageName, userId);
    148                     }
    149                 }
    150             }
    151             intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
    152             mPendingIntent.send(this, 0, intent);
    153         } catch (PendingIntent.CanceledException e) {
    154             Log.w(TAG, "PendingIntent was cancelled");
    155         } catch (RemoteException e) {
    156             Log.e(TAG, "IUsbService connection failed", e);
    157         }
    158 
    159         if (mDisconnectedReceiver != null) {
    160             unregisterReceiver(mDisconnectedReceiver);
    161         }
    162         super.onDestroy();
    163     }
    164 
    165     public void onClick(DialogInterface dialog, int which) {
    166         if (which == AlertDialog.BUTTON_POSITIVE) {
    167             mPermissionGranted = true;
    168         }
    169         finish();
    170     }
    171 
    172     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    173         if (mClearDefaultHint == null) return;
    174 
    175         if(isChecked) {
    176             mClearDefaultHint.setVisibility(View.VISIBLE);
    177         } else {
    178             mClearDefaultHint.setVisibility(View.GONE);
    179         }
    180     }
    181 }
    182