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