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