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