|
20 | 20 |
|
21 | 21 | import javax.lang.model.type.TypeMirror; |
22 | 22 |
|
| 23 | +import com.sun.codemodel.JCodeModel; |
| 24 | +import com.sun.codemodel.JFieldRef; |
| 25 | + |
23 | 26 | public class AndroidSystemServices { |
24 | 27 |
|
25 | | - private Map<String, String> registeredServices = new HashMap<String, String>(); |
26 | | - |
27 | | - public AndroidSystemServices() { |
28 | | - registeredServices.put("android.view.WindowManager", "android.content.Context.WINDOW_SERVICE"); |
29 | | - registeredServices.put("android.view.LayoutInflater", "android.content.Context.LAYOUT_INFLATER_SERVICE"); |
30 | | - registeredServices.put("android.app.ActivityManager", "android.content.Context.ACTIVITY_SERVICE"); |
31 | | - registeredServices.put("android.os.PowerManager", "android.content.Context.POWER_SERVICE"); |
32 | | - registeredServices.put("android.app.AlarmManager", "android.content.Context.ALARM_SERVICE"); |
33 | | - registeredServices.put("android.app.NotificationManager", "android.content.Context.NOTIFICATION_SERVICE"); |
34 | | - registeredServices.put("android.app.KeyguardManager", "android.content.Context.KEYGUARD_SERVICE"); |
35 | | - registeredServices.put("android.location.LocationManager", "android.content.Context.LOCATION_SERVICE"); |
36 | | - registeredServices.put("android.app.SearchManager", "android.content.Context.SEARCH_SERVICE"); |
37 | | - registeredServices.put("android.os.Vibrator", "android.content.Context.VIBRATOR_SERVICE"); |
38 | | - registeredServices.put("android.net.ConnectivityManager", "android.content.Context.CONNECTIVITY_SERVICE"); |
39 | | - registeredServices.put("android.net.wifi.WifiManager", "android.content.Context.WIFI_SERVICE"); |
40 | | - registeredServices.put("android.view.inputMethod.InputMethodManager", "android.content.Context.INPUT_METHOD_SERVICE"); |
41 | | - registeredServices.put("android.app.UiModeManager", "android.content.Context.UI_MODE_SERVICE"); |
42 | | - registeredServices.put("android.app.DownloadManager", "android.content.Context.DOWNLOAD_SERVICE"); |
43 | | - } |
44 | | - |
45 | | - public boolean contains(TypeMirror serviceType) { |
46 | | - return registeredServices.containsKey(serviceType.toString()); |
47 | | - } |
48 | | - |
49 | | - public String getServiceConstant(TypeMirror serviceType) { |
50 | | - return registeredServices.get(serviceType.toString()); |
51 | | - } |
| 28 | + private Map<String, String> registeredServices = new HashMap<String, String>(); |
| 29 | + |
| 30 | + public AndroidSystemServices() { |
| 31 | + registeredServices.put("android.view.WindowManager", "android.content.Context.WINDOW_SERVICE"); |
| 32 | + registeredServices.put("android.view.LayoutInflater", "android.content.Context.LAYOUT_INFLATER_SERVICE"); |
| 33 | + registeredServices.put("android.app.ActivityManager", "android.content.Context.ACTIVITY_SERVICE"); |
| 34 | + registeredServices.put("android.os.PowerManager", "android.content.Context.POWER_SERVICE"); |
| 35 | + registeredServices.put("android.app.AlarmManager", "android.content.Context.ALARM_SERVICE"); |
| 36 | + registeredServices.put("android.app.NotificationManager", "android.content.Context.NOTIFICATION_SERVICE"); |
| 37 | + registeredServices.put("android.app.KeyguardManager", "android.content.Context.KEYGUARD_SERVICE"); |
| 38 | + registeredServices.put("android.location.LocationManager", "android.content.Context.LOCATION_SERVICE"); |
| 39 | + registeredServices.put("android.app.SearchManager", "android.content.Context.SEARCH_SERVICE"); |
| 40 | + registeredServices.put("android.os.Vibrator", "android.content.Context.VIBRATOR_SERVICE"); |
| 41 | + registeredServices.put("android.net.ConnectivityManager", "android.content.Context.CONNECTIVITY_SERVICE"); |
| 42 | + registeredServices.put("android.net.wifi.WifiManager", "android.content.Context.WIFI_SERVICE"); |
| 43 | + registeredServices.put("android.view.inputMethod.InputMethodManager", "android.content.Context.INPUT_METHOD_SERVICE"); |
| 44 | + registeredServices.put("android.app.UiModeManager", "android.content.Context.UI_MODE_SERVICE"); |
| 45 | + registeredServices.put("android.app.DownloadManager", "android.content.Context.DOWNLOAD_SERVICE"); |
| 46 | + } |
| 47 | + |
| 48 | + public boolean contains(TypeMirror serviceType) { |
| 49 | + return registeredServices.containsKey(serviceType.toString()); |
| 50 | + } |
| 51 | + |
| 52 | + public String getServiceConstant(TypeMirror serviceType) { |
| 53 | + return registeredServices.get(serviceType.toString()); |
| 54 | + } |
| 55 | + |
| 56 | + public JFieldRef getServiceConstant(TypeMirror serviceType, JCodeModel codeModel) { |
| 57 | + return extractIdStaticRef(codeModel, registeredServices.get(serviceType.toString())); |
| 58 | + } |
| 59 | + |
| 60 | + private JFieldRef extractIdStaticRef(JCodeModel codeModel, String staticFieldQualifiedName) { |
| 61 | + if (staticFieldQualifiedName != null) { |
| 62 | + int fieldSuffix = staticFieldQualifiedName.lastIndexOf('.'); |
| 63 | + String fieldName = staticFieldQualifiedName.substring(fieldSuffix + 1); |
| 64 | + String className = staticFieldQualifiedName.substring(0, fieldSuffix); |
| 65 | + |
| 66 | + return codeModel.ref(className).staticRef(fieldName); |
| 67 | + } else { |
| 68 | + return null; |
| 69 | + } |
| 70 | + } |
52 | 71 |
|
53 | 72 | } |
0 commit comments