Skip to content

Commit

Permalink
new status page (fixes #145) (#159)
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 5090dda commit 1b0108b
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 89 deletions.
223 changes: 190 additions & 33 deletions app/src/main/java/io/treehouses/remote/Fragments/StatusFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -17,10 +18,16 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class StatusFragment extends androidx.fragment.app.Fragment {

Expand All @@ -30,7 +37,10 @@ public StatusFragment(){}

String readMessage = "";

private static final String TAG = "BluetoothChatFragment";
static boolean processing = false;
static String processedMessage = "";

private static final String TAG = "StatusFragment";

//current connection status
static String currentStatus = "not connected";
Expand All @@ -40,6 +50,7 @@ public StatusFragment(){}
* Name of the connected device
*/
private String mConnectedDeviceName = null;
private String deviceName = "";

/**
* String buffer for outgoing messages
Expand All @@ -56,19 +67,54 @@ public StatusFragment(){}
*/
private static boolean isCountdown = false;

Button btStatus, wifiStatus, btRPIName, rpiType;
ImageView wifiStatus, btRPIName, rpiType;

ImageView btStatus, ivUpgrad;

TextView tvStatus, tvStatus1, tvStatus2, tvStatus3, tvUpgrade;

ArrayList<Button> allButtons;

List<String> outs = new ArrayList<String>();

Boolean wifiStatusVal = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_status_fragment, container, false);

// ArrayList<String> list = new ArrayList<String>();
// list.add("Bluetooth RPI Connection");
// list.add("RPI Wifi Connection:");
// list.add("Connected RPI Name:");
// list.add("RPI Type:");
// list.add("Upgrade Status:");
//
// ArrayList<Drawable> list1 = new ArrayList<Drawable>();
// list1.add(getResources().getDrawable(R.drawable.tick));
// list1.add("RPI Wifi Connection:");
// list1.add("Connected RPI Name:");
// list1.add("RPI Type:");
// list1.add("Upgrade Status:");
//
// ListView listView = view.findViewById(R.id.listView);
//// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.cardlist, R.id.tvStatus, list, R.id.ivStatus, list1);
// listView.setAdapter(adapter);

btStatus = view.findViewById(R.id.btStatus);
wifiStatus = view.findViewById(R.id.wifiStatus);
btRPIName = view.findViewById(R.id.rpiName);
rpiType = view.findViewById(R.id.rpiType);
ivUpgrad = view.findViewById(R.id.upgradeCheck);


tvStatus = view.findViewById(R.id.tvStatus);
tvStatus1 = view.findViewById(R.id.tvStatus1);
tvStatus2 = view.findViewById(R.id.tvStatus2);
tvStatus3 = view.findViewById(R.id.tvStatus3);
tvUpgrade = view.findViewById(R.id.tvUpgradeCheck);

// allButtons.add(btStatus);
// allButtons.add(wifiStatus);
Expand All @@ -81,54 +127,144 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mChatService = chatService;
mChatService.updateHandler(mHandler);


deviceName = mChatService.getConnectedDeviceName();
Log.e("STATUS","device name: "+deviceName);
if(mChatService.getState() == Constants.STATE_CONNECTED){
btStatus.setImageDrawable(getResources().getDrawable(R.drawable.tick));
}
checkStatusNow(view);
String ping = "ping -c 1 google.com";
// String ping = "ping -c 1 google.com";
// byte[] pSend = ping.getBytes();
// mChatService.write(pSend);



ping = "treehouses detectrpi";
byte[] pSend1 = ping.getBytes();
mChatService.write(pSend1);


// final Handler h = new Handler();
// final int delay = 20000;
// h.postDelayed(new Runnable(){
// public void run(){
String ping = "treehouses detectrpi";
byte[] pSend1 = ping.getBytes();
mChatService.write(pSend1);
//// h.postDelayed(this, delay);
// }
// }, delay);

// final Handler h1 = new Handler();
// final int delay1 = 20000;
// h1.postDelayed(new Runnable(){
// public void run(){
// String ping1 = "treehouses internet";
// byte[] pSend2 = ping1.getBytes();
// mChatService.write(pSend2);
// h1.postDelayed(this, delay1);
// }
// }, delay1);

// Log.e("PROCESSING", ""+processing);
//// while(processing){System.out.print("-");}
//// Log.e("PROCESSING", ""+processing);
// for(int i = 0; i < 1000; i++){System.out.print("-");}
// Log.e("INCOMING MESSAGE", ""+processedMessage);


// for(int i = 0; i < 1000; i++){System.out.print("-");}
// String ping2 = "treehouses rebootneeded";
// byte[] pSend3 = ping2.getBytes();
// mChatService.write(pSend3);

// initialActivity.sendMessage("treehouses detectrpi");


return view;
}

