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.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.Intent;
     24 import android.content.pm.PackageManager;
     25 import android.content.pm.ResolveInfo;
     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.ServiceManager;
     33 import android.os.UserHandle;
     34 import android.util.Log;
     35 import android.view.LayoutInflater;
     36 import android.view.View;
     37 import android.widget.CheckBox;
     38 import android.widget.CompoundButton;
     39 import android.widget.TextView;
     40 
     41 import com.android.internal.app.AlertActivity;
     42 import com.android.internal.app.AlertController;
     43 import com.android.systemui.R;
     44 
     45 public class UsbConfirmActivity extends AlertActivity
     46         implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
     47 
     48     private static final String TAG = "UsbConfirmActivity";
     49 
     50     private CheckBox mAlwaysUse;
     51     private TextView mClearDefaultHint;
     52     private UsbDevice mDevice;
     53     private UsbAccessory mAccessory;
     54     private ResolveInfo mResolveInfo;
     55     private boolean mPermissionGranted;
     56     private UsbDisconnectedReceiver mDisconnectedReceiver;
     57 
     58     @Override
     59     public void onCreate(Bundle icicle) {
     60         super.onCreate(icicle);
     61 
     62         Intent intent = getIntent();
     63         mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
     64         mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
     65         mResolveInfo = (ResolveInfo) intent.getParcelableExtra("rinfo");
     66 
     67         PackageManager packageManager = getPackageManager();
     68         String appName = mResolveInfo.loadLabel(packageManager).toString();
     69 
     70         final AlertController.AlertParams ap = mAlertParams;
     71         ap.mIcon = mResolveInfo.loadIcon(packageManager);
     72         ap.mTitle = appName;
     73         if (mDevice == null) {
     74             ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName);
     75             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
     76         } else {
     77             ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName);
     78             mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice);
     79         }
     80         ap.mPositiveButtonText = getString(android.R.string.ok);
     81         ap.mNegativeButtonText = getString(android.R.string.cancel);
     82         ap.mPositiveButtonListener = this;
     83         ap.mNegativeButtonListener = this;
     84 
     85         // add "always use" checkbox
     86         LayoutInflater inflater = (LayoutInflater)getSystemService(
     87                 Context.LAYOUT_INFLATER_SERVICE);
     88         ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
     89         mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
     90         if (mDevice == null) {
     91             mAlwaysUse.setText(R.string.always_use_accessory);
     92         } else {
     93             mAlwaysUse.setText(R.string.always_use_device);
     94         }
     95         mAlwaysUse.setOnCheckedChangeListener(this);
     96         mClearDefaultHint = (TextView)ap.mView.findViewById(
     97                                                     com.android.internal.R.id.clearDefaultHint);
     98         mClearDefaultHint.setVisibility(View.GONE);
     99 
    100         setupAlert();
    101 
    102     }
    103 
    104     @Override
    105     protected void onDestroy() {
    106         if (mDisconnectedReceiver != null) {
    107             unregisterReceiver(mDisconnectedReceiver);
    108         }
    109         super.onDestroy();
    110     }
    111 
    112     public void onClick(DialogInterface dialog, int which) {
    113         if (which == AlertDialog.BUTTON_POSITIVE) {
    114             try {
    115                 IBinder b = ServiceManager.getService(USB_SERVICE);
    116                 IUsbManager service = IUsbManager.Stub.asInterface(b);
    117                 final int uid = mResolveInfo.activityInfo.applicationInfo.uid;
    118                 final int userId = UserHandle.myUserId();
    119                 boolean alwaysUse = mAlwaysUse.isChecked();
    120                 Intent intent = null;
    121 
    122                 if (mDevice != null) {
    123                     intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    124                     intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice);
    125 
    126                     // grant permission for the device
    127                     service.grantDevicePermission(mDevice, uid);
    128                     // set or clear default setting
    129                     if (alwaysUse) {
    130                         service.setDevicePackage(
    131                                 mDevice, mResolveInfo.activityInfo.packageName, userId);
    132                     } else {
    133                         service.setDevicePackage(mDevice, null, userId);
    134                     }
    135                 } else if (mAccessory != null) {
    136                     intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
    137                     intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
    138 
    139                     // grant permission for the accessory
    140                     service.grantAccessoryPermission(mAccessory, uid);
    141                     // set or clear default setting
    142                     if (alwaysUse) {
    143                         service.setAccessoryPackage(
    144                                 mAccessory, mResolveInfo.activityInfo.packageName, userId);
    145                     } else {
    146                         service.setAccessoryPackage(mAccessory, null, userId);
    147                     }
    148                 }
    149 
    150                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    151                 intent.setComponent(
    152                     new ComponentName(mResolveInfo.activityInfo.packageName,
    153                             mResolveInfo.activityInfo.name));
    154                 startActivityAsUser(intent, new UserHandle(userId));
    155             } catch (Exception e) {
    156                 Log.e(TAG, "Unable to start activity", e);
    157             }
    158         }
    159         finish();
    160     }
    161 
    162     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    163         if (mClearDefaultHint == null) return;
    164 
    165         if(isChecked) {
    166             mClearDefaultHint.setVisibility(View.VISIBLE);
    167         } else {
    168             mClearDefaultHint.setVisibility(View.GONE);
    169         }
    170     }
    171 }
    172