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.Activity;
     20 import android.app.AlertDialog;
     21 import android.content.ComponentName;
     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.content.pm.ResolveInfo;
     28 import android.hardware.usb.IUsbManager;
     29 import android.hardware.usb.UsbDevice;
     30 import android.hardware.usb.UsbAccessory;
     31 import android.hardware.usb.UsbManager;
     32 import android.os.Bundle;
     33 import android.os.IBinder;
     34 import android.os.RemoteException;
     35 import android.os.ServiceManager;
     36 import android.util.Log;
     37 import android.view.LayoutInflater;
     38 import android.view.View;
     39 import android.widget.CheckBox;
     40 import android.widget.CompoundButton;
     41 import android.widget.TextView;
     42 
     43 import com.android.internal.app.AlertActivity;
     44 import com.android.internal.app.AlertController;
     45 
     46 import com.android.systemui.R;
     47 
     48 public 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(android.R.string.ok);
     84         ap.mNegativeButtonText = getString(android.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         if (mDevice == null) {
     94             mAlwaysUse.setText(R.string.always_use_accessory);
     95         } else {
     96             mAlwaysUse.setText(R.string.always_use_device);
     97         }
     98         mAlwaysUse.setOnCheckedChangeListener(this);
     99         mClearDefaultHint = (TextView)ap.mView.findViewById(
    100                                                     com.android.internal.R.id.clearDefaultHint);
    101         mClearDefaultHint.setVisibility(View.GONE);
    102 
    103         setupAlert();
    104 
    105     }
    106 
    107     @Override
    108     protected void onDestroy() {
    109         if (mDisconnectedReceiver != null) {
    110             unregisterReceiver(mDisconnectedReceiver);
    111         }
    112         super.onDestroy();
    113     }
    114 
    115     public void onClick(DialogInterface dialog, int which) {
    116         if (which == AlertDialog.BUTTON_POSITIVE) {
    117             try {
    118                 IBinder b = ServiceManager.getService(USB_SERVICE);
    119                 IUsbManager service = IUsbManager.Stub.asInterface(b);
    120                 int uid = mResolveInfo.activityInfo.applicationInfo.uid;
    121                 boolean alwaysUse = mAlwaysUse.isChecked();
    122                 Intent intent = null;
    123 
    124                 if (mDevice != null) {
    125                     intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    126                     intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
    127 
    128                     // grant permission for the device
    129                     service.grantDevicePermission(mDevice, uid);
    130                     // set or clear default setting
    131                     if (alwaysUse) {
    132                         service.setDevicePackage(mDevice, mResolveInfo.activityInfo.packageName);
    133                     } else {
    134                         service.setDevicePackage(mDevice, null);
    135                     }
    136                 } else if (mAccessory != null) {
    137                     intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
    138                     intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
    139 
    140                     // grant permission for the accessory
    141                     service.grantAccessoryPermission(mAccessory, uid);
    142                     // set or clear default setting
    143                     if (alwaysUse) {
    144                         service.setAccessoryPackage(mAccessory,
    145                                 mResolveInfo.activityInfo.packageName);
    146                     } else {
    147                         service.setAccessoryPackage(mAccessory, null);
    148                     }
    149                 }
    150 
    151                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    152                 intent.setComponent(
    153                     new ComponentName(mResolveInfo.activityInfo.packageName,
    154                             mResolveInfo.activityInfo.name));
    155                 startActivity(intent);
    156             } catch (Exception e) {
    157                 Log.e(TAG, "Unable to start activity", e);
    158             }
    159         }
    160         finish();
    161     }
    162 
    163     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    164         if (mClearDefaultHint == null) return;
    165 
    166         if(isChecked) {
    167             mClearDefaultHint.setVisibility(View.VISIBLE);
    168         } else {
    169             mClearDefaultHint.setVisibility(View.GONE);
    170         }
    171     }
    172 }
    173