mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-07-13 05:16:43 +02:00
Move to plugins🐱🎉
This commit is contained in:
@ -92,18 +92,6 @@ repositories {
|
||||
maven { url 'https://jitpack.io' }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
def shizuku_version = '13.1.5'
|
||||
implementation "dev.rikka.shizuku:api:$shizuku_version"
|
||||
implementation "dev.rikka.shizuku:provider:$shizuku_version"
|
||||
|
||||
def hidden_api_version = '4.3.1'
|
||||
implementation "dev.rikka.tools.refine:runtime:$hidden_api_version"
|
||||
implementation "dev.rikka.hidden:compat:$hidden_api_version"
|
||||
compileOnly "dev.rikka.hidden:stub:$hidden_api_version"
|
||||
implementation "org.lsposed.hiddenapibypass:hiddenapibypass:4.3"
|
||||
}
|
||||
|
||||
ext.abiCodes = ["x86_64": 1, "armeabi-v7a": 2, "arm64-v8a": 3]
|
||||
import com.android.build.OutputFile
|
||||
android.applicationVariants.all { variant ->
|
||||
|
@ -1,156 +1,5 @@
|
||||
package dev.imranr.obtainium
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.IntentSender
|
||||
import android.content.pm.IPackageInstaller
|
||||
import android.content.pm.IPackageInstallerSession
|
||||
import android.content.pm.PackageInstaller
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Process
|
||||
import androidx.annotation.NonNull
|
||||
import dev.imranr.obtainium.util.IIntentSenderAdaptor
|
||||
import dev.imranr.obtainium.util.IntentSenderUtils
|
||||
import dev.imranr.obtainium.util.PackageInstallerUtils
|
||||
import dev.imranr.obtainium.util.ShizukuSystemServerApi
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import org.lsposed.hiddenapibypass.HiddenApiBypass
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import rikka.shizuku.Shizuku
|
||||
import rikka.shizuku.Shizuku.OnRequestPermissionResultListener
|
||||
import rikka.shizuku.ShizukuBinderWrapper
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
private var nativeChannel: MethodChannel? = null
|
||||
private val SHIZUKU_PERMISSION_REQUEST_CODE = (1000..2000).random()
|
||||
|
||||
private fun shizukuCheckPermission(result: Result) {
|
||||
try {
|
||||
if (Shizuku.isPreV11()) { // Unsupported
|
||||
result.success(-1)
|
||||
} else if (Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED) {
|
||||
result.success(1)
|
||||
} else if (Shizuku.shouldShowRequestPermissionRationale()) { // Deny and don't ask again
|
||||
result.success(0)
|
||||
} else {
|
||||
Shizuku.requestPermission(SHIZUKU_PERMISSION_REQUEST_CODE)
|
||||
result.success(-2)
|
||||
}
|
||||
} catch (_: Exception) { // If shizuku binder not found
|
||||
result.success(-1)
|
||||
}
|
||||
}
|
||||
|
||||
private val shizukuRequestPermissionResultListener = OnRequestPermissionResultListener {
|
||||
requestCode: Int, grantResult: Int ->
|
||||
if (requestCode == SHIZUKU_PERMISSION_REQUEST_CODE) {
|
||||
val res = if (grantResult == PackageManager.PERMISSION_GRANTED) 1 else 0
|
||||
nativeChannel!!.invokeMethod("resPermShizuku", mapOf("res" to res))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun shizukuInstallApk(apkFileUri: String, result: Result) {
|
||||
val uri = Uri.parse(apkFileUri)
|
||||
var res = false
|
||||
var session: PackageInstaller.Session? = null
|
||||
try {
|
||||
val iPackageInstaller: IPackageInstaller =
|
||||
ShizukuSystemServerApi.PackageManager_getPackageInstaller()
|
||||
val isRoot = Shizuku.getUid() == 0
|
||||
// The reason for use "com.android.shell" as installer package under adb
|
||||
// is that getMySessions will check installer package's owner
|
||||
val installerPackageName = if (isRoot) packageName else "com.android.shell"
|
||||
var installerAttributionTag: String? = null
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
installerAttributionTag = attributionTag
|
||||
}
|
||||
val userId = if (isRoot) Process.myUserHandle().hashCode() else 0
|
||||
val packageInstaller = PackageInstallerUtils.createPackageInstaller(
|
||||
iPackageInstaller, installerPackageName, installerAttributionTag, userId)
|
||||
val params =
|
||||
PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
|
||||
var installFlags: Int = PackageInstallerUtils.getInstallFlags(params)
|
||||
installFlags = installFlags or (0x00000002/*PackageManager.INSTALL_REPLACE_EXISTING*/
|
||||
or 0x00000004 /*PackageManager.INSTALL_ALLOW_TEST*/)
|
||||
PackageInstallerUtils.setInstallFlags(params, installFlags)
|
||||
val sessionId = packageInstaller.createSession(params)
|
||||
val iSession = IPackageInstallerSession.Stub.asInterface(
|
||||
ShizukuBinderWrapper(iPackageInstaller.openSession(sessionId).asBinder()))
|
||||
session = PackageInstallerUtils.createSession(iSession)
|
||||
val inputStream = contentResolver.openInputStream(uri)
|
||||
val openedSession = session.openWrite("apk.apk", 0, -1)
|
||||
val buffer = ByteArray(8192)
|
||||
var length: Int
|
||||
try {
|
||||
while (inputStream!!.read(buffer).also { length = it } > 0) {
|
||||
openedSession.write(buffer, 0, length)
|
||||
openedSession.flush()
|
||||
session.fsync(openedSession)
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
inputStream!!.close()
|
||||
openedSession.close()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
val results = arrayOf<Intent?>(null)
|
||||
val countDownLatch = CountDownLatch(1)
|
||||
val intentSender: IntentSender =
|
||||
IntentSenderUtils.newInstance(object : IIntentSenderAdaptor() {
|
||||
override fun send(intent: Intent?) {
|
||||
results[0] = intent
|
||||
countDownLatch.countDown()
|
||||
}
|
||||
})
|
||||
session.commit(intentSender)
|
||||
countDownLatch.await()
|
||||
res = results[0]!!.getIntExtra(
|
||||
PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE) == 0
|
||||
} catch (_: Exception) {
|
||||
res = false
|
||||
} finally {
|
||||
if (session != null) {
|
||||
try {
|
||||
session.close()
|
||||
} catch (_: Exception) {
|
||||
res = false
|
||||
}
|
||||
}
|
||||
}
|
||||
result.success(res)
|
||||
}
|
||||
|
||||
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
HiddenApiBypass.addHiddenApiExemptions("")
|
||||
}
|
||||
Shizuku.addRequestPermissionResultListener(shizukuRequestPermissionResultListener)
|
||||
nativeChannel = MethodChannel(
|
||||
flutterEngine.dartExecutor.binaryMessenger, "native")
|
||||
nativeChannel!!.setMethodCallHandler {
|
||||
call, result ->
|
||||
if (call.method == "checkPermissionShizuku") {
|
||||
shizukuCheckPermission(result)
|
||||
} else if (call.method == "installWithShizuku") {
|
||||
val apkFileUri: String = call.argument("apkFileUri")!!
|
||||
GlobalScope.async { shizukuInstallApk(apkFileUri, result) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
Shizuku.removeRequestPermissionResultListener(shizukuRequestPermissionResultListener)
|
||||
}
|
||||
}
|
||||
class MainActivity: FlutterActivity()
|
||||
|
@ -1,37 +0,0 @@
|
||||
package dev.imranr.obtainium.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Application;
|
||||
import android.os.Build;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ApplicationUtils {
|
||||
|
||||
private static Application application;
|
||||
|
||||
public static Application getApplication() {
|
||||
return application;
|
||||
}
|
||||
|
||||
public static void setApplication(Application application) {
|
||||
ApplicationUtils.application = application;
|
||||
}
|
||||
|
||||
public static String getProcessName() {
|
||||
if (Build.VERSION.SDK_INT >= 28)
|
||||
return Application.getProcessName();
|
||||
else {
|
||||
try {
|
||||
@SuppressLint("PrivateApi")
|
||||
Class<?> activityThread = Class.forName("android.app.ActivityThread");
|
||||
@SuppressLint("DiscouragedPrivateApi")
|
||||
Method method = activityThread.getDeclaredMethod("currentProcessName");
|
||||
return (String) method.invoke(null);
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dev.imranr.obtainium.util;
|
||||
|
||||
import android.content.IIntentReceiver;
|
||||
import android.content.IIntentSender;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
|
||||
public abstract class IIntentSenderAdaptor extends IIntentSender.Stub {
|
||||
|
||||
public abstract void send(Intent intent);
|
||||
|
||||
@Override
|
||||
public int send(int code, Intent intent, String resolvedType, IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
|
||||
send(intent);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
|
||||
send(intent);
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package dev.imranr.obtainium.util;
|
||||
|
||||
import android.content.IIntentSender;
|
||||
import android.content.IntentSender;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class IntentSenderUtils {
|
||||
|
||||
public static IntentSender newInstance(IIntentSender binder) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
//noinspection JavaReflectionMemberAccess
|
||||
return IntentSender.class.getConstructor(IIntentSender.class).newInstance(binder);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package dev.imranr.obtainium.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.IPackageInstaller;
|
||||
import android.content.pm.IPackageInstallerSession;
|
||||
import android.content.pm.PackageInstaller;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
@SuppressWarnings({"JavaReflectionMemberAccess"})
|
||||
public class PackageInstallerUtils {
|
||||
|
||||
public static PackageInstaller createPackageInstaller(IPackageInstaller installer, String installerPackageName, String installerAttributionTag, int userId) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
return PackageInstaller.class.getConstructor(IPackageInstaller.class, String.class, String.class, int.class)
|
||||
.newInstance(installer, installerPackageName, installerAttributionTag, userId);
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
return PackageInstaller.class.getConstructor(IPackageInstaller.class, String.class, int.class)
|
||||
.newInstance(installer, installerPackageName, userId);
|
||||
} else {
|
||||
return PackageInstaller.class.getConstructor(Context.class, PackageManager.class, IPackageInstaller.class, String.class, int.class)
|
||||
.newInstance(ApplicationUtils.getApplication(), ApplicationUtils.getApplication().getPackageManager(), installer, installerPackageName, userId);
|
||||
}
|
||||
}
|
||||
|
||||
public static PackageInstaller.Session createSession(IPackageInstallerSession session) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
return PackageInstaller.Session.class.getConstructor(IPackageInstallerSession.class)
|
||||
.newInstance(session);
|
||||
|
||||
}
|
||||
|
||||
public static int getInstallFlags(PackageInstaller.SessionParams params) throws NoSuchFieldException, IllegalAccessException {
|
||||
return (int) PackageInstaller.SessionParams.class.getDeclaredField("installFlags").get(params);
|
||||
}
|
||||
|
||||
public static void setInstallFlags(PackageInstaller.SessionParams params, int newValue) throws NoSuchFieldException, IllegalAccessException {
|
||||
PackageInstaller.SessionParams.class.getDeclaredField("installFlags").set(params, newValue);
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
package dev.imranr.obtainium.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.IPackageInstaller;
|
||||
import android.content.pm.IPackageManager;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.Build;
|
||||
import android.os.IUserManager;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rikka.shizuku.ShizukuBinderWrapper;
|
||||
import rikka.shizuku.SystemServiceHelper;
|
||||
|
||||
public class ShizukuSystemServerApi {
|
||||
|
||||
private static final Singleton<IPackageManager> PACKAGE_MANAGER = new Singleton<IPackageManager>() {
|
||||
@Override
|
||||
protected IPackageManager create() {
|
||||
return IPackageManager.Stub.asInterface(new ShizukuBinderWrapper(SystemServiceHelper.getSystemService("package")));
|
||||
}
|
||||
};
|
||||
|
||||
private static final Singleton<IUserManager> USER_MANAGER = new Singleton<IUserManager>() {
|
||||
@Override
|
||||
protected IUserManager create() {
|
||||
return IUserManager.Stub.asInterface(new ShizukuBinderWrapper(SystemServiceHelper.getSystemService(Context.USER_SERVICE)));
|
||||
}
|
||||
};
|
||||
|
||||
public static IPackageInstaller PackageManager_getPackageInstaller() throws RemoteException {
|
||||
IPackageInstaller packageInstaller = PACKAGE_MANAGER.get().getPackageInstaller();
|
||||
return IPackageInstaller.Stub.asInterface(new ShizukuBinderWrapper(packageInstaller.asBinder()));
|
||||
}
|
||||
|
||||
public static List<UserInfo> UserManager_getUsers(boolean excludePartial, boolean excludeDying, boolean excludePreCreated) throws RemoteException {
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
return USER_MANAGER.get().getUsers(excludePartial, excludeDying, excludePreCreated);
|
||||
} else {
|
||||
try {
|
||||
return USER_MANAGER.get().getUsers(excludeDying);
|
||||
} catch (NoSuchFieldError e) {
|
||||
return USER_MANAGER.get().getUsers(excludePartial, excludeDying, excludePreCreated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// method 2: use transactRemote directly
|
||||
/*public static List<UserInfo> UserManager_getUsers(boolean excludeDying) {
|
||||
Parcel data = SystemServiceHelper.obtainParcel(Context.USER_SERVICE, "android.os.IUserManager", "getUsers");
|
||||
Parcel reply = Parcel.obtain();
|
||||
data.writeInt(excludeDying ? 1 : 0);
|
||||
|
||||
List<UserInfo> res = null;
|
||||
try {
|
||||
ShizukuService.transactRemote(data, reply, 0);
|
||||
reply.readException();
|
||||
res = reply.createTypedArrayList(UserInfo.CREATOR);
|
||||
} catch (RemoteException e) {
|
||||
Log.e("ShizukuSample", "UserManager#getUsers", e);
|
||||
} finally {
|
||||
data.recycle();
|
||||
reply.recycle();
|
||||
}
|
||||
return res;
|
||||
}*/
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package dev.imranr.obtainium.util;
|
||||
|
||||
public abstract class Singleton<T> {
|
||||
|
||||
private T mInstance;
|
||||
|
||||
protected abstract T create();
|
||||
|
||||
public final T get() {
|
||||
synchronized (this) {
|
||||
if (mInstance == null) {
|
||||
mInstance = create();
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
org.gradle.jvmargs=-Xmx1536M
|
||||
org.gradle.jvmargs=-Xmx2048M
|
||||
android.useAndroidX=true
|
||||
android.enableJetifier=true
|
||||
|
Reference in New Issue
Block a user