package com.forbroteam.xposedparentalcontrol;

import android.app.ActivityManager;
import android.app.AndroidAppHelper;
import android.content.Context;
import android.widget.Toast;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookConstructor;

/**
 * Created by bogatenkov on 28/10/17.
 */

public class LoadHook implements IXposedHookLoadPackage {

    // TODO: add a specific application that should be tracked
    String targetPackage = "com.android.chrome";
    String packageName = null;
    boolean forceClose = false;

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        packageName = lpparam.packageName;

        XposedBridge.log("ParentalControl: Loaded " + packageName);
        if (!targetPackage.isEmpty() && !targetPackage.equals(packageName))
            return;

        findAndHookConstructor("java.net.URL", lpparam.classLoader, String.class,
                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("ParentalControl: " + packageName + " URL param 0: " + param.args[0]);
                        String url = (param != null && param.args.length > 0) ? ((String) param.args[0]) : null;
                        XposedBridge.log("ParentalControl: ");
                        XposedBridge.log("ParentalControl: " + packageName + " URL: " + url);
                        if (url.toString().contains("youtube")) {
                            forceClose = true;
                        }
                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        if (forceClose) {
                            Toast.makeText(AndroidAppHelper.currentApplication(),
                                    "Sorry, you are restricted to be here!", Toast.LENGTH_SHORT)
                                    .show();
                            forceClose = false;

                            ActivityManager am = (ActivityManager) AndroidAppHelper
                                    .currentApplication().getSystemService(Context.ACTIVITY_SERVICE);
                            am.killBackgroundProcesses(targetPackage);
                        }
                    }
                });
    }
}
@@ -565,71 +565,8 @@
     
-// TODO: add a specific application that should be tracked%0A    
 Stri
@@ -602,16 +602,57 @@
 id.chrom
+e%22;%0A    String urlToProtectFrom = %22youtub
 e%22;%0A    
@@ -1284,120 +1284,8 @@
 e %7B%0A
-                        XposedBridge.log(%22ParentalControl: %22 + packageName + %22 URL param 0: %22 + param.args%5B0%5D);%0A
     
@@ -1397,71 +1397,8 @@
 ll;%0A
-                        XposedBridge.log(%22ParentalControl: %22);%0A
     
@@ -1539,25 +1539,32 @@
 ontains(
-%22youtube%22
+urlToProtectFrom
 )) %7B%0A   
@@ -2108,32 +2108,181 @@
                 
+        terminateApplication();%0A                        %7D%0A                    %7D%0A                %7D);%0A    %7D%0A%0A    private void terminateApplication() %7B%0A
         Activity
@@ -2345,36 +2345,16 @@
         
-            
-        
 .current
@@ -2411,36 +2411,16 @@
 RVICE);%0A
-                    
         
@@ -2466,76 +2466,8 @@
 e);%0A
-                        %7D%0A                    %7D%0A                %7D);%0A
     

Refactor code