package com.gmail.alexellingsen.sense6transparentrecents;

import android.app.Activity;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class Sense6TransparentRecents implements IXposedHookLoadPackage {

    public static final String PACKAGE = "com.android.systemui";
    public static final String TAG = "Sense6TransparentRecents";

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

        Class<?> findClass;

        try {
            findClass = XposedHelpers.findClass(
                    PACKAGE + ".recent.RecentAppFxActivity",
                    lpparam.classLoader
            );
        } catch (Throwable e) {
            XposedBridge.log(e);
            return;
        }

        XposedHelpers.findAndHookMethod(
                findClass,
                "onCreate",
                android.os.Bundle.class,

                new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        Activity thiz = (Activity) param.thisObject;

                        // Show status bar
                        thiz.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

                        // Enable translucent status bar
                        thiz.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

                        // Get status bar height
                        final int statusBarHeight = getStatusBarHeight(thiz.getResources());

                        // Get the action bar view
                        final View actionBarView = (View) XposedHelpers.getObjectField(thiz, "actionContainer");

                        // Insert a new View to color the status bar & move action bar down
                        View statusBarColor = new View(actionBarView.getContext());

                        // Match action bar background, and set minimum height
                        statusBarColor.setBackground(actionBarView.getBackground());
                        statusBarColor.setMinimumHeight(statusBarHeight);

                        // Insert view before action bar into parent
                        ((ViewGroup) actionBarView.getParent()).addView(
                                statusBarColor,
                                0,
                                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight)
                        );

                        // Enable translucent navigation bar
                        thiz.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

                        // Get 3rd child view to fix bottom padding
                        View background = ((ViewGroup) actionBarView.getParent()).getChildAt(2);

                        // Get navigation bar height
                        int navigationBarHeight = getNavigationBarHeight(thiz.getResources());

                        // Increase the bottom padding by navigation bar height
                        background.setPadding(
                                background.getPaddingLeft(),
                                background.getPaddingTop(),
                                background.getPaddingRight(),
                                background.getPaddingBottom() + navigationBarHeight
                        );
                    }
                });

        XposedBridge.log("[" + TAG + "] Hooked recent apps activity");
    }

    public int getNavigationBarHeight(Resources resources) {
        int result = 0;
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public int getStatusBarHeight(Resources resources) {
        int result = 0;
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

@@ -211,16 +211,52 @@
 anager;%0A
+import android.widget.LinearLayout;%0A
 import d
@@ -1991,32 +1991,77 @@
 tResources());%0A%0A
+                        View actionBarView;%0A%0A
                 
@@ -2123,96 +2123,707 @@
     
-final View actionBarView = (View) XposedHelpers.getObjectField(thiz, %22actionContainer%22);
+try %7B%0A                            // Try to get 'actionContainer' field. It will throw an NoSuchFieldError if it doesn't exists,%0A                            // and if it doesn't try an alternative method.%0A                            actionBarView = (View) XposedHelpers.getObjectField(thiz, %22actionContainer%22);%0A                        %7D catch (NoSuchFieldError e) %7B%0A                            // Find root layout. ID is from decompiling SystemUI.apk%0A                            LinearLayout rootLayout = (LinearLayout) thiz.findViewById(0x7f070044);%0A%0A                            // Get first child view%0A                            actionBarView = rootLayout.getChildAt(0);%0A                        %7D
 %0A%0A  

Add support for newer Sense 6