Skip to content

Commit

Permalink
create hotspot (fixes #9) (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
TerrenceEJ authored and dogi committed Mar 15, 2018
1 parent f148695 commit 54c0622
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 44 deletions.
79 changes: 74 additions & 5 deletions app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ public class BluetoothChatFragment extends android.support.v4.app.Fragment {
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
public static final int REQUEST_DIALOG_FRAGMENT = 4;
public static final int REQUEST_DIALOG_FRAGMENT_HOTSPOT = 5;


// Layout Views
private ListView mConversationView;
private EditText mOutEditText;
private Button mSendButton;
private ProgressDialog mProgressDialog;
private ProgressDialog hProgressDialog;
private Button Tbutton;
private Button Dbutton;
private Button Vbutton;
Expand Down Expand Up @@ -250,6 +252,12 @@ public void onClick(View view) {
mProgressDialog.setMessage(getString(R.string.progress_dialog_message));
mProgressDialog.setCancelable(false); // disable dismiss by tapping outside of the dialog

//get spinner for hotspot
hProgressDialog = new ProgressDialog(getActivity());
hProgressDialog.setTitle(R.string.progress_dialog_title_hotspot);
hProgressDialog.setMessage(getString(R.string.progress_dialog_message));
hProgressDialog.setCancelable(false); // disable dismiss by tapping outside of the dialog

}

/**
Expand Down Expand Up @@ -381,6 +389,10 @@ public void run() {
mProgressDialog.dismiss();
Toast.makeText(getActivity(),"No response from RPi",Toast.LENGTH_LONG).show();
}
if(hProgressDialog.isShowing()){
hProgressDialog.dismiss();
Toast.makeText(getActivity(),"No response from RPi",Toast.LENGTH_LONG).show();
}
}
};

Expand Down Expand Up @@ -436,6 +448,10 @@ public void handleMessage(Message msg) {
mProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig, Toast.LENGTH_SHORT).show();
}
if(hProgressDialog.isShowing()){
hProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig_hotspot, Toast.LENGTH_SHORT).show();
}
//remove the space at the very end of the readMessage -> eliminate space between items
readMessage = readMessage.substring(0,readMessage.length()-1);
//mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
Expand Down Expand Up @@ -521,6 +537,41 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}else{
Log.d(TAG, "back from dialog, fail");
}
case REQUEST_DIALOG_FRAGMENT_HOTSPOT:
if(resultCode == Activity.RESULT_OK){

//check status
if(mChatService.getState() != BluetoothChatService.STATE_CONNECTED){
Toast.makeText(getActivity(), R.string.not_connected,
Toast.LENGTH_SHORT).show();
return;
}

//show the progress bar, disable user interaction
hProgressDialog.show();
//TODO: start watchdog
isCountdown = true;
mHandler.postDelayed(watchDogTimeOut,30000);
Log.d(TAG, "watchDog start");

//get SSID & PWD from user input
String SSID = data.getStringExtra("SSID") == null? "":data.getStringExtra("SSID");
String PWD = data.getStringExtra("PWD") == null? "":data.getStringExtra("PWD");

String hotSpot = "pirateship hotspot " + SSID + " " + PWD;

Log.d(TAG, "back from dialog_hotspot: ok, SSID = " + SSID + ", PWD = " + PWD);

//TODO: 1. check Valid input 2. get the SSID and password from data object and send it to RPi through sendMessage() method
// Toast.makeText(getActivity(), R.string.config_success,
// Toast.LENGTH_SHORT).show();

sendMessage(hotSpot);
//TODO:1. lock the app when configuring. 2. listen to configuration result and do the logic

}else{
Log.d(TAG, "back from dialog_hotspot, fail");
}
}
}

Expand Down Expand Up @@ -550,21 +601,31 @@ public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.wifi_configuration: {
showNWifiDialog();
return true;
//return true;
break;
}
case R.id.insecure_connect_scan: {
// Launch the DeviceListActivity to see devices and do scan
Intent serverIntent = new Intent(getActivity(), DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
return true;
//return true;
break;
}
case R.id.discoverable: {
// Ensure this device is discoverable by others
ensureDiscoverable();
return true;
//return true;
break;
}
case R.id.hotspot_configuration: {
showHotspotDialog();
//return true;
break;
}
default:
return false;
}
return false;
return true;
}

public void showNWifiDialog() {
Expand All @@ -577,6 +638,15 @@ public void showNWifiDialog() {

}

public void showHotspotDialog(){
//Reusing WifiDialogFragment code for Hotspot

DialogFragment hDialogFragment = HotspotDialogFragment.newInstance(123);
hDialogFragment.setTargetFragment(this,REQUEST_DIALOG_FRAGMENT_HOTSPOT);
hDialogFragment.show(getFragmentManager().beginTransaction(),"hDialog");

}

public boolean isJson(String str) {
try {
new JSONObject(str);
Expand Down Expand Up @@ -617,5 +687,4 @@ public void handleCallback(String str){
}

}

}
53 changes: 53 additions & 0 deletions app/src/main/java/io/treehouses/remote/HotspotDialogFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.treehouses.remote;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

/**
* Created by Terrence on 3/12/2018.
*/

public class HotspotDialogFragment extends WifiDialogFragment {

private static final String TAG = "HotspotDialogFragment";


public static HotspotDialogFragment newInstance(int num) {
HotspotDialogFragment hDialogFragment = new HotspotDialogFragment();
// Bundle bundle = new Bundle();
// bundle.putInt("num", num);
// dialogFragment.setArguments(bundle);

return hDialogFragment;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d(TAG,"In onCreateDialog()");

// Build the dialog and set up the button click handlers
LayoutInflater inflater = getActivity().getLayoutInflater();
View mView = inflater.inflate(R.layout.dialog_design,null);
initLayoutView(mView);

final AlertDialog mDialog = getAlertDialog(mView);
mDialog.setTitle(R.string.dialog_message_hotspot);

//initially disable button click
getListener(mDialog);
setTextChangeListener(mDialog);

return mDialog;
}
}


79 changes: 44 additions & 35 deletions app/src/main/java/io/treehouses/remote/WifiDialogFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class WifiDialogFragment extends DialogFragment {
private static final String TAG = "WifiDialogFragment";

// Layout Views
private EditText mSSIDEditText;
private EditText mPWDEditText;
protected EditText mSSIDEditText;
protected EditText mPWDEditText;

private boolean isValidInput;
protected boolean isValidInput;

public static WifiDialogFragment newInstance(int num){

Expand All @@ -51,34 +51,18 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.dialog_design,null);
initLayoutView(mView);

final AlertDialog mDialog = new AlertDialog.Builder(getActivity())
.setView(mView)
.setTitle(R.string.dialog_message)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.start_configuration,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// getActivity().getIntent().putExtra("isValidInput", mSSIDEditText.getText().toString().length() > 0? Boolean.TRUE: Boolean.FALSE);
if(isValidInput){
String SSID = mSSIDEditText.getText().toString();
String PWD = mPWDEditText.getText().toString();

Intent intent = new Intent();
intent.putExtra("SSID", SSID);
intent.putExtra("PWD", PWD);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
}
}
}
)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
}
})
.create();
final AlertDialog mDialog = getAlertDialog(mView);

