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
onCreate(Bundle savedStates)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
onResume()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      */
createMainDialog()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.removePasspointConfiguration(
97                                         mPasspointConfig.getHomeSp().getFqdn());
98                             } catch (IllegalArgumentException e) {
99                                 // Do nothing. This is expected if a profile with this FQDN does not
100                                 // exist.
101                             }
102                             try {
103                                 mWifiManager.addOrUpdatePasspointConfiguration(mPasspointConfig);
104                             } catch (RuntimeException rte) {
105                                 Log.w(TAG, "Caught exception while installing wifi config: " +
106                                            rte, rte);
107                                 success = false;
108                             }
109                             if (success) {
110                                 Intent intent = new Intent(getApplicationContext(),
111                                         CredentialsInstallDialog.class);
112                                 intent.putExtra(NETWORK_NAME,
113                                         mPasspointConfig.getHomeSp().getFriendlyName());
114                                 intent.putExtra(INSTALL_STATE, INSTALL_SUCCESS);
115                                 startActivity(intent);
116                             } else {
117                                 Intent intent = new Intent(getApplicationContext(),
118                                         CredentialsInstallDialog.class);
119                                 intent.putExtra(INSTALL_STATE, INSTALL_FAIL);
120                                 startActivity(intent);
121                             }
122                             finish();
123                         }
124                     });
125                     dialog.dismiss();
126                 }
127             });
128 
129             builder.setNegativeButton(R.string.wifi_cancel_label, new
130                     DialogInterface.OnClickListener() {
131                 @Override
132                 public void onClick(DialogInterface dialog, int which) {
133                     dialog.dismiss();
134                     finish();
135                 }
136             });
137         } else {
138             text.setText(getResources().getString(R.string.wifi_installer_download_error));
139             builder.setPositiveButton(R.string.done_label, new DialogInterface.OnClickListener() {
140                 @Override
141                 public void onClick(DialogInterface dialog, int which) {
142                     dialog.dismiss();
143                     finish();
144                 }
145             });
146         }
147         builder.create().show();
148     }
149 
150     /**
151      * Delete the file specified by the given URI.
152      *
153      * @param uri The URI of the file
154      * @param context The context of the current application
155      */
dropFile(Uri uri, Context context)156     private static void dropFile(Uri uri, Context context) {
157       try {
158         if (DocumentsContract.isDocumentUri(context, uri)) {
159           DocumentsContract.deleteDocument(context.getContentResolver(), uri);
160         } else {
161           context.getContentResolver().delete(uri, null, null);
162         }
163       } catch (Exception e) {
164         Log.e(TAG, "could not delete document " + uri);
165       }
166     }
167 }
168