425 lines
15 KiB
Java
425 lines
15 KiB
Java
package androidx.core.content;
|
|
|
|
import android.accounts.AccountManager;
|
|
import android.app.ActivityManager;
|
|
import android.app.AlarmManager;
|
|
import android.app.AppOpsManager;
|
|
import android.app.DownloadManager;
|
|
import android.app.KeyguardManager;
|
|
import android.app.NotificationManager;
|
|
import android.app.SearchManager;
|
|
import android.app.UiModeManager;
|
|
import android.app.WallpaperManager;
|
|
import android.app.admin.DevicePolicyManager;
|
|
import android.app.job.JobScheduler;
|
|
import android.app.usage.UsageStatsManager;
|
|
import android.appwidget.AppWidgetManager;
|
|
import android.bluetooth.BluetoothManager;
|
|
import android.content.ClipboardManager;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.RestrictionsManager;
|
|
import android.content.pm.LauncherApps;
|
|
import android.content.res.ColorStateList;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.hardware.ConsumerIrManager;
|
|
import android.hardware.SensorManager;
|
|
import android.hardware.camera2.CameraManager;
|
|
import android.hardware.display.DisplayManager;
|
|
import android.hardware.input.InputManager;
|
|
import android.hardware.usb.UsbManager;
|
|
import android.location.LocationManager;
|
|
import android.media.AudioManager;
|
|
import android.media.MediaRouter;
|
|
import android.media.projection.MediaProjectionManager;
|
|
import android.media.session.MediaSessionManager;
|
|
import android.media.tv.TvInputManager;
|
|
import android.net.ConnectivityManager;
|
|
import android.net.nsd.NsdManager;
|
|
import android.net.wifi.WifiManager;
|
|
import android.net.wifi.p2p.WifiP2pManager;
|
|
import android.nfc.NfcManager;
|
|
import android.os.BatteryManager;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.DropBoxManager;
|
|
import android.os.Handler;
|
|
import android.os.PowerManager;
|
|
import android.os.Process;
|
|
import android.os.UserManager;
|
|
import android.os.Vibrator;
|
|
import android.os.storage.StorageManager;
|
|
import android.print.PrintManager;
|
|
import android.telecom.TelecomManager;
|
|
import android.telephony.SubscriptionManager;
|
|
import android.telephony.TelephonyManager;
|
|
import android.util.Log;
|
|
import android.util.TypedValue;
|
|
import android.view.LayoutInflater;
|
|
import android.view.WindowManager;
|
|
import android.view.accessibility.AccessibilityManager;
|
|
import android.view.accessibility.CaptioningManager;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.view.textservice.TextServicesManager;
|
|
import androidx.annotation.ColorInt;
|
|
import androidx.annotation.ColorRes;
|
|
import androidx.annotation.DoNotInline;
|
|
import androidx.annotation.DrawableRes;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RequiresApi;
|
|
import androidx.appcompat.widget.ActivityChooserModel;
|
|
import androidx.core.app.NotificationCompat;
|
|
import androidx.core.content.res.ResourcesCompat;
|
|
import androidx.core.os.ExecutorCompat;
|
|
import com.discord.utilities.analytics.ChatInputComponentTypes;
|
|
import java.io.File;
|
|
import java.util.HashMap;
|
|
import java.util.concurrent.Executor;
|
|
import org.webrtc.MediaStreamTrack;
|
|
public class ContextCompat {
|
|
private static final String TAG = "ContextCompat";
|
|
private static final Object sLock = new Object();
|
|
private static final Object sSync = new Object();
|
|
private static TypedValue sTempValue;
|
|
|
|
@RequiresApi(16)
|
|
public static class Api16Impl {
|
|
private Api16Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static void startActivities(Context context, Intent[] intentArr, Bundle bundle) {
|
|
context.startActivities(intentArr, bundle);
|
|
}
|
|
|
|
@DoNotInline
|
|
public static void startActivity(Context context, Intent intent, Bundle bundle) {
|
|
context.startActivity(intent, bundle);
|
|
}
|
|
}
|
|
|
|
@RequiresApi(19)
|
|
public static class Api19Impl {
|
|
private Api19Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static File[] getExternalCacheDirs(Context context) {
|
|
return context.getExternalCacheDirs();
|
|
}
|
|
|
|
@DoNotInline
|
|
public static File[] getExternalFilesDirs(Context context, String str) {
|
|
return context.getExternalFilesDirs(str);
|
|
}
|
|
|
|
@DoNotInline
|
|
public static File[] getObbDirs(Context context) {
|
|
return context.getObbDirs();
|
|
}
|
|
}
|
|
|
|
@RequiresApi(21)
|
|
public static class Api21Impl {
|
|
private Api21Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static File getCodeCacheDir(Context context) {
|
|
return context.getCodeCacheDir();
|
|
}
|
|
|
|
@DoNotInline
|
|
public static Drawable getDrawable(Context context, int i) {
|
|
return context.getDrawable(i);
|
|
}
|
|
|
|
@DoNotInline
|
|
public static File getNoBackupFilesDir(Context context) {
|
|
return context.getNoBackupFilesDir();
|
|
}
|
|
}
|
|
|
|
@RequiresApi(23)
|
|
public static class Api23Impl {
|
|
private Api23Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static int getColor(Context context, int i) {
|
|
return context.getColor(i);
|
|
}
|
|
|
|
@DoNotInline
|
|
public static ColorStateList getColorStateList(Context context, int i) {
|
|
return context.getColorStateList(i);
|
|
}
|
|
|
|
@DoNotInline
|
|
public static <T> T getSystemService(Context context, Class<T> cls) {
|
|
return (T) context.getSystemService(cls);
|
|
}
|
|
|
|
@DoNotInline
|
|
public static String getSystemServiceName(Context context, Class<?> cls) {
|
|
return context.getSystemServiceName(cls);
|
|
}
|
|
}
|
|
|
|
@RequiresApi(24)
|
|
public static class Api24Impl {
|
|
private Api24Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static Context createDeviceProtectedStorageContext(Context context) {
|
|
return context.createDeviceProtectedStorageContext();
|
|
}
|
|
|
|
@DoNotInline
|
|
public static File getDataDir(Context context) {
|
|
return context.getDataDir();
|
|
}
|
|
|
|
@DoNotInline
|
|
public static boolean isDeviceProtectedStorage(Context context) {
|
|
return context.isDeviceProtectedStorage();
|
|
}
|
|
}
|
|
|
|
@RequiresApi(26)
|
|
public static class Api26Impl {
|
|
private Api26Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static ComponentName startForegroundService(Context context, Intent intent) {
|
|
return context.startForegroundService(intent);
|
|
}
|
|
}
|
|
|
|
@RequiresApi(28)
|
|
public static class Api28Impl {
|
|
private Api28Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static Executor getMainExecutor(Context context) {
|
|
return context.getMainExecutor();
|
|
}
|
|
}
|
|
|
|
@RequiresApi(30)
|
|
public static class Api30Impl {
|
|
private Api30Impl() {
|
|
}
|
|
|
|
@DoNotInline
|
|
public static String getAttributionTag(Context context) {
|
|
return context.getAttributionTag();
|
|
}
|
|
}
|
|
|
|
public static final class LegacyServiceMapHolder {
|
|
public static final HashMap<Class<?>, String> SERVICES;
|
|
|
|
static {
|
|
HashMap<Class<?>, String> hashMap = new HashMap<>();
|
|
SERVICES = hashMap;
|
|
if (Build.VERSION.SDK_INT >= 22) {
|
|
hashMap.put(SubscriptionManager.class, "telephony_subscription_service");
|
|
hashMap.put(UsageStatsManager.class, "usagestats");
|
|
}
|
|
hashMap.put(AppWidgetManager.class, "appwidget");
|
|
hashMap.put(BatteryManager.class, "batterymanager");
|
|
hashMap.put(CameraManager.class, ChatInputComponentTypes.CAMERA);
|
|
hashMap.put(JobScheduler.class, "jobscheduler");
|
|
hashMap.put(LauncherApps.class, "launcherapps");
|
|
hashMap.put(MediaProjectionManager.class, "media_projection");
|
|
hashMap.put(MediaSessionManager.class, "media_session");
|
|
hashMap.put(RestrictionsManager.class, "restrictions");
|
|
hashMap.put(TelecomManager.class, "telecom");
|
|
hashMap.put(TvInputManager.class, "tv_input");
|
|
hashMap.put(AppOpsManager.class, "appops");
|
|
hashMap.put(CaptioningManager.class, "captioning");
|
|
hashMap.put(ConsumerIrManager.class, "consumer_ir");
|
|
hashMap.put(PrintManager.class, "print");
|
|
hashMap.put(BluetoothManager.class, "bluetooth");
|
|
hashMap.put(DisplayManager.class, "display");
|
|
hashMap.put(UserManager.class, "user");
|
|
hashMap.put(InputManager.class, "input");
|
|
hashMap.put(MediaRouter.class, "media_router");
|
|
hashMap.put(NsdManager.class, "servicediscovery");
|
|
hashMap.put(AccessibilityManager.class, "accessibility");
|
|
hashMap.put(AccountManager.class, "account");
|
|
hashMap.put(ActivityManager.class, ActivityChooserModel.ATTRIBUTE_ACTIVITY);
|
|
hashMap.put(AlarmManager.class, NotificationCompat.CATEGORY_ALARM);
|
|
hashMap.put(AudioManager.class, MediaStreamTrack.AUDIO_TRACK_KIND);
|
|
hashMap.put(ClipboardManager.class, "clipboard");
|
|
hashMap.put(ConnectivityManager.class, "connectivity");
|
|
hashMap.put(DevicePolicyManager.class, "device_policy");
|
|
hashMap.put(DownloadManager.class, "download");
|
|
hashMap.put(DropBoxManager.class, "dropbox");
|
|
hashMap.put(InputMethodManager.class, "input_method");
|
|
hashMap.put(KeyguardManager.class, "keyguard");
|
|
hashMap.put(LayoutInflater.class, "layout_inflater");
|
|
hashMap.put(LocationManager.class, "location");
|
|
hashMap.put(NfcManager.class, "nfc");
|
|
hashMap.put(NotificationManager.class, "notification");
|
|
hashMap.put(PowerManager.class, "power");
|
|
hashMap.put(SearchManager.class, "search");
|
|
hashMap.put(SensorManager.class, "sensor");
|
|
hashMap.put(StorageManager.class, "storage");
|
|
hashMap.put(TelephonyManager.class, "phone");
|
|
hashMap.put(TextServicesManager.class, "textservices");
|
|
hashMap.put(UiModeManager.class, "uimode");
|
|
hashMap.put(UsbManager.class, "usb");
|
|
hashMap.put(Vibrator.class, "vibrator");
|
|
hashMap.put(WallpaperManager.class, "wallpaper");
|
|
hashMap.put(WifiP2pManager.class, "wifip2p");
|
|
hashMap.put(WifiManager.class, "wifi");
|
|
hashMap.put(WindowManager.class, "window");
|
|
}
|
|
|
|
private LegacyServiceMapHolder() {
|
|
}
|
|
}
|
|
|
|
public static int checkSelfPermission(@NonNull Context context, @NonNull String str) {
|
|
if (str != null) {
|
|
return context.checkPermission(str, Process.myPid(), Process.myUid());
|
|
}
|
|
throw new IllegalArgumentException("permission is null");
|
|
}
|
|
|
|
@Nullable
|
|
public static Context createDeviceProtectedStorageContext(@NonNull Context context) {
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
return Api24Impl.createDeviceProtectedStorageContext(context);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static File createFilesDir(File file) {
|
|
synchronized (sSync) {
|
|
if (!file.exists()) {
|
|
if (file.mkdirs()) {
|
|
return file;
|
|
}
|
|
Log.w(TAG, "Unable to create files subdir " + file.getPath());
|
|
}
|
|
return file;
|
|
}
|
|
}
|
|
|
|
@Nullable
|
|
public static String getAttributionTag(@NonNull Context context) {
|
|
if (Build.VERSION.SDK_INT >= 30) {
|
|
return Api30Impl.getAttributionTag(context);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static File getCodeCacheDir(@NonNull Context context) {
|
|
return Api21Impl.getCodeCacheDir(context);
|
|
}
|
|
|
|
@ColorInt
|
|
public static int getColor(@NonNull Context context, @ColorRes int i) {
|
|
return Build.VERSION.SDK_INT >= 23 ? Api23Impl.getColor(context, i) : context.getResources().getColor(i);
|
|
}
|
|
|
|
@Nullable
|
|
public static ColorStateList getColorStateList(@NonNull Context context, @ColorRes int i) {
|
|
return ResourcesCompat.getColorStateList(context.getResources(), i, context.getTheme());
|
|
}
|
|
|
|
@Nullable
|
|
public static File getDataDir(@NonNull Context context) {
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
return Api24Impl.getDataDir(context);
|
|
}
|
|
String str = context.getApplicationInfo().dataDir;
|
|
if (str != null) {
|
|
return new File(str);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
public static Drawable getDrawable(@NonNull Context context, @DrawableRes int i) {
|
|
return Api21Impl.getDrawable(context, i);
|
|
}
|
|
|
|
@NonNull
|
|
public static File[] getExternalCacheDirs(@NonNull Context context) {
|
|
return Api19Impl.getExternalCacheDirs(context);
|
|
}
|
|
|
|
@NonNull
|
|
public static File[] getExternalFilesDirs(@NonNull Context context, @Nullable String str) {
|
|
return Api19Impl.getExternalFilesDirs(context, str);
|
|
}
|
|
|
|
public static Executor getMainExecutor(Context context) {
|
|
return Build.VERSION.SDK_INT >= 28 ? Api28Impl.getMainExecutor(context) : ExecutorCompat.create(new Handler(context.getMainLooper()));
|
|
}
|
|
|
|
@Nullable
|
|
public static File getNoBackupFilesDir(@NonNull Context context) {
|
|
return Api21Impl.getNoBackupFilesDir(context);
|
|
}
|
|
|
|
@NonNull
|
|
public static File[] getObbDirs(@NonNull Context context) {
|
|
return Api19Impl.getObbDirs(context);
|
|
}
|
|
|
|
@Nullable
|
|
public static <T> T getSystemService(@NonNull Context context, @NonNull Class<T> cls) {
|
|
if (Build.VERSION.SDK_INT >= 23) {
|
|
return (T) Api23Impl.getSystemService(context, cls);
|
|
}
|
|
String systemServiceName = getSystemServiceName(context, cls);
|
|
if (systemServiceName != null) {
|
|
return (T) context.getSystemService(systemServiceName);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
public static String getSystemServiceName(@NonNull Context context, @NonNull Class<?> cls) {
|
|
return Build.VERSION.SDK_INT >= 23 ? Api23Impl.getSystemServiceName(context, cls) : LegacyServiceMapHolder.SERVICES.get(cls);
|
|
}
|
|
|
|
public static boolean isDeviceProtectedStorage(@NonNull Context context) {
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
return Api24Impl.isDeviceProtectedStorage(context);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean startActivities(@NonNull Context context, @NonNull Intent[] intentArr) {
|
|
return startActivities(context, intentArr, null);
|
|
}
|
|
|
|
public static boolean startActivities(@NonNull Context context, @NonNull Intent[] intentArr, @Nullable Bundle bundle) {
|
|
Api16Impl.startActivities(context, intentArr, bundle);
|
|
return true;
|
|
}
|
|
|
|
public static void startActivity(@NonNull Context context, @NonNull Intent intent, @Nullable Bundle bundle) {
|
|
Api16Impl.startActivity(context, intent, bundle);
|
|
}
|
|
|
|
public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) {
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
Api26Impl.startForegroundService(context, intent);
|
|
} else {
|
|
context.startService(intent);
|
|
}
|
|
}
|
|
}
|