private void checkStatusNow(View view){
if(mChatService.getState() == Constants.STATE_CONNECTED){
btStatus.setBackgroundResource((R.drawable.circle));
GradientDrawable bgShape = (GradientDrawable)btStatus.getBackground();
bgShape.setColor(Color.GREEN);
wifiStatus.setBackgroundResource((R.drawable.circle));
bgShape = (GradientDrawable)wifiStatus.getBackground();
bgShape.setColor(Color.GREEN);
btRPIName.setBackgroundResource((R.drawable.circle));
bgShape = (GradientDrawable)btRPIName.getBackground();
bgShape.setColor(Color.GREEN);
rpiType.setBackgroundResource((R.drawable.circle));
bgShape = (GradientDrawable)rpiType.getBackground();
bgShape.setColor(Color.GREEN);
// if(mChatService.getState() == Constants.STATE_CONNECTED){
// btStatus.setBackgroundResource((R.drawable.circle));
// GradientDrawable bgShape = (GradientDrawable)btStatus.getBackground();
// bgShape.setColor(Color.GREEN);
// wifiStatus.setBackgroundResource((R.drawable.circle));
// bgShape = (GradientDrawable)wifiStatus.getBackground();
// bgShape.setColor(Color.GREEN);
// btRPIName.setBackgroundResource((R.drawable.circle));
// bgShape = (GradientDrawable)btRPIName.getBackground();
// bgShape.setColor(Color.GREEN);
// rpiType.setBackgroundResource((R.drawable.circle));
// bgShape = (GradientDrawable)rpiType.getBackground();
// bgShape.setColor(Color.GREEN);
// }else{
// btStatus.setBackgroundResource((R.drawable.circle));
// GradientDrawable bgShape = (GradientDrawable)btStatus.getBackground();
// bgShape.setColor(Color.RED);
// wifiStatus.setBackgroundResource((R.drawable.circle));
// bgShape = (GradientDrawable)wifiStatus.getBackground();
// bgShape.setColor(Color.RED);
// btRPIName.setBackgroundResource((R.drawable.circle));
// bgShape = (GradientDrawable)btRPIName.getBackground();
// bgShape.setColor(Color.RED);
// rpiType.setBackgroundResource((R.drawable.circle));
// bgShape = (GradientDrawable)rpiType.getBackground();
// bgShape.setColor(Color.RED);
// }
Log.e("DEVICE",""+mConnectedDeviceName);
}

public void updateStatus(){
setRPIDeviceName();
if(outs.size() == 1){
setRPIType();
}
if(outs.size() == 2){
checkWifiStatus();
}
if(outs.size() == 3){
checkUpgradeStatus();
}
}

void writeToRPI(String ping){
byte[] pSend = ping.getBytes();
mChatService.write(pSend);
}

void setRPIDeviceName(){
tvStatus2.setText("Connected RPI Name: "+deviceName);
btRPIName.setImageDrawable(getResources().getDrawable(R.drawable.tick));
}

void setRPIType(){
tvStatus3.setText("RPI Type: "+outs.get(0));
rpiType.setImageDrawable(getResources().getDrawable(R.drawable.tick));
writeToRPI("treehouses internet");
}

void checkWifiStatus(){
tvStatus1.setText("RPI Wifi Connection: "+outs.get(1));
Log.e("StatusFragment","**"+outs.get(1)+"**"+outs.get(1).equals("true "));
if(outs.get(1).equals("true ")){
Log.e("StatusFragment","TRUE");
wifiStatusVal = true;
wifiStatus.setImageDrawable(getResources().getDrawable(R.drawable.tick));
}
if(wifiStatusVal){
writeToRPI("treehouses upgrade --check");
}else{
btStatus.setBackgroundResource((R.drawable.circle));
GradientDrawable bgShape = (GradientDrawable)btStatus.getBackground();
bgShape.setColor(Color.RED);
wifiStatus.setBackgroundResource((R.drawable.circle));
bgShape = (GradientDrawable)wifiStatus.getBackground();
bgShape.setColor(Color.RED);
btRPIName.setBackgroundResource((R.drawable.circle));
bgShape = (GradientDrawable)btRPIName.getBackground();
bgShape.setColor(Color.RED);
rpiType.setBackgroundResource((R.drawable.circle));
bgShape = (GradientDrawable)rpiType.getBackground();
bgShape.setColor(Color.RED);
tvUpgrade.setText("Upgrade Status: NO INTERNET");
}
Log.e("DEVICE",""+mConnectedDeviceName);
}

void checkUpgradeStatus(){
if(outs.get(2).equals("false")){
ivUpgrad.setImageDrawable(getResources().getDrawable(R.drawable.tick));
tvUpgrade.setText("Upgrade Status: Latest Version");
}else{
ivUpgrad.setImageDrawable(getResources().getDrawable(R.drawable.tick_png));
tvUpgrade.setText("Upgrade Status: Required for Version: "+outs.get(2).substring(4));
}
}
// /**
// * The Handler that gets information back from the BluetoothChatService
// */
Expand All @@ -140,14 +276,35 @@ public void handleMessage(Message msg) {
case Constants.MESSAGE_STATE_CHANGE:
checkStatusNow(view);
break;
case Constants.MESSAGE_WRITE:
Log.e("StatusFragment", "WRITE");
// isRead = false;
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
// if(!writeMessage.contains("google.com")) {
// Log.d(TAG, "writeMessage = " + writeMessage);
// mConversationArrayAdapter.add("Command: " + writeMessage);
// }
Log.d(TAG, "writeMessage = " + writeMessage);
processing = true;
break;
case Constants.MESSAGE_READ:
Log.e("StatusFragment", "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);
// processedMessage = readMessage;
outs.add(readMessage);
// for(String out : outs){
// Log.e("OUT", out);
// }
updateStatus();
processing = false;
//TODO: if message is json -> callback from RPi
// if(isJson(readMessage)){
// //handleCallback(readMessage);
Expand Down
53 changes: 45 additions & 8 deletions app/src/main/java/io/treehouses/remote/InitialActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
mChatService.updateHandler(mHandler);
}


public void setChatService(BluetoothChatService chatService){
mChatService = chatService;
mChatService.updateHandler(mHandler);
Expand Down Expand Up @@ -228,6 +229,42 @@ private void checkStatusNow(){
// }, delay);

}

public String getDeviceName(){ return mConnectedDeviceName; }
// /**
// * This block is to create a dialog box for creating a new name or changing the password for the PI device
// * Sets the dialog button to be disabled if no text is in the EditText
// */
// private void showDialog(View view) {
// final EditText input = new EditText(InitialActivity.this);
// final AlertDialog alertDialog = showAlertDialog(
// "Rename Hostname",
// "Please enter new hostname",
// "treehouses rename ", 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 showAlertDialog(){
return new AlertDialog.Builder(InitialActivity.this)
.setTitle("ALERT:")
Expand Down Expand Up @@ -350,14 +387,14 @@ public void handleMessage(Message msg) {
};


public boolean isJson(String str) {
try {
new JSONObject(str);
} catch (JSONException ex) {
return false;
}
return true;
}
// public boolean isJson(String str) {
// try {
// new JSONObject(str);
// } catch (JSONException ex) {
// return false;
// }
// return true;
// }

public void mOffline(){
Log.e("STATUS","OFFLINE");
Expand Down
Binary file added app/src/main/res/drawable/loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tick_png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1b0108b

Please sign in to comment.