That fuck shit the fascists are using
at master 120 lines 3.9 kB view raw
1package org.tm.archive.util; 2 3import android.app.ActivityManager; 4import android.app.ActivityManager.MemoryInfo; 5import android.content.Context; 6import android.net.ConnectivityManager; 7import android.os.Build; 8 9import androidx.annotation.NonNull; 10import androidx.annotation.RequiresApi; 11 12import org.signal.core.util.logging.Log; 13 14/** 15 * Easy access to various properties of the device, typically to make performance-related decisions. 16 */ 17public final class DeviceProperties { 18 19 private static final String TAG = Log.tag(DeviceProperties.class); 20 21 /** 22 * Whether or not we believe the device has the performance capabilities to efficiently render 23 * large numbers of APNGs simultaneously. 24 */ 25 public static boolean shouldAllowApngStickerAnimation(@NonNull Context context) { 26 MemoryInfo memoryInfo = getMemoryInfo(context); 27 int memoryMb = (int) ByteUnit.BYTES.toMegabytes(memoryInfo.totalMem); 28 29 if (isLowMemoryDevice(context)) { 30 return false; 31 } 32 33 if (memoryMb < FeatureFlags.animatedStickerMinimumTotalMemoryMb()) { 34 return false; 35 } 36 37 if (getMemoryClass(context) < FeatureFlags.animatedStickerMinimumMemoryClass()) { 38 return false; 39 } 40 41 if (memoryInfo.lowMemory) { 42 Log.w(TAG, "Currently in a low-memory situation! Can't render APNG."); 43 return false; 44 } 45 46 return true; 47 } 48 49 public static boolean isLowMemoryDevice(@NonNull Context context) { 50 ActivityManager activityManager = ServiceUtil.getActivityManager(context); 51 return activityManager.isLowRamDevice(); 52 } 53 54 public static int getMemoryClass(@NonNull Context context) { 55 ActivityManager activityManager = ServiceUtil.getActivityManager(context); 56 return activityManager.getMemoryClass(); 57 } 58 59 public static @NonNull MemoryInfo getMemoryInfo(@NonNull Context context) { 60 MemoryInfo info = new MemoryInfo(); 61 ActivityManager activityManager = ServiceUtil.getActivityManager(context); 62 63 activityManager.getMemoryInfo(info); 64 65 return info; 66 } 67 68 @RequiresApi(28) 69 public static boolean isBackgroundRestricted(@NonNull Context context) { 70 ActivityManager activityManager = ServiceUtil.getActivityManager(context); 71 return activityManager.isBackgroundRestricted(); 72 } 73 74 public static DataSaverState getDataSaverState(@NonNull Context context) { 75 if (Build.VERSION.SDK_INT >= 24) { 76 switch (ServiceUtil.getConnectivityManager(context).getRestrictBackgroundStatus()) { 77 case ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED: 78 return DataSaverState.ENABLED; 79 case ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED: 80 return DataSaverState.ENABLED_BUT_EXEMPTED; 81 case ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED: 82 return DataSaverState.DISABLED; 83 } 84 } 85 86 return DataSaverState.DISABLED; 87 } 88 89 public enum DataSaverState { 90 /** Data saver is enabled system-wide, and we are subject to the restrictions. */ 91 ENABLED(true, true), 92 93 /** Data saver is enabled system-wide, but the user has exempted us by giving us 'unrestricted access' to data in the system settings */ 94 ENABLED_BUT_EXEMPTED(true, false), 95 96 /** Data saver is disabled. */ 97 DISABLED(false, false); 98 99 private final boolean enabled; 100 private final boolean restricted; 101 102 DataSaverState(boolean enabled, boolean restricted) { 103 this.enabled = enabled; 104 this.restricted = restricted; 105 } 106 107 /** True if the device has data saver enabled, otherwise false. */ 108 public boolean isEnabled() { 109 return enabled; 110 } 111 112 /** 113 * True if we're subject to data saver restrictions, otherwise false. 114 * Even if data saver is enabled device-wide, this could still be false if the user has given us 'unrestricted access' to data in the system settings. 115 */ 116 public boolean isRestricted() { 117 return restricted; 118 } 119 } 120}