1 /*
2  * Copyright 2016, 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.parser;
18 
19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE;
23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM;
24 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER;
25 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION;
26 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
27 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM;
28 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED;
29 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCALE;
30 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME;
31 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MAIN_COLOR;
32 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION;
33 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_USER_SETUP;
34 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE;
35 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_HIDDEN;
36 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PAC_URL;
37 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PASSWORD;
38 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_BYPASS;
39 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_HOST;
40 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_PROXY_PORT;
41 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SECURITY_TYPE;
42 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID;
43 import static android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED;
44 import static com.android.internal.util.Preconditions.checkNotNull;
45 import static com.android.managedprovisioning.common.Globals.ACTION_RESUME_PROVISIONING;
46 
47 import android.content.Context;
48 import android.content.Intent;
49 import android.os.Bundle;
50 import android.support.annotation.VisibleForTesting;
51 
52 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
53 import com.android.managedprovisioning.common.Utils;
54 import com.android.managedprovisioning.model.ProvisioningParams;
55 
56 import java.util.IllformedLocaleException;
57 import java.util.Locale;
58 import java.util.Properties;
59 
60 /**
61  * This class can initialize a {@link ProvisioningParams} object from an intent.
62  *
63  * <p>A {@link ProvisioningParams} object stores various parameters both for the device owner
64  * provisioning and profile owner provisioning.
65  */
66 public class MessageParser implements ProvisioningDataParser {
67     public static final String EXTRA_PROVISIONING_ACTION =
68             "com.android.managedprovisioning.extra.provisioning_action";
69     @VisibleForTesting
70     static final String EXTRA_PROVISIONING_DEVICE_ADMIN_SUPPORT_SHA1_PACKAGE_CHECKSUM =
71             "com.android.managedprovisioning.extra.device_admin_support_sha1_package_checksum";
72     @VisibleForTesting
73     static final String EXTRA_PROVISIONING_STARTED_BY_TRUSTED_SOURCE  =
74             "com.android.managedprovisioning.extra.started_by_trusted_source";
75 
76     private final Utils mUtils;
77 
78     /**
79      * Converts {@link ProvisioningParams} to {@link Intent}.
80      *
81      * <p/>One of the use cases is to store {@link ProvisioningParams} before device-encryption
82      * takes place. After device encryption is completed, the managed provisioning is resumed by
83      * sending this intent.
84      */
getIntentFromProvisioningParams(ProvisioningParams params)85     public Intent getIntentFromProvisioningParams(ProvisioningParams params) {
86         Intent intent = new Intent(ACTION_RESUME_PROVISIONING)
87                 .putExtra(EXTRA_PROVISIONING_ACTION, params.provisioningAction)
88                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
89                         params.deviceAdminPackageName)
90                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
91                         params.deviceAdminComponentName)
92                 .putExtra(EXTRA_PROVISIONING_TIME_ZONE, params.timeZone)
93                 .putExtra(EXTRA_PROVISIONING_LOCALE, localeToString(params.locale))
94                 .putExtra(EXTRA_PROVISIONING_LOCAL_TIME, params.localTime)
95                 .putExtra(EXTRA_PROVISIONING_STARTED_BY_TRUSTED_SOURCE,
96                         params.startedByTrustedSource)
97                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
98                         params.leaveAllSystemAppsEnabled)
99                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, params.adminExtrasBundle)
100                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, params.skipEncryption)
101                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, params.accountToMigrate)
102                 .putExtra(EXTRA_PROVISIONING_SKIP_USER_SETUP, params.skipUserSetup);
103         if (params.wifiInfo != null) {
104             intent.putExtra(EXTRA_PROVISIONING_WIFI_SSID, params.wifiInfo.ssid)
105                     .putExtra(EXTRA_PROVISIONING_WIFI_SECURITY_TYPE, params.wifiInfo.securityType)
106                     .putExtra(EXTRA_PROVISIONING_WIFI_PASSWORD, params.wifiInfo.password)
107                     .putExtra(EXTRA_PROVISIONING_WIFI_PROXY_HOST, params.wifiInfo.proxyHost)
108                     .putExtra(EXTRA_PROVISIONING_WIFI_PROXY_BYPASS, params.wifiInfo.proxyBypassHosts)
109                     .putExtra(EXTRA_PROVISIONING_WIFI_PAC_URL, params.wifiInfo.pacUrl)
110                     .putExtra(EXTRA_PROVISIONING_WIFI_PROXY_PORT, params.wifiInfo.proxyPort)
111                     .putExtra(EXTRA_PROVISIONING_WIFI_HIDDEN, params.wifiInfo.hidden);
112         }
113         if (params.deviceAdminDownloadInfo != null) {
114             intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_MINIMUM_VERSION_CODE,
115                     params.deviceAdminDownloadInfo.minVersion)
116                     .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION,
117                             params.deviceAdminDownloadInfo.location)
118                     .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_COOKIE_HEADER,
119                             params.deviceAdminDownloadInfo.cookieHeader)
120                     .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM,
121                             mUtils.byteArrayToString(
122                                     params.deviceAdminDownloadInfo.packageChecksum))
123                     .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_SUPPORT_SHA1_PACKAGE_CHECKSUM,
124                             params.deviceAdminDownloadInfo.packageChecksumSupportsSha1)
125                     .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM,
126                             mUtils.byteArrayToString(
127                                     params.deviceAdminDownloadInfo.signatureChecksum));
128         }
129         if (params.mainColor != null) {
130             intent.putExtra(EXTRA_PROVISIONING_MAIN_COLOR, params.mainColor);
131         }
132         return intent;
133     }
134 
MessageParser()135     public MessageParser() {
136         this(new Utils());
137     }
138 
139     @VisibleForTesting
MessageParser(Utils utils)140     MessageParser(Utils utils) {
141         mUtils = checkNotNull(utils);
142     }
143 
144     @Override
parse(Intent provisioningIntent, Context context)145     public ProvisioningParams parse(Intent provisioningIntent, Context context)
146             throws IllegalProvisioningArgumentException {
147         return getParser(provisioningIntent).parse(provisioningIntent, context);
148     }
149 
150     @VisibleForTesting
getParser(Intent provisioningIntent)151     ProvisioningDataParser getParser(Intent provisioningIntent) {
152         if (ACTION_NDEF_DISCOVERED.equals(provisioningIntent.getAction())) {
153             return new PropertiesProvisioningDataParser(mUtils);
154         } else {
155             return new ExtrasProvisioningDataParser(mUtils);
156         }
157     }
158 
stringToLocale(String string)159     static Locale stringToLocale(String string) throws IllformedLocaleException {
160         if (string != null) {
161             return new Locale.Builder().setLanguageTag(string.replace("_", "-")).build();
162         } else {
163             return null;
164         }
165     }
166 
localeToString(Locale locale)167     static String localeToString(Locale locale) {
168         if (locale != null) {
169             return locale.getLanguage() + "_" + locale.getCountry();
170         } else {
171             return null;
172         }
173     }
174 }
175