Skip to content

Instantly share code, notes, and snippets.

@eduardb
Created August 25, 2015 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardb/ac1ca088ea1257298656 to your computer and use it in GitHub Desktop.
Save eduardb/ac1ca088ea1257298656 to your computer and use it in GitHub Desktop.
Helper class to conveniently get a handle to a system-level service by class, similar to android.content.Context#getSystemService(Class)} that was introduced in API level 23
package com.deveddy.contextcompat;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.DownloadManager;
import android.app.KeyguardManager;
import android.app.NotificationManager;
import android.app.SearchManager;
import android.app.UiModeManager;
import android.app.job.JobScheduler;
import android.content.Context;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.MediaRouter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
/**
* Helper class to conveniently get a handle to a system-level service by class, similar to
* {@link android.content.Context#getSystemService(Class)} that was introduced in API level 23
*/
public class ContextCompat {
private final Context mContext;
private ContextCompat(Context context) {
mContext = context;
}
/**
* Create a new instance of this helper class
*
* @param context The context to use for getting system-level services
*/
public static ContextCompat with(Context context) {
return new ContextCompat(context);
}
/**
* Return the handle to a system-level service by class.
* <p>
* Currently available classes are:
* {@link android.view.WindowManager}, {@link android.view.LayoutInflater},
* {@link android.app.ActivityManager}, {@link android.os.PowerManager},
* {@link android.app.AlarmManager}, {@link android.app.NotificationManager},
* {@link android.app.KeyguardManager}, {@link android.location.LocationManager},
* {@link android.app.SearchManager}, {@link android.os.Vibrator},
* {@link android.net.ConnectivityManager},
* {@link android.net.wifi.WifiManager},
* {@link android.media.AudioManager}, {@link android.media.MediaRouter},
* {@link android.telephony.TelephonyManager}, {@link android.telephony.SubscriptionManager},
* {@link android.view.inputmethod.InputMethodManager},
* {@link android.app.UiModeManager}, {@link android.app.DownloadManager},
* {@link android.os.BatteryManager}, {@link android.app.job.JobScheduler},
* {@link android.app.usage.NetworkStatsManager}.
* </p><p>
* Note: System services obtained via this API may be closely associated with
* the Context in which they are obtained from. In general, do not share the
* service objects between various different contexts (Activities, Applications,
* Services, Providers, etc.)
* </p>
*
* @param serviceClass The class of the desired service.
* @return The service or null if the class is not a supported system service.
*/
@SuppressWarnings("unchecked")
public final <T> T getSystemService(Class<T> serviceClass) {
if (Build.VERSION.SDK_INT >= 23) {
return mContext.getSystemService(serviceClass);
}
String serviceName = getSystemServiceName(serviceClass);
//noinspection ResourceType
return serviceName != null ? (T) mContext.getSystemService(serviceName) : null;
}
@SuppressLint("InlinedApi")
@Nullable
private String getSystemServiceName(Class<?> serviceClass) {
if (serviceClass == WindowManager.class) {
return Context.WINDOW_SERVICE;
}
if (serviceClass == LayoutInflater.class) {
return Context.LAYOUT_INFLATER_SERVICE;
}
if (serviceClass == ActivityManager.class) {
return Context.ACTIVITY_SERVICE;
}
if (serviceClass == PowerManager.class) {
return Context.POWER_SERVICE;
}
if (serviceClass == AlarmManager.class) {
return Context.ALARM_SERVICE;
}
if (serviceClass == NotificationManager.class) {
return Context.NOTIFICATION_SERVICE;
}
if (serviceClass == KeyguardManager.class) {
return Context.KEYGUARD_SERVICE;
}
if (serviceClass == LocationManager.class) {
return Context.LOCATION_SERVICE;
}
if (serviceClass == SearchManager.class) {
return Context.SEARCH_SERVICE;
}
if (serviceClass == Vibrator.class) {
return Context.VIBRATOR_SERVICE;
}
if (serviceClass == ConnectivityManager.class) {
return Context.CONNECTIVITY_SERVICE;
}
if (serviceClass == WifiManager.class) {
return Context.WIFI_SERVICE;
}
if (serviceClass == AudioManager.class) {
return Context.AUDIO_SERVICE;
}
if (serviceClass == MediaRouter.class) {
return Context.MEDIA_ROUTER_SERVICE;
}
if (serviceClass == TelephonyManager.class) {
return Context.TELEPHONY_SERVICE;
}
if (serviceClass == SubscriptionManager.class) {
return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
}
if (serviceClass == InputMethodManager.class) {
return Context.INPUT_METHOD_SERVICE;
}
if (serviceClass == UiModeManager.class) {
return Context.UI_MODE_SERVICE;
}
if (serviceClass == DownloadManager.class) {
return Context.DOWNLOAD_SERVICE;
}
if (serviceClass == BatteryManager.class) {
return Context.BATTERY_SERVICE;
}
if (serviceClass == JobScheduler.class) {
return Context.JOB_SCHEDULER_SERVICE;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment