Remove obsolete SDK_INT check

This commit is contained in:
vvb2060 2021-04-22 00:41:42 +08:00 committed by John Wu
parent 0db405f2cc
commit fb93af665d
10 changed files with 46 additions and 163 deletions

View File

@ -7,7 +7,6 @@ import android.content.pm.ProviderInfo;
import android.database.Cursor; import android.database.Cursor;
import android.database.MatrixCursor; import android.database.MatrixCursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
@ -24,12 +23,11 @@ import java.util.Map;
* Modified from androidx.core.content.FileProvider * Modified from androidx.core.content.FileProvider
*/ */
public class FileProvider extends ContentProvider { public class FileProvider extends ContentProvider {
private static final String[] COLUMNS = { private static final String[] COLUMNS = {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };
private static final File DEVICE_ROOT = new File("/"); private static final File DEVICE_ROOT = new File("/");
private static HashMap<String, PathStrategy> sCache = new HashMap<>(); private static final HashMap<String, PathStrategy> sCache = new HashMap<>();
private PathStrategy mStrategy; private PathStrategy mStrategy;
@ -42,7 +40,6 @@ public class FileProvider extends ContentProvider {
public void attachInfo(Context context, ProviderInfo info) { public void attachInfo(Context context, ProviderInfo info) {
super.attachInfo(context, info); super.attachInfo(context, info);
if (info.exported) { if (info.exported) {
throw new SecurityException("Provider must not be exported"); throw new SecurityException("Provider must not be exported");
} }
@ -50,21 +47,16 @@ public class FileProvider extends ContentProvider {
throw new SecurityException("Provider must grant uri permissions"); throw new SecurityException("Provider must grant uri permissions");
} }
mStrategy = getPathStrategy(context, info.authority); mStrategy = getPathStrategy(context, info.authority.split(";")[0]);
} }
public static Uri getUriForFile(Context context, String authority, File file) {
public static Uri getUriForFile(Context context, String authority,
File file) {
final PathStrategy strategy = getPathStrategy(context, authority); final PathStrategy strategy = getPathStrategy(context, authority);
return strategy.getUriForFile(file); return strategy.getUriForFile(file);
} }
@Override @Override
public Cursor query(Uri uri, String[] projection, String selection, public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
String[] selectionArgs,
String sortOrder) {
final File file = mStrategy.getFileForUri(uri); final File file = mStrategy.getFileForUri(uri);
if (projection == null) { if (projection == null) {
@ -94,7 +86,6 @@ public class FileProvider extends ContentProvider {
@Override @Override
public String getType(Uri uri) { public String getType(Uri uri) {
final File file = mStrategy.getFileForUri(uri); final File file = mStrategy.getFileForUri(uri);
final int lastDot = file.getName().lastIndexOf('.'); final int lastDot = file.getName().lastIndexOf('.');
@ -115,23 +106,18 @@ public class FileProvider extends ContentProvider {
} }
@Override @Override
public int update(Uri uri, ContentValues values, String selection, public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
String[] selectionArgs) {
throw new UnsupportedOperationException("No external updates"); throw new UnsupportedOperationException("No external updates");
} }
@Override @Override
public int delete(Uri uri, String selection, public int delete(Uri uri, String selection, String[] selectionArgs) {
String[] selectionArgs) {
final File file = mStrategy.getFileForUri(uri); final File file = mStrategy.getFileForUri(uri);
return file.delete() ? 1 : 0; return file.delete() ? 1 : 0;
} }
@Override @Override
public ParcelFileDescriptor openFile(Uri uri, String mode) public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
throws FileNotFoundException {
final File file = mStrategy.getFileForUri(uri); final File file = mStrategy.getFileForUri(uri);
final int fileMode = modeToMode(mode); final int fileMode = modeToMode(mode);
return ParcelFileDescriptor.open(file, fileMode); return ParcelFileDescriptor.open(file, fileMode);
@ -156,30 +142,24 @@ public class FileProvider extends ContentProvider {
strat.addRoot("internal_files", buildPath(context.getFilesDir(), ".")); strat.addRoot("internal_files", buildPath(context.getFilesDir(), "."));
strat.addRoot("cache_files", buildPath(context.getCacheDir(), ".")); strat.addRoot("cache_files", buildPath(context.getCacheDir(), "."));
strat.addRoot("external_files", buildPath(Environment.getExternalStorageDirectory(), ".")); strat.addRoot("external_files", buildPath(Environment.getExternalStorageDirectory(), "."));
{
File[] externalFilesDirs = getExternalFilesDirs(context, null); File[] externalFilesDirs = context.getExternalFilesDirs(null);
if (externalFilesDirs.length > 0) { if (externalFilesDirs.length > 0) {
strat.addRoot("external_file_files", buildPath(externalFilesDirs[0], ".")); strat.addRoot("external_file_files", buildPath(externalFilesDirs[0], "."));
}
} }
{ File[] externalCacheDirs = context.getExternalCacheDirs();
File[] externalCacheDirs = getExternalCacheDirs(context); if (externalCacheDirs.length > 0) {
if (externalCacheDirs.length > 0) { strat.addRoot("external_cache_files", buildPath(externalCacheDirs[0], "."));
strat.addRoot("external_cache_files", buildPath(externalCacheDirs[0], "."));
}
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { File[] externalMediaDirs = context.getExternalMediaDirs();
File[] externalMediaDirs = context.getExternalMediaDirs(); if (externalMediaDirs.length > 0) {
if (externalMediaDirs.length > 0) { strat.addRoot("external_media_files", buildPath(externalMediaDirs[0], "."));
strat.addRoot("external_media_files", buildPath(externalMediaDirs[0], "."));
}
} }
return strat; return strat;
} }
interface PathStrategy { interface PathStrategy {
Uri getUriForFile(File file); Uri getUriForFile(File file);
File getFileForUri(Uri uri); File getFileForUri(Uri uri);
@ -199,7 +179,6 @@ public class FileProvider extends ContentProvider {
} }
try { try {
root = root.getCanonicalFile(); root = root.getCanonicalFile();
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -218,7 +197,6 @@ public class FileProvider extends ContentProvider {
throw new IllegalArgumentException("Failed to resolve canonical path for " + file); throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
} }
Map.Entry<String, File> mostSpecific = null; Map.Entry<String, File> mostSpecific = null;
for (Map.Entry<String, File> root : mRoots.entrySet()) { for (Map.Entry<String, File> root : mRoots.entrySet()) {
final String rootPath = root.getValue().getPath(); final String rootPath = root.getValue().getPath();
@ -233,7 +211,6 @@ public class FileProvider extends ContentProvider {
"Failed to find configured root that contains " + path); "Failed to find configured root that contains " + path);
} }
final String rootPath = mostSpecific.getValue().getPath(); final String rootPath = mostSpecific.getValue().getPath();
if (rootPath.endsWith("/")) { if (rootPath.endsWith("/")) {
path = path.substring(rootPath.length()); path = path.substring(rootPath.length());
@ -241,7 +218,6 @@ public class FileProvider extends ContentProvider {
path = path.substring(rootPath.length() + 1); path = path.substring(rootPath.length() + 1);
} }
path = Uri.encode(mostSpecific.getKey()) + '/' + Uri.encode(path, "/"); path = Uri.encode(mostSpecific.getKey()) + '/' + Uri.encode(path, "/");
return new Uri.Builder().scheme("content") return new Uri.Builder().scheme("content")
.authority(mAuthority).encodedPath(path).build(); .authority(mAuthority).encodedPath(path).build();
@ -275,7 +251,6 @@ public class FileProvider extends ContentProvider {
} }
} }
private static int modeToMode(String mode) { private static int modeToMode(String mode) {
int modeBits; int modeBits;
if ("r".equals(mode)) { if ("r".equals(mode)) {
@ -322,20 +297,4 @@ public class FileProvider extends ContentProvider {
System.arraycopy(original, 0, result, 0, newLength); System.arraycopy(original, 0, result, 0, newLength);
return result; return result;
} }
private static File[] getExternalFilesDirs(Context context, String type) {
if (Build.VERSION.SDK_INT >= 19) {
return context.getExternalFilesDirs(type);
} else {
return new File[] { context.getExternalFilesDir(type) };
}
}
private static File[] getExternalCacheDirs(Context context) {
if (Build.VERSION.SDK_INT >= 19) {
return context.getExternalCacheDirs();
} else {
return new File[] { context.getExternalCacheDir() };
}
}
} }

View File

@ -57,26 +57,24 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
.use { it.getDrawable(0) } .use { it.getDrawable(0) }
.also { window.setBackgroundDrawable(it) } .also { window.setBackgroundDrawable(it) }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window?.decorView?.let {
window?.decorView?.let { it.systemUiVisibility = (it.systemUiVisibility
it.systemUiVisibility = (it.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) }
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
window?.decorView?.post { window?.decorView?.post {
// If navigation bar is short enough (gesture navigation enabled), make it transparent // If navigation bar is short enough (gesture navigation enabled), make it transparent
if (window.decorView.rootWindowInsets?.systemWindowInsetBottom ?: 0 < Resources.getSystem().displayMetrics.density * 40) { if (window.decorView.rootWindowInsets?.systemWindowInsetBottom ?: 0 < Resources.getSystem().displayMetrics.density * 40) {
window.navigationBarColor = Color.TRANSPARENT window.navigationBarColor = Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.navigationBarDividerColor = Color.TRANSPARENT window.navigationBarDividerColor = Color.TRANSPARENT
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = false window.isNavigationBarContrastEnforced = false
window.isStatusBarContrastEnforced = false window.isStatusBarContrastEnforced = false
}
} }
} }
} }

View File

@ -8,18 +8,8 @@ import java.io.File
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
object Const { object Const {
val CPU_ABI: String val CPU_ABI: String = Build.SUPPORTED_ABIS[0]
val CPU_ABI_32: String val CPU_ABI_32: String = Build.SUPPORTED_32_BIT_ABIS.firstOrNull() ?: CPU_ABI
init {
if (Build.VERSION.SDK_INT >= 21) {
CPU_ABI = Build.SUPPORTED_ABIS[0]
CPU_ABI_32 = Build.SUPPORTED_32_BIT_ABIS.firstOrNull() ?: CPU_ABI
} else {
CPU_ABI = Build.CPU_ABI
CPU_ABI_32 = CPU_ABI
}
}
// Paths // Paths
lateinit var MAGISKTMP: String lateinit var MAGISKTMP: String

View File

@ -1,6 +1,7 @@
package com.topjohnwu.magisk.core.tasks package com.topjohnwu.magisk.core.tasks
import android.net.Uri import android.net.Uri
import android.system.Os
import android.widget.Toast import android.widget.Toast
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
@ -13,7 +14,6 @@ import com.topjohnwu.magisk.core.utils.MediaStoreUtils.inputStream
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.di.ServiceLocator import com.topjohnwu.magisk.di.ServiceLocator
import com.topjohnwu.magisk.ktx.reboot import com.topjohnwu.magisk.ktx.reboot
import com.topjohnwu.magisk.ktx.symlink
import com.topjohnwu.magisk.ktx.withStreams import com.topjohnwu.magisk.ktx.withStreams
import com.topjohnwu.magisk.ktx.writeTo import com.topjohnwu.magisk.ktx.writeTo
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
@ -107,7 +107,7 @@ abstract class MagiskInstallImpl protected constructor(
} ?: emptyArray() } ?: emptyArray()
for (lib in libs) { for (lib in libs) {
val name = lib.name.substring(3, lib.name.length - 3) val name = lib.name.substring(3, lib.name.length - 3)
symlink(lib.path, "$installDir/$name") Os.symlink(lib.path, "$installDir/$name")
} }
} }

View File

@ -20,11 +20,10 @@ abstract class NetworkObserver(
companion object { companion object {
fun observe(context: Context, callback: ConnectionCallback): NetworkObserver { fun observe(context: Context, callback: ConnectionCallback): NetworkObserver {
return when (Build.VERSION.SDK_INT) { val observer: NetworkObserver = if (Build.VERSION.SDK_INT >= 23)
in 23 until Int.MAX_VALUE -> MarshmallowNetworkObserver(context, callback) MarshmallowNetworkObserver(context, callback)
in 21 until 23 -> LollipopNetworkObserver(context, callback) else LollipopNetworkObserver(context, callback)
else -> PreLollipopNetworkObserver(context, callback) return observer.apply { getCurrentState() }
}.apply { getCurrentState() }
} }
} }
} }

View File

@ -1,38 +0,0 @@
@file:Suppress("DEPRECATION")
package com.topjohnwu.magisk.core.utils.net
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import androidx.core.net.ConnectivityManagerCompat
class PreLollipopNetworkObserver(
context: Context,
callback: ConnectionCallback
): NetworkObserver(context, callback) {
private val receiver = ConnectivityBroadcastReceiver()
init {
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
app.registerReceiver(receiver, filter)
}
override fun stopObserving() {
app.unregisterReceiver(receiver)
}
override fun getCurrentState() {
callback(manager.activeNetworkInfo?.isConnected ?: false)
}
private inner class ConnectivityBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
val info = ConnectivityManagerCompat.getNetworkInfoFromBroadcast(manager, intent)
callback(info?.isConnected ?: false)
}
}
}

View File

@ -22,7 +22,6 @@ 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.VERSION.SDK_INT import android.os.Build.VERSION.SDK_INT
import android.system.Os
import android.text.PrecomputedText import android.text.PrecomputedText
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -57,24 +56,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.lang.reflect.Method
import java.lang.reflect.Array as JArray import java.lang.reflect.Array as JArray
private lateinit var osSymlink: Method
private lateinit var os: Any
fun symlink(oldPath: String, newPath: String) {
if (SDK_INT >= 21) {
Os.symlink(oldPath, newPath)
} else {
if (!::osSymlink.isInitialized) {
os = Class.forName("libcore.io.Libcore").getField("os").get(null)!!
osSymlink = os.javaClass.getMethod("symlink", String::class.java, String::class.java)
}
osSymlink.invoke(os, oldPath, newPath)
}
}
val ServiceInfo.isIsolated get() = (flags and FLAG_ISOLATED_PROCESS) != 0 val ServiceInfo.isIsolated get() = (flags and FLAG_ISOLATED_PROCESS) != 0
@get:SuppressLint("InlinedApi") @get:SuppressLint("InlinedApi")

View File

@ -26,9 +26,7 @@ open class LayoutInflaterFactory(private val delegate: AppCompatDelegate) : Layo
open fun onViewCreated(view: View?, parent: View?, name: String, context: Context, attrs: AttributeSet) { open fun onViewCreated(view: View?, parent: View?, name: String, context: Context, attrs: AttributeSet) {
if (view == null) return if (view == null) return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { WindowInsetsHelper.attach(view, attrs)
WindowInsetsHelper.attach(view, attrs)
}
} }
} }
@ -85,4 +83,4 @@ private object LayoutInflaterFactoryDefaultImpl {
null null
} }
} }
} }

View File

@ -59,7 +59,7 @@ class SettingsViewModel(
)) ))
if (Info.env.isActive) { if (Info.env.isActive) {
list.add(ClearRepoCache) list.add(ClearRepoCache)
if (Build.VERSION.SDK_INT >= 21 && Const.USER_ID == 0) { if (Const.USER_ID == 0) {
if (hidden) if (hidden)
list.add(Restore) list.add(Restore)
else if (Info.isConnected.get()) else if (Info.isConnected.get())

View File

@ -47,12 +47,6 @@
tools:layout_marginTop="24dp" tools:layout_marginTop="24dp"
tools:title="Home" /> tools:title="Home" />
<View
gone="@{android.os.Build.VERSION.SDK_INT >= 21}"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="?colorSurfaceVariant"/>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<!-- <!--