1 /* 2 * Copyright (C) 2017 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.googlecode.android_scripting.facade; 18 19 import com.google.common.collect.Maps; 20 import com.googlecode.android_scripting.Log; 21 import com.googlecode.android_scripting.facade.bluetooth.BluetoothA2dpFacade; 22 import com.googlecode.android_scripting.facade.bluetooth.BluetoothA2dpSinkFacade; 23 import com.googlecode.android_scripting.facade.bluetooth.BluetoothAvrcpFacade; 24 import com.googlecode.android_scripting.facade.bluetooth.BluetoothConnectionFacade; 25 import com.googlecode.android_scripting.facade.bluetooth.BluetoothFacade; 26 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHfpClientFacade; 27 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHidDeviceFacade; 28 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHidFacade; 29 import com.googlecode.android_scripting.facade.bluetooth.BluetoothHspFacade; 30 import com.googlecode.android_scripting.facade.bluetooth.BluetoothLeAdvertiseFacade; 31 import com.googlecode.android_scripting.facade.bluetooth.BluetoothLeAdvertisingSetFacade; 32 import com.googlecode.android_scripting.facade.bluetooth.BluetoothLeScanFacade; 33 import com.googlecode.android_scripting.facade.bluetooth.BluetoothMapClientFacade; 34 import com.googlecode.android_scripting.facade.bluetooth.BluetoothMapFacade; 35 import com.googlecode.android_scripting.facade.bluetooth.BluetoothMediaFacade; 36 import com.googlecode.android_scripting.facade.bluetooth.BluetoothPanFacade; 37 import com.googlecode.android_scripting.facade.bluetooth.BluetoothPbapClientFacade; 38 import com.googlecode.android_scripting.facade.bluetooth.BluetoothRfcommFacade; 39 import com.googlecode.android_scripting.facade.bluetooth.GattClientFacade; 40 import com.googlecode.android_scripting.facade.bluetooth.GattServerFacade; 41 import com.googlecode.android_scripting.facade.media.AudioManagerFacade; 42 import com.googlecode.android_scripting.facade.media.MediaPlayerFacade; 43 import com.googlecode.android_scripting.facade.media.MediaRecorderFacade; 44 import com.googlecode.android_scripting.facade.media.MediaScannerFacade; 45 import com.googlecode.android_scripting.facade.media.MediaSessionFacade; 46 import com.googlecode.android_scripting.facade.net.IpSecManagerFacade; 47 import com.googlecode.android_scripting.facade.net.SocketFacade; 48 import com.googlecode.android_scripting.facade.net.SocketKeepaliveFacade; 49 import com.googlecode.android_scripting.facade.net.nsd.NsdManagerFacade; 50 import com.googlecode.android_scripting.facade.telephony.CarrierConfigFacade; 51 import com.googlecode.android_scripting.facade.telephony.ImsManagerFacade; 52 import com.googlecode.android_scripting.facade.telephony.ImsMmTelManagerFacade; 53 import com.googlecode.android_scripting.facade.telephony.ProvisioningManagerFacade; 54 import com.googlecode.android_scripting.facade.telephony.SmsFacade; 55 import com.googlecode.android_scripting.facade.telephony.SubscriptionManagerFacade; 56 import com.googlecode.android_scripting.facade.telephony.TelecomCallFacade; 57 import com.googlecode.android_scripting.facade.telephony.TelecomManagerFacade; 58 import com.googlecode.android_scripting.facade.telephony.TelephonyManagerFacade; 59 import com.googlecode.android_scripting.facade.ui.UiFacade; 60 import com.googlecode.android_scripting.facade.webcam.WebCamFacade; 61 import com.googlecode.android_scripting.facade.wifi.HttpFacade; 62 import com.googlecode.android_scripting.facade.wifi.WifiAwareManagerFacade; 63 import com.googlecode.android_scripting.facade.wifi.WifiManagerFacade; 64 import com.googlecode.android_scripting.facade.wifi.WifiP2pManagerFacade; 65 import com.googlecode.android_scripting.facade.wifi.WifiRtt2ManagerFacade; 66 import com.googlecode.android_scripting.facade.wifi.WifiRttManagerFacade; 67 import com.googlecode.android_scripting.facade.wifi.WifiScannerFacade; 68 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 69 import com.googlecode.android_scripting.rpc.MethodDescriptor; 70 import com.googlecode.android_scripting.rpc.RpcDeprecated; 71 import com.googlecode.android_scripting.rpc.RpcMinSdk; 72 import com.googlecode.android_scripting.rpc.RpcStartEvent; 73 import com.googlecode.android_scripting.rpc.RpcStopEvent; 74 75 import java.lang.reflect.Method; 76 import java.util.ArrayList; 77 import java.util.Collection; 78 import java.util.HashSet; 79 import java.util.List; 80 import java.util.Map; 81 import java.util.Set; 82 import java.util.SortedMap; 83 import java.util.TreeMap; 84 85 /** 86 * Encapsulates the list of supported facades and their construction. 87 */ 88 public class FacadeConfiguration { 89 private final static Set<Class<? extends RpcReceiver>> sFacadeClassList; 90 private final static SortedMap<String, MethodDescriptor> sRpcs = 91 new TreeMap<String, MethodDescriptor>(); 92 93 private static int sSdkLevel; 94 95 static { 96 sSdkLevel = android.os.Build.VERSION.SDK_INT; 97 98 sFacadeClassList = new HashSet<Class<? extends RpcReceiver>>(); 99 sFacadeClassList.add(ActivityResultFacade.class); 100 sFacadeClassList.add(AndroidFacade.class); 101 sFacadeClassList.add(ApplicationManagerFacade.class); 102 sFacadeClassList.add(AudioManagerFacade.class); 103 sFacadeClassList.add(BatteryManagerFacade.class); 104 sFacadeClassList.add(CallLogFacade.class); 105 sFacadeClassList.add(CameraFacade.class); 106 sFacadeClassList.add(CommonIntentsFacade.class); 107 sFacadeClassList.add(ContactsFacade.class); 108 sFacadeClassList.add(EventFacade.class); 109 sFacadeClassList.add(ImsManagerFacade.class); 110 sFacadeClassList.add(ImsMmTelManagerFacade.class); 111 sFacadeClassList.add(LocationFacade.class); 112 sFacadeClassList.add(TelephonyManagerFacade.class); 113 sFacadeClassList.add(ProvisioningManagerFacade.class); 114 sFacadeClassList.add(PreferencesFacade.class); 115 sFacadeClassList.add(MediaPlayerFacade.class); 116 sFacadeClassList.add(MediaRecorderFacade.class); 117 sFacadeClassList.add(MediaScannerFacade.class); 118 sFacadeClassList.add(MediaSessionFacade.class); 119 sFacadeClassList.add(SensorManagerFacade.class); 120 sFacadeClassList.add(SettingsFacade.class); 121 sFacadeClassList.add(SmsFacade.class); 122 sFacadeClassList.add(SpeechRecognitionFacade.class); 123 sFacadeClassList.add(ToneGeneratorFacade.class); 124 sFacadeClassList.add(WakeLockFacade.class); 125 sFacadeClassList.add(HttpFacade.class); 126 sFacadeClassList.add(WifiManagerFacade.class); 127 sFacadeClassList.add(UiFacade.class); 128 sFacadeClassList.add(TextToSpeechFacade.class); 129 sFacadeClassList.add(BluetoothFacade.class); 130 sFacadeClassList.add(BluetoothA2dpFacade.class); 131 sFacadeClassList.add(BluetoothAvrcpFacade.class); 132 sFacadeClassList.add(BluetoothConnectionFacade.class); 133 sFacadeClassList.add(BluetoothHspFacade.class); 134 sFacadeClassList.add(BluetoothHidFacade.class); 135 sFacadeClassList.add(BluetoothHidDeviceFacade.class); 136 sFacadeClassList.add(BluetoothMapFacade.class); 137 sFacadeClassList.add(BluetoothPanFacade.class); 138 sFacadeClassList.add(BluetoothMediaFacade.class); 139 sFacadeClassList.add(BluetoothRfcommFacade.class); 140 sFacadeClassList.add(WebCamFacade.class); 141 sFacadeClassList.add(WifiP2pManagerFacade.class); 142 sFacadeClassList.add(BluetoothLeScanFacade.class); 143 sFacadeClassList.add(BluetoothLeAdvertiseFacade.class); 144 sFacadeClassList.add(GattClientFacade.class); 145 sFacadeClassList.add(GattServerFacade.class); 146 sFacadeClassList.add(ConnectivityManagerFacade.class); 147 sFacadeClassList.add(DisplayFacade.class); 148 sFacadeClassList.add(TelecomManagerFacade.class); 149 sFacadeClassList.add(WifiRttManagerFacade.class); 150 sFacadeClassList.add(WifiScannerFacade.class); 151 sFacadeClassList.add(SubscriptionManagerFacade.class); 152 sFacadeClassList.add(TelecomCallFacade.class); 153 sFacadeClassList.add(CarrierConfigFacade.class); 154 sFacadeClassList.add(NfcManagerFacade.class); 155 sFacadeClassList.add(VpnFacade.class); 156 157 /* Compatibility reset to >= Marshmallow */ 158 if (sSdkLevel >= 23) { 159 // add new facades here 160 sFacadeClassList.add(WifiAwareManagerFacade.class); 161 sFacadeClassList.add(BluetoothHfpClientFacade.class); 162 sFacadeClassList.add(BluetoothA2dpSinkFacade.class); 163 sFacadeClassList.add(BluetoothPbapClientFacade.class); 164 sFacadeClassList.add(NsdManagerFacade.class); 165 sFacadeClassList.add(BluetoothMapClientFacade.class); 166 sFacadeClassList.add(BluetoothLeAdvertisingSetFacade.class); 167 sFacadeClassList.add(IpSecManagerFacade.class); 168 sFacadeClassList.add(SocketFacade.class); 169 } 170 171 if (sSdkLevel >= 27) { 172 sFacadeClassList.add(WifiRtt2ManagerFacade.class); 173 } 174 175 if (sSdkLevel >= 29) { 176 sFacadeClassList.add(SocketKeepaliveFacade.class); 177 } 178 179 for (Class<? extends RpcReceiver> recieverClass : sFacadeClassList) { 180 for (MethodDescriptor rpcMethod : MethodDescriptor.collectFrom(recieverClass)) { rpcMethod.getName()181 sRpcs.put(rpcMethod.getName(), rpcMethod); 182 } 183 } 184 } 185 FacadeConfiguration()186 private FacadeConfiguration() { 187 // Utility class. 188 } 189 getSdkLevel()190 public static int getSdkLevel() { 191 return sSdkLevel; 192 } 193 194 /** Returns a list of {@link MethodDescriptor} objects for all facades. */ collectMethodDescriptors()195 public static List<MethodDescriptor> collectMethodDescriptors() { 196 return new ArrayList<MethodDescriptor>(sRpcs.values()); 197 } 198 199 /** 200 * Returns a list of not deprecated {@link MethodDescriptor} objects for facades supported by 201 * the current SDK version. 202 */ collectSupportedMethodDescriptors()203 public static List<MethodDescriptor> collectSupportedMethodDescriptors() { 204 List<MethodDescriptor> list = new ArrayList<MethodDescriptor>(); 205 for (MethodDescriptor descriptor : sRpcs.values()) { 206 Method method = descriptor.getMethod(); 207 if (method.isAnnotationPresent(RpcDeprecated.class)) { 208 continue; 209 } else if (method.isAnnotationPresent(RpcMinSdk.class)) { 210 int requiredSdkLevel = method.getAnnotation(RpcMinSdk.class).value(); 211 if (sSdkLevel < requiredSdkLevel) { 212 continue; 213 } 214 } 215 list.add(descriptor); 216 } 217 return list; 218 } 219 collectStartEventMethodDescriptors()220 public static Map<String, MethodDescriptor> collectStartEventMethodDescriptors() { 221 Map<String, MethodDescriptor> map = Maps.newHashMap(); 222 for (MethodDescriptor descriptor : sRpcs.values()) { 223 Method method = descriptor.getMethod(); 224 if (method.isAnnotationPresent(RpcStartEvent.class)) { 225 String eventName = method.getAnnotation(RpcStartEvent.class).value(); 226 if (map.containsKey(eventName)) { 227 Log.d("Duplicate eventName " + eventName); 228 throw new RuntimeException("Duplicate start event method descriptor found."); 229 } 230 map.put(eventName, descriptor); 231 } 232 } 233 return map; 234 } 235 collectStopEventMethodDescriptors()236 public static Map<String, MethodDescriptor> collectStopEventMethodDescriptors() { 237 Map<String, MethodDescriptor> map = Maps.newHashMap(); 238 for (MethodDescriptor descriptor : sRpcs.values()) { 239 Method method = descriptor.getMethod(); 240 if (method.isAnnotationPresent(RpcStopEvent.class)) { 241 String eventName = method.getAnnotation(RpcStopEvent.class).value(); 242 if (map.containsKey(eventName)) { 243 throw new RuntimeException("Duplicate stop event method descriptor found."); 244 } 245 map.put(eventName, descriptor); 246 } 247 } 248 return map; 249 } 250 251 /** Returns a method by name. */ getMethodDescriptor(String name)252 public static MethodDescriptor getMethodDescriptor(String name) { 253 return sRpcs.get(name); 254 } 255 getFacadeClasses()256 public static Collection<Class<? extends RpcReceiver>> getFacadeClasses() { 257 return sFacadeClassList; 258 } 259 } 260