package mobi.acpm.inspeckage.hooks;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import mobi.acpm.inspeckage.receivers.InspeckageReceiver;

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

/**
 * Created by acpm on 29/12/15.
 */
public class MiscHook extends XC_MethodHook {

    //Miscelllaneous
    public static final String TAG = "Inspeckage_Misc:";

    public static void initAllHooks(final XC_LoadPackage.LoadPackageParam loadPackageParam) {



        findAndHookMethod(Uri.class, "parse", String.class, new XC_MethodHook() {

            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                XposedBridge.log(TAG + "URI: " + param.args[0] + "");
            }
        });

        findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {

            protected void afterHookedMethod(MethodHookParam param) throws Throwable {

                Activity appCompatActivity = (Activity) param.thisObject;

                Context context = (Context) appCompatActivity.getApplicationContext();
                context.registerReceiver(new InspeckageReceiver(param.thisObject),
                        new IntentFilter("mobi.acpm.inspeckage.INSPECKAGE_FILTER"));

            }
        });

        findAndHookMethod(Fragment.class, "onCreate", Bundle.class, new XC_MethodHook() {

            protected void afterHookedMethod(MethodHookParam param) throws Throwable {

                Fragment fragment = (Fragment) param.thisObject;

                Context context = (Context) fragment.getActivity().getApplicationContext();
                context.registerReceiver(new InspeckageReceiver(param.thisObject),
                        new IntentFilter("mobi.acpm.inspeckage.INSPECKAGE_FILTER"));

            }
        });

        findAndHookMethod(Activity.class, "finish", new XC_MethodHook() {

            protected void afterHookedMethod(MethodHookParam param) throws Throwable {

                android.os.Process.killProcess(android.os.Process.myPid());

            }
        });





/**
        findAndHookMethod("com.squareup.okhttp.internal.http.HttpsURLConnectionImpl", loadPackageParam.classLoader, "getInputStream", new XC_MethodHook() {

            protected void afterHookedMethod(MethodHookParam param) throws Throwable {

                HttpsURLConnection urlConn = (HttpsURLConnection) param.thisObject;

                if (urlConn != null) {
                    StringBuilder sb = new StringBuilder();
                    Map<String, List<String>> properties = urlConn.getRequestProperties();
                    if (properties != null && properties.size() > 0) {
                        Collection<List<String>> coll = properties.values();
                        if (coll != null && coll.size() > 0) {
                            for (List<String> ls : coll) {
                                for (String s : ls) {
                                    sb.append(s);
                                }
                            }
                        }
                    }

                    XposedBridge.log(TAG + "HTTPS REQUEST: method=" + urlConn.getRequestMethod() + " " +
                            "URL=" + urlConn.getURL().toString() + " " +
                            "Params=" + sb.toString());
                }

            }
        });
**/
        /**
        Class<?> ahc = findClass("com.loopj.android.http.AsyncHttpClient", loadPackageParam.classLoader);
        findMethodBestMatch(ahc, "post", Context.class, String.class, "RequestParams", new XC_MethodHook() {
            //findAndHookMethod("com.loopj.android.http.AsyncHttpClient",loadPackageParam.classLoader, "post",Context.class, String.class, "RequestParams",new XC_MethodHook() {

                protected void afterHookedMethod (MethodHookParam param)throws Throwable {

                    XposedBridge.log(TAG + "URL: " + param.args[1] + "");
                }
            });**/
        }
}
@@ -49,168 +49,15 @@
 oid.
-app.Activity;%0Aimport android.app.Fragment;%0Aimport android.content.Context;%0Aimport android.content.IntentFilter;%0Aimport android.net.Uri;%0Aimport android.os.Bundle
+net.Uri
 ;%0A%0Ai
@@ -203,66 +203,8 @@
 age;
-%0Aimport mobi.acpm.inspeckage.receivers.InspeckageReceiver;
 %0A%0Aim
@@ -534,17 +534,16 @@
 am) %7B%0A%0A%0A
