package com.ceco.gm2.gravitybox;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Message;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class GeminiPhoneWrapper {
    private static final String TAG = "PhoneWrapper";

    public static final int NT_WCDMA_PREFERRED = 0;
    public static final int NT_GSM_ONLY = 1;
    public static final int NT_WCDMA_ONLY = 2;
    public static final int NT_GSM_WCDMA_AUTO = 3;

    public static final String PREFERRED_NETWORK_MODE = "preferred_network_mode";

    public static final String ACTION_CHANGE_NETWORK_TYPE = "gravitybox.intent.action.CHANGE_NETWORK_TYPE";
    public static final String EXTRA_NETWORK_TYPE = "networkType";

    private static Class<?> mClsPhoneFactory;
    private static Context mContext;

    private static void log(String msg) {
        XposedBridge.log(TAG + ": " + msg);
    }

    private static BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_CHANGE_NETWORK_TYPE) &&
                    intent.hasExtra(EXTRA_NETWORK_TYPE)) {
                int networkType = intent.getIntExtra(EXTRA_NETWORK_TYPE, NT_WCDMA_PREFERRED);
                log("received ACTION_CHANGE_NETWORK_TYPE broadcast: networkType = " + networkType);
                setPreferredNetworkType(networkType);
            }
        }
    };

    public static void initZygote() {

        log("Entering init state");
        
        mClsPhoneFactory = XposedHelpers.findClass("com.android.internal.telephony.PhoneFactory", null);

        XposedHelpers.findAndHookMethod(mClsPhoneFactory, "makeDefaultPhones", 
                Context.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                mContext = (Context) param.args[0];
                log("PhoneFactory makeDefaultPhones - phone wrapper initialized");
                onInitialize();
            }
        });
    }

    private static void onInitialize() {
        if (mContext != null) {
            IntentFilter intentFilter = new IntentFilter(ACTION_CHANGE_NETWORK_TYPE);
            mContext.registerReceiver(mBroadcastReceiver, intentFilter);
        }
    }

    private static void setPreferredNetworkType(int networkType) {
        Object defPhone = XposedHelpers.callStaticMethod(mClsPhoneFactory, "getDefaultPhone");
        if (defPhone == null) return;

        if (Utils.isMtkDevice()) {
            Class<?>[] paramArgs = new Class<?>[3];
            paramArgs[0] = int.class;
            paramArgs[1] = Message.class;
            paramArgs[2] = int.class;
            XposedHelpers.callMethod(defPhone, "setPreferredNetworkTypeGemini", paramArgs, networkType, null, 0);
        } else {
            Class<?>[] paramArgs = new Class<?>[2];
            paramArgs[0] = int.class;
            paramArgs[1] = Message.class;
            XposedHelpers.callMethod(defPhone, "setPreferredNetworkType", paramArgs, networkType, null);
        }
    }
}
@@ -1710,26 +1710,24 @@
 Zygote() %7B%0D%0A
-%0D%0A
         log(
@@ -1751,24 +1751,26 @@
 ate%22);%0D%0A
+%0D%0A
         
 %0D%0A      
@@ -1761,18 +1761,27 @@
         
-%0D%0A
+try %7B%0D%0A    
         
@@ -1876,24 +1876,125 @@
 , null);%0D%0A%0D%0A
+            String methodName = Utils.isMtkDevice() ? %22makeDefaultPhones%22 : %22makeDefaultPhone%22;%0D%0A    
         Xpos
@@ -2043,31 +2043,26 @@
 ry, 
-%22makeDefaultPhones%22, %0D%0A
+methodName, %0D%0A    
     
@@ -2115,32 +2115,36 @@
  %7B%0D%0A            
+  
+  
 @Override%0D%0A     
@@ -2130,32 +2130,36 @@
      @Override%0D%0A
+    
             prot
@@ -2244,32 +2244,36 @@
                 
+  
+  
 mContext = (Cont
@@ -2289,24 +2289,28 @@
 m.args%5B0%5D;%0D%0A
+    
             
@@ -2397,16 +2397,20 @@
         
+ 
+   
 onInitia
@@ -2422,32 +2422,36 @@
 );%0D%0A            
+    
 %7D%0D%0A        %7D);%0D%0A
@@ -2449,13 +2449,95 @@
     
+  
+  
 %7D);%0D%0A
+        %7D catch (Exception e) %7B%0D%0A            XposedBridge.log(e);%0D%0A        %7D%0D%0A
     

use correct method name based on device type