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.Activity;
     20 import android.app.AlertDialog;
     21 import android.content.ActivityNotFoundException;
     22 import android.content.Context;
     23 import android.content.DialogInterface;
     24 import android.content.Intent;
     25 import android.net.Uri;
     26 import android.hardware.usb.UsbAccessory;
     27 import android.hardware.usb.UsbManager;
     28 import android.os.Bundle;
     29 import android.os.UserHandle;
     30 import android.util.Log;
     31 
     32 import com.android.internal.app.AlertActivity;
     33 import com.android.internal.app.AlertController;
     34 
     35 import 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  */
     43 public 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);
     80         ap.mNegativeButtonText = getString(android.R.string.cancel);
     81         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 {
     94                 startActivityAsUser(intent, UserHandle.CURRENT);
     95             } catch (ActivityNotFoundException e) {
     96                 Log.e(TAG, "startActivity failed for " + mUri);
     97             }
     98         }
     99         finish();
    100     }
    101 }
    102