Skip to content

Commit

Permalink
make rooted installer compatible with some devices
Browse files Browse the repository at this point in the history
update gradle
increment version
  • Loading branch information
Aefyr committed Aug 24, 2019
1 parent e621579 commit 68d61cc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,13 +18,18 @@
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;

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;

Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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<String> 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<Pair<String, String>> 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<String, String> 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;
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 68d61cc

Please sign in to comment.