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