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.app.Activity; 20 import android.os.Bundle; 21 import android.telephony.TelephonyManager; 22 import android.util.Log; 23 import android.view.View; 24 import android.widget.Button; 25 import android.widget.EditText; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 29 import java.io.PrintWriter; 30 import java.io.StringWriter; 31 import java.lang.reflect.Method; 32 import java.lang.reflect.Modifier; 33 34 /** 35 * Activity to call a specific method of TelephonyManager. 36 */ 37 public class CallingMethodActivity extends Activity { 38 private Class[] mParameterTypes; 39 private Object[] mParameterValues; 40 EditText[] mEditTexts; 41 private Button mGoButton; 42 private Method mMethod; 43 private TextView mReturnValue; 44 private EditText mSubIdField; 45 46 @Override onCreate(Bundle savedInstanceState)47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 setContentView(R.layout.calling_method); 50 51 if (TelephonyManagerTestApp.sCurrentMethod == null) { 52 finish(); 53 return; 54 } 55 56 mMethod = TelephonyManagerTestApp.sCurrentMethod; 57 58 mGoButton = findViewById(R.id.go_button); 59 mReturnValue = findViewById(R.id.return_value); 60 mSubIdField = findViewById(R.id.sub_id_value); 61 62 mParameterTypes = mMethod.getParameterTypes(); 63 mParameterValues = new Object[mParameterTypes.length]; 64 mEditTexts = new EditText[mParameterTypes.length]; 65 populateParamList(); 66 67 String tags = Modifier.toString(mMethod.getModifiers()) + ' ' 68 + TelephonyManagerTestApp.getShortTypeName(mMethod.getReturnType().toString()); 69 ((TextView) findViewById(R.id.tags)).setText(tags); 70 ((TextView) findViewById(R.id.method_name)).setText(mMethod.getName()); 71 72 mGoButton.setOnClickListener((View v) -> executeCallMethod()); 73 mReturnValue.setText("Return value: "); 74 } 75 executeCallMethod()76 private void executeCallMethod() { 77 try { 78 int subId = Integer.parseInt(mSubIdField.getText().toString()); 79 80 for (int i = 0; i < mParameterTypes.length; i++) { 81 String text = mEditTexts[i].getText().toString(); 82 if (mParameterTypes[i] == int.class) { 83 mParameterValues[i] = Integer.parseInt(text); 84 } else if (mParameterTypes[i] == boolean.class) { 85 mParameterValues[i] = Boolean.parseBoolean(text); 86 } else if (mParameterTypes[i] == long.class) { 87 mParameterValues[i] = Long.parseLong(text); 88 } else if (mParameterTypes[i] == String.class) { 89 mParameterValues[i] = text; 90 } else { 91 mParameterValues[i] = 92 ParameterParser.get(this).executeParser(mParameterTypes[i], text); 93 } 94 } 95 Log.d(TelephonyManagerTestApp.TAG, "Invoking method " + mMethod.getName()); 96 97 mMethod.setAccessible(true); 98 if (!mMethod.getReturnType().equals(Void.TYPE)) { 99 Object result = mMethod.invoke(new TelephonyManager(this, subId), mParameterValues); 100 if (result instanceof String) { 101 if (((String) result).isEmpty()) { 102 result = "empty string"; 103 } 104 } 105 Log.d(TelephonyManagerTestApp.TAG, "result is " + result); 106 mReturnValue.setText("Return value: " + result); 107 } else { 108 mMethod.invoke(new TelephonyManager(this, subId), mParameterValues); 109 mReturnValue.setText("Return value: successfully returned"); 110 } 111 112 } catch (Exception exception) { 113 Log.d(TelephonyManagerTestApp.TAG, "Exception: " + exception); 114 StringWriter s = new StringWriter(); 115 PrintWriter stack = new PrintWriter(s); 116 exception.printStackTrace(stack); 117 mReturnValue.setText("Exception " + exception.getMessage() + "\n" + s.toString()); 118 } 119 } 120 populateParamList()121 private void populateParamList() { 122 for (int i = 0; i < mParameterTypes.length; i++) { 123 View view = getLayoutInflater().inflate(R.layout.parameter_field, null); 124 Class aClass = mParameterTypes[i]; 125 ((TextView) view.findViewById(R.id.field_name)).setText( 126 TelephonyManagerTestApp.getShortTypeName(aClass.toString()) + ": "); 127 mEditTexts[i] = view.findViewById(R.id.field_value); 128 ((LinearLayout) findViewById(R.id.method_params)).addView(view); 129 } 130 } 131 } 132