diff --git a/app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java b/app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java index 9ff54f974..4f95373f8 100644 --- a/app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java +++ b/app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java @@ -20,9 +20,12 @@ import android.app.ActionBar; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; import android.app.ProgressDialog; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; @@ -32,6 +35,9 @@ import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; +import android.text.Editable; +import android.text.InputType; +import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -84,6 +90,9 @@ public class BluetoothChatFragment extends android.support.v4.app.Fragment { private Button Tbutton; private Button Dbutton; private Button Vbutton; + private Button HNbutton; + private String hnInput; + private Boolean isValidInput; /** * Name of the connected device @@ -175,6 +184,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { Tbutton = (Button) view.findViewById(R.id.TB); Dbutton = (Button)view.findViewById(R.id.DB); Vbutton = (Button)view.findViewById(R.id.VB); + HNbutton = (Button)view.findViewById(R.id.HN); } /** @@ -240,6 +250,14 @@ public void onClick(View view) { sendMessage(v); } }); + + HNbutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + showDialog(); + } + + }); // Initialize the BluetoothChatService to perform bluetooth connections mChatService = new BluetoothChatService(getActivity(), mHandler); @@ -260,6 +278,61 @@ public void onClick(View view) { } + /** + * This block is to create a dialog box for creating a new name for the PI device + * Sets the dialog button to be disabled if no text is in the EditText + */ + private void showDialog() { + final EditText input = new EditText(getActivity()); + final AlertDialog alertDialog = getAlertDialog(input); + + alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setClickable(false); + alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setEnabled(false); + + input.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + if(s.length() > 0) { + alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setClickable(true); + alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setEnabled(true); + }else{ + alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setClickable(false); + alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setEnabled(false); + } + } + @Override + public void afterTextChanged(Editable s) { + } + }); + } + + private AlertDialog getAlertDialog(final EditText input) { + return new AlertDialog.Builder(getActivity()) + .setTitle("Rename Hostname") + .setMessage("Please enter new hostname") + .setIcon(android.R.drawable.ic_dialog_info) + .setView(input) + .setPositiveButton("Change Hostname", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + hnInput = input.getText().toString(); + String h = "pirateship rename " + hnInput.toString(); + sendMessage(h); + } + }) + .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }) + .show(); + } + /** * Makes this device discoverable for 300 seconds (5 minutes). */ diff --git a/app/src/main/res/layout-large-land/fragment_bluetooth_chat.xml b/app/src/main/res/layout-large-land/fragment_bluetooth_chat.xml index dfaac015d..07883e345 100644 --- a/app/src/main/res/layout-large-land/fragment_bluetooth_chat.xml +++ b/app/src/main/res/layout-large-land/fragment_bluetooth_chat.xml @@ -99,6 +99,14 @@ limitations under the License. android:text="@string/DockerButton" android:textColor="@android:color/background_light" /> +