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

BIN
snet.apk

Binary file not shown.

View File

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