package com.sabik.assistantenabler;

import android.content.SharedPreferences;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

import static de.robv.android.xposed.XposedBridge.log;
import static de.robv.android.xposed.XposedHelpers.findAndHookConstructor;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.findClass;
import static de.robv.android.xposed.XposedHelpers.getObjectField;

public class AssistantEnabler implements IXposedHookLoadPackage {

	private static final String PACKAGE_NAME = "com.google.android.googlequicksearchbox";
	private static final String GSA_PACKAGE = "com.google.android.apps.gsa";
	private static final String ASSISTANT_PACKAGE = GSA_PACKAGE + ".assistant";

	@Override
	public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
		if (!PACKAGE_NAME.equals(lpparam.packageName))
			return;

		try {
			Class a = findClass(ASSISTANT_PACKAGE + ".a.e", lpparam.classLoader);

			findAndHookConstructor(a, findClass("com.google.android.apps.gsa.search.core.config.GsaConfigFlags", lpparam.classLoader), SharedPreferences.class, new XC_MethodHook() {
				@Override
				protected void afterHookedMethod(MethodHookParam param) throws Throwable {
					SharedPreferences prefs = (SharedPreferences) getObjectField(param.thisObject, "bhX");
					prefs.edit().putBoolean("key_opa_eligible", true)
							.putBoolean("opa_enabled", true)
							.putBoolean("opa_hotword_enabled", true)
							.putBoolean("opa_hotword_transition_seen", true).apply();
				}
			});

			findAndHookMethod(a, "aG", boolean.class, new XC_MethodHook() {
				@Override
				protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
					param.args[0] = true;
				}
			});

			findAndHookMethod(a, "pa", new XC_MethodReplacement() {
				@Override
				protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
					return true;
				}
			});

			findAndHookMethod(a, "oZ", new XC_MethodReplacement() {
				@Override
				protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
					return true;
				}
			});

			findAndHookMethod(a, "pb", new XC_MethodReplacement() {
				@Override
				protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
					return true;
				}
			});

			findAndHookMethod(GSA_PACKAGE + ".shared.util.c", lpparam.classLoader, "v", String.class, boolean.class, new XC_MethodHook() {
				@Override
				protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
					if (param.args[0].toString().equals("ro.opa.eligible_device")) {
						param.setResult(true);
					}
				}
			});
		} catch (Throwable t) {
			log(t);
		}
	}

}

@@ -30,16 +30,48 @@
 abler;%0A%0A
+import android.content.Context;%0A
 import a
@@ -256,16 +256,61 @@
 cement;%0A
+import de.robv.android.xposed.XposedHelpers;%0A
 import d
@@ -1010,18 +1010,50 @@
 stant%22;%0A
-%0A
 %09
+private String bhX = %22bhX%22;%0A%0A    
 @Overrid
@@ -1191,16 +1191,692 @@
 eturn;%0A%0A
+%09%09Object activityThread = XposedHelpers.callStaticMethod(XposedHelpers.findClass(%22android.app.ActivityThread%22, null), %22currentActivityThread%22);%0A%09%09Context context = (Context) XposedHelpers.callMethod(activityThread, %22getSystemContext%22);%0A%09%09String versionName = context.getPackageManager().getPackageInfo(lpparam.packageName, 0).versionName;%0A%0A        String aG;%0A        String pa;%0A        String oZ;%0A        String pb;%0A        if (versionName.matches(%226.6.*%22)) %7B%0A%09%09%09bhX = %22bhX%22;%0A%09%09%09aG = %22aG%22;%0A%09%09%09pa = %22pa%22;%0A%09%09%09oZ = %22oZ%22;%0A%09%09%09pb = %22pb%22;%0A%09%09%7D else if (versionName.matches(%226.7.*%22)) %7B%0A%09%09%09bhX = %22biJ%22;%0A%09%09%09aG = %22aK%22;%0A%09%09%09pa = %22pc%22;%0A%09%09%09oZ = %22pb%22;%0A%09%09%09pb = %22pd%22;%0A%09%09%7D else %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A
 %09%09try %7B%0A
@@ -2299,21 +2299,19 @@
 Object, 
-%22
 bhX
-%22
 );%0A%09%09%09%09%09
@@ -2551,20 +2551,18 @@
 thod(a, 
-%22
 aG
-%22
 , boolea
@@ -2751,20 +2751,18 @@
 thod(a, 
-%22
 pa
-%22
 , new XC
@@ -2937,20 +2937,18 @@
 thod(a, 
-%22
 oZ
-%22
 , new XC
@@ -3123,20 +3123,18 @@
 thod(a, 
-%22
 pb
-%22
 , new XC

Fix compatibility issues with Google App 6.7