blob: ff0663074ee32e34ecd13cc79e2183699bc470dc [file] [log] [blame]
Mike Lockwood024b4f12011-03-10 12:12:31 -05001/*
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
17package com.android.systemui.usb;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.ActivityNotFoundException;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.net.Uri;
26import android.hardware.usb.UsbAccessory;
27import android.hardware.usb.UsbManager;
28import android.os.Bundle;
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070029import android.os.UserHandle;
Mike Lockwood024b4f12011-03-10 12:12:31 -050030import android.util.Log;
31
32import com.android.internal.app.AlertActivity;
33import com.android.internal.app.AlertController;
34
35import com.android.systemui.R;
36
37/**
38 * If the attached USB accessory has a URL associated with it, and that URL is valid,
39 * show this dialog to the user to allow them to optionally visit that URL for more
40 * information or software downloads.
41 * Otherwise (no valid URL) this activity does nothing at all, finishing immediately.
42 */
43public class UsbAccessoryUriActivity extends AlertActivity
44 implements DialogInterface.OnClickListener {
45
46 private static final String TAG = "UsbAccessoryUriActivity";
47
48 private UsbAccessory mAccessory;
49 private Uri mUri;
50
51 @Override
52 public void onCreate(Bundle icicle) {
53 super.onCreate(icicle);
54
55 Intent intent = getIntent();
56 mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
57 String uriString = intent.getStringExtra("uri");
58 mUri = (uriString == null ? null : Uri.parse(uriString));
59
60 // sanity check before displaying dialog
61 if (mUri == null) {
62 Log.e(TAG, "could not parse Uri " + uriString);
63 finish();
64 return;
65 }
66 String scheme = mUri.getScheme();
67 if (!"http".equals(scheme) && !"https".equals(scheme)) {
68 Log.e(TAG, "Uri not http or https: " + mUri);
69 finish();
70 return;
71 }
72
73 final AlertController.AlertParams ap = mAlertParams;
74 ap.mTitle = mAccessory.getDescription();
75 if (ap.mTitle == null || ap.mTitle.length() == 0) {
76 ap.mTitle = getString(R.string.title_usb_accessory);
77 }
78 ap.mMessage = getString(R.string.usb_accessory_uri_prompt, mUri);
79 ap.mPositiveButtonText = getString(R.string.label_view);
Mike Lockwoodad5f83e2011-03-15 16:04:12 -040080 ap.mNegativeButtonText = getString(android.R.string.cancel);
Mike Lockwood024b4f12011-03-10 12:12:31 -050081 ap.mPositiveButtonListener = this;
82 ap.mNegativeButtonListener = this;
83
84 setupAlert();
85 }
86
87 public void onClick(DialogInterface dialog, int which) {
88 if (which == AlertDialog.BUTTON_POSITIVE) {
89 // launch the browser
90 Intent intent = new Intent(Intent.ACTION_VIEW, mUri);
91 intent.addCategory(Intent.CATEGORY_BROWSABLE);
92 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
93 try {
Jeff Sharkeyfc3f24b2012-10-01 21:45:52 -070094 startActivityAsUser(intent, UserHandle.CURRENT);
Mike Lockwood024b4f12011-03-10 12:12:31 -050095 } catch (ActivityNotFoundException e) {
96 Log.e(TAG, "startActivity failed for " + mUri);
97 }
98 }
99 finish();
100 }
101}