Use VERSION_CODES instead of raw numbers

This commit is contained in:
topjohnwu 2022-08-23 01:43:53 -07:00
parent c2f96975ce
commit 3f7f6e619a
13 changed files with 33 additions and 26 deletions

View File

@ -11,6 +11,7 @@ import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.loader.ResourcesLoader; import android.content.res.loader.ResourcesLoader;
import android.content.res.loader.ResourcesProvider; import android.content.res.loader.ResourcesProvider;
import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import java.io.File; import java.io.File;
@ -28,7 +29,7 @@ public class StubApk {
private static File getDynDir(ApplicationInfo info) { private static File getDynDir(ApplicationInfo info) {
if (dynDir == null) { if (dynDir == null) {
final String dataDir; final String dataDir;
if (SDK_INT >= 24) { if (SDK_INT >= Build.VERSION_CODES.N) {
// Use device protected path to allow directBootAware // Use device protected path to allow directBootAware
dataDir = info.deviceProtectedDataDir; dataDir = info.deviceProtectedDataDir;
} else { } else {
@ -57,7 +58,7 @@ public class StubApk {
} }
public static void addAssetPath(Resources res, String path) { public static void addAssetPath(Resources res, String path) {
if (SDK_INT >= 30) { if (SDK_INT >= Build.VERSION_CODES.R) {
try (var fd = ParcelFileDescriptor.open(new File(path), MODE_READ_ONLY)) { try (var fd = ParcelFileDescriptor.open(new File(path), MODE_READ_ONLY)) {
var loader = new ResourcesLoader(); var loader = new ResourcesLoader();
loader.addProvider(ResourcesProvider.loadFromApk(fd)); loader.addProvider(ResourcesProvider.loadFromApk(fd));

View File

@ -52,7 +52,7 @@ abstract class BaseActivity : AppCompatActivity() {
val realCallingPackage: String? get() { val realCallingPackage: String? get() {
callingPackage?.let { return it } callingPackage?.let { return it }
if (Build.VERSION.SDK_INT >= 22) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
mReferrerField.get(this)?.let { return it as String } mReferrerField.get(this)?.let { return it as String }
} }
return null return null
@ -82,8 +82,9 @@ abstract class BaseActivity : AppCompatActivity() {
} }
fun withPermission(permission: String, callback: (Boolean) -> Unit) { fun withPermission(permission: String, callback: (Boolean) -> Unit) {
if (permission == WRITE_EXTERNAL_STORAGE && Build.VERSION.SDK_INT >= 30) { if (permission == WRITE_EXTERNAL_STORAGE &&
// We do not need external rw on 30+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// We do not need external rw on R+
callback(true) callback(true)
return return
} }

View File

@ -199,7 +199,7 @@ class DownloadService : NotificationService() {
fun getPendingIntent(context: Context, subject: Subject): PendingIntent { fun getPendingIntent(context: Context, subject: Subject): PendingIntent {
val flag = FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT or FLAG_ONE_SHOT val flag = FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT or FLAG_ONE_SHOT
val intent = intent(context, subject) val intent = intent(context, subject)
return if (Build.VERSION.SDK_INT >= 26) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getForegroundService(context, REQUEST_CODE, intent, flag) getForegroundService(context, REQUEST_CODE, intent, flag)
} else { } else {
getService(context, REQUEST_CODE, intent, flag) getService(context, REQUEST_CODE, intent, flag)
@ -208,7 +208,7 @@ class DownloadService : NotificationService() {
fun start(context: Context, subject: Subject) { fun start(context: Context, subject: Subject) {
val app = context.applicationContext val app = context.applicationContext
if (Build.VERSION.SDK_INT >= 26) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
app.startForegroundService(intent(app, subject)) app.startForegroundService(intent(app, subject))
} else { } else {
app.startService(intent(app, subject)) app.startService(intent(app, subject))

View File

@ -87,7 +87,7 @@ object MediaStoreUtils {
@Throws(IOException::class) @Throws(IOException::class)
fun getFile(displayName: String, skipQuery: Boolean = false): UriFile { fun getFile(displayName: String, skipQuery: Boolean = false): UriFile {
if (Build.VERSION.SDK_INT < 30) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
// Fallback to file based I/O pre Android 11 // Fallback to file based I/O pre Android 11
val parent = File(Environment.getExternalStorageDirectory(), relativePath) val parent = File(Environment.getExternalStorageDirectory(), relativePath)
parent.mkdirs() parent.mkdirs()

View File

@ -25,7 +25,7 @@ class RequestInstall : ActivityResultContract<Unit, Boolean>() {
context: Context, context: Context,
input: Unit input: Unit
): SynchronousResult<Boolean>? { ): SynchronousResult<Boolean>? {
if (Build.VERSION.SDK_INT < 26) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return SynchronousResult(true) return SynchronousResult(true)
if (context.packageManager.canRequestPackageInstalls()) if (context.packageManager.canRequestPackageInstalls())
return SynchronousResult(true) return SynchronousResult(true)

View File

@ -20,7 +20,7 @@ abstract class NetworkObserver(
companion object { companion object {
fun observe(context: Context, callback: ConnectionCallback): NetworkObserver { fun observe(context: Context, callback: ConnectionCallback): NetworkObserver {
val observer: NetworkObserver = if (Build.VERSION.SDK_INT >= 23) val observer: NetworkObserver = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
MarshmallowNetworkObserver(context, callback) MarshmallowNetworkObserver(context, callback)
else LollipopNetworkObserver(context, callback) else LollipopNetworkObserver(context, callback)
return observer.apply { getCurrentState() } return observer.apply { getCurrentState() }

View File

@ -17,6 +17,7 @@ import android.graphics.drawable.AdaptiveIconDrawable
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION.SDK_INT
import android.os.Process import android.os.Process
import android.view.View import android.view.View
@ -44,7 +45,7 @@ fun Context.getBitmap(id: Int): Bitmap {
var drawable = AppCompatResources.getDrawable(this, id)!! var drawable = AppCompatResources.getDrawable(this, id)!!
if (drawable is BitmapDrawable) if (drawable is BitmapDrawable)
return drawable.bitmap return drawable.bitmap
if (SDK_INT >= 26 && drawable is AdaptiveIconDrawable) { if (SDK_INT >= Build.VERSION_CODES.O && drawable is AdaptiveIconDrawable) {
drawable = LayerDrawable(arrayOf(drawable.background, drawable.foreground)) drawable = LayerDrawable(arrayOf(drawable.background, drawable.foreground))
} }
val bitmap = Bitmap.createBitmap( val bitmap = Bitmap.createBitmap(
@ -58,7 +59,7 @@ fun Context.getBitmap(id: Int): Bitmap {
} }
val Context.deviceProtectedContext: Context get() = val Context.deviceProtectedContext: Context get() =
if (SDK_INT >= 24) { if (SDK_INT >= Build.VERSION_CODES.N) {
createDeviceProtectedStorageContext() createDeviceProtectedStorageContext()
} else { this } } else { this }

View File

@ -7,6 +7,7 @@ import android.content.pm.PackageManager
import android.content.pm.PackageManager.* import android.content.pm.PackageManager.*
import android.content.pm.ServiceInfo import android.content.pm.ServiceInfo
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION.SDK_INT
import androidx.core.os.ProcessCompat import androidx.core.os.ProcessCompat
import com.topjohnwu.magisk.core.utils.currentLocale import com.topjohnwu.magisk.core.utils.currentLocale
@ -67,7 +68,8 @@ class AppProcessInfo(
val proc = info.processName ?: info.packageName val proc = info.processName ?: info.packageName
createProcess("${proc}_zygote") createProcess("${proc}_zygote")
} else { } else {
val proc = if (SDK_INT >= 29) "${it.getProcName()}:${it.name}" else it.getProcName() val proc = if (SDK_INT >= Build.VERSION_CODES.Q)
"${it.getProcName()}:${it.name}" else it.getProcName()
createProcess(proc, ISOLATED_MAGIC) createProcess(proc, ISOLATED_MAGIC)
} }
} else { } else {

View File

@ -53,7 +53,8 @@ class SettingsViewModel : BaseViewModel(), BaseSettingsItem.Handler {
AppSettings, AppSettings,
UpdateChannel, UpdateChannelUrl, DoHToggle, UpdateChecker, DownloadPath UpdateChannel, UpdateChannelUrl, DoHToggle, UpdateChecker, DownloadPath
)) ))
if (Build.VERSION.SDK_INT >= 22 && Info.env.isActive && Const.USER_ID == 0) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 &&
Info.env.isActive && Const.USER_ID == 0) {
if (hidden) list.add(Restore) else list.add(Hide) if (hidden) list.add(Restore) else list.add(Hide)
} }
@ -75,15 +76,15 @@ class SettingsViewModel : BaseViewModel(), BaseSettingsItem.Handler {
Tapjack, Biometrics, AccessMode, MultiuserMode, MountNamespaceMode, Tapjack, Biometrics, AccessMode, MultiuserMode, MountNamespaceMode,
AutomaticResponse, RequestTimeout, SUNotification AutomaticResponse, RequestTimeout, SUNotification
)) ))
if (Build.VERSION.SDK_INT < 23) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Biometric is only available on 6.0+ // Biometric is only available on 6.0+
list.remove(Biometrics) list.remove(Biometrics)
} }
if (Build.VERSION.SDK_INT < 26) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// Re-authenticate is not feasible on 8.0+ // Re-authenticate is not feasible on 8.0+
list.add(Reauthenticate) list.add(Reauthenticate)
} }
if (Build.VERSION.SDK_INT >= 31) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Can hide overlay windows on 12.0+ // Can hide overlay windows on 12.0+
list.remove(Tapjack) list.remove(Tapjack)
} }

View File

@ -7,6 +7,7 @@ import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build
import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION.SDK_INT
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toIcon import androidx.core.graphics.drawable.toIcon
@ -32,7 +33,7 @@ object Notifications {
private val nextId = AtomicInteger(APP_UPDATE_NOTIFICATION_ID) private val nextId = AtomicInteger(APP_UPDATE_NOTIFICATION_ID)
fun setup(context: Context) { fun setup(context: Context) {
if (SDK_INT >= 26) { if (SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(UPDATE_CHANNEL, val channel = NotificationChannel(UPDATE_CHANNEL,
context.getString(R.string.update_channel), NotificationManager.IMPORTANCE_DEFAULT) context.getString(R.string.update_channel), NotificationManager.IMPORTANCE_DEFAULT)
val channel2 = NotificationChannel(PROGRESS_CHANNEL, val channel2 = NotificationChannel(PROGRESS_CHANNEL,
@ -55,7 +56,7 @@ object Notifications {
setup(context) setup(context)
val flag = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT val flag = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
val pending = PendingIntent.getActivity(context, 0, selfLaunchIntent(context), flag) val pending = PendingIntent.getActivity(context, 0, selfLaunchIntent(context), flag)
val builder = if (SDK_INT >= 26) { val builder = if (SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, UPDATED_CHANNEL) Notification.Builder(context, UPDATED_CHANNEL)
.setSmallIcon(context.getBitmap(R.drawable.ic_magisk_outline).toIcon()) .setSmallIcon(context.getBitmap(R.drawable.ic_magisk_outline).toIcon())
} else { } else {
@ -73,7 +74,7 @@ object Notifications {
val intent = DownloadService.getPendingIntent(context, Subject.App()) val intent = DownloadService.getPendingIntent(context, Subject.App())
val bitmap = context.getBitmap(R.drawable.ic_magisk_outline) val bitmap = context.getBitmap(R.drawable.ic_magisk_outline)
val builder = if (SDK_INT >= 26) { val builder = if (SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, UPDATE_CHANNEL) Notification.Builder(context, UPDATE_CHANNEL)
.setSmallIcon(bitmap.toIcon()) .setSmallIcon(bitmap.toIcon())
} else { } else {
@ -90,7 +91,7 @@ object Notifications {
} }
fun progress(context: Context, title: CharSequence): Notification.Builder { fun progress(context: Context, title: CharSequence): Notification.Builder {
val builder = if (SDK_INT >= 26) { val builder = if (SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, PROGRESS_CHANNEL) Notification.Builder(context, PROGRESS_CHANNEL)
} else { } else {
Notification.Builder(context).setPriority(Notification.PRIORITY_LOW) Notification.Builder(context).setPriority(Notification.PRIORITY_LOW)

View File

@ -19,7 +19,7 @@ import com.topjohnwu.magisk.utils.Utils
object Shortcuts { object Shortcuts {
fun setupDynamic(context: Context) { fun setupDynamic(context: Context) {
if (Build.VERSION.SDK_INT >= 25) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val manager = context.getSystemService<ShortcutManager>() ?: return val manager = context.getSystemService<ShortcutManager>() ?: return
manager.dynamicShortcuts = getShortCuts(context) manager.dynamicShortcuts = getShortCuts(context)
} }
@ -36,7 +36,7 @@ object Shortcuts {
} }
private fun Context.getIconCompat(id: Int): IconCompat { private fun Context.getIconCompat(id: Int): IconCompat {
return if (Build.VERSION.SDK_INT >= 26) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
IconCompat.createWithAdaptiveBitmap(getBitmap(id)) IconCompat.createWithAdaptiveBitmap(getBitmap(id))
else else
IconCompat.createWithBitmap(getBitmap(id)) IconCompat.createWithBitmap(getBitmap(id))

View File

@ -186,7 +186,7 @@ public class DownloadActivity extends Activity {
} }
private void loadResources() throws Exception { private void loadResources() throws Exception {
if (Build.VERSION.SDK_INT >= 30) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
var fd = Os.memfd_create("res.apk", 0); var fd = Os.memfd_create("res.apk", 0);
try { try {
decryptResources(new FileOutputStream(fd)); decryptResources(new FileOutputStream(fd));

View File

@ -104,7 +104,7 @@ public class DynLoad {
// Dynamically load APK and create the Application instance from the loaded APK // Dynamically load APK and create the Application instance from the loaded APK
static Application createAndSetupApp(Application context) { static Application createAndSetupApp(Application context) {
// On API >= 29, AppComponentFactory will replace the ClassLoader for us // On API >= 29, AppComponentFactory will replace the ClassLoader for us
if (Build.VERSION.SDK_INT < 29) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
replaceClassLoader(context); replaceClassLoader(context);
// noinspection InlinedApi // noinspection InlinedApi
@ -145,7 +145,7 @@ public class DynLoad {
.newInstance(data.getObject()); .newInstance(data.getObject());
// Create the receiver component factory // Create the receiver component factory
if (Build.VERSION.SDK_INT >= 28 && componentFactory != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && componentFactory != null) {
Object factory = cl.loadClass(appInfo.appComponentFactory).newInstance(); Object factory = cl.loadClass(appInfo.appComponentFactory).newInstance();
var delegate = (DelegateComponentFactory) componentFactory; var delegate = (DelegateComponentFactory) componentFactory;
delegate.receiver = (AppComponentFactory) factory; delegate.receiver = (AppComponentFactory) factory;