Home | History | Annotate | Download | only in usb
      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 
     30 import com.android.internal.app.AlertActivity;
     31 import com.android.internal.app.AlertController;
     32 import 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  */
     40 public 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