• 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.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.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 UsbConfirmActivity extends AlertActivity
48         implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
49 
50     private static final String TAG = "UsbConfirmActivity";
51 
52     private CheckBox mAlwaysUse;
53     private TextView mClearDefaultHint;
54     private UsbAccessory mAccessory;
55     private ResolveInfo mResolveInfo;
56     private boolean mPermissionGranted;
57     private UsbDisconnectedReceiver mDisconnectedReceiver;
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62 
63         Intent intent = getIntent();
64         mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
65         mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
66         mResolveInfo = (ResolveInfo)intent.getParcelableExtra("rinfo");
67 
68         PackageManager packageManager = getPackageManager();
69         String appName = mResolveInfo.loadLabel(packageManager).toString();
70 
71         final AlertController.AlertParams ap = mAlertParams;
72         ap.mIcon = mResolveInfo.loadIcon(packageManager);
73         ap.mTitle = appName;
74         ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName);
75         ap.mPositiveButtonText = getString(android.R.string.ok);
76         ap.mNegativeButtonText = getString(android.R.string.cancel);
77         ap.mPositiveButtonListener = this;
78         ap.mNegativeButtonListener = this;
79 
80         // add "always use" checkbox
81         LayoutInflater inflater = (LayoutInflater)getSystemService(
82                 Context.LAYOUT_INFLATER_SERVICE);
83         ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
84         mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
85         mAlwaysUse.setText(R.string.always_use_accessory);
86         mAlwaysUse.setOnCheckedChangeListener(this);
87         mClearDefaultHint = (TextView)ap.mView.findViewById(
88                                                     com.android.internal.R.id.clearDefaultHint);
89         mClearDefaultHint.setVisibility(View.GONE);
90 
91         setupAlert();
92 
93     }
94 
95     @Override
onDestroy()96     protected void onDestroy() {
97         if (mDisconnectedReceiver != null) {
98             unregisterReceiver(mDisconnectedReceiver);
99         }
100         super.onDestroy();
101     }
102 
onClick(DialogInterface dialog, int which)103     public void onClick(DialogInterface dialog, int which) {
104         if (which == AlertDialog.BUTTON_POSITIVE) {
105             try {
106                 IBinder b = ServiceManager.getService(USB_SERVICE);
107                 IUsbManager service = IUsbManager.Stub.asInterface(b);
108                 int uid = mResolveInfo.activityInfo.applicationInfo.uid;
109                 boolean alwaysUse = mAlwaysUse.isChecked();
110                 Intent intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
111                 intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
112                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
113                 intent.setComponent(
114                     new ComponentName(mResolveInfo.activityInfo.packageName,
115                             mResolveInfo.activityInfo.name));
116 
117                 // grant permission for the accessory
118                 service.grantAccessoryPermission(mAccessory, uid);
119                 // set or clear default setting
120                 if (alwaysUse) {
121                     service.setAccessoryPackage(mAccessory,
122                             mResolveInfo.activityInfo.packageName);
123                 } else {
124                     service.setAccessoryPackage(mAccessory, null);
125                 }
126                 startActivity(intent);
127             } catch (Exception e) {
128                 Log.e(TAG, "Unable to start activity", e);
129             }
130         }
131         finish();
132     }
133 
onCheckedChanged(CompoundButton buttonView, boolean isChecked)134     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
135         if (mClearDefaultHint == null) return;
136 
137         if(isChecked) {
138             mClearDefaultHint.setVisibility(View.VISIBLE);
139         } else {
140             mClearDefaultHint.setVisibility(View.GONE);
141         }
142     }
143 }
144