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