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.ListActivity; 20 import android.app.SearchManager; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.telephony.TelephonyManager; 24 import android.view.Menu; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.BaseAdapter; 28 import android.widget.ListView; 29 import android.widget.SearchView; 30 import android.widget.TextView; 31 32 import java.lang.reflect.Method; 33 import java.lang.reflect.Modifier; 34 import java.lang.reflect.Parameter; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.List; 38 39 /** 40 * Main activity. 41 * Activity to choose which method to call. 42 */ 43 public class TelephonyManagerTestApp extends ListActivity implements 44 SearchView.OnQueryTextListener { 45 public static String TAG = "TMTestApp"; 46 47 private List<Method> mMethods = new ArrayList<>(); 48 private List<Method> mFilteredMethods = new ArrayList<>(); 49 static Method sCurrentMethod; 50 51 @Override onCreateOptionsMenu(Menu menu)52 public boolean onCreateOptionsMenu(Menu menu) { 53 // Initialize search view 54 getMenuInflater().inflate(R.menu.search_input, menu); 55 SearchView searchView = (SearchView) menu.findItem(R.id.search_input).getActionView(); 56 SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 57 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 58 searchView.setOnQueryTextListener(this); 59 searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { 60 @Override 61 public void onViewDetachedFromWindow(View arg0) { 62 mFilteredMethods.clear(); 63 mFilteredMethods.addAll(mMethods); 64 ((ListViewAdapter) mAdapter).notifyDataSetChanged(); 65 } 66 67 @Override 68 public void onViewAttachedToWindow(View arg0) { 69 } 70 }); 71 return true; 72 } 73 74 @Override onCreate(Bundle savedInstanceState)75 protected void onCreate(Bundle savedInstanceState) { 76 super.onCreate(savedInstanceState); 77 setContentView(R.layout.activity_main); 78 79 try { 80 Class c = TelephonyManager.class; 81 mMethods = Arrays.asList(c.getDeclaredMethods()); 82 mFilteredMethods.addAll(mMethods); 83 mAdapter = new ListViewAdapter(); 84 setListAdapter(mAdapter); 85 } catch (Throwable e) { 86 System.err.println(e); 87 finish(); 88 } 89 } 90 91 private class ListViewAdapter extends BaseAdapter { 92 @Override getCount()93 public int getCount() { 94 return mFilteredMethods.size(); 95 } 96 97 @Override getView(int position, View convertView, ViewGroup container)98 public View getView(int position, View convertView, ViewGroup container) { 99 if (mFilteredMethods.size() <= position) { 100 return null; 101 } 102 103 if (convertView == null) { 104 convertView = getLayoutInflater().inflate( 105 R.layout.abstract_method_view, container, false); 106 } 107 108 Method method = mFilteredMethods.get(position); 109 String tags = Modifier.toString(method.getModifiers()) + ' ' 110 + getShortTypeName(method.getReturnType().toString()); 111 String parameters = getParameters(method.getParameterTypes(), method.getParameters()); 112 String methodName = (parameters == null) ? (method.getName() + "()") : method.getName(); 113 114 ((TextView) convertView.findViewById(R.id.tags)).setText(tags); 115 ((TextView) convertView.findViewById(R.id.method_name)).setText(methodName); 116 ((TextView) convertView.findViewById(R.id.parameters)).setText(parameters); 117 return convertView; 118 } 119 120 @Override getItem(int position)121 public Object getItem(int position) { 122 if (mFilteredMethods.size() <= position) { 123 return null; 124 } 125 126 return mFilteredMethods.get(position); 127 } 128 129 @Override getItemId(int position)130 public long getItemId(int position) { 131 return position; 132 } 133 } 134 135 @Override onListItemClick(ListView l, View v, int position, long id)136 public void onListItemClick(ListView l, View v, int position, long id) { 137 sCurrentMethod = mFilteredMethods.get(position); 138 Intent intent = new Intent(this, CallingMethodActivity.class); 139 140 startActivity(intent); 141 } 142 143 @Override onQueryTextSubmit(String query)144 public boolean onQueryTextSubmit(String query) { 145 filterMethods(query); 146 return true; 147 } 148 149 @Override onQueryTextChange(String newText)150 public boolean onQueryTextChange(String newText) { 151 return false; 152 } 153 filterMethods(String text)154 private void filterMethods(String text) { 155 mFilteredMethods.clear(); 156 157 if (text == null || text.isEmpty()) { 158 mFilteredMethods.addAll(mMethods); 159 } else { 160 for (Method method : mMethods) { 161 if (method.getName().toLowerCase().contains(text.toLowerCase())) { 162 mFilteredMethods.add(method); 163 } 164 } 165 } 166 167 ((ListViewAdapter) mAdapter).notifyDataSetChanged(); 168 169 } 170 getParameters(Class<?>[] types, Parameter[] parameters)171 private String getParameters(Class<?>[] types, Parameter[] parameters) { 172 if (types == null || types.length == 0) { 173 return null; 174 } 175 176 StringBuilder sb = new StringBuilder(); 177 sb.append('('); 178 for (int j = 0; j < types.length; j++) { 179 String typeName = getShortTypeName(types[j].getTypeName()); 180 sb.append(typeName); 181 if (j < (types.length - 1)) { 182 sb.append(", "); 183 } 184 } 185 sb.append(')'); 186 187 return sb.toString(); 188 } 189 getShortTypeName(String typeName)190 static String getShortTypeName(String typeName) { 191 if (typeName == null) { 192 return null; 193 } 194 195 String[] parts = typeName.split("[. ]"); 196 return parts[parts.length - 1]; 197 } 198 } 199