Fix crashes when Google Play Service require update

This commit is contained in:
topjohnwu 2017-11-24 22:15:46 +08:00
parent f5ceee547c
commit 43745edac0
3 changed files with 38 additions and 18 deletions

View File

@ -19,8 +19,9 @@ import com.topjohnwu.magisk.utils.Utils;
public class Activity extends AppCompatActivity {
private AssetManager mAssetManager = null;
private Resources mResources = null;
private AssetManager swappedAssetManager = null;
private Resources swappedResources = null;
private Resources.Theme swappedTheme = null;
private ActivityResultListener activityResultListener;
public Activity() {
@ -57,14 +58,19 @@ public class Activity extends AppCompatActivity {
mm.permissionGrantCallback = null;
}
@Override
public Resources.Theme getTheme() {
return swappedTheme == null ? super.getTheme() : swappedTheme;
}
@Override
public AssetManager getAssets() {
return mAssetManager == null ? super.getAssets() : mAssetManager;
return swappedAssetManager == null ? super.getAssets() : swappedAssetManager;
}
@Override
public Resources getResources() {
return mResources == null ? super.getResources() : mResources;
return swappedResources == null ? super.getResources() : swappedResources;
}
public MagiskManager getMagiskManager() {
@ -92,30 +98,27 @@ public class Activity extends AppCompatActivity {
activityResultListener = null;
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
startActivityForResult(intent, requestCode, this::onActivityResult);
}
public void startActivityForResult(Intent intent, int requestCode, ActivityResultListener listener) {
activityResultListener = listener;
super.startActivityForResult(intent, requestCode);
}
@Keep
public void swapResources(String dexPath) {
mAssetManager = Utils.getAssets(dexPath);
if (mAssetManager == null)
public void swapResources(String dexPath, int resId) {
swappedAssetManager = Utils.getAssets(dexPath);
if (swappedAssetManager == null)
return;
Resources res = super.getResources();
mResources = new Resources(mAssetManager, res.getDisplayMetrics(), res.getConfiguration());
mResources.newTheme().setTo(super.getTheme());
swappedResources = new Resources(swappedAssetManager, res.getDisplayMetrics(), res.getConfiguration());
swappedTheme = swappedResources.newTheme();
swappedTheme.applyStyle(resId, true);
}
@Keep
public void restoreResources() {
mAssetManager = null;
mResources = null;
swappedAssetManager = null;
swappedResources = null;
swappedTheme = null;
}
public interface ActivityResultListener {

BIN
snet.apk

Binary file not shown.

View File

@ -1,6 +1,7 @@
package com.topjohnwu.snet;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -17,6 +18,7 @@ import com.google.android.gms.safetynet.SafetyNetApi;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Field;
import java.security.SecureRandom;
public class SafetyNetHelper
@ -35,12 +37,22 @@ public class SafetyNetHelper
private int responseCode;
private SafetyNetCallback cb;
private String dexPath;
private boolean isDarkTheme;
public SafetyNetHelper(Activity activity, String dexPath, SafetyNetCallback cb) {
mActivity = activity;
this.cb = cb;
this.dexPath = dexPath;
responseCode = 0;
// Get theme
try {
Context context = activity.getApplicationContext();
Field theme = context.getClass().getField("isDarkTheme");
isDarkTheme = (boolean) theme.get(context);
} catch (Exception e) {
e.printStackTrace();
}
}
// Entry point to start test
@ -64,8 +76,13 @@ public class SafetyNetHelper
Class<? extends Activity> clazz = mActivity.getClass();
try {
// Use external resources
clazz.getMethod("swapResources", String.class).invoke(mActivity, dexPath);
clazz.getMethod("swapResources", String.class, int.class).invoke(mActivity, dexPath,
isDarkTheme ? android.R.style.Theme_Material : android.R.style.Theme_Material_Light);
try {
GoogleApiAvailability.getInstance().getErrorDialog(mActivity, result.getErrorCode(), 0).show();
} catch (Exception e) {
e.printStackTrace();
}
clazz.getMethod("restoreResources").invoke(mActivity);
} catch (Exception e) {
e.printStackTrace();