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 com.android.internal.app.ResolverActivity;
     20 
     21 import android.content.ActivityNotFoundException;
     22 import android.content.Intent;
     23 import android.content.pm.ResolveInfo;
     24 import android.hardware.usb.IUsbManager;
     25 import android.hardware.usb.UsbAccessory;
     26 import android.hardware.usb.UsbManager;
     27 import android.os.Bundle;
     28 import android.os.IBinder;
     29 import android.os.Parcelable;
     30 import android.os.RemoteException;
     31 import android.os.ServiceManager;
     32 import android.util.Log;
     33 import android.widget.CheckBox;
     34 
     35 import com.android.systemui.R;
     36 
     37 import java.util.ArrayList;
     38 
     39 /* Activity for choosing an application for a USB device or accessory */
     40 public class UsbResolverActivity extends ResolverActivity {
     41     public static final String TAG = "UsbResolverActivity";
     42     public static final String EXTRA_RESOLVE_INFOS = "rlist";
     43 
     44     private UsbAccessory mAccessory;
     45     private UsbDisconnectedReceiver mDisconnectedReceiver;
     46 
     47     @Override
     48     protected void onCreate(Bundle savedInstanceState) {
     49         Intent intent = getIntent();
     50         Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
     51         if (!(targetParcelable instanceof Intent)) {
     52             Log.w("UsbResolverActivity", "Target is not an intent: " + targetParcelable);
     53             finish();
     54             return;
     55         }
     56         Intent target = (Intent)targetParcelable;
     57         ArrayList<ResolveInfo> rList = intent.getParcelableArrayListExtra(EXTRA_RESOLVE_INFOS);
     58         CharSequence title = getResources().getText(com.android.internal.R.string.chooseUsbActivity);
     59         super.onCreate(savedInstanceState, target, title, null, rList,
     60                 true /* Set alwaysUseOption to true to enable "always use this app" checkbox. */ );
     61 
     62         CheckBox alwaysUse = (CheckBox)findViewById(com.android.internal.R.id.alwaysUse);
     63         if (alwaysUse != null) {
     64             alwaysUse.setText(R.string.always_use_accessory);
     65         }
     66 
     67         mAccessory = (UsbAccessory)target.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
     68         if (mAccessory == null) {
     69             Log.e(TAG, "accessory is null");
     70             finish();
     71             return;
     72         }
     73         mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
     74     }
     75 
     76     @Override
     77     protected void onDestroy() {
     78         if (mDisconnectedReceiver != null) {
     79             unregisterReceiver(mDisconnectedReceiver);
     80         }
     81         super.onDestroy();
     82     }
     83 
     84     protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) {
     85         try {
     86             IBinder b = ServiceManager.getService(USB_SERVICE);
     87             IUsbManager service = IUsbManager.Stub.asInterface(b);
     88             int uid = ri.activityInfo.applicationInfo.uid;
     89 
     90             // grant permission for the accessory
     91             service.grantAccessoryPermission(mAccessory, uid);
     92             // set or clear default setting
     93             if (alwaysCheck) {
     94                 service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName);
     95             } else {
     96                 service.setAccessoryPackage(mAccessory, null);
     97             }
     98 
     99             try {
    100                 startActivity(intent);
    101             } catch (ActivityNotFoundException e) {
    102                 Log.e(TAG, "startActivity failed", e);
    103             }
    104         } catch (RemoteException e) {
    105             Log.e(TAG, "onIntentSelected failed", e);
    106         }
    107     }
    108 }
    109