-%0A
         
@@ -805,3255 +805,8 @@
 );%0A%0A
-        findAndHookMethod(Activity.class, %22onCreate%22, Bundle.class, new XC_MethodHook() %7B%0A%0A            protected void afterHookedMethod(MethodHookParam param) throws Throwable %7B%0A%0A                Activity appCompatActivity = (Activity) param.thisObject;%0A%0A                Context context = (Context) appCompatActivity.getApplicationContext();%0A                context.registerReceiver(new InspeckageReceiver(param.thisObject),%0A                        new IntentFilter(%22mobi.acpm.inspeckage.INSPECKAGE_FILTER%22));%0A%0A            %7D%0A        %7D);%0A%0A        findAndHookMethod(Fragment.class, %22onCreate%22, Bundle.class, new XC_MethodHook() %7B%0A%0A            protected void afterHookedMethod(MethodHookParam param) throws Throwable %7B%0A%0A                Fragment fragment = (Fragment) param.thisObject;%0A%0A                Context context = (Context) fragment.getActivity().getApplicationContext();%0A                context.registerReceiver(new InspeckageReceiver(param.thisObject),%0A                        new IntentFilter(%22mobi.acpm.inspeckage.INSPECKAGE_FILTER%22));%0A%0A            %7D%0A        %7D);%0A%0A        findAndHookMethod(Activity.class, %22finish%22, new XC_MethodHook() %7B%0A%0A            protected void afterHookedMethod(MethodHookParam param) throws Throwable %7B%0A%0A                android.os.Process.killProcess(android.os.Process.myPid());%0A%0A            %7D%0A        %7D);%0A%0A%0A%0A%0A%0A/**%0A        findAndHookMethod(%22com.squareup.okhttp.internal.http.HttpsURLConnectionImpl%22, loadPackageParam.classLoader, %22getInputStream%22, new XC_MethodHook() %7B%0A%0A            protected void afterHookedMethod(MethodHookParam param) throws Throwable %7B%0A%0A                HttpsURLConnection urlConn = (HttpsURLConnection) param.thisObject;%0A%0A                if (urlConn != null) %7B%0A                    StringBuilder sb = new StringBuilder();%0A                    Map%3CString, List%3CString%3E%3E properties = urlConn.getRequestProperties();%0A                    if (properties != null && properties.size() %3E 0) %7B%0A                        Collection%3CList%3CString%3E%3E coll = properties.values();%0A                        if (coll != null && coll.size() %3E 0) %7B%0A                            for (List%3CString%3E ls : coll) %7B%0A                                for (String s : ls) %7B%0A                                    sb.append(s);%0A                                %7D%0A                            %7D%0A                        %7D%0A                    %7D%0A%0A                    XposedBridge.log(TAG + %22HTTPS REQUEST: method=%22 + urlConn.getRequestMethod() + %22 %22 +%0A                            %22URL=%22 + urlConn.getURL().toString() + %22 %22 +%0A                            %22Params=%22 + sb.toString());%0A                %7D%0A%0A            %7D%0A        %7D);%0A**/%0A        /**%0A        Class%3C?%3E ahc = findClass(%22com.loopj.android.http.AsyncHttpClient%22, loadPackageParam.classLoader);%0A        findMethodBestMatch(ahc, %22post%22, Context.class, String.class, %22RequestParams%22, new XC_MethodHook() %7B%0A            //findAndHookMethod(%22com.loopj.android.http.AsyncHttpClient%22,loadPackageParam.classLoader, %22post%22,Context.class, String.class, %22RequestParams%22,new XC_MethodHook() %7B%0A%0A                protected void afterHookedMethod (MethodHookParam param)throws Throwable %7B%0A%0A                    XposedBridge.log(TAG + %22URL: %22 + param.args%5B1%5D + %22%22);%0A                %7D%0A            %7D);**/%0A    
     

move Activity and Fragment hooks from MiscHook to UIHook