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.hardware.usb.IUsbManager;
27import android.hardware.usb.UsbManager;
28import android.os.Bundle;
29import android.os.IBinder;
30import android.os.ServiceManager;
31import android.os.SystemProperties;
32import android.util.Log;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.widget.CheckBox;
36
37import com.android.internal.app.AlertActivity;
38import com.android.internal.app.AlertController;
39import com.android.systemui.R;
40
41public 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.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
69        ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
70        ap.mPositiveButtonText = getString(android.R.string.ok);
71        ap.mNegativeButtonText = getString(android.R.string.cancel);
72        ap.mPositiveButtonListener = this;
73        ap.mNegativeButtonListener = this;
74
75        // add "always allow" checkbox
76        LayoutInflater inflater = LayoutInflater.from(ap.mContext);
77        View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
78        mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
79        mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
80        ap.mView = checkbox;
81
82        setupAlert();
83    }
84
85    private class UsbDisconnectedReceiver extends BroadcastReceiver {
86        private final Activity mActivity;
87        public UsbDisconnectedReceiver(Activity activity) {
88            mActivity = activity;
89        }
90
91        @Override
92        public void onReceive(Context content, Intent intent) {
93            String action = intent.getAction();
94            if (!UsbManager.ACTION_USB_STATE.equals(action)) {
95                return;
96            }
97            boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
98            if (!connected) {
99                mActivity.finish();
100            }
101        }
102    }
103
104    @Override
105    public void onStart() {
106        super.onStart();
107        IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
108        registerReceiver(mDisconnectedReceiver, filter);
109    }
110
111    @Override
112    protected void onStop() {
113        if (mDisconnectedReceiver != null) {
114            unregisterReceiver(mDisconnectedReceiver);
115        }
116        super.onStop();
117    }
118
119    @Override
120    public void onClick(DialogInterface dialog, int which) {
121        boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
122        boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
123        try {
124            IBinder b = ServiceManager.getService(USB_SERVICE);
125            IUsbManager service = IUsbManager.Stub.asInterface(b);
126            if (allow) {
127                service.allowUsbDebugging(alwaysAllow, mKey);
128            } else {
129                service.denyUsbDebugging();
130            }
131        } catch (Exception e) {
132            Log.e(TAG, "Unable to notify Usb service", e);
133        }
134        finish();
135    }
136}
137