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 {

    String targetPackage = "com.android.chrome";
    String urlToProtectFrom = "youtube";
    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 {
                        String url = (param != null && param.args.length > 0) ? ((String) param.args[0]) : null;
                        XposedBridge.log("ParentalControl: " + packageName + " URL: " + url);
                        if (url.toString().contains(urlToProtectFrom)) {
                            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;

                            terminateApplication();
                        }
                    }
                });
    }

    private void terminateApplication() {
        ActivityManager am = (ActivityManager) AndroidAppHelper
                .currentApplication().getSystemService(Context.ACTIVITY_SERVICE);
        am.killBackgroundProcesses(targetPackage);
    }
}
@@ -145,16 +145,43 @@
 ontext;%0A
+import android.os.Handler;%0A
 import a
@@ -2172,19 +2172,23 @@
 ication(
+5000
 );%0A
-
         
@@ -2292,20 +2292,263 @@
 ication(
-) %7B%0A
+int milliseconds) %7B%0A        if (milliseconds %3C 0) %7B%0A            milliseconds = 0;%0A        %7D%0A%0A        final Handler handler = new Handler();%0A        handler.postDelayed(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         
@@ -2619,16 +2619,24 @@
         
+  
+      
 .current
@@ -2693,16 +2693,24 @@
 RVICE);%0A
+        
         
@@ -2756,11 +2756,53 @@
 e);%0A    
+        %7D%0A        %7D, milliseconds);%0A    %7D%0A
 %7D%0A%7D

Add timer for termination