Home | History | Annotate | Download | only in usb
      1 /*
      2  * Copyright (C) 2012 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.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.DialogInterface;
     24 import android.content.Intent;
     25 import android.content.IntentFilter;
     26 import android.hardware.usb.IUsbManager;
     27 import android.hardware.usb.UsbManager;
     28 import android.os.Bundle;
     29 import android.os.IBinder;
     30 import android.os.ServiceManager;
     31 import android.os.SystemProperties;
     32 import android.util.Log;
     33 import android.view.LayoutInflater;
     34 import android.view.View;
     35 import android.widget.CheckBox;
     36 
     37 import com.android.internal.app.AlertActivity;
     38 import com.android.internal.app.AlertController;
     39 import com.android.systemui.R;
     40 
     41 public class UsbDebuggingActivity extends AlertActivity
     42                                   implements DialogInterface.OnClickListener {
     43     private static final String TAG = "UsbDebuggingActivity";
     44 
     45     private CheckBox mAlwaysAllow;
     46     private UsbDisconnectedReceiver mDisconnectedReceiver;
     47     private String mKey;
     48 
     49     @Override
     50     public void onCreate(Bundle icicle) {
     51         super.onCreate(icicle);
     52 
     53         if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0) {
     54             mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
     55         }
     56 
     57         Intent intent = getIntent();
     58         String fingerprints = intent.getStringExtra("fingerprints");
     59         mKey = intent.getStringExtra("key");
     60 
     61         if (fingerprints == null || mKey == null) {
     62             finish();
     63             return;
     64         }
     65 
     66         final AlertController.AlertParams ap = mAlertParams;
     67         ap.mTitle = getString(R.string.usb_debugging_title);
     68         ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
     69         ap.mPositiveButtonText = getString(android.R.string.ok);
     70         ap.mNegativeButtonText = getString(android.R.string.cancel);
     71         ap.mPositiveButtonListener = this;
     72         ap.mNegativeButtonListener = this;
     73 
     74         // add "always allow" checkbox
     75         LayoutInflater inflater = LayoutInflater.from(ap.mContext);
     76         View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
     77         mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
     78         mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
     79         ap.mView = checkbox;
     80 
     81         setupAlert();
     82     }
     83 
     84     private class UsbDisconnectedReceiver extends BroadcastReceiver {
     85         private final Activity mActivity;
     86         public UsbDisconnectedReceiver(Activity activity) {
     87             mActivity = activity;
     88         }
     89 
     90         @Override
     91         public void onReceive(Context content, Intent intent) {
     92             String action = intent.getAction();
     93             if (!UsbManager.ACTION_USB_STATE.equals(action)) {
     94                 return;
     95             }
     96             boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
     97             if (!connected) {
     98                 mActivity.finish();
     99             }
    100         }
    101     }
    102 
    103     @Override
    104     public void onStart() {
    105         super.onStart();
    106         IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
    107         registerReceiver(mDisconnectedReceiver, filter);
    108     }
    109 
    110     @Override
    111     protected void onStop() {
    112         if (mDisconnectedReceiver != null) {
    113             unregisterReceiver(mDisconnectedReceiver);
    114         }
    115         super.onStop();
    116     }
    117 
    118     @Override
    119     public void onClick(DialogInterface dialog, int which) {
    120         boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
    121         boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
    122         try {
    123             IBinder b = ServiceManager.getService(USB_SERVICE);
    124             IUsbManager service = IUsbManager.Stub.asInterface(b);
    125             if (allow) {
    126                 service.allowUsbDebugging(alwaysAllow, mKey);
    127             } else {
    128                 service.denyUsbDebugging();
    129             }
    130         } catch (Exception e) {
    131             Log.e(TAG, "Unable to notify Usb service", e);
    132         }
    133         finish();
    134     }
    135 }
    136