// Path: android-client/app/src/main/java/edu/cmu/cs/utils/NetworkUtils.java
// public class NetworkUtils {
//     protected static final String
//             IPV4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
//     protected static final String IPV6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";
// 
//     public static boolean isOnline(Context m) {
//         ConnectivityManager connMgr = (ConnectivityManager)
//                 m.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//         return (networkInfo != null && networkInfo.isConnected());
//     }
// 
//     public static boolean isIpAddress(String ipAddress) {
//         if (ipAddress.matches(IPV4Pattern) || ipAddress.matches(IPV6Pattern)) {
//             return true;
//         }
//         return false;
//     }
// 
//     public static boolean checkOnline(Activity a){
//         if (isOnline(a)) {
//             return true;
//         }
//         notifyError(Const.CONNECTIVITY_NOT_AVAILABLE, false, a);
//         return false;
//     }
// 
// 
// }


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Set;
import edu.cmu.cs.utils.NetworkUtils;
import static edu.cmu.cs.CustomExceptions.CustomExceptions.notifyError;

        //TODO: read off sharedpreferences about current cloudlet ip and cloudip
        mActivity=this;
        addIpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = ipNameEditText.getText().toString();
                String ip = ipEditText.getText().toString();
                //assume type 0 is cloudlet
                // type 1 is cloud
                int type=placeSpinner.getSelectedItemPosition();
                String sharedPreferenceIpDictName=
                        getResources().getStringArray(R.array.shared_preference_ip_dict_names)[type];
                // a deep copy is needed
                //http://stackoverflow.com/questions/21396358/sharedpreferences-putstringset-doesnt-work
                Set<String> existingNames =
                        new HashSet<String>(mSharedPreferences.getStringSet(sharedPreferenceIpDictName,
                        new HashSet<String>()));
                if (name.isEmpty()){
                    notifyError("Invalid Name", false, mActivity);
                    return;
                } else if (existingNames.contains(name)){
                    notifyError("Duplicate Name", false, mActivity);
                    return;
                } else if (mSharedPreferences.getAll().containsKey(name)){
                    //TODO: here cloud and cloudlet server cannot share
                    // the same name
                    notifyError("Duplicate Name", false, mActivity);
                    return;
                }
// Path: android-client/app/src/main/java/edu/cmu/cs/utils/NetworkUtils.java
// public class NetworkUtils {
//     protected static final String
//             IPV4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
//     protected static final String IPV6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";
// 
//     public static boolean isOnline(Context m) {
//         ConnectivityManager connMgr = (ConnectivityManager)
//                 m.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//         return (networkInfo != null && networkInfo.isConnected());
//     }
// 
//     public static boolean isIpAddress(String ipAddress) {
//         if (ipAddress.matches(IPV4Pattern) || ipAddress.matches(IPV6Pattern)) {
//             return true;
//         }
//         return false;
//     }
// 
//     public static boolean checkOnline(Activity a){
//         if (isOnline(a)) {
//             return true;
//         }
//         notifyError(Const.CONNECTIVITY_NOT_AVAILABLE, false, a);
//         return false;
//     }
// 
// 
// }


// Path: android-client/app/src/main/java/edu/cmu/cs/faceswap/IPSettingActivity.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Set;
import edu.cmu.cs.utils.NetworkUtils;
import static edu.cmu.cs.CustomExceptions.CustomExceptions.notifyError;


        //TODO: read off sharedpreferences about current cloudlet ip and cloudip
        mActivity=this;
        addIpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = ipNameEditText.getText().toString();
                String ip = ipEditText.getText().toString();
                //assume type 0 is cloudlet
                // type 1 is cloud
                int type=placeSpinner.getSelectedItemPosition();
                String sharedPreferenceIpDictName=
                        getResources().getStringArray(R.array.shared_preference_ip_dict_names)[type];
                // a deep copy is needed
                //http://stackoverflow.com/questions/21396358/sharedpreferences-putstringset-doesnt-work
                Set<String> existingNames =
                        new HashSet<String>(mSharedPreferences.getStringSet(sharedPreferenceIpDictName,
                        new HashSet<String>()));
                if (name.isEmpty()){
                    notifyError("Invalid Name", false, mActivity);
                    return;
                } else if (existingNames.contains(name)){
                    notifyError("Duplicate Name", false, mActivity);
                    return;
                } else if (mSharedPreferences.getAll().containsKey(name)){
                    //TODO: here cloud and cloudlet server cannot share
                    // the same name
                    notifyError("Duplicate Name", false, mActivity);
                    return;
                }
                if (!NetworkUtils.isIpAddress(ip)){