package com.pyler.youtubebackgroundplayback;

import android.content.Context;
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.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class YouTubeBackgroundPlayback implements IXposedHookLoadPackage {

	public static final String YOUTUBE_PACKAGE = "com.google.android.youtube";
	public static final int[] YOUTUBE_VERSION = { 1076, 1077, 1078, 1079, 1080 };
	public static final String[] CLASS_1 = { "knb", "koj", "kqp", "kul", "kys" };
	public static final String[] METHOD_1_1 = { "J", "K", "O", "P", "P" };
	public static final String[] FIELD_1_1 = { "f", "e", "e", "e", "e" };
	public static final String[] FIELD_1_2 = { "d", "e", "e", "e", "e" };
	public static final String[] CLASS_2 = { "iha", "ihy", "ijx", "ink", "iqq" };
	public static final String[] METHOD_2_1 = { "a", "a", "a", "a", "a" };
	public static final String[] FIELD_2_1 = { "c", "c", "c", "c", "c" };
	public int id;

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

		Object activityThread = XposedHelpers.callStaticMethod(
			XposedHelpers.findClass("android.app.ActivityThread", null),
			"currentActivityThread");
		Context context = (Context) XposedHelpers.callMethod(activityThread,
			"getSystemContext");
		int versionCode = context.getPackageManager().getPackageInfo(
			lpparam.packageName, 0).versionCode / 100000;
		id = getVersionIndex(versionCode);
		
		if (id != -1 ) {

			XposedHelpers.findAndHookMethod(CLASS_1[id], lpparam.classLoader, METHOD_1_1[id], new XC_MethodHook() {
					@Override
					protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
						super.beforeHookedMethod(param);
						Object o = XposedHelpers.getObjectField(param.thisObject, FIELD_1_1[id]);
						XposedHelpers.setBooleanField(o, FIELD_1_2[id], true);
					}
			});

			XposedHelpers.findAndHookMethod(CLASS_2[id], lpparam.classLoader, METHOD_2_1[id], new XC_MethodHook() {
					@Override
					protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
						super.beforeHookedMethod(param);
						XposedHelpers.setBooleanField(param.thisObject, FIELD_2_1[id], true);
					}
			});	
			
		} else {
			XposedBridge.log("This YouTube version is not supported yet.");
		}
	}

	public int getVersionIndex(int versionCode) {
		for (int i = 0; i < YOUTUBE_VERSION.length; i++) {
			if ( versionCode == YOUTUBE_VERSION[i] ) return i;
		}
		return -1;
	}
}

@@ -590,34 +590,16 @@
 %7B 10
-76, 1077, 1078, 1079
+80
 , 108
-0
+3
  %7D;%0A
@@ -646,38 +646,17 @@
 %7B %22k
-nb%22, %22koj%22, %22kqp%22, %22kul%22, %22kys
+yr%22, %22lco
 %22 %7D;
@@ -704,31 +704,16 @@
  = %7B
- %22J%22, %22K%22, %22O%22,
  %22P%22, %22
-P
+a
 %22 %7D;
@@ -762,29 +762,14 @@
  %7B %22
-f%22, %22e%22, %22e%22, %22e%22, %22e
+e%22, %22d
 %22 %7D;
@@ -816,23 +816,8 @@
  = %7B
- %22d%22, %22e%22, %22e%22,
  %22e%22
@@ -873,38 +873,17 @@
 %7B %22i
-ha%22, %22ihy%22, %22ijx%22, %22ink%22, %22iqq
+qp%22, %22iur
 %22 %7D;
@@ -932,31 +932,16 @@
 = %7B %22a%22,
- %22a%22, %22a%22, %22a%22,
  %22a%22 %7D;%0A
@@ -992,30 +992,130 @@
 %22c%22, %22c%22
-, %22c%22,
+ %7D;%0A%09public static final String%5B%5D CLASS_3 = %7B %22azq%22, %22azl%22 %7D;%0A%09public static final String%5B%5D METHOD_3_1 = %7B
  %22c%22, %22
-c
+d
 %22 %7D;%0A%09pu
@@ -2439,16 +2439,146 @@
 );%09%0A%09%09%09%0A
+%09%09%09XposedHelpers.findAndHookMethod(CLASS_3%5Bid%5D,lpparam.classLoader,METHOD_3_1%5Bid%5D,XC_MethodReplacement.returnConstant(%22on%22));%0A%09%09%09%0A
 %09%09%7D else
@@ -2831,8 +2831,9 @@
 1;%0A%09%7D%0A%7D%0A
+%0A

Support for yt 10.43.xx