package com.example.airplanerefresh;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MainActivity extends Activity {

    private static final long AUTO_REFRESH_MS =
            5 * 60 * 1000L;

    private TextView status;
    private CheckBox autoRefresh;

    private final Handler handler =
            new Handler(Looper.getMainLooper());

    private final Runnable autoTask =
            new Runnable() {

        @Override
        public void run() {

            if (autoRefresh != null
                    && autoRefresh.isChecked()) {

                refreshNetwork();

                handler.postDelayed(
                        this,
                        AUTO_REFRESH_MS
                );
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(
                R.layout.activity_main
        );

        status =
                findViewById(
                        R.id.status
                );

        Button refresh =
                findViewById(
                        R.id.refresh_button
                );

        autoRefresh =
                findViewById(
                        R.id.auto_refresh
                );

        refresh.setOnClickListener(
                v -> refreshNetwork()
        );

        autoRefresh.setOnCheckedChangeListener(
                (buttonView, checked) -> {

                    handler.removeCallbacks(
                            autoTask
                    );

                    if (checked) {

                        status.setText(
                                "רענון אוטומטי הופעל"
                        );

                        handler.post(
                                autoTask
                        );

                    } else {

                        status.setText(
                                "רענון אוטומטי כבוי"
                        );
                    }
                }
        );
    }

    private void refreshNetwork() {

        status.setText(
                "מבצע רענון..."
        );

        new Thread(() -> {

            boolean root =
                    hasRoot();

            boolean success =
                    false;

            if (root) {

                success =
                        runRootRefresh();
            }

            final boolean result =
                    success;

            runOnUiThread(() -> {

                if (result) {

                    status.setText(
                            "הרענון הסתיים בהצלחה"
                    );

                } else if (!root) {

                    status.setText(
                            "Root לא זמין — לא ניתן לבצע את פעולת הרענון"
                    );

                } else {

                    status.setText(
                            "הרענון נכשל"
                    );
                }
            });

        }).start();
    }

    private boolean hasRoot() {

        try {

            Process process =
                    Runtime.getRuntime().exec(
                            new String[] {
                                    "su",
                                    "-c",
                                    "id"
                            }
                    );

            BufferedReader reader =
                    new BufferedReader(
                            new InputStreamReader(
                                    process.getInputStream()
                            )
                    );

            String line =
                    reader.readLine();

            int exitCode =
                    process.waitFor();

            return exitCode == 0
                    && line != null
                    && line.contains("uid=0");

        } catch (Exception e) {

            return false;
        }
    }

    private boolean runRootRefresh() {

        try {

            String command =
                    "settings put global airplane_mode_on 1; "
                    + "am broadcast "
                    + "-a android.intent.action.AIRPLANE_MODE "
                    + "--ez state true; "
                    + "sleep 3; "
                    + "settings put global airplane_mode_on 0; "
                    + "am broadcast "
                    + "-a android.intent.action.AIRPLANE_MODE "
                    + "--ez state false";

            Process process =
                    Runtime.getRuntime().exec(
                            new String[] {
                                    "su",
                                    "-c",
                                    command
                            }
                    );

            return process.waitFor() == 0;

        } catch (Exception e) {

            return false;
        }
    }

    public boolean isNetworkAvailable() {

        ConnectivityManager manager =
                (ConnectivityManager)
                        getSystemService(
                                Context.CONNECTIVITY_SERVICE
                        );

        if (manager == null) {

            return false;
        }

        Network network =
                manager.getActiveNetwork();

        return network != null;
    }

    @Override
    protected void onDestroy() {

        handler.removeCallbacksAndMessages(
                null
        );

        super.onDestroy();
    }
}