• 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.telephony.RadioAccessSpecifier;
23 import android.text.TextUtils;
24 import android.widget.Toast;
25 
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.Executor;
32 import java.util.function.Consumer;
33 import java.util.function.Function;
34 import java.util.stream.Collectors;
35 
36 class ParameterParser {
37     private static ParameterParser sInstance;
38 
get(Context context)39     static ParameterParser get(Context context) {
40         if (sInstance == null) {
41             sInstance = new ParameterParser(context);
42         }
43         return sInstance;
44     }
45 
46     private final Context mContext;
47     private final Map<Class, Function<String, Object>> mParsers =
48             new HashMap<Class, Function<String, Object>>() {{
49                 put(PhoneNumberRange.class, ParameterParser::parsePhoneNumberRange);
50                 put(Executor.class, s -> parseExecutor(s));
51                 put(NumberVerificationCallback.class, s -> parseNumberVerificationCallback(s));
52                 put(Consumer.class, s -> parseConsumer(s));
53                 put(List.class, s -> parseList(s));
54                 put(RadioAccessSpecifier.class, s -> parseRadioAccessSpecifier(s));
55             }};
56 
ParameterParser(Context context)57     private ParameterParser(Context context) {
58         mContext = context;
59     }
60 
executeParser(Class type, String input)61     Object executeParser(Class type, String input) {
62         if ("null".equals(input)) return null;
63         return mParsers.getOrDefault(type, s -> null).apply(input);
64     }
65 
parsePhoneNumberRange(String input)66     private static PhoneNumberRange parsePhoneNumberRange(String input) {
67         String[] parts = input.split(" ");
68         if (parts.length != 4) {
69             return null;
70         }
71         return new PhoneNumberRange(parts[0], parts[1], parts[2], parts[3]);
72     }
73 
parseExecutor(String input)74     private Executor parseExecutor(String input) {
75         return mContext.getMainExecutor();
76     }
77 
parseConsumer(String input)78     private Consumer parseConsumer(String input) {
79         return o -> Toast.makeText(mContext, "Consumer: " + String.valueOf(o), Toast.LENGTH_SHORT)
80                 .show();
81     }
82 
83     /**
84      * input format: (ran)/(band1)+(band2)+.../(chan1)+(chan2)+...
85      * @return
86      */
parseRadioAccessSpecifier(String input)87     private RadioAccessSpecifier parseRadioAccessSpecifier(String input) {
88         String[] parts = input.split("/");
89         int ran = Integer.parseInt(parts[0]);
90         String[] bandStrings = parts[1].split("\\+");
91         int[] bands = new int[bandStrings.length];
92         String[] chanStrings = parts[2].split("\\+");
93         int[] chans = new int[chanStrings.length];
94 
95         for (int i = 0; i < bands.length; i++) {
96             bands[i] = Integer.parseInt(bandStrings[i]);
97         }
98 
99         for (int i = 0; i < chans.length; i++) {
100             chans[i] = Integer.parseInt(chanStrings[i]);
101         }
102         return new RadioAccessSpecifier(ran, bands, chans);
103     }
104 
parseList(String input)105     private List parseList(String input) {
106         if (TextUtils.isEmpty(input)) {
107             return Collections.emptyList();
108         }
109         String[] components = input.split(" ");
110         String className = components[0];
111         Class c;
112         try {
113             c = mContext.getClassLoader().loadClass(className);
114         } catch (ClassNotFoundException e) {
115             Toast.makeText(mContext, "Invalid class " + className,
116                     Toast.LENGTH_SHORT).show();
117             return null;
118         }
119         if (!mParsers.containsKey(c)) {
120             Toast.makeText(mContext, "Cannot parse " + className,
121                     Toast.LENGTH_SHORT).show();
122             return null;
123         }
124         return Arrays.stream(components).skip(1)
125                 .map(mParsers.get(c))
126                 .collect(Collectors.toList());
127     }
128 
parseNumberVerificationCallback(String input)129     private NumberVerificationCallback parseNumberVerificationCallback(String input) {
130         return new NumberVerificationCallback() {
131             @Override
132             public void onCallReceived(String phoneNumber) {
133                 Toast.makeText(mContext, "Received verification " + phoneNumber,
134                         Toast.LENGTH_SHORT).show();
135             }
136 
137             @Override
138             public void onVerificationFailed(int reason) {
139                 Toast.makeText(mContext, "Verification failed " + reason,
140                         Toast.LENGTH_SHORT).show();
141             }
142         };
143     }
144 }
145