1 /*
2  * Copyright (C) 2023 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.bedstead.nene.services;
18 
19 import static java.util.Map.entry;
20 
21 import android.accounts.AccountManager;
22 import android.annotation.SuppressLint;
23 import android.app.ActivityManager;
24 //import android.app.ActivityTaskManager;
25 import android.app.AlarmManager;
26 import android.app.AppOpsManager;
27 import android.app.DownloadManager;
28 // import android.app.DreamManager;
29 import android.app.GameManager;
30 import android.app.GrammaticalInflectionManager;
31 import android.app.KeyguardManager;
32 import android.app.LocaleManager;
33 import android.app.NotificationManager;
34 import android.app.SearchManager;
35 import android.app.StatusBarManager;
36 import android.app.UiModeManager;
37 //import android.app.UriGrantsManager;
38 import android.app.VrManager;
39 import android.app.WallpaperManager;
40 import android.app.admin.DevicePolicyManager;
41 import android.app.ambientcontext.AmbientContextManager;
42 import android.app.contentsuggestions.ContentSuggestionsManager;
43 import android.app.people.PeopleManager;
44 import android.app.prediction.AppPredictionManager;
45 import android.app.search.SearchUiManager;
46 //import android.app.slice.SliceManager;
47 import android.app.smartspace.SmartspaceManager;
48 import android.app.time.TimeManager;
49 //import android.app.timedetector.TimeDetector;
50 //import android.app.timezonedetector.TimeZoneDetector;
51 //import android.app.trust.TrustManager;
52 import android.app.usage.StorageStatsManager;
53 import android.app.usage.UsageStatsManager;
54 import android.app.wallpapereffectsgeneration.WallpaperEffectsGenerationManager;
55 import android.app.wearable.WearableSensingManager;
56 import android.apphibernation.AppHibernationManager;
57 import android.appwidget.AppWidgetManager;
58 import android.companion.CompanionDeviceManager;
59 import android.companion.virtual.VirtualDeviceManager;
60 import android.content.ClipboardManager;
61 import android.content.Context;
62 import android.content.RestrictionsManager;
63 import android.content.integrity.AppIntegrityManager;
64 import android.content.om.OverlayManager;
65 import android.content.pm.CrossProfileApps;
66 //import android.content.pm.DataLoaderManager;
67 import android.content.pm.LauncherApps;
68 import android.content.pm.ShortcutManager;
69 import android.content.pm.verify.domain.DomainVerificationManager;
70 import android.credentials.CredentialManager;
71 //import android.debug.AdbManager;
72 import android.graphics.fonts.FontManager;
73 import android.hardware.ConsumerIrManager;
74 import android.hardware.SensorManager;
75 //import android.hardware.SensorPrivacyManager;
76 //import android.hardware.SerialManager;
77 import android.hardware.biometrics.BiometricManager;
78 import android.hardware.camera2.CameraManager;
79 //import android.hardware.devicestate.DeviceStateManager;
80 //import android.hardware.display.ColorDisplayManager;
81 import android.hardware.display.DisplayManager;
82 //import android.hardware.face.FaceManager;
83 //import android.hardware.fingerprint.FingerprintManager;
84 import android.hardware.hdmi.HdmiControlManager;
85 import android.hardware.input.InputManager;
86 //import android.hardware.iris.IrisManager;
87 //import android.hardware.lights.LightsManager;
88 import android.hardware.location.ContextHubManager;
89 //import android.hardware.radio.RadioManager;
90 import android.hardware.usb.UsbManager;
91 //import android.location.CountryDetector;
92 import android.location.LocationManager;
93 import android.media.AudioDeviceVolumeManager;
94 import android.media.AudioManager;
95 import android.media.MediaRouter;
96 import android.media.metrics.MediaMetricsManager;
97 import android.media.midi.MidiManager;
98 import android.media.musicrecognition.MusicRecognitionManager;
99 import android.media.projection.MediaProjectionManager;
100 //import android.media.soundtrigger.SoundTriggerManager;
101 import android.media.tv.TvInputManager;
102 import android.media.tv.interactive.TvInteractiveAppManager;
103 //import android.media.tv.tunerresourcemanager.TunerResourceManager;
104 //import android.net.NetworkPolicyManager;
105 import android.net.NetworkScoreManager;
106 //import android.net.NetworkWatchlistManager;
107 //import android.net.PacProxyManager;
108 import android.net.TetheringManager;
109 import android.net.VpnManager;
110 //import android.net.vcn.VcnManager;
111 import android.net.wifi.nl80211.WifiNl80211Manager;
112 import android.net.wifi.sharedconnectivity.app.SharedConnectivityManager;
113 import android.os.BatteryManager;
114 import android.os.BatteryStatsManager;
115 import android.os.BugreportManager;
116 import android.os.DropBoxManager;
117 import android.os.HardwarePropertiesManager;
118 import android.os.IBinder;
119 //import android.os.IncidentManager;
120 import android.os.PerformanceHintManager;
121 //import android.os.PermissionEnforcer;
122 import android.os.PowerManager;
123 //import android.os.RecoverySystem;
124 import android.os.SystemConfigManager;
125 import android.os.SystemUpdateManager;
126 import android.os.UserManager;
127 import android.os.Vibrator;
128 import android.os.VibratorManager;
129 import android.os.health.SystemHealthManager;
130 //import android.os.image.DynamicSystemManager;
131 //import android.os.incremental.IncrementalManager;
132 import android.os.storage.StorageManager;
133 //import android.permission.LegacyPermissionManager;
134 //import android.permission.PermissionCheckerManager;
135 import android.permission.PermissionControllerManager;
136 import android.permission.PermissionManager;
137 import android.print.PrintManager;
138 import android.security.FileIntegrityManager;
139 //import android.security.attestationverification.AttestationVerificationManager;
140 import android.service.oemlock.OemLockManager;
141 import android.service.persistentdata.PersistentDataBlockManager;
142 import android.telecom.TelecomManager;
143 //import android.telephony.MmsManager;
144 //import android.telephony.TelephonyRegistryManager;
145 //import android.transparency.BinaryTransparencyManager;
146 import android.view.LayoutInflater;
147 import android.view.WindowManager;
148 import android.view.accessibility.AccessibilityManager;
149 import android.view.accessibility.CaptioningManager;
150 //import android.view.autofill.AutofillManager;
151 //import android.view.contentcapture.ContentCaptureManager;
152 import android.view.displayhash.DisplayHashManager;
153 import android.view.inputmethod.InputMethodManager;
154 //import android.view.selectiontoolbar.SelectionToolbarManager;
155 import android.view.textclassifier.TextClassificationManager;
156 import android.view.textservice.TextServicesManager;
157 import android.view.translation.TranslationManager;
158 import android.view.translation.UiTranslationManager;
159 
160 import com.android.bedstead.nene.TestApis;
161 import com.android.bedstead.nene.annotations.Experimental;
162 import com.android.bedstead.nene.exceptions.NeneException;
163 import com.android.bedstead.nene.utils.ShellCommand;
164 
165 import java.util.HashMap;
166 import java.util.Map;
167 import java.util.stream.Collectors;
168 
169 /**
170  * TestApis related to system services.
171  */
172 @Experimental
173 public final class Services {
174 
175     public static final Services sInstance = new Services();
176 
177     // Mapping from SystemServiceRegistry.java
178     @SuppressLint("NewApi")
179     private static final Map<String, Class<?>> sServiceMapping =
180             Map.ofEntries(
181                     entry(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class),
182                     entry(Context.CAPTIONING_SERVICE, CaptioningManager.class),
183                     entry(Context.ACCOUNT_SERVICE, AccountManager.class),
184                     entry(Context.ACTIVITY_SERVICE, ActivityManager.class),
185 //        entry(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class),
186 //        entry(Context.URI_GRANTS_SERVICE, UriGrantsManager.class),
187                     entry(Context.ALARM_SERVICE, AlarmManager.class),
188                     entry(Context.AUDIO_SERVICE, AudioManager.class),
189                     entry(Context.AUDIO_DEVICE_VOLUME_SERVICE, AudioDeviceVolumeManager.class),
190                     entry(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class),
191                     entry(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class),
192                     entry(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class),
193 //        entry(Context.SELECTION_TOOLBAR_SERVICE, SelectionToolbarManager.class),
194                     entry(Context.FONT_SERVICE, FontManager.class),
195                     entry(Context.CLIPBOARD_SERVICE, ClipboardManager.class),
196 //        entry(Context.PAC_PROXY_SERVICE, PacProxyManager.class),
197                     entry(Context.NETD_SERVICE, IBinder.class),
198                     entry(Context.TETHERING_SERVICE, TetheringManager.class),
199                     entry(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class),
200 //        entry(Context.VCN_MANAGEMENT_SERVICE, VcnManager.class),
201 //        entry(Context.COUNTRY_DETECTOR, CountryDetector.class),
202                     entry(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class),
203                     entry(Context.DOWNLOAD_SERVICE, DownloadManager.class),
204                     entry(Context.BATTERY_SERVICE, BatteryManager.class),
205                     entry(Context.DROPBOX_SERVICE, DropBoxManager.class),
206 //        entry(Context.BINARY_TRANSPARENCY_SERVICE, BinaryTransparencyManager.class),
207                     entry(Context.INPUT_SERVICE, InputManager.class),
208                     entry(Context.DISPLAY_SERVICE, DisplayManager.class),
209 //        entry(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class),
210                     entry(Context.INPUT_METHOD_SERVICE, InputMethodManager.class),
211                     entry(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class),
212                     entry(Context.KEYGUARD_SERVICE, KeyguardManager.class),
213                     entry(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class),
214                     entry(Context.LOCATION_SERVICE, LocationManager.class),
215 //        entry(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class),
216                     entry(Context.NOTIFICATION_SERVICE, NotificationManager.class),
217                     entry(Context.PEOPLE_SERVICE, PeopleManager.class),
218                     entry(Context.POWER_SERVICE, PowerManager.class),
219                     entry(Context.PERFORMANCE_HINT_SERVICE, PerformanceHintManager.class),
220 //        entry(Context.RECOVERY_SERVICE, RecoverySystem.class),
221                     entry(Context.SEARCH_SERVICE, SearchManager.class),
222                     entry(Context.SENSOR_SERVICE, SensorManager.class),
223 //        entry(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class),
224                     entry(Context.STATUS_BAR_SERVICE, StatusBarManager.class),
225                     entry(Context.STORAGE_SERVICE, StorageManager.class),
226                     entry(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class),
227                     entry(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class),
228                     entry(Context.SYSTEM_CONFIG_SERVICE, SystemConfigManager.class),
229 //        entry(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class),
230                     entry(Context.TELECOM_SERVICE, TelecomManager.class),
231 //        entry(Context.MMS_SERVICE, MmsManager.class),
232                     entry(Context.UI_MODE_SERVICE, UiModeManager.class),
233                     entry(Context.USB_SERVICE, UsbManager.class),
234 //        entry(Context.ADB_SERVICE, AdbManager.class),
235 //        entry(Context.SERIAL_SERVICE, SerialManager.class),
236                     entry(Context.VIBRATOR_MANAGER_SERVICE, VibratorManager.class),
237                     entry(Context.VIBRATOR_SERVICE, Vibrator.class),
238                     entry(Context.WALLPAPER_SERVICE, WallpaperManager.class),
239                     entry(Context.WIFI_NL80211_SERVICE, WifiNl80211Manager.class),
240                     entry(Context.WINDOW_SERVICE, WindowManager.class),
241                     entry(Context.USER_SERVICE, UserManager.class),
242                     entry(Context.APP_OPS_SERVICE, AppOpsManager.class),
243                     entry(Context.CAMERA_SERVICE, CameraManager.class),
244                     entry(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class),
245                     entry(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class),
246                     entry(Context.PRINT_SERVICE, PrintManager.class),
247                     entry(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class),
248                     entry(Context.VIRTUAL_DEVICE_SERVICE, VirtualDeviceManager.class),
249                     entry(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class),
250 //        entry(Context.TRUST_SERVICE, TrustManager.class),
251 //                    entry(Context.FINGERPRINT_SERVICE, FingerprintManager.class),
252 //        entry(Context.FACE_SERVICE, FaceManager.class),
253 //        entry(Context.IRIS_SERVICE, IrisManager.class),
254                     entry(Context.BIOMETRIC_SERVICE, BiometricManager.class),
255                     entry(Context.TV_INTERACTIVE_APP_SERVICE, TvInteractiveAppManager.class),
256                     entry(Context.TV_INPUT_SERVICE, TvInputManager.class),
257 //        entry(Context.TV_TUNER_RESOURCE_MGR_SERVICE, TunerResourceManager.class),
258                     entry(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class),
259                     entry(Context.USAGE_STATS_SERVICE, UsageStatsManager.class),
260                     entry(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class),
261                     entry(Context.OEM_LOCK_SERVICE, OemLockManager.class),
262                     entry(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class),
263                     entry(Context.APPWIDGET_SERVICE, AppWidgetManager.class),
264                     entry(Context.MIDI_SERVICE, MidiManager.class),
265 //        entry(Context.RADIO_SERVICE, RadioManager.class),
266                     entry(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class),
267 //        entry(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class),
268                     entry(Context.SHORTCUT_SERVICE, ShortcutManager.class),
269                     entry(Context.OVERLAY_SERVICE, OverlayManager.class),
270 //        entry(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class),
271                     entry(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class),
272                     entry(Context.CONTEXTHUB_SERVICE, ContextHubManager.class),
273 //        entry(Context.INCIDENT_SERVICE, IncidentManager.class),
274                     entry(Context.BUGREPORT_SERVICE, BugreportManager.class),
275 //        entry(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class),
276                     entry(Context.CREDENTIAL_SERVICE, CredentialManager.class),
277                     entry(Context.MUSIC_RECOGNITION_SERVICE, MusicRecognitionManager.class),
278 //                    entry(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class),
279                     entry(Context.TRANSLATION_MANAGER_SERVICE, TranslationManager.class),
280                     entry(Context.UI_TRANSLATION_SERVICE, UiTranslationManager.class),
281                     entry(Context.SEARCH_UI_SERVICE, SearchUiManager.class),
282                     entry(Context.SMARTSPACE_SERVICE, SmartspaceManager.class),
283                     entry(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class),
284                     entry(Context.VR_SERVICE, VrManager.class),
285                     entry(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class),
286 //        entry(Context.SLICE_SERVICE, SliceManager.class),
287 //        entry(Context.TIME_DETECTOR_SERVICE, TimeDetector.class),
288 //        entry(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class),
289                     entry(Context.TIME_MANAGER_SERVICE, TimeManager.class),
290                     entry(Context.PERMISSION_SERVICE, PermissionManager.class),
291 //        entry(Context.LEGACY_PERMISSION_SERVICE, LegacyPermissionManager.class),
292                     entry(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class),
293 //        entry(Context.PERMISSION_CHECKER_SERVICE, PermissionCheckerManager.class),
294 //        entry(Context.PERMISSION_ENFORCER_SERVICE, PermissionEnforcer.class),
295 //        entry(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class),
296                     entry(Context.BATTERY_STATS_SERVICE, BatteryStatsManager.class),
297 //        entry(Context.DATA_LOADER_MANAGER_SERVICE, DataLoaderManager.class),
298 //        entry(Context.LIGHTS_SERVICE, LightsManager.class),
299                     entry(Context.LOCALE_SERVICE, LocaleManager.class),
300 //        entry(Context.INCREMENTAL_SERVICE, IncrementalManager.class),
301                     entry(Context.FILE_INTEGRITY_SERVICE, FileIntegrityManager.class),
302                     entry(Context.APP_INTEGRITY_SERVICE, AppIntegrityManager.class),
303                     entry(Context.APP_HIBERNATION_SERVICE, AppHibernationManager.class),
304 //                    entry(Context.DREAM_SERVICE, DreamManager.class),
305 //        entry(Context.DEVICE_STATE_SERVICE, DeviceStateManager.class),
306                     entry(Context.MEDIA_METRICS_SERVICE, MediaMetricsManager.class),
307                     entry(Context.GAME_SERVICE, GameManager.class),
308                     entry(Context.DOMAIN_VERIFICATION_SERVICE, DomainVerificationManager.class),
309                     entry(Context.DISPLAY_HASH_SERVICE, DisplayHashManager.class),
310                     entry(Context.AMBIENT_CONTEXT_SERVICE, AmbientContextManager.class),
311                     entry(Context.WEARABLE_SENSING_SERVICE, WearableSensingManager.class),
312                     entry(Context.GRAMMATICAL_INFLECTION_SERVICE, GrammaticalInflectionManager.class),
313                     entry(Context.SHARED_CONNECTIVITY_SERVICE, SharedConnectivityManager.class),
314                     entry(Context.CONTENT_SUGGESTIONS_SERVICE, ContentSuggestionsManager.class),
315                     entry(Context.WALLPAPER_EFFECTS_GENERATION_SERVICE,
316                             WallpaperEffectsGenerationManager.class)
317 //        entry(Context.ATTESTATION_VERIFICATION_SERVICE,
318 //                AttestationVerificationManager.class),
319             );
320     private static final Map<Class<?>, String> sServiceNameMapping =
321             sServiceMapping.entrySet().stream().collect(
322                     Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
323 
324 
Services()325     private Services() {
326 
327     }
328 
serviceIsAvailable(String service)329     public boolean serviceIsAvailable(String service) {
330         if (!sServiceMapping.containsKey(service)) {
331             throw new NeneException("Unknown service " + service + ". Check Nene Services map");
332         }
333         return serviceIsAvailable(service, sServiceMapping.get(service));
334     }
335 
serviceIsAvailable(Class<?> serviceClass)336     public boolean serviceIsAvailable(Class<?> serviceClass) {
337         if (!sServiceNameMapping.containsKey(serviceClass)) {
338             throw new NeneException("Unknown service " + serviceClass + ". Check Nene Services map");
339         }
340         return serviceIsAvailable(sServiceNameMapping.get(serviceClass), serviceClass);
341     }
342 
serviceIsAvailable(String service, Class<?> serviceClass)343     private boolean serviceIsAvailable(String service, Class<?> serviceClass) {
344         if (TestApis.context().instrumentedContext().getSystemService(serviceClass) == null) {
345             return false;
346         }
347 
348         return ShellCommand.builder("cmd -l")
349                 .executeOrThrowNeneException("Error getting service list")
350                 .contains(service);
351 
352     }
353 
354 }
355