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 17package com.android.systemui.usb; 18 19import android.app.AlertDialog; 20import android.content.ActivityNotFoundException; 21import android.content.DialogInterface; 22import android.content.Intent; 23import android.hardware.usb.UsbAccessory; 24import android.hardware.usb.UsbManager; 25import android.net.Uri; 26import android.os.Bundle; 27import android.os.UserHandle; 28import android.util.Log; 29 30import com.android.internal.app.AlertActivity; 31import com.android.internal.app.AlertController; 32import 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); 77 ap.mNegativeButtonText = getString(android.R.string.cancel); 78 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 { 91 startActivityAsUser(intent, UserHandle.CURRENT); 92 } catch (ActivityNotFoundException e) { 93 Log.e(TAG, "startActivity failed for " + mUri); 94 } 95 } 96 finish(); 97 } 98} 99