• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.SuppressLint;
22 import android.content.Context;
23 
24 import com.android.bedstead.nene.TestApis;
25 import com.android.bedstead.nene.annotations.Experimental;
26 import com.android.bedstead.nene.exceptions.NeneException;
27 import com.android.bedstead.nene.utils.ShellCommand;
28 
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 
32 /**
33  * TestApis related to system services.
34  */
35 @Experimental
36 public final class Services {
37 
38     public static final Services sInstance = new Services();
39 
40     // Mapping from SystemServiceRegistry.java
41     @SuppressLint("NewApi")
42     private static final Map<String, String> sServiceMapping =
43             Map.ofEntries(
44                     entry(Context.ACCESSIBILITY_SERVICE, "android.view.accessibility.AccessibilityManager"),
45                     entry(Context.CAPTIONING_SERVICE, "android.view.accessibility.CaptioningManager"),
46                     entry(Context.ACCOUNT_SERVICE, "android.accounts.AccountManager"),
47                     entry(Context.ACTIVITY_SERVICE, "android.app.ActivityManager"),
48                     entry(Context.ALARM_SERVICE, "android.app.AlarmManager"),
49                     entry(Context.AUDIO_SERVICE, "android.media.AudioManager"),
50                     entry(Context.AUDIO_DEVICE_VOLUME_SERVICE, "android.media.AudioDeviceVolumeManager"),
51                     entry(Context.MEDIA_ROUTER_SERVICE, "android.media.MediaRouter"),
52                     entry(Context.HDMI_CONTROL_SERVICE, "android.hardware.hdmi.HdmiControlManager"),
53                     entry(Context.TEXT_CLASSIFICATION_SERVICE, "android.view.textclassifier.TextClassificationManager"),
54                     entry(Context.FONT_SERVICE, "android.graphic.fonts.FontManager"),
55                     entry(Context.CLIPBOARD_SERVICE, "android.content.ClipboardManager"),
56                     entry(Context.NETD_SERVICE, "android.os.IBinder"),
57                     entry(Context.TETHERING_SERVICE, "android.net.TetheringManager"),
58                     entry(Context.VPN_MANAGEMENT_SERVICE, "android.net.VpnManager"),
59                     entry(Context.DEVICE_POLICY_SERVICE, "android.app.admin.DevicePolicyManager"),
60                     entry(Context.DOWNLOAD_SERVICE, "android.app.DownloadManager"),
61                     entry(Context.BATTERY_SERVICE, "android.os.BatteryManager"),
62                     entry(Context.DROPBOX_SERVICE, "android.os.DropBoxManager"),
63                     entry(Context.INPUT_SERVICE, "android.hardware.input.InputManager"),
64                     entry(Context.DISPLAY_SERVICE, "android.hardware.display.DisplayManager"),
65                     entry(Context.INPUT_METHOD_SERVICE, "android.view.inputmethod.InputMethodManager"),
66                     entry(Context.TEXT_SERVICES_MANAGER_SERVICE, "android.view.textservice.TextServicesManager"),
67                     entry(Context.KEYGUARD_SERVICE, "android.app.KeyguardManager"),
68                     entry(Context.LAYOUT_INFLATER_SERVICE, "android.view.LayoutInflater"),
69                     entry(Context.LOCATION_SERVICE, "android.location.LocationManager"),
70                     entry(Context.NOTIFICATION_SERVICE, "android.app.NotificationManager"),
71                     entry(Context.PEOPLE_SERVICE, "android.app.people.PeopleManager"),
72                     entry(Context.POWER_SERVICE, "android.os.PowerManager"),
73                     entry(Context.PERFORMANCE_HINT_SERVICE, "android.os.PerformanceHintManager"),
74                     entry(Context.SEARCH_SERVICE, "android.app.SearchManager"),
75                     entry(Context.SENSOR_SERVICE, "android.hardware.SensorManager"),
76                     entry(Context.STATUS_BAR_SERVICE, "android.app.StatusBarManager"),
77                     entry(Context.STORAGE_SERVICE, "android.os.storage.StorageManager"),
78                     entry(Context.STORAGE_STATS_SERVICE, "android.app.usage.StorageStatsManager"),
79                     entry(Context.SYSTEM_UPDATE_SERVICE, "android.os.SystemUpdateManager"),
80                     entry(Context.SYSTEM_CONFIG_SERVICE, "android.os.SystemConfigManager"),
81                     entry(Context.TELECOM_SERVICE, "android.telecom.TelecomManager"),
82                     entry(Context.UI_MODE_SERVICE, "android.app.UiModeManager"),
83                     entry(Context.USB_SERVICE, "android.hardware.usb.UsbManager"),
84                     entry(Context.VIBRATOR_MANAGER_SERVICE, "android.os.VibratorManager"),
85                     entry(Context.VIBRATOR_SERVICE, "android.os.Vibrator"),
86                     entry(Context.WALLPAPER_SERVICE, "android.app.WallpaperManager"),
87                     entry(Context.WIFI_NL80211_SERVICE, "android.net.wifi.nl80211.WifiNl80211Manager"),
88                     entry(Context.WINDOW_SERVICE, "android.view.WindowManager"),
89                     entry(Context.USER_SERVICE, "android.os.UserManager"),
90                     entry(Context.APP_OPS_SERVICE, "android.app.AppOpsManager"),
91                     entry(Context.CAMERA_SERVICE, "android.hardware.camera2.CameraManager"),
92                     entry(Context.LAUNCHER_APPS_SERVICE, "android.content.pm.LauncherApps"),
93                     entry(Context.RESTRICTIONS_SERVICE, "android.content.RestrictionsManager"),
94                     entry(Context.PRINT_SERVICE, "android.print.PrintManager"),
95                     entry(Context.COMPANION_DEVICE_SERVICE, "android.companion.CompanionDeviceManager"),
96                     entry(Context.VIRTUAL_DEVICE_SERVICE, "android.companion.virtual.VirtualDeviceManager"),
97                     entry(Context.CONSUMER_IR_SERVICE, "android.hardware.ConsumerIrManager"),
98                     entry(Context.FINGERPRINT_SERVICE, "android.hardware.fingerprint.FingerprintManager"),
99                     entry(Context.BIOMETRIC_SERVICE, "android.hardware.biometrics.BiometricManager"),
100                     entry(Context.TV_INTERACTIVE_APP_SERVICE, "android.media.tv.interactive.TvInteractiveAppManager"),
101                     entry(Context.TV_INPUT_SERVICE, "android.media.tv.TvInputManager"),
102                     entry(Context.NETWORK_SCORE_SERVICE, "android.net.NetworkScoreManager"),
103                     entry(Context.USAGE_STATS_SERVICE, "android.app.usage.UsageStatsManager"),
104                     entry(Context.PERSISTENT_DATA_BLOCK_SERVICE, "android.service.persistentdata.PersistentDataBlockManager"),
105                     entry(Context.OEM_LOCK_SERVICE, "android.service.oemlock.OemLockManager"),
106                     entry(Context.MEDIA_PROJECTION_SERVICE, "android.media.projection.MediaProjectionManager"),
107                     entry(Context.APPWIDGET_SERVICE, "android.appwidget.AppWidgetManager"),
108                     entry(Context.MIDI_SERVICE, "android.media.midi.MidiManager"),
109                     entry(Context.HARDWARE_PROPERTIES_SERVICE, "android.os.HardwarePropertiesManager"),
110                     entry(Context.SHORTCUT_SERVICE, "android.content.pm.ShortcutManager"),
111                     entry(Context.OVERLAY_SERVICE, "android.content.om.OverlayManager"),
112                     entry(Context.SYSTEM_HEALTH_SERVICE, "android.os.health.SystemHealthManager"),
113                     entry(Context.CONTEXTHUB_SERVICE, "android.hardware.location.ContextHubManager"),
114                     entry(Context.BUGREPORT_SERVICE, "android.os.BugreportManager"),
115                     entry(Context.CREDENTIAL_SERVICE, "android.credentials.CredentialManager"),
116                     entry(Context.MUSIC_RECOGNITION_SERVICE, "android.media.musicrecognition.MusicRecognitionManager"),
117                     // TODO: b/400899590 - Make service present in G3
118                     //entry(Context.CONTENT_CAPTURE_MANAGER_SERVICE, "android.view.contentcapture.ContentCaptureManager"),
119                     entry(Context.TRANSLATION_MANAGER_SERVICE, "android.view.translation.TranslationManager"),
120                     entry(Context.UI_TRANSLATION_SERVICE, "android.view.translation.UiTranslationManager"),
121                     entry(Context.SEARCH_UI_SERVICE, "android.app.search.SearchUiManager"),
122                     entry(Context.SMARTSPACE_SERVICE, "android.app.smartspace.SmartspaceManager"),
123                     entry(Context.APP_PREDICTION_SERVICE, "android.app.prediction.AppPredictionManager"),
124                     entry(Context.VR_SERVICE, "android.app.VrManager"),
125                     entry(Context.CROSS_PROFILE_APPS_SERVICE, "android.content.pm.CrossProfileApps"),
126                     entry(Context.TIME_MANAGER_SERVICE, "android.app.time.TimeManager"),
127                     entry(Context.PERMISSION_SERVICE, "android.permission.PermissionManager"),
128                     entry(Context.PERMISSION_CONTROLLER_SERVICE, "android.permission.PermissionControllerManager"),
129                     entry(Context.BATTERY_STATS_SERVICE, "android.os.BatteryStatsManager"),
130                     entry(Context.LOCALE_SERVICE, "android.app.LocaleManager"),
131                     entry(Context.FILE_INTEGRITY_SERVICE, "android.security.FileIntegrityManager"),
132                     entry(Context.APP_INTEGRITY_SERVICE, "android.content.integrity.AppIntegrityManager"),
133                     entry(Context.APP_HIBERNATION_SERVICE, "android.apphibernation.AppHibernationManager"),
134                     // TODO: b/400899590 - Make service present in G3
135                     //entry(Context.DREAM_SERVICE, "android.app.DreamManager"),
136                     entry(Context.MEDIA_METRICS_SERVICE, "android.media.metrics.MediaMetricsManager"),
137                     entry(Context.GAME_SERVICE, "android.app.GameManager"),
138                     entry(Context.DOMAIN_VERIFICATION_SERVICE, "android.content.pm.verify.domain.DomainVerificationManager"),
139                     entry(Context.DISPLAY_HASH_SERVICE, "android.view.displayhash.DisplayHashManager"),
140                     entry(Context.AMBIENT_CONTEXT_SERVICE, "android.app.ambientcontext.AmbientContextManager"),
141                     entry(Context.WEARABLE_SENSING_SERVICE, "android.app.wearable.WearableSensingManager"),
142                     entry(Context.GRAMMATICAL_INFLECTION_SERVICE, "android.app.GrammaticalInflectionManager"),
143                     entry(Context.SHARED_CONNECTIVITY_SERVICE, "android.net.wifi.sharedconnectivity.app.SharedConnectivityManager"),
144                     entry(Context.CONTENT_SUGGESTIONS_SERVICE, "android.app.contentsuggestions.ContentSuggestionsManager"),
145                     entry(Context.WALLPAPER_EFFECTS_GENERATION_SERVICE, "android.app.wallpapereffectsgeneration.WallpaperEffectsGenerationManager")
146             );
147 
148     private static final Map<String, String> sServiceNameMapping =
149             sServiceMapping.entrySet().stream().collect(
150                     Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
151 
Services()152     private Services() {
153 
154     }
155 
156     /** Checks if {@code service} is available. */
serviceIsAvailable(String service)157     public boolean serviceIsAvailable(String service) {
158         if (!sServiceMapping.containsKey(service)) {
159             throw new NeneException("Unknown service " + service + ". Check Nene Services map");
160         }
161         try {
162             return serviceIsAvailable(service, Class.forName(sServiceMapping.get(service)));
163         } catch (ClassNotFoundException e) {
164             throw new NeneException("Unable to get service " + service, e);
165         }
166     }
167 
168     /** Checks if {@code serviceClass} is available. */
serviceIsAvailable(Class<?> serviceClass)169     public boolean serviceIsAvailable(Class<?> serviceClass) {
170         String service = serviceClass.getName();
171         if (!sServiceNameMapping.containsKey(service)) {
172             throw new NeneException("Unknown service " + serviceClass + ". Check Nene Services map");
173         }
174         return serviceIsAvailable(sServiceNameMapping.get(service), serviceClass);
175     }
176 
serviceIsAvailable(String service, Class<?> serviceClass)177     private boolean serviceIsAvailable(String service, Class<?> serviceClass) {
178         if (TestApis.context().instrumentedContext().getSystemService(serviceClass) == null) {
179             return false;
180         }
181 
182         return ShellCommand.builder("cmd -l")
183                 .executeOrThrowNeneException("Error getting service list")
184                 .contains(service);
185 
186     }
187 
188 }
189