package xyz.garywzh.simpleadb;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private static final String[] ENABLE = {
            "setprop service.adb.tcp.port 5555",
            "stop adbd",
            "start adbd",
            "getprop service.adb.tcp.port",
            "getprop dhcp.wlan0.ipaddress"};
    private static final String[] DISABLE = {
            "setprop service.adb.tcp.port -1",
            "stop adbd",
            "start adbd",
            "getprop service.adb.tcp.port",
            "getprop dhcp.wlan0.ipaddress"};
    private static final String[] CHECK = {
            "getprop service.adb.tcp.port",
            "getprop dhcp.wlan0.ipaddress"};
    private static final String WIFI_ADB_PORT = "5555";

    private TextView tipView;
    private SwitchCompat ADBSwitch;
    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tipView = (TextView) findViewById(R.id.tip);
        ADBSwitch = (SwitchCompat) findViewById(R.id.ADBSwitch);
        mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setWIFIADB(isChecked);
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        updateADBSwitch();
    }

    private void updateADBSwitch() {
        ADBSwitch.setOnCheckedChangeListener(null);
        configADB(CHECK, true);
    }

    private void setWIFIADB(final boolean enable) {
        configADB(enable ? ENABLE : DISABLE, false);
    }

    private void configADB(final String[] strings, final boolean isInit) {
        ADBSwitch.setEnabled(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                final String result = sudoForResult(strings).trim();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        final boolean isEnabled = result.contains(WIFI_ADB_PORT);
                        if (isEnabled) {
                            String[] parts = result.split("\n");
                            tipView.setText(String.format("%s%s", getString(R.string.tip), parts[1]));
                        }
                        tipView.setVisibility(isEnabled ? View.VISIBLE : View.INVISIBLE);
                        ADBSwitch.setChecked(isEnabled);
                        if (isInit) {
                            ADBSwitch.setOnCheckedChangeListener(mOnCheckedChangeListener);
                        }
                        ADBSwitch.setEnabled(true);
                    }
                });
            }
        }).start();
    }

    private String sudoForResult(String[] strings) {
        String res = "";

        try {
            Process proc = Runtime.getRuntime().exec("su");

            //input commands
            try (DataOutputStream outputStream = new DataOutputStream(proc.getOutputStream())) {
                for (String s : strings) {
                    outputStream.writeBytes(s + "\n");
                    outputStream.flush();
                }
                outputStream.writeBytes("exit\n");
                outputStream.flush();
                outputStream.close();
            }

            //wait for execution to finish
            try {
                proc.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //read response
            int read;
            char[] buffer = new char[1024];
            StringBuilder builder = new StringBuilder();
            try (BufferedReader inputReader = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
                while ((read = inputReader.read(buffer)) > 0) {
                    builder.append(buffer, 0, read);
                }
            }

            res = builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }
}

@@ -243,16 +243,44 @@
 xtView;%0A
+import android.widget.Toast;
 %0Aimport 
@@ -449,24 +449,25 @@
 tActivity %7B%0A
+%0A
     private 
@@ -499,36 +499,32 @@
 BLE = %7B%0A        
-    
 %22setprop service
@@ -544,36 +544,32 @@
  5555%22,%0A        
-    
 %22stop adbd%22,%0A   
@@ -557,36 +557,32 @@
    %22stop adbd%22,%0A
-    
         %22start a
@@ -587,36 +587,32 @@
  adbd%22,%0A        
-    
 %22getprop service
@@ -619,36 +619,32 @@
 .adb.tcp.port%22,%0A
-    
         %22getprop
@@ -710,28 +710,24 @@
 DISABLE = %7B%0A
-    
         %22set
@@ -761,28 +761,24 @@
 1%22,%0A        
-    
 %22stop adbd%22,
@@ -782,28 +782,24 @@
 d%22,%0A        
-  
-  
 %22start adbd%22
@@ -796,28 +796,24 @@
 tart adbd%22,%0A
-    
         %22get
@@ -840,36 +840,32 @@
 .port%22,%0A        
-    
 %22getprop dhcp.wl
@@ -925,36 +925,32 @@
 ECK = %7B%0A        
-    
 %22getprop service
@@ -961,28 +961,24 @@
 .tcp.port%22,%0A
-    
         %22get
@@ -2476,32 +2476,306 @@
 ic void run() %7B%0A
+                        if (result.equals(%22%22)) %7B%0A                            Toast.makeText(MainActivity.this,%0A                                %22Permission denied, need ROOT permission!%22, Toast.LENGTH_SHORT)%0A                                .show();%0A                        %7D%0A
                 
@@ -2977,24 +2977,57 @@
      tipView
+%0A                                
 .setText(Str
@@ -4534,16 +4534,33 @@
 dReader(
+%0A                
 new Inpu

Add permission denied toast