Home | History | Annotate | Download | only in certinstaller
      1 package com.android.certinstaller;
      2 
      3 import android.app.Activity;
      4 import android.app.AlertDialog;
      5 import android.content.Context;
      6 import android.content.DialogInterface;
      7 import android.content.Intent;
      8 import android.content.res.Resources;
      9 import android.net.Uri;
     10 import android.net.wifi.WifiManager;
     11 import android.net.wifi.hotspot2.ConfigParser;
     12 import android.net.wifi.hotspot2.PasspointConfiguration;
     13 import android.os.AsyncTask;
     14 import android.os.Bundle;
     15 import android.provider.DocumentsContract;
     16 import android.util.Log;
     17 import android.view.View;
     18 import android.widget.TextView;
     19 import android.widget.Toast;
     20 
     21 /**
     22  * An Activity for provisioning a Hotspot 2.0 Release 1 configuration.
     23  */
     24 public class WiFiInstaller extends Activity {
     25 
     26     private static final String TAG = "WifiInstaller";
     27     private static final String NETWORK_NAME = "network_name";
     28     private static final String INSTALL_STATE = "install_state";
     29     public static final int INSTALL_SUCCESS = 2;
     30     public static final int INSTALL_FAIL = 1;
     31     public static final int INSTALL_FAIL_NO_WIFI = 0;
     32     PasspointConfiguration mPasspointConfig;
     33     WifiManager mWifiManager;
     34     boolean doNotInstall;
     35 
     36     @Override
     37     protected void onCreate(Bundle savedStates) {
     38         super.onCreate(savedStates);
     39 
     40         Bundle bundle = getIntent().getExtras();
     41         String uriString = bundle.getString(CertInstallerMain.WIFI_CONFIG_FILE);
     42         String mimeType = bundle.getString(CertInstallerMain.WIFI_CONFIG);
     43         byte[] data = bundle.getByteArray(CertInstallerMain.WIFI_CONFIG_DATA);
     44 
     45         Log.d(TAG, "WiFi data for " + CertInstallerMain.WIFI_CONFIG + ": " +
     46                 mimeType + " is " + (data != null ? data.length : "-"));
     47 
     48         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
     49         mPasspointConfig = ConfigParser.parsePasspointConfig(mimeType, data);
     50         dropFile(Uri.parse(uriString), getApplicationContext());
     51 
     52         if (mPasspointConfig == null) {
     53             Log.w(TAG, "failed to build passpoint configuration");
     54             doNotInstall = true;
     55         } else if (mPasspointConfig.getHomeSp() == null) {
     56             Log.w(TAG, "Passpoint profile missing HomeSP information");
     57             doNotInstall = true;
     58         }
     59     }
     60 
     61     @Override
     62     protected void onResume() {
     63         super.onResume();
     64         createMainDialog();
     65     }
     66 
     67     /**
     68      * Create a dialog that guides the user through Hotspot 2.0 Release 1 configuration file
     69      * installation.
     70      */
     71     private void createMainDialog() {
     72         Resources res = getResources();
     73         AlertDialog.Builder builder = new AlertDialog.Builder(this);
     74         View layout = getLayoutInflater().inflate(R.layout.wifi_main_dialog, null);
     75         builder.setView(layout);
     76 
     77         TextView text = (TextView) layout.findViewById(R.id.wifi_info);
     78         if (!doNotInstall) {
     79             text.setText(String.format(getResources().getString(R.string.wifi_installer_detail),
     80                     mPasspointConfig.getHomeSp().getFriendlyName()));
     81 
     82             builder.setTitle(mPasspointConfig.getHomeSp().getFriendlyName());
     83             builder.setIcon(res.getDrawable(R.drawable.signal_wifi_4_bar_lock_black_24dp));
     84 
     85             builder.setPositiveButton(R.string.wifi_install_label,
     86                     new DialogInterface.OnClickListener() {
     87                 @Override
     88                 public void onClick(DialogInterface dialog, int which) {
     89                     Toast.makeText(WiFiInstaller.this, getString(R.string.wifi_installing_label),
     90                             Toast.LENGTH_LONG).show();
     91                     AsyncTask.execute(new Runnable() {
     92                         @Override
     93                         public void run() {
     94                             boolean success = true;
     95                             try {
     96                                 mWifiManager.addOrUpdatePasspointConfiguration(mPasspointConfig);
     97                             } catch (RuntimeException rte) {
     98                                 Log.w(TAG, "Caught exception while installing wifi config: " +
     99                                            rte, rte);
    100                                 success = false;
    101                             }
    102                             if (success) {
    103                                 Intent intent = new Intent(getApplicationContext(),
    104                                         CredentialsInstallDialog.class);
    105                                 intent.putExtra(NETWORK_NAME,
    106                                         mPasspointConfig.getHomeSp().getFriendlyName());
    107                                 intent.putExtra(INSTALL_STATE, INSTALL_SUCCESS);
    108                                 startActivity(intent);
    109                             } else {
    110                                 Intent intent = new Intent(getApplicationContext(),
    111                                         CredentialsInstallDialog.class);
    112                                 intent.putExtra(INSTALL_STATE, INSTALL_FAIL);
    113                                 startActivity(intent);
    114                             }
    115                             finish();
    116                         }
    117                     });
    118                     dialog.dismiss();
    119                 }
    120             });
    121 
    122             builder.setNegativeButton(R.string.wifi_cancel_label, new
    123                     DialogInterface.OnClickListener() {
    124                 @Override
    125                 public void onClick(DialogInterface dialog, int which) {
    126                     dialog.dismiss();
    127                     finish();
    128                 }
    129             });
    130         } else {
    131             text.setText(getResources().getString(R.string.wifi_installer_download_error));
    132             builder.setPositiveButton(R.string.done_label, new DialogInterface.OnClickListener() {
    133                 @Override
    134                 public void onClick(DialogInterface dialog, int which) {
    135                     dialog.dismiss();
    136                     finish();
    137                 }
    138             });
    139         }
    140         builder.create().show();
    141     }
    142 
    143     /**
    144      * Delete the file specified by the given URI.
    145      *
    146      * @param uri The URI of the file
    147      * @param context The context of the current application
    148      */
    149     private static void dropFile(Uri uri, Context context) {
    150         if (DocumentsContract.isDocumentUri(context, uri)) {
    151             try {
    152                 DocumentsContract.deleteDocument(context.getContentResolver(), uri);
    153             } catch (Exception e) {
    154                 Log.e(TAG, "could not delete document " + uri);
    155             }
    156         } else {
    157             context.getContentResolver().delete(uri, null, null);
    158         }
    159     }
    160 }
    161