package com.steps.tmuapp.lib;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.steps.tmuapp.R;

public class NetworkUtils {
    public static boolean isNetworkAvaliable(Context ctx)
    {
        ConnectivityManager cm;
        NetworkInfo info = null;
        try
        {
            cm = (ConnectivityManager)
                    ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            info = cm.getActiveNetworkInfo();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return (info != null);
    }
    public static void checkAndRetryNetwork(Context ctx) {
        while (!isNetworkAvaliable(ctx)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
            builder.setTitle("網路連線通知").setMessage("請開啟網路連線");
            // Add the buttons
            builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK button
                }
            });
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
// Set other dialog properties


// Create the AlertDialog
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
}
