Skip to content

Commit

Permalink
stop spinning when connected (fixes #153) (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sriharsha-Singam authored and dogi committed Jan 24, 2019
1 parent 1b0108b commit 7bfc20a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
Expand All @@ -27,6 +28,7 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import io.treehouses.remote.FragmentsOld.CustomHandler;
import io.treehouses.remote.InitialActivity;
import io.treehouses.remote.MiscOld.Constants;
Expand Down Expand Up @@ -78,8 +80,8 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
mChatService.connect(devices.get(position), true);
int status = mChatService.getState();
mDialog.cancel();
InitialActivity initialActivity = new InitialActivity();
initialActivity.setChatService(mChatService);
// InitialActivity initialActivity = new InitialActivity();
// initialActivity.setChatService(mChatService);
finish(status, mView);
Log.e("Connecting Bluetooth","Position: "+position+" ;; Status: "+status);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
Expand Down Expand Up @@ -220,15 +222,81 @@ private void setupBluetoothService() {

mChatService = new BluetoothChatService(mHandler);
}
private final CustomHandler mHandler = new CustomHandler(getActivity()){
// private final CustomHandler mHandler = new CustomHandler(getActivity()){
// @Override
// public void handleMessage(Message msg) {
// super.handleMessage(msg);
// if(msg.what == Constants.MESSAGE_DEVICE_NAME){
// String mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
// Toast.makeText(getContext(), "Connected to "+mConnectedDeviceName, Toast.LENGTH_LONG).show();
// dialog.dismiss();
// mDialog.cancel();
// }
// }
// };
public final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == Constants.MESSAGE_DEVICE_NAME){
String mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
Toast.makeText(getContext(), "Connected to "+mConnectedDeviceName, Toast.LENGTH_LONG).show();
dialog.dismiss();
mDialog.cancel();
Log.e("RPIDialogFragment",""+msg.what);
FragmentActivity activity = getActivity();
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
Log.e("RPIDialogFragment","TEST STATE CHANGE "+msg.arg1);
switch (msg.arg1) {
case Constants.STATE_CONNECTED:
Log.e("RPIDialogFragment", "Bluetooth Connection Status Change: State Listen");
dialog.dismiss();
InitialActivity initialActivity = new InitialActivity();
initialActivity.setChatService(mChatService);
break;
case Constants.STATE_NONE:
Log.e("RPIDialogFragment", "Bluetooth Connection Status Change: State None");
break;
}
break;
// case Constants.MESSAGE_READ:
// // isRead = true;
// // byte[] readBuf = (byte[]) msg.obj;
// // construct a string from the valid bytes in the buffer
// // String readMessage = new String(readBuf, 0, msg.arg1);
// // String readMessage = new String(readBuf);
// readMessage = (String)msg.obj;
// Log.d(TAG, "readMessage = " + readMessage);
// //TODO: if message is json -> callback from RPi
// // if(isJson(readMessage)){
// // //handleCallback(readMessage);
// // }else{
// // if(isCountdown){
// // //mHandler.removeCallbacks(watchDogTimeOut);
// // isCountdown = false;
// // }
// // //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);
// //
// // //check if ping was successful
// // if(readMessage.contains("1 packets")){
// // mConnect();
// // }
// // if(readMessage.contains("Unreachable") || readMessage.contains("failure")){
// // mOffline();
// // }
// // //make it so text doesn't show on chat (need a better way to check multiple strings since mConversationArrayAdapter only takes messages line by line)
// // if (!readMessage.contains("1 packets") && !readMessage.contains("64 bytes") && !readMessage.contains("google.com") &&
// // !readMessage.contains("rtt") && !readMessage.trim().isEmpty()){
// // mConversationArrayAdapter.add(readMessage);
// // }
// // }
// break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
Log.e("RPIDialogFragment","Device Name "+msg.getData().getString(Constants.DEVICE_NAME));
// mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
// if (null != activity) {
// Toast.makeText(activity, "Connected to "
// + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
// }
break;
}
}
};
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/io/treehouses/remote/InitialActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,15 @@ public void handleMessage(Message msg) {
// FragmentActivity activity = getActivity();
InitialActivity activity = InitialActivity.this;
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case Constants.STATE_LISTEN:
case Constants.STATE_NONE:
// setStatus(R.string.title_not_connected);
mIdle();
break;
}
break;
// case Constants.MESSAGE_STATE_CHANGE:
// switch (msg.arg1) {
// case Constants.STATE_LISTEN:
// case Constants.STATE_NONE:
//// setStatus(R.string.title_not_connected);
// mIdle();
// break;
// }
// break;
// case Constants.MESSAGE_WRITE:
// isRead = false;
// byte[] writeBuf = (byte[]) msg.obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private synchronized void updateUserInterfaceTitle() {
mNewState = mCurrentState;

// Give the new state to the Handler so the UI Activity can update
mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, mNewState, -1).sendToTarget();
mHandler.sendMessage(mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, mNewState, -1));
}

/**
Expand Down Expand Up @@ -208,13 +208,16 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice
mConnectedThread.start();

// Send the name of the connected device back to the UI Activity

updateUserInterfaceTitle();

Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(Constants.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
// Update UI title
updateUserInterfaceTitle();
Log.e(TAG,"Connected");
}

/**
Expand Down

0 comments on commit 7bfc20a

Please sign in to comment.