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.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED;
20 
21 import static com.android.internal.util.Preconditions.checkNotNull;
22 
23 import android.content.Context;
24 import android.content.Intent;
25 
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
29 import com.android.managedprovisioning.common.SettingsFacade;
30 import com.android.managedprovisioning.common.Utils;
31 import com.android.managedprovisioning.model.ProvisioningParams;
32 
33 /**
34  * This class can initialize a {@link ProvisioningParams} object from an intent.
35  *
36  * <p>A {@link ProvisioningParams} object stores various parameters both for the device owner
37  * provisioning and profile owner provisioning.
38  */
39 public class MessageParser implements ProvisioningDataParser {
40 
41     private final Utils mUtils;
42     private final ParserUtils mParserUtils;
43     private final SettingsFacade mSettingsFacade;
44     private final Context mContext;
45 
MessageParser(Context context)46     public MessageParser(Context context) {
47         this(context, new Utils(), new ParserUtils(), new SettingsFacade());
48     }
49 
50     @VisibleForTesting
MessageParser(Context context, Utils utils, ParserUtils parserUtils, SettingsFacade settingsFacade)51     MessageParser(Context context, Utils utils, ParserUtils parserUtils,
52             SettingsFacade settingsFacade) {
53         mContext = checkNotNull(context);
54         mUtils = checkNotNull(utils);
55         mParserUtils = checkNotNull(parserUtils);
56         mSettingsFacade = checkNotNull(settingsFacade);
57     }
58 
59     @Override
parse(Intent provisioningIntent)60     public ProvisioningParams parse(Intent provisioningIntent)
61             throws IllegalProvisioningArgumentException {
62         return getParser(provisioningIntent).parse(provisioningIntent);
63     }
64 
65     @VisibleForTesting
getParser(Intent provisioningIntent)66     ProvisioningDataParser getParser(Intent provisioningIntent) {
67         if (ACTION_NDEF_DISCOVERED.equals(provisioningIntent.getAction())) {
68             return new PropertiesProvisioningDataParser(mContext, mParserUtils, mSettingsFacade);
69         } else {
70             return new ExtrasProvisioningDataParser(
71                     mContext, mUtils, mParserUtils, mSettingsFacade);
72         }
73     }
74 }
75