1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade; 18 19 import java.lang.reflect.Method; 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.HashSet; 23 import java.util.List; 24 import java.util.Map; 25 import java.util.Set; 26 import java.util.SortedMap; 27 import java.util.TreeMap; 28 29 import com.google.common.collect.Maps; 30 import com.googlecode.android_scripting.Log; 31 import com.googlecode.android_scripting.facade.bluetooth.BluetoothA2dpFacade; 32 import com.googlecode.android_scripting.facade.bluetooth.BluetoothAvrcpFacade; 33 import com.googlecode.android_scripting.facade.bluetooth.BluetoothConnectionFacade; 34 import com.googlecode.android_scripting.facade.bluetooth.BluetoothFacade; 35 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHfpClientFacade; 36 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHidFacade; 37 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHspFacade; 38 import com.googlecode.android_scripting.facade.bluetooth.BluetoothLeAdvertiseFacade; 39 import com.googlecode.android_scripting.facade.bluetooth.BluetoothLeScanFacade; 40 import com.googlecode.android_scripting.facade.bluetooth.BluetoothMapFacade; 41 import com.googlecode.android_scripting.facade.bluetooth.BluetoothRfcommFacade; 42 import com.googlecode.android_scripting.facade.bluetooth.GattClientFacade; 43 import com.googlecode.android_scripting.facade.bluetooth.GattServerFacade; 44 import com.googlecode.android_scripting.facade.media.AudioManagerFacade; 45 import com.googlecode.android_scripting.facade.media.MediaPlayerFacade; 46 import com.googlecode.android_scripting.facade.media.MediaRecorderFacade; 47 import com.googlecode.android_scripting.facade.media.MediaScannerFacade; 48 import com.googlecode.android_scripting.facade.media.MediaSessionFacade; 49 import com.googlecode.android_scripting.facade.telephony.CarrierConfigFacade; 50 import com.googlecode.android_scripting.facade.telephony.ImsManagerFacade; 51 import com.googlecode.android_scripting.facade.telephony.SmsFacade; 52 import com.googlecode.android_scripting.facade.telephony.SubscriptionManagerFacade; 53 import com.googlecode.android_scripting.facade.telephony.TelecomCallFacade; 54 import com.googlecode.android_scripting.facade.telephony.TelecomManagerFacade; 55 import com.googlecode.android_scripting.facade.telephony.TelephonyManagerFacade; 56 import com.googlecode.android_scripting.facade.ui.UiFacade; 57 import com.googlecode.android_scripting.facade.wifi.HttpFacade; 58 import com.googlecode.android_scripting.facade.wifi.WifiManagerFacade; 59 import com.googlecode.android_scripting.facade.wifi.WifiNanManagerFacade; 60 import com.googlecode.android_scripting.facade.wifi.WifiP2pManagerFacade; 61 import com.googlecode.android_scripting.facade.wifi.WifiRttManagerFacade; 62 import com.googlecode.android_scripting.facade.wifi.WifiScannerFacade; 63 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 64 import com.googlecode.android_scripting.rpc.MethodDescriptor; 65 import com.googlecode.android_scripting.rpc.RpcDeprecated; 66 import com.googlecode.android_scripting.rpc.RpcMinSdk; 67 import com.googlecode.android_scripting.rpc.RpcStartEvent; 68 import com.googlecode.android_scripting.rpc.RpcStopEvent; 69 import com.googlecode.android_scripting.webcam.WebCamFacade; 70 71 /** 72 * Encapsulates the list of supported facades and their construction. 73 */ 74 public class FacadeConfiguration { 75 private final static Set<Class<? extends RpcReceiver>> sFacadeClassList; 76 private final static SortedMap<String, MethodDescriptor> sRpcs = 77 new TreeMap<String, MethodDescriptor>(); 78 79 private static int sSdkLevel; 80 81 static { 82 sSdkLevel = android.os.Build.VERSION.SDK_INT; 83 84 sFacadeClassList = new HashSet<Class<? extends RpcReceiver>>(); 85 sFacadeClassList.add(ActivityResultFacade.class); 86 sFacadeClassList.add(AndroidFacade.class); 87 sFacadeClassList.add(ApplicationManagerFacade.class); 88 sFacadeClassList.add(AudioManagerFacade.class); 89 sFacadeClassList.add(BatteryManagerFacade.class); 90 sFacadeClassList.add(CameraFacade.class); 91 sFacadeClassList.add(CommonIntentsFacade.class); 92 sFacadeClassList.add(ContactsFacade.class); 93 sFacadeClassList.add(EventFacade.class); 94 sFacadeClassList.add(ImsManagerFacade.class); 95 sFacadeClassList.add(LocationFacade.class); 96 sFacadeClassList.add(TelephonyManagerFacade.class); 97 sFacadeClassList.add(PreferencesFacade.class); 98 sFacadeClassList.add(MediaPlayerFacade.class); 99 sFacadeClassList.add(MediaRecorderFacade.class); 100 sFacadeClassList.add(MediaScannerFacade.class); 101 sFacadeClassList.add(MediaSessionFacade.class); 102 sFacadeClassList.add(SensorManagerFacade.class); 103 sFacadeClassList.add(SettingsFacade.class); 104 sFacadeClassList.add(SmsFacade.class); 105 sFacadeClassList.add(SpeechRecognitionFacade.class); 106 sFacadeClassList.add(ToneGeneratorFacade.class); 107 sFacadeClassList.add(WakeLockFacade.class); 108 sFacadeClassList.add(HttpFacade.class); 109 sFacadeClassList.add(WifiManagerFacade.class); 110 sFacadeClassList.add(UiFacade.class); 111 sFacadeClassList.add(TextToSpeechFacade.class); 112 sFacadeClassList.add(BluetoothFacade.class); 113 sFacadeClassList.add(BluetoothA2dpFacade.class); 114 sFacadeClassList.add(BluetoothAvrcpFacade.class); 115 sFacadeClassList.add(BluetoothConnectionFacade.class); 116 sFacadeClassList.add(BluetoothHspFacade.class); 117 sFacadeClassList.add(BluetoothHidFacade.class); 118 sFacadeClassList.add(BluetoothMapFacade.class); 119 sFacadeClassList.add(BluetoothRfcommFacade.class); 120 sFacadeClassList.add(WebCamFacade.class); 121 sFacadeClassList.add(WifiP2pManagerFacade.class); 122 sFacadeClassList.add(BluetoothLeScanFacade.class); 123 sFacadeClassList.add(BluetoothLeAdvertiseFacade.class); 124 sFacadeClassList.add(GattClientFacade.class); 125 sFacadeClassList.add(GattServerFacade.class); 126 sFacadeClassList.add(ConnectivityManagerFacade.class); 127 sFacadeClassList.add(DisplayFacade.class); 128 sFacadeClassList.add(TelecomManagerFacade.class); 129 sFacadeClassList.add(WifiRttManagerFacade.class); 130 sFacadeClassList.add(WifiScannerFacade.class); 131 sFacadeClassList.add(SubscriptionManagerFacade.class); 132 sFacadeClassList.add(TelecomCallFacade.class); 133 sFacadeClassList.add(CarrierConfigFacade.class); 134 sFacadeClassList.add(NfcManagerFacade.class); 135 136 /* Compatibility reset to >= Marshmallow */ 137 if (sSdkLevel >= 23) { 138 // add new facades here 139 sFacadeClassList.add(WifiNanManagerFacade.class); 140 sFacadeClassList.add(BluetoothHfpClientFacade.class); 141 } 142 143 for (Class<? extends RpcReceiver> recieverClass : sFacadeClassList) { 144 for (MethodDescriptor rpcMethod : MethodDescriptor.collectFrom(recieverClass)) { rpcMethod.getName()145 sRpcs.put(rpcMethod.getName(), rpcMethod); 146 } 147 } 148 } 149 FacadeConfiguration()150 private FacadeConfiguration() { 151 // Utility class. 152 } 153 getSdkLevel()154 public static int getSdkLevel() { 155 return sSdkLevel; 156 } 157 158 /** Returns a list of {@link MethodDescriptor} objects for all facades. */ collectMethodDescriptors()159 public static List<MethodDescriptor> collectMethodDescriptors() { 160 return new ArrayList<MethodDescriptor>(sRpcs.values()); 161 } 162 163 /** 164 * Returns a list of not deprecated {@link MethodDescriptor} objects for facades supported by 165 * the current SDK version. 166 */ collectSupportedMethodDescriptors()167 public static List<MethodDescriptor> collectSupportedMethodDescriptors() { 168 List<MethodDescriptor> list = new ArrayList<MethodDescriptor>(); 169 for (MethodDescriptor descriptor : sRpcs.values()) { 170 Method method = descriptor.getMethod(); 171 if (method.isAnnotationPresent(RpcDeprecated.class)) { 172 continue; 173 } else if (method.isAnnotationPresent(RpcMinSdk.class)) { 174 int requiredSdkLevel = method.getAnnotation(RpcMinSdk.class).value(); 175 if (sSdkLevel < requiredSdkLevel) { 176 continue; 177 } 178 } 179 list.add(descriptor); 180 } 181 return list; 182 } 183 collectStartEventMethodDescriptors()184 public static Map<String, MethodDescriptor> collectStartEventMethodDescriptors() { 185 Map<String, MethodDescriptor> map = Maps.newHashMap(); 186 for (MethodDescriptor descriptor : sRpcs.values()) { 187 Method method = descriptor.getMethod(); 188 if (method.isAnnotationPresent(RpcStartEvent.class)) { 189 String eventName = method.getAnnotation(RpcStartEvent.class).value(); 190 if (map.containsKey(eventName)) { 191 Log.d("Duplicate eventName " + eventName); 192 throw new RuntimeException("Duplicate start event method descriptor found."); 193 } 194 map.put(eventName, descriptor); 195 } 196 } 197 return map; 198 } 199 collectStopEventMethodDescriptors()200 public static Map<String, MethodDescriptor> collectStopEventMethodDescriptors() { 201 Map<String, MethodDescriptor> map = Maps.newHashMap(); 202 for (MethodDescriptor descriptor : sRpcs.values()) { 203 Method method = descriptor.getMethod(); 204 if (method.isAnnotationPresent(RpcStopEvent.class)) { 205 String eventName = method.getAnnotation(RpcStopEvent.class).value(); 206 if (map.containsKey(eventName)) { 207 throw new RuntimeException("Duplicate stop event method descriptor found."); 208 } 209 map.put(eventName, descriptor); 210 } 211 } 212 return map; 213 } 214 215 /** Returns a method by name. */ getMethodDescriptor(String name)216 public static MethodDescriptor getMethodDescriptor(String name) { 217 return sRpcs.get(name); 218 } 219 getFacadeClasses()220 public static Collection<Class<? extends RpcReceiver>> getFacadeClasses() { 221 return sFacadeClassList; 222 } 223 } 224