package com.gmail.alexellingsen.g2skintweaks.hooks;

import android.content.res.XModuleResources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.gmail.alexellingsen.g2skintweaks.Prefs;
import com.gmail.alexellingsen.g2skintweaks.R;
import com.gmail.alexellingsen.g2skintweaks.utils.SettingsHelper;
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_InitPackageResources.InitPackageResourcesParam;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class LGLockScreenHook {

    private static final String PACKAGE = "com.android.keyguard";
    private static final String USE_PACKAGE = "com.lge.lockscreen";

    private static XModuleResources mModRes;
    private static SettingsHelper mSettings;

    public static void init(SettingsHelper settings) {
        mSettings = settings;
    }

    public static void handleInitPackageResources(InitPackageResourcesParam resparam, XModuleResources modRes) {
        if (!resparam.packageName.equals(PACKAGE)) {
            return;
        }

        mModRes = modRes;
    }

    private static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    public static void handleLoadPackage(final LoadPackageParam lpparam) {
        if (!lpparam.packageName.equals(PACKAGE)) {
            return;
        }

        Class<?> findClass;

        try {
            findClass = XposedHelpers.findClass(
                    USE_PACKAGE + ".widget.pattern.LgeLockPatternView",
                    lpparam.classLoader
            );
        } catch (Throwable e) {
            XposedBridge.log(e);
            XposedBridge.log("Couldn't find class");
            return;
        }

        XposedHelpers.findAndHookMethod(
                findClass,
                "drawCircle",
                "android.graphics.Canvas",
                "int",
                "int",
                "boolean",

                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        if (mSettings.getBoolean(Prefs.ENABLE_AOSP_PATTERN_DOTS, false)) {
                            Drawable d1 = mModRes.getDrawable(R.drawable.indicator_code_lock_point_area_default_holo);
                            Drawable d2 = mModRes.getDrawable(R.drawable.indicator_code_lock_point_area_green_holo);
                            Drawable d3 = mModRes.getDrawable(R.drawable.indicator_code_lock_point_area_red_holo);

                            Bitmap mBitmap1 = drawableToBitmap(d1);
                            Bitmap mBitmap2 = drawableToBitmap(d2);
                            Bitmap mBitmap3 = drawableToBitmap(d3);

                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleDefault", mBitmap1);
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleGreen", mBitmap2);
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleRed", mBitmap3);
                        }
                    }
                }
        );
    }
}
@@ -1065,733 +1065,8 @@
  %7D%0A%0A
-    public static void handleInitPackageResources(InitPackageResourcesParam resparam, XModuleResources modRes) %7B%0A        if (!resparam.packageName.equals(PACKAGE)) %7B%0A            return;%0A        %7D%0A%0A        mModRes = modRes;%0A    %7D%0A%0A    private static Bitmap drawableToBitmap(Drawable drawable) %7B%0A        if (drawable instanceof BitmapDrawable) %7B%0A            return ((BitmapDrawable) drawable).getBitmap();%0A        %7D%0A%0A        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);%0A        Canvas canvas = new Canvas(bitmap);%0A        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());%0A        drawable.draw(canvas);%0A%0A        return bitmap;%0A    %7D%0A%0A
     
@@ -3020,10 +3020,735 @@
 );%0A    %7D
+%0A%0A    public static void handleInitPackageResources(InitPackageResourcesParam resparam, XModuleResources modRes) %7B%0A        if (!resparam.packageName.equals(PACKAGE)) %7B%0A            return;%0A        %7D%0A%0A        mModRes = modRes;%0A    %7D%0A%0A    private static Bitmap drawableToBitmap(Drawable drawable) %7B%0A        if (drawable instanceof BitmapDrawable) %7B%0A            return ((BitmapDrawable) drawable).getBitmap();%0A        %7D%0A%0A        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);%0A        Canvas canvas = new Canvas(bitmap);%0A        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());%0A        drawable.draw(canvas);%0A%0A        return bitmap;%0A    %7D
 %0A%7D

Move method up