Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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.managedprovisioning; |
| 18 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 19 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE; |
| 20 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME; |
| 21 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCALE; |
| 22 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID; |
| 23 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_HIDDEN; |
| 24 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE; |
| 25 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD; |
| 26 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST; |
| 27 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT; |
| 28 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS; |
| 29 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL; |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 30 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME; |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 31 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION; |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 32 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER; |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 33 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM; |
Sander Alewijnse | ae9b821 | 2014-07-18 17:25:53 +0100 | [diff] [blame] | 34 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_EMAIL_ADDRESS; |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 35 | import static android.app.admin.DevicePolicyManager.PROVISIONING_NFC_MIME_TYPE; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 36 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 37 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 38 | import android.content.Intent; |
| 39 | import android.nfc.NdefMessage; |
| 40 | import android.nfc.NdefRecord; |
| 41 | import android.nfc.NfcAdapter; |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 42 | import android.os.Bundle; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 43 | import android.os.Parcelable; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 44 | import android.text.TextUtils; |
Sander Alewijnse | 48fdbe0 | 2014-07-03 14:25:45 +0100 | [diff] [blame] | 45 | import android.util.Base64; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 46 | |
| 47 | import java.io.IOException; |
| 48 | import java.io.StringReader; |
| 49 | import java.util.Enumeration; |
| 50 | import java.util.HashMap; |
Narayan Kamath | 53ff3c0 | 2014-07-17 14:02:58 +0100 | [diff] [blame] | 51 | import java.util.IllformedLocaleException; |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 52 | import java.util.Locale; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 53 | import java.util.Properties; |
| 54 | |
| 55 | /** |
| 56 | * This class can initialize a ProvisioningParams object from an intent. |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 57 | * There are two kinds of intents that can be parsed. |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 58 | * |
| 59 | * <p> |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 60 | * Intent was received via Nfc. |
| 61 | * The intent contains the extra {@link NfcAdapter.EXTRA_NDEF_MESSAGES}, which indicates that |
| 62 | * provisioning was started via Nfc bump. This extra contains an NDEF message, which contains an |
| 63 | * NfcRecord with mime type {@link PROVISIONING_NFC_MIME_TYPE}. This record stores a serialized |
| 64 | * properties object, which contains the serialized extra's described in the next option. |
| 65 | * A typical use case would be a programmer application that sends an Nfc bump to start Nfc |
| 66 | * provisioning from a programmer device. |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 67 | * |
| 68 | * <p> |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 69 | * Intent was received directly. |
| 70 | * The intent contains the extra {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME}, |
| 71 | * and may contain {@link #EXTRA_PROVISIONING_TIME_ZONE}, |
| 72 | * {@link #EXTRA_PROVISIONING_LOCAL_TIME}, and {@link #EXTRA_PROVISIONING_LOCALE}. A download |
| 73 | * location may be specified in {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION} |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 74 | * together with an optional http cookie header |
| 75 | * {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER} accompanied by the SHA-1 |
| 76 | * sum of the target file {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM}. |
| 77 | * Furthermore a wifi network may be specified in {@link #EXTRA_PROVISIONING_WIFI_SSID}, and if |
| 78 | * applicable {@link #EXTRA_PROVISIONING_WIFI_HIDDEN}, |
| 79 | * {@link #EXTRA_PROVISIONING_WIFI_SECURITY_TYPE}, {@link #EXTRA_PROVISIONING_WIFI_PASSWORD}, |
| 80 | * {@link #EXTRA_PROVISIONING_WIFI_PROXY_HOST}, {@link #EXTRA_PROVISIONING_WIFI_PROXY_PORT}, |
| 81 | * {@link #EXTRA_PROVISIONING_WIFI_PROXY_BYPASS}. |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 82 | * A typical use case would be the {@link BootReminder} sending the intent after device encryption |
| 83 | * and reboot. |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 84 | * |
| 85 | * <p> |
| 86 | * Furthermore this class can construct the bundle of extras for the second kind of intent, and it |
| 87 | * keeps track of the types of the extras in the DEVICE_OWNER_x_EXTRAS, with x the appropriate type. |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 88 | */ |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 89 | public class MessageParser { |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 90 | protected static final String[] DEVICE_OWNER_STRING_EXTRAS = { |
| 91 | EXTRA_PROVISIONING_TIME_ZONE, |
| 92 | EXTRA_PROVISIONING_LOCALE, |
| 93 | EXTRA_PROVISIONING_WIFI_SSID, |
| 94 | EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, |
| 95 | EXTRA_PROVISIONING_WIFI_PASSWORD, |
| 96 | EXTRA_PROVISIONING_WIFI_PROXY_HOST, |
| 97 | EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, |
| 98 | EXTRA_PROVISIONING_WIFI_PAC_URL, |
| 99 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, |
| 100 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION, |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 101 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER, |
Sander Alewijnse | ae9b821 | 2014-07-18 17:25:53 +0100 | [diff] [blame] | 102 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM, |
| 103 | EXTRA_PROVISIONING_EMAIL_ADDRESS |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | protected static final String[] DEVICE_OWNER_LONG_EXTRAS = { |
| 107 | EXTRA_PROVISIONING_LOCAL_TIME |
| 108 | }; |
| 109 | |
| 110 | protected static final String[] DEVICE_OWNER_INT_EXTRAS = { |
| 111 | EXTRA_PROVISIONING_WIFI_PROXY_PORT |
| 112 | }; |
| 113 | |
| 114 | protected static final String[] DEVICE_OWNER_BOOLEAN_EXTRAS = { |
| 115 | EXTRA_PROVISIONING_WIFI_HIDDEN |
| 116 | }; |
| 117 | |
| 118 | public void addProvisioningParamsToBundle(Bundle bundle, ProvisioningParams params) { |
| 119 | bundle.putString(EXTRA_PROVISIONING_TIME_ZONE, params.mTimeZone); |
| 120 | bundle.putString(EXTRA_PROVISIONING_LOCALE, params.getLocaleAsString()); |
| 121 | bundle.putString(EXTRA_PROVISIONING_WIFI_SSID, params.mWifiSsid); |
| 122 | bundle.putString(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, params.mWifiSecurityType); |
| 123 | bundle.putString(EXTRA_PROVISIONING_WIFI_PASSWORD, params.mWifiPassword); |
| 124 | bundle.putString(EXTRA_PROVISIONING_WIFI_PROXY_HOST, params.mWifiProxyHost); |
| 125 | bundle.putString(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, params.mWifiProxyBypassHosts); |
| 126 | bundle.putString(EXTRA_PROVISIONING_WIFI_PAC_URL, params.mWifiPacUrl); |
| 127 | bundle.putString(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, |
| 128 | params.mDeviceAdminPackageName); |
| 129 | bundle.putString(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION, |
| 130 | params.mDeviceAdminPackageDownloadLocation); |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 131 | bundle.putString(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER, |
| 132 | params.mDeviceAdminPackageDownloadCookieHeader); |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 133 | bundle.putString(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM, |
| 134 | params.getDeviceAdminPackageChecksumAsString()); |
Sander Alewijnse | ae9b821 | 2014-07-18 17:25:53 +0100 | [diff] [blame] | 135 | bundle.putString(EXTRA_PROVISIONING_EMAIL_ADDRESS, params.mManagedDeviceEmailAddress); |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 136 | |
| 137 | bundle.putLong(EXTRA_PROVISIONING_LOCAL_TIME, params.mLocalTime); |
| 138 | |
| 139 | bundle.putInt(EXTRA_PROVISIONING_WIFI_PROXY_PORT, params.mWifiProxyPort); |
| 140 | |
| 141 | bundle.putBoolean(EXTRA_PROVISIONING_WIFI_HIDDEN, params.mWifiHidden); |
| 142 | } |
| 143 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 144 | public ProvisioningParams parseIntent(Intent intent) throws ParseException { |
| 145 | ProvisionLogger.logi("Processing intent."); |
| 146 | if (intent.hasExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) { |
| 147 | return parseNfcIntent(intent); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 148 | } else { |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 149 | return parseNonNfcIntent(intent); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 150 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 153 | public ProvisioningParams parseNfcIntent(Intent nfcIntent) |
| 154 | throws ParseException { |
| 155 | ProvisionLogger.logi("Processing Nfc Payload."); |
| 156 | // Only one first message with NFC_MIME_TYPE is used. |
| 157 | for (Parcelable rawMsg : nfcIntent |
| 158 | .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) { |
| 159 | NdefMessage msg = (NdefMessage) rawMsg; |
| 160 | |
| 161 | // Assume only first record of message is used. |
| 162 | NdefRecord firstRecord = msg.getRecords()[0]; |
| 163 | String mimeType = new String(firstRecord.getType(), UTF_8); |
| 164 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 165 | if (PROVISIONING_NFC_MIME_TYPE.equals(mimeType)) { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 166 | return parseProperties(new String(firstRecord.getPayload(), UTF_8)); |
| 167 | } |
| 168 | } |
| 169 | throw new ParseException( |
| 170 | "Intent does not contain NfcRecord with the correct MIME type.", |
Bill Yi | 125bf21 | 2014-07-28 10:54:17 -0700 | [diff] [blame] | 171 | R.string.device_owner_error_general); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private ProvisioningParams parseProperties(String data) |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 175 | throws ParseException { |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 176 | ProvisioningParams params = new ProvisioningParams(); |
| 177 | try { |
| 178 | Properties props = new Properties(); |
| 179 | props.load(new StringReader(data)); |
| 180 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 181 | String s; // Used for parsing non-Strings. |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 182 | params.mTimeZone |
| 183 | = props.getProperty(EXTRA_PROVISIONING_TIME_ZONE); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 184 | if ((s = props.getProperty(EXTRA_PROVISIONING_LOCALE)) != null) { |
| 185 | params.mLocale = stringToLocale(s); |
| 186 | } |
| 187 | params.mWifiSsid = props.getProperty(EXTRA_PROVISIONING_WIFI_SSID); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 188 | params.mWifiSecurityType = props.getProperty(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE); |
| 189 | params.mWifiPassword = props.getProperty(EXTRA_PROVISIONING_WIFI_PASSWORD); |
| 190 | params.mWifiProxyHost = props.getProperty(EXTRA_PROVISIONING_WIFI_PROXY_HOST); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 191 | params.mWifiProxyBypassHosts = props.getProperty(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS); |
| 192 | params.mWifiPacUrl = props.getProperty(EXTRA_PROVISIONING_WIFI_PAC_URL); |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 193 | params.mDeviceAdminPackageName |
| 194 | = props.getProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 195 | params.mDeviceAdminPackageDownloadLocation |
| 196 | = props.getProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION); |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 197 | params.mDeviceAdminPackageDownloadCookieHeader = props.getProperty( |
| 198 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 199 | if ((s = props.getProperty(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM)) != null) { |
| 200 | params.mDeviceAdminPackageChecksum = stringToByteArray(s); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 201 | } |
Sander Alewijnse | ae9b821 | 2014-07-18 17:25:53 +0100 | [diff] [blame] | 202 | params.mManagedDeviceEmailAddress = props.getProperty(EXTRA_PROVISIONING_EMAIL_ADDRESS); |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 203 | |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 204 | if ((s = props.getProperty(EXTRA_PROVISIONING_LOCAL_TIME)) != null) { |
| 205 | params.mLocalTime = Long.parseLong(s); |
| 206 | } |
| 207 | |
| 208 | if ((s = props.getProperty(EXTRA_PROVISIONING_WIFI_PROXY_PORT)) != null) { |
| 209 | params.mWifiProxyPort = Integer.parseInt(s); |
| 210 | } |
| 211 | |
| 212 | if ((s = props.getProperty(EXTRA_PROVISIONING_WIFI_HIDDEN)) != null) { |
| 213 | params.mWifiHidden = Boolean.parseBoolean(s); |
| 214 | } |
| 215 | |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 216 | checkValidityOfProvisioningParams(params); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 217 | return params; |
| 218 | } catch (IOException e) { |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 219 | throw new ParseException("Couldn't load payload", |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 220 | R.string.device_owner_error_general, e); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 221 | } catch (NumberFormatException e) { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 222 | throw new ParseException("Incorrect numberformat.", |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 223 | R.string.device_owner_error_general, e); |
Narayan Kamath | 53ff3c0 | 2014-07-17 14:02:58 +0100 | [diff] [blame] | 224 | } catch (IllformedLocaleException e) { |
| 225 | throw new ParseException("Invalid locale.", |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 226 | R.string.device_owner_error_general, e); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 230 | public ProvisioningParams parseNonNfcIntent(Intent intent) |
| 231 | throws ParseException { |
| 232 | ProvisionLogger.logi("Processing intent."); |
| 233 | ProvisioningParams params = new ProvisioningParams(); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 234 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 235 | params.mTimeZone = intent.getStringExtra(EXTRA_PROVISIONING_TIME_ZONE); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 236 | String localeString = intent.getStringExtra(EXTRA_PROVISIONING_LOCALE); |
| 237 | if (localeString != null) { |
| 238 | params.mLocale = stringToLocale(localeString); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 239 | } |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 240 | params.mWifiSsid = intent.getStringExtra(EXTRA_PROVISIONING_WIFI_SSID); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 241 | params.mWifiSecurityType = intent.getStringExtra(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE); |
| 242 | params.mWifiPassword = intent.getStringExtra(EXTRA_PROVISIONING_WIFI_PASSWORD); |
| 243 | params.mWifiProxyHost = intent.getStringExtra(EXTRA_PROVISIONING_WIFI_PROXY_HOST); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 244 | params.mWifiProxyBypassHosts = intent.getStringExtra(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS); |
| 245 | params.mWifiPacUrl = intent.getStringExtra(EXTRA_PROVISIONING_WIFI_PAC_URL); |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 246 | params.mDeviceAdminPackageName |
| 247 | = intent.getStringExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 248 | params.mDeviceAdminPackageDownloadLocation |
| 249 | = intent.getStringExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION); |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 250 | params.mDeviceAdminPackageDownloadCookieHeader = intent.getStringExtra( |
| 251 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 252 | String hashString = intent.getStringExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM); |
| 253 | if (hashString != null) { |
| 254 | params.mDeviceAdminPackageChecksum = stringToByteArray(hashString); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 255 | } |
Sander Alewijnse | ae9b821 | 2014-07-18 17:25:53 +0100 | [diff] [blame] | 256 | params.mManagedDeviceEmailAddress |
| 257 | = intent.getStringExtra(EXTRA_PROVISIONING_EMAIL_ADDRESS); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 258 | |
Sander Alewijnse | 9aff140 | 2014-07-18 17:15:32 +0100 | [diff] [blame] | 259 | params.mLocalTime = intent.getLongExtra(EXTRA_PROVISIONING_LOCAL_TIME, |
| 260 | ProvisioningParams.DEFAULT_LOCAL_TIME); |
| 261 | |
| 262 | params.mWifiProxyPort = intent.getIntExtra(EXTRA_PROVISIONING_WIFI_PROXY_PORT, |
| 263 | ProvisioningParams.DEFAULT_WIFI_PROXY_PORT); |
| 264 | |
| 265 | params.mWifiHidden = intent.getBooleanExtra(EXTRA_PROVISIONING_WIFI_HIDDEN, |
| 266 | ProvisioningParams.DEFAULT_WIFI_HIDDEN); |
| 267 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 268 | checkValidityOfProvisioningParams(params); |
| 269 | return params; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 273 | * Check whether necessary fields are set. |
| 274 | */ |
| 275 | private void checkValidityOfProvisioningParams(ProvisioningParams params) |
| 276 | throws ParseException { |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 277 | if (TextUtils.isEmpty(params.mDeviceAdminPackageName)) { |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 278 | throw new ParseException("Must provide the name of the device admin package.", |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 279 | R.string.device_owner_error_general); |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 280 | } |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 281 | if (!TextUtils.isEmpty(params.mDeviceAdminPackageDownloadLocation)) { |
| 282 | if (params.mDeviceAdminPackageChecksum == null || |
| 283 | params.mDeviceAdminPackageChecksum.length == 0) { |
| 284 | throw new ParseException("Checksum of installer file is required for downloading " + |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 285 | "device admin file, but not provided.", |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 286 | R.string.device_owner_error_general); |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 287 | } |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 292 | * Exception thrown when the ProvisioningParams initialization failed completely. |
| 293 | * |
| 294 | * Note: We're using a custom exception to avoid catching subsequent exceptions that might be |
| 295 | * significant. |
| 296 | */ |
Sander Alewijnse | 885c349 | 2014-04-24 17:30:31 +0100 | [diff] [blame] | 297 | public static class ParseException extends Exception { |
| 298 | private int mErrorMessageId; |
| 299 | |
| 300 | public ParseException(String message, int errorMessageId) { |
| 301 | super(message); |
| 302 | mErrorMessageId = errorMessageId; |
| 303 | } |
| 304 | |
| 305 | public ParseException(String message, int errorMessageId, Throwable t) { |
| 306 | super(message, t); |
| 307 | mErrorMessageId = errorMessageId; |
| 308 | } |
| 309 | |
| 310 | public int getErrorMessageId() { |
| 311 | return mErrorMessageId; |
| 312 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 313 | } |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 314 | |
| 315 | public static byte[] stringToByteArray(String s) |
| 316 | throws NumberFormatException { |
Sander Alewijnse | 48fdbe0 | 2014-07-03 14:25:45 +0100 | [diff] [blame] | 317 | try { |
| 318 | return Base64.decode(s, Base64.URL_SAFE); |
| 319 | } catch (IllegalArgumentException e) { |
| 320 | throw new NumberFormatException("Incorrect checksum format."); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 321 | } |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | public static Locale stringToLocale(String s) |
Narayan Kamath | 53ff3c0 | 2014-07-17 14:02:58 +0100 | [diff] [blame] | 325 | throws IllformedLocaleException { |
| 326 | return new Locale.Builder().setLanguageTag(s.replace("_", "-")).build(); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 327 | } |
Sander Alewijnse | 226d36c | 2014-06-26 16:33:05 +0000 | [diff] [blame] | 328 | } |