Skip to content

Commit

Permalink
change hostname (fixes #43) (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Going-Gone authored and dogi committed Mar 15, 2018
1 parent 54c0622 commit 445994e
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
73 changes: 73 additions & 0 deletions app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ limitations under the License.
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout-large/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ limitations under the License.
android:background="@android:color/holo_blue_dark"
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />
<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ limitations under the License.
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout-small/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ limitations under the License.
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ limitations under the License.
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout-xlarge/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ limitations under the License.
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/fragment_bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@
android:text="@string/DockerButton"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/HN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:text="Rename Hostname"
android:textColor="@android:color/background_light" />

</LinearLayout>
</ScrollView>

Expand Down

0 comments on commit 445994e

Please sign in to comment.