diff --git a/app/build.gradle b/app/build.gradle index 84861ebc..45c68560 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.aefyr.sai" minSdkVersion 21 targetSdkVersion 28 - versionCode 31 - versionName "2.1" + versionCode 32 + versionName "2.2" } buildTypes { release { diff --git a/app/src/main/java/com/aefyr/sai/installer/rooted/RootedSAIPackageInstaller.java b/app/src/main/java/com/aefyr/sai/installer/rooted/RootedSAIPackageInstaller.java index 134631d9..50ea4686 100644 --- a/app/src/main/java/com/aefyr/sai/installer/rooted/RootedSAIPackageInstaller.java +++ b/app/src/main/java/com/aefyr/sai/installer/rooted/RootedSAIPackageInstaller.java @@ -8,6 +8,7 @@ import android.content.pm.PackageManager; import android.os.Build; import android.util.Log; +import android.util.Pair; import com.aefyr.sai.BuildConfig; import com.aefyr.sai.R; @@ -17,6 +18,8 @@ import com.aefyr.sai.utils.Root; import com.aefyr.sai.utils.Utils; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,6 +27,9 @@ public class RootedSAIPackageInstaller extends SAIPackageInstaller { private static final String TAG = "RootedSAIPI"; + private static final String COMMAND_CREATE_SESSION_NORMAL = "pm install-create -r --install-location 0 -i %s"; + private static final String COMMAND_CREATE_SESSION_LITE = "pm install-create -r -i %s"; + @SuppressLint("StaticFieldLeak")//This is application context, lul private static RootedSAIPackageInstaller sInstance; @@ -77,11 +83,7 @@ protected void installApkFiles(ApkSource apkSource) { return; } - String result = ensureCommandSucceeded(Root.exec(String.format("pm install-create -r --install-location 0 -i %s", BuildConfig.APPLICATION_ID))); - Pattern sessionIdPattern = Pattern.compile("(\\d+)"); - Matcher sessionIdMatcher = sessionIdPattern.matcher(result); - sessionIdMatcher.find(); - int sessionId = Integer.parseInt(sessionIdMatcher.group(1)); + int sessionId = createSession(); while (apkSource.nextApk()) ensureCommandSucceeded(Root.exec(String.format("pm install-write -S %d %d \"%s\"", apkSource.getApkLength(), sessionId, apkSource.getApkName()), apkSource.openApkInputStream())); @@ -115,4 +117,46 @@ private String getSessionInfo(ApkSource apkSource) { } return String.format("%s: %s %s | %s | Android %s | Using %s ApkSource implementation | SAI %s", getContext().getString(R.string.installer_device), Build.BRAND, Build.MODEL, Utils.isMiui() ? "MIUI" : "Not MIUI", Build.VERSION.RELEASE, apkSource.getClass().getSimpleName(), saiVersion); } + + private int createSession() throws RuntimeException { + ArrayList commandsToAttempt = new ArrayList<>(); + commandsToAttempt.add(String.format(COMMAND_CREATE_SESSION_NORMAL, BuildConfig.APPLICATION_ID)); + commandsToAttempt.add(String.format(COMMAND_CREATE_SESSION_LITE, BuildConfig.APPLICATION_ID)); + + List> attemptedCommands = new ArrayList<>(); + + for (String commandToAttempt : commandsToAttempt) { + String result = ensureCommandSucceeded(Root.exec(commandToAttempt)); + attemptedCommands.add(new Pair<>(commandToAttempt, result)); + + Integer sessionId = extractSessionId(result); + if (sessionId != null) + return sessionId; + else + Log.w(TAG, String.format("Command failed: %s > %s", commandToAttempt, result)); + } + + StringBuilder exceptionMessage = new StringBuilder("Unable to create session, attempted commands: "); + for (Pair attemptedCommand : attemptedCommands) { + exceptionMessage.append("\n\n==========================\n") + .append(attemptedCommand.first) + .append("\nVVVVVVVVVVVVVVVV\n") + .append(attemptedCommand.second); + } + exceptionMessage.append("\n"); + + throw new IllegalStateException(exceptionMessage.toString()); + } + + private Integer extractSessionId(String commandResult) { + try { + Pattern sessionIdPattern = Pattern.compile("(\\d+)"); + Matcher sessionIdMatcher = sessionIdPattern.matcher(commandResult); + sessionIdMatcher.find(); + return Integer.parseInt(sessionIdMatcher.group(1)); + } catch (Exception e) { + Log.w(TAG, commandResult, e); + return null; + } + } } diff --git a/build.gradle b/build.gradle index 514e4539..b22d1fde 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ buildscript { } dependencies { classpath 'com.google.gms:google-services:4.3.0' - classpath 'com.android.tools.build:gradle:3.4.2' + classpath 'com.android.tools.build:gradle:3.5.0' classpath 'io.fabric.tools:gradle:1.26.1' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a169119c..01ad9889 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 01 14:59:44 MSK 2019 +#Sat Aug 24 23:37:25 MSK 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip