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