• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.phone.testapps.telephonymanagertestapp;
18 
19 import android.content.Context;
20 import android.telephony.NumberVerificationCallback;
21 import android.telephony.PhoneNumberRange;
22 import android.widget.Toast;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.Executor;
27 import java.util.function.Function;
28 
29 class ParameterParser {
30     private static ParameterParser sInstance;
31 
get(Context context)32     static ParameterParser get(Context context) {
33         if (sInstance == null) {
34             sInstance = new ParameterParser(context);
35         }
36         return sInstance;
37     }
38 
39     private final Context mContext;
40     private final Map<Class, Function<String, Object>> mParsers =
41             new HashMap<Class, Function<String, Object>>() {{
42                 put(PhoneNumberRange.class, ParameterParser::parsePhoneNumberRange);
43                 put(Executor.class, s -> parseExecutor(s));
44                 put(NumberVerificationCallback.class, s -> parseNumberVerificationCallback(s));
45             }};
46 
ParameterParser(Context context)47     private ParameterParser(Context context) {
48         mContext = context;
49     }
50 
executeParser(Class type, String input)51     Object executeParser(Class type, String input) {
52         return mParsers.getOrDefault(type, s -> null).apply(input);
53     }
54 
parsePhoneNumberRange(String input)55     private static PhoneNumberRange parsePhoneNumberRange(String input) {
56         String[] parts = input.split(" ");
57         if (parts.length != 4) {
58             return null;
59         }
60         return new PhoneNumberRange(parts[0], parts[1], parts[2], parts[3]);
61     }
62 
parseExecutor(String input)63     private Executor parseExecutor(String input) {
64         return mContext.getMainExecutor();
65     }
66 
parseNumberVerificationCallback(String input)67     private NumberVerificationCallback parseNumberVerificationCallback(String input) {
68         return new NumberVerificationCallback() {
69             @Override
70             public void onCallReceived(String phoneNumber) {
71                 Toast.makeText(mContext, "Received verification " + phoneNumber,
72                         Toast.LENGTH_SHORT).show();
73             }
74 
75             @Override
76             public void onVerificationFailed(int reason) {
77                 Toast.makeText(mContext, "Verification failed " + reason,
78                         Toast.LENGTH_SHORT).show();
79             }
80         };
81     }
82 }
83