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