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.AlertDialog;
20 import android.content.ActivityNotFoundException;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.hardware.usb.UsbAccessory;
24 import android.hardware.usb.UsbManager;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.util.Log;
29 import android.view.WindowManager;
30 
31 import com.android.internal.app.AlertActivity;
32 import com.android.internal.app.AlertController;
33 import com.android.systemui.R;
34 import com.android.systemui.statusbar.policy.DeviceProvisionedController;
35 
36 import javax.inject.Inject;
37 
38 /**
39  * If the attached USB accessory has a URL associated with it, and that URL is valid,
40  * show this dialog to the user to allow them to optionally visit that URL for more
41  * information or software downloads.
42  * Otherwise (no valid URL) this activity does nothing at all, finishing immediately.
43  */
44 public class UsbAccessoryUriActivity extends AlertActivity
45         implements DialogInterface.OnClickListener {
46 
47     private static final String TAG = "UsbAccessoryUriActivity";
48 
49     private UsbAccessory mAccessory;
50     private Uri mUri;
51 
52     private final DeviceProvisionedController mDeviceProvisionedController;
53 
54     @Inject
UsbAccessoryUriActivity(DeviceProvisionedController deviceProvisionedController)55     UsbAccessoryUriActivity(DeviceProvisionedController deviceProvisionedController) {
56         mDeviceProvisionedController = deviceProvisionedController;
57     }
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         getWindow().addSystemFlags(
62                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
63         super.onCreate(icicle);
64 
65         // Don't show this dialog during Setup Wizard
66         if (!mDeviceProvisionedController.isDeviceProvisioned()) {
67             Log.e(TAG, "device not provisioned");
68             finish();
69             return;
70         }
71 
72         Intent intent = getIntent();
73         mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
74         String uriString = intent.getStringExtra("uri");
75         mUri = (uriString == null ? null : Uri.parse(uriString));
76 
77         // Exception check before displaying dialog
78         if (mUri == null) {
79             Log.e(TAG, "could not parse Uri " + uriString);
80             finish();
81             return;
82         }
83         String scheme = mUri.getScheme();
84         if (!"http".equals(scheme) && !"https".equals(scheme)) {
85             Log.e(TAG, "Uri not http or https: " + mUri);
86             finish();
87             return;
88         }
89 
90         final AlertController.AlertParams ap = mAlertParams;
91         ap.mTitle = mAccessory.getDescription();
92         if (ap.mTitle == null || ap.mTitle.length() == 0) {
93             ap.mTitle = getString(R.string.title_usb_accessory);
94         }
95         ap.mMessage = getString(R.string.usb_accessory_uri_prompt, mUri);
96         ap.mPositiveButtonText = getString(R.string.label_view);
97         ap.mNegativeButtonText = getString(android.R.string.cancel);
98         ap.mPositiveButtonListener = this;
99         ap.mNegativeButtonListener = this;
100 
101         setupAlert();
102     }
103 
onClick(DialogInterface dialog, int which)104     public void onClick(DialogInterface dialog, int which) {
105         if (which == AlertDialog.BUTTON_POSITIVE) {
106             // launch the browser
107             Intent intent = new Intent(Intent.ACTION_VIEW, mUri);
108             intent.addCategory(Intent.CATEGORY_BROWSABLE);
109             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
110             try {
111                 startActivityAsUser(intent, UserHandle.CURRENT);
112             } catch (ActivityNotFoundException e) {
113                 Log.e(TAG, "startActivity failed for " + mUri);
114             }
115         }
116         finish();
117     }
118 }
119