//initially disable button click
getListener(mDialog);
setTextChangeListener(mDialog);

return mDialog;


}

protected void getListener(final AlertDialog mDialog) {
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Expand All @@ -87,14 +71,38 @@ public void onShow(DialogInterface dialog) {
mSSIDEditText.setError(getString(R.string.error_ssid_empty));
}
});
setTextChangeListener(mDialog);

return mDialog;

}

protected AlertDialog getAlertDialog(View mView) {
return new AlertDialog.Builder(getActivity())
.setView(mView)
.setTitle(R.string.dialog_message)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.start_configuration,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// getActivity().getIntent().putExtra("isValidInput", mSSIDEditText.getText().toString().length() > 0? Boolean.TRUE: Boolean.FALSE);
if(isValidInput){
String SSID = mSSIDEditText.getText().toString();
String PWD = mPWDEditText.getText().toString();

Intent intent = new Intent();
intent.putExtra("SSID", SSID);
intent.putExtra("PWD", PWD);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
}
}
}
)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
}
})
.create();
}

private void setTextChangeListener(final AlertDialog mDialog) {
public void setTextChangeListener(final AlertDialog mDialog) {

mSSIDEditText.addTextChangedListener(new TextWatcher() {
@Override
Expand Down Expand Up @@ -125,10 +133,11 @@ public void afterTextChanged(Editable s) {
});
}

private void initLayoutView(View mView) {
protected void initLayoutView(View mView) {
mSSIDEditText = (EditText)mView.findViewById(R.id.SSID);
mPWDEditText = (EditText)mView.findViewById(R.id.password);

}

}

11 changes: 8 additions & 3 deletions app/src/main/res/menu/bluetooth_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <!--added so I could add other options into the menu-->

<item
android:id="@+id/wifi_configuration"
Expand All @@ -29,13 +30,17 @@

<item
android:id="@+id/insecure_connect_scan"
android:showAsAction="never"
app:showAsAction="never"
android:title="@string/insecure_connect"/>

<item
android:id="@+id/discoverable"
android:showAsAction="never"
app:showAsAction="never"
android:visible="false"
android:title="@string/discoverable"/>
<item
android:id="@+id/hotspot_configuration"
app:showAsAction="ifRoom"
android:title="@string/create_hotspot"/>

</menu>
7 changes: 6 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<string name="VersionButton">PIRATESHIP DETECTRPI</string>
<!-- ProgressDialog -->
<string name="progress_dialog_title">Configuring RPi\'s Wi-Fi setting</string>
<string name="progress_dialog_title_hotspot">Configuring RPi as a Hotspot</string>
<string name="progress_dialog_message">Wait while configuring...</string>

<!-- DeviceListActivity -->
Expand Down Expand Up @@ -69,10 +70,14 @@
<string name="dialog_message">Configure RPi\'s Wi-Fi setting</string>
<string name="ssid">SSID</string>
<string name="password">Password</string>
<string name="error_ssid_empty">Tpye in the name of the Wi-Fi</string>
<string name="error_ssid_empty">Type in the name of the network \nyou want to use </string>
<string name="config_configuring">Start configuring...</string>
<string name="config_success">Configure Wifi successfully, IP: </string>
<string name="config_fail">Fail to configure Wifi! Please check SSID and/or password and try again.</string>
<string name="config_alreadyConfig">You have already configured the wifi</string>
<!--Hotspot-->
<string name="create_hotspot">Create Hotspot</string>
<string name="dialog_message_hotspot">Configure RPi\'s Hotspot setting</string>
<string name="config_alreadyConfig_hotspot">You have already configured the hotspot</string>

</resources>

0 comments on commit 54c0622

Please sign in to comment.