Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 1 | package com.android.certinstaller; |
| 2 | |
| 3 | import android.app.Activity; |
| 4 | import android.app.AlertDialog; |
Jan Nordqvist | 8eca13b | 2015-04-15 15:22:50 -0700 | [diff] [blame] | 5 | import android.content.ActivityNotFoundException; |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 6 | import android.content.Context; |
| 7 | import android.content.DialogInterface; |
| 8 | import android.content.Intent; |
| 9 | import android.content.res.Resources; |
Peter Qiu | ffac655 | 2016-12-12 13:50:01 -0800 | [diff] [blame] | 10 | import android.net.Uri; |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 11 | import android.net.wifi.WifiConfiguration; |
| 12 | import android.net.wifi.WifiEnterpriseConfig; |
| 13 | import android.net.wifi.WifiManager; |
| 14 | import android.os.Bundle; |
Jan Nordqvist | 8eca13b | 2015-04-15 15:22:50 -0700 | [diff] [blame] | 15 | import android.security.Credentials; |
| 16 | import android.security.KeyStore; |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 17 | import android.util.Log; |
| 18 | import android.view.View; |
| 19 | import android.widget.Button; |
| 20 | import android.widget.TextView; |
Sanket Padawe | 909641b | 2015-08-05 14:06:23 -0700 | [diff] [blame] | 21 | import android.widget.Toast; |
| 22 | import android.os.AsyncTask; |
Peter Qiu | ffac655 | 2016-12-12 13:50:01 -0800 | [diff] [blame] | 23 | import android.provider.DocumentsContract; |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 24 | |
| 25 | import java.security.PrivateKey; |
| 26 | import java.security.interfaces.RSAPrivateKey; |
| 27 | import java.util.Collection; |
Jan Nordqvist | 580c5f5 | 2015-07-09 12:10:52 -0700 | [diff] [blame] | 28 | import java.util.Iterator; |
| 29 | import java.util.LinkedList; |
| 30 | import java.util.List; |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 31 | |
| 32 | public class WiFiInstaller extends Activity { |
| 33 | |
Jan Nordqvist | 7c543b7 | 2015-04-17 12:12:31 -0700 | [diff] [blame] | 34 | private static final String TAG = "WifiInstaller"; |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 35 | private static final String NETWORK_NAME = "network_name"; |
| 36 | private static final String INSTALL_STATE = "install_state"; |
Sanket Padawe | 66177c3 | 2015-06-22 19:30:58 -0700 | [diff] [blame] | 37 | public static final int INSTALL_SUCCESS = 2; |
| 38 | public static final int INSTALL_FAIL = 1; |
| 39 | public static final int INSTALL_FAIL_NO_WIFI = 0; |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 40 | WifiConfiguration mWifiConfiguration; |
| 41 | WifiManager mWifiManager; |
| 42 | boolean doNotInstall; |
Jan Nordqvist | 8eca13b | 2015-04-15 15:22:50 -0700 | [diff] [blame] | 43 | |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 44 | @Override |
| 45 | protected void onCreate(Bundle savedStates) { |
| 46 | super.onCreate(savedStates); |
| 47 | |
| 48 | Bundle bundle = getIntent().getExtras(); |
| 49 | String uriString = bundle.getString(CertInstallerMain.WIFI_CONFIG_FILE); |
| 50 | String mimeType = bundle.getString(CertInstallerMain.WIFI_CONFIG); |
| 51 | byte[] data = bundle.getByteArray(CertInstallerMain.WIFI_CONFIG_DATA); |
| 52 | |
Jan Nordqvist | 8eca13b | 2015-04-15 15:22:50 -0700 | [diff] [blame] | 53 | Log.d(TAG, "WiFi data for " + CertInstallerMain.WIFI_CONFIG + ": " + |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 54 | mimeType + " is " + (data != null ? data.length : "-")); |
| 55 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 56 | mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 57 | mWifiConfiguration = mWifiManager.buildWifiConfig(uriString, mimeType, data); |
Peter Qiu | ffac655 | 2016-12-12 13:50:01 -0800 | [diff] [blame] | 58 | dropFile(Uri.parse(uriString), getApplicationContext()); |
Jan Nordqvist | 8eca13b | 2015-04-15 15:22:50 -0700 | [diff] [blame] | 59 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 60 | if (mWifiConfiguration != null) { |
| 61 | WifiEnterpriseConfig enterpriseConfig = mWifiConfiguration.enterpriseConfig; |
| 62 | doNotInstall = (enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS |
| 63 | || enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS) |
| 64 | && enterpriseConfig.getCaCertificate() == null; |
| 65 | if (!doNotInstall && (enterpriseConfig.getClientCertificate() != null |
| 66 | || enterpriseConfig.getCaCertificate() != null)) { |
| 67 | if (!KeyStore.getInstance().isUnlocked()) { |
| 68 | try { |
| 69 | startActivity(new Intent(Credentials.UNLOCK_ACTION)); |
| 70 | } catch (ActivityNotFoundException e) { |
| 71 | Log.w(TAG, e); |
| 72 | } |
Jan Nordqvist | 8eca13b | 2015-04-15 15:22:50 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 75 | } else { |
Jan Nordqvist | fb90c6f | 2015-07-31 15:33:29 -0700 | [diff] [blame] | 76 | Log.w(TAG, "failed to build wifi configuration"); |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 77 | doNotInstall = true; |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 81 | @Override |
| 82 | protected void onResume() { |
| 83 | super.onResume(); |
| 84 | createMainDialog(); |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jan Nordqvist | 580c5f5 | 2015-07-09 12:10:52 -0700 | [diff] [blame] | 87 | public static List<String> splitDomain(String domain) { |
| 88 | if (domain.endsWith(".")) { |
| 89 | domain = domain.substring(0, domain.length() - 1); |
| 90 | } |
| 91 | |
| 92 | String[] labels = domain.toLowerCase().split("\\."); |
| 93 | LinkedList<String> labelList = new LinkedList<>(); |
| 94 | for (String label : labels) { |
| 95 | labelList.addFirst(label); |
| 96 | } |
| 97 | |
| 98 | return labelList; |
| 99 | } |
| 100 | |
| 101 | public static boolean sameBaseDomain(List<String> arg1, String domain) { |
| 102 | if (domain == null) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | List<String> arg2 = splitDomain(domain); |
| 107 | if (arg2.isEmpty()) { |
| 108 | return false; |
| 109 | } |
| 110 | Iterator<String> l1 = arg1.iterator(); |
| 111 | Iterator<String> l2 = arg2.iterator(); |
| 112 | |
| 113 | while(l1.hasNext() && l2.hasNext()) { |
| 114 | if (!l1.next().equals(l2.next())) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 121 | private void createMainDialog() { |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 122 | Resources res = getResources(); |
| 123 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 124 | View layout = getLayoutInflater().inflate(R.layout.wifi_main_dialog, null); |
| 125 | builder.setView(layout); |
| 126 | |
| 127 | TextView text = (TextView) layout.findViewById(R.id.wifi_info); |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 128 | if (!doNotInstall) { |
| 129 | text.setText(String.format(getResources().getString(R.string.wifi_installer_detail), |
| 130 | mWifiConfiguration.providerFriendlyName)); |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 131 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 132 | builder.setTitle(mWifiConfiguration.providerFriendlyName); |
| 133 | builder.setIcon(res.getDrawable(R.drawable.signal_wifi_4_bar_lock_black_24dp)); |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 134 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 135 | builder.setPositiveButton(R.string.wifi_install_label, |
| 136 | new DialogInterface.OnClickListener() { |
| 137 | @Override |
| 138 | public void onClick(DialogInterface dialog, int which) { |
Sanket Padawe | 909641b | 2015-08-05 14:06:23 -0700 | [diff] [blame] | 139 | final boolean wifiEnabled = mWifiManager.isWifiEnabled(); |
| 140 | if (wifiEnabled) { |
| 141 | Toast.makeText(WiFiInstaller.this, getString(R.string.wifi_installing_label), |
| 142 | Toast.LENGTH_LONG).show(); |
| 143 | } |
| 144 | AsyncTask.execute(new Runnable() { |
| 145 | @Override |
| 146 | public void run() { |
| 147 | boolean success = false; |
| 148 | if (wifiEnabled) { |
| 149 | List<String> newDomain = splitDomain(mWifiConfiguration.FQDN); |
| 150 | for (WifiConfiguration config : |
| 151 | mWifiManager.getConfiguredNetworks()) { |
| 152 | if (sameBaseDomain(newDomain, config.FQDN)) { |
| 153 | mWifiManager.removeNetwork(config.networkId); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | try { |
| 158 | success = mWifiManager.addNetwork(mWifiConfiguration) != -1 |
| 159 | && mWifiManager.saveConfiguration(); |
| 160 | } |
| 161 | catch (RuntimeException rte) { |
| 162 | Log.w(TAG, "Caught exception while installing wifi config: " + |
| 163 | rte, rte); |
| 164 | success = false; |
| 165 | } |
| 166 | } |
| 167 | if (success) { |
| 168 | Intent intent = new Intent(getApplicationContext(), |
| 169 | CredentialsInstallDialog.class); |
| 170 | intent.putExtra(NETWORK_NAME, |
| 171 | mWifiConfiguration.providerFriendlyName); |
| 172 | intent.putExtra(INSTALL_STATE, INSTALL_SUCCESS); |
| 173 | startActivity(intent); |
| 174 | } else { |
| 175 | Intent intent = new Intent(getApplicationContext(), |
| 176 | CredentialsInstallDialog.class); |
| 177 | if (!wifiEnabled) { |
| 178 | intent.putExtra(INSTALL_STATE, INSTALL_FAIL_NO_WIFI); |
| 179 | } else { |
| 180 | intent.putExtra(INSTALL_STATE, INSTALL_FAIL); |
| 181 | } |
| 182 | startActivity(intent); |
| 183 | } |
| 184 | finish(); |
Jan Nordqvist | 580c5f5 | 2015-07-09 12:10:52 -0700 | [diff] [blame] | 185 | } |
Sanket Padawe | 909641b | 2015-08-05 14:06:23 -0700 | [diff] [blame] | 186 | }); |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 187 | dialog.dismiss(); |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 188 | } |
| 189 | }); |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 190 | |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 191 | builder.setNegativeButton(R.string.wifi_cancel_label, new |
| 192 | DialogInterface.OnClickListener() { |
| 193 | @Override |
| 194 | public void onClick(DialogInterface dialog, int which) { |
| 195 | dialog.dismiss(); |
| 196 | finish(); |
| 197 | } |
Sanket Padawe | 909641b | 2015-08-05 14:06:23 -0700 | [diff] [blame] | 198 | }); |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 199 | } else { |
| 200 | text.setText(getResources().getString(R.string.wifi_installer_download_error)); |
| 201 | builder.setPositiveButton(R.string.done_label, new DialogInterface.OnClickListener() { |
| 202 | @Override |
| 203 | public void onClick(DialogInterface dialog, int which) { |
| 204 | dialog.dismiss(); |
| 205 | finish(); |
| 206 | } |
| 207 | }); |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 208 | } |
Sanket Padawe | 122def3 | 2015-05-13 16:12:26 -0700 | [diff] [blame] | 209 | builder.create().show(); |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 210 | } |
Peter Qiu | ffac655 | 2016-12-12 13:50:01 -0800 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * Delete the file specified by the given URI. |
| 214 | * |
| 215 | * @param uri The URI of the file |
| 216 | * @param context The context of the current application |
| 217 | */ |
| 218 | private static void dropFile(Uri uri, Context context) { |
| 219 | if (DocumentsContract.isDocumentUri(context, uri)) { |
| 220 | DocumentsContract.deleteDocument(context.getContentResolver(), uri); |
| 221 | } else { |
| 222 | context.getContentResolver().delete(uri, null, null); |
| 223 | } |
| 224 | } |
Jan Nordqvist | 994932f | 2015-03-25 18:56:30 -0700 | [diff] [blame] | 225 | } |