Skip to content

Commit

Permalink
Material UX implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TerrenceEJ authored and leomaxi committed Apr 4, 2018
1 parent 5a58463 commit 8bd9967
Show file tree
Hide file tree
Showing 18 changed files with 1,231 additions and 156 deletions.
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_round"
Expand All @@ -47,6 +49,9 @@
</intent-filter>
</activity>

<activity android:name=".pirateship"/>
<activity android:name=".docker"/>

<activity
android:name="io.treehouses.remote.DeviceListActivity"
android:configChanges="orientation|keyboardHidden"
Expand Down
186 changes: 87 additions & 99 deletions app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/*
* Copyright 2017 The Android Open Source Project, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. The ASF licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
* Copyright 2017 The Android Open Source Project, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. The ASF licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

package io.treehouses.remote;

Expand Down Expand Up @@ -328,7 +325,7 @@ public void afterTextChanged(Editable s) {
}

private AlertDialog showAlertDialog(String title, String message, final String command, final EditText input){
return new AlertDialog.Builder(getActivity())
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(message)
.setIcon(android.R.drawable.ic_dialog_info)
Expand Down Expand Up @@ -502,90 +499,77 @@ public void run() {
@Override
public void handleMessage(Message msg) {
FragmentActivity activity = getActivity();
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
try {
Thread.sleep(500);
String[] firstRun = {"cd boot\n", "cat version.txt\n", "pirateship detectrpi\n", "cd ..\n"};
for (int i = 0; i <= 3; i++) {
mChatService.write(firstRun[i].getBytes());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
mConversationArrayAdapter.clear();
mConversationArrayAdapter.notifyDataSetChanged();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case Constants.MESSAGE_WRITE:
isRead = false;
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
Log.d(TAG, "writeMessage = " + writeMessage);
mConversationArrayAdapter.add("Command: " + writeMessage);

break;
case Constants.MESSAGE_READ:
isRead = true;
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case Constants.MESSAGE_WRITE:
isRead = false;
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
Log.d(TAG, "writeMessage = " + writeMessage);
mConversationArrayAdapter.add("Command: " + writeMessage);

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);
String 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;
}
if (mProgressDialog.isShowing()) {
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);
mConversationArrayAdapter.add(readMessage);
String 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;
}

break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
if (null != activity) {
Toast.makeText(activity, "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig, Toast.LENGTH_SHORT).show();
}
break;
case Constants.MESSAGE_TOAST:
if (null != activity) {
Toast.makeText(activity, msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
if(hProgressDialog.isShowing()){
hProgressDialog.dismiss();
Toast.makeText(activity, R.string.config_alreadyConfig_hotspot, Toast.LENGTH_SHORT).show();
}
break;
}
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
//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);
mConversationArrayAdapter.add(readMessage);
}

break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
if (null != activity) {
Toast.makeText(activity, "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
}
break;
case Constants.MESSAGE_TOAST:
if (null != activity) {
Toast.makeText(activity, msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
}
break;
}
}
};

Expand Down Expand Up @@ -705,7 +689,7 @@ private void connectDevice(Intent data, boolean secure) {

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.bluetooth_chat, menu);
//inflater.inflate(R.menu.bluetooth_chat, menu);
}

@Override
Expand Down Expand Up @@ -747,6 +731,7 @@ public void showNWifiDialog() {
dialogFrag.setTargetFragment(this, REQUEST_DIALOG_FRAGMENT);
dialogFrag.show(getFragmentManager().beginTransaction(), "dialog");


}

public void showHotspotDialog(){
Expand Down Expand Up @@ -785,14 +770,17 @@ public void handleCallback(String str){

if(!result.equals("SUCCESS")){
Toast.makeText(getActivity(), R.string.config_fail,
Toast.LENGTH_LONG).show();
}else{ Toast.makeText(getActivity(),getString(R.string.config_success) + ip,Toast.LENGTH_LONG).show(); }
Toast.LENGTH_LONG).show();
}else{
// Toast.makeText(getActivity(), R.string.config_success,
// Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(),getString(R.string.config_success) + ip,Toast.LENGTH_LONG).show();
}

}catch (JSONException e){
// error handling
Toast.makeText(getActivity(), "SOMETHING WENT WRONG", Toast.LENGTH_LONG).show();
}

}

}
}
89 changes: 47 additions & 42 deletions app/src/main/java/io/treehouses/remote/BluetoothChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
* Created by yubo on 7/11/17.
*/

import android.app.ActionBar;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import java.io.IOException;
Expand Down Expand Up @@ -72,9 +71,6 @@ public class BluetoothChatService {
private ConnectedThread mConnectedThread;
private int mState;
private int mNewState;
private FragmentActivity mActivity;
private String HWver = "";
private String SWver = "";


// Constants that indicate the current connection state
Expand All @@ -86,15 +82,14 @@ public class BluetoothChatService {
/**
* Constructor. Prepares a new BluetoothChat session.
*
* @param activity The BluetoothCharFragment Activity
* @param context The UI Activity Context
* @param handler A Handler to send messages back to the UI Activity
*/
public BluetoothChatService(FragmentActivity activity, Handler handler) {
public BluetoothChatService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mNewState = mState;
mHandler = handler;
mActivity = activity;
}

/**
Expand Down Expand Up @@ -506,10 +501,47 @@ public void run() {
byte[] buffer = new byte[1024];
int bytes = -1;
String out = "";
boolean getSWString = false;
boolean getHWString = false;
boolean alreadyExecutedDisplay = false;

// List<String> tempOutputList = new ArrayList<String>();
// Keep listening to the InputStream while connected
// while (mState == STATE_CONNECTED) {
// try {
// // Read from the InputStream
// //bytes = mmInStream.read(buffer);
// //Log.d(TAG, "bytes = " + bytes + ", buffer = " + buffer);
//
// while(true){
// bytes = mmInStream.read(buffer);
// out += new String(buffer, 0, bytes);
// if(bytes < 1024){
// break;
// }
// }
// tempOutputList = getTokens("[a-zA-Z._]+", out);
// HashSet<String> mSet = new HashSet<String>();
// for(String s : tempOutputList){
// if(!mSet.contains(s)){
// mSet.add(s);
// }
// }
//
// System.out.println("mSet = " + mSet);
//
//
// Log.d(TAG, "out = " + out + "size of out = " + out.length());
// Log.d(TAG, "tempOutputList = " + mSet.iterator().next());
//
//
// // Send the obtained bytes to the UI Activity
// mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
// } catch (IOException e) {
// Log.e(TAG, "disconnected", e);
// connectionLost();
// break;
// }
// }

// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
Expand All @@ -518,38 +550,11 @@ public void run() {
Log.d(TAG, "out = " + out + "size of out = " + out.length() + ", bytes = " + bytes);
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, out)
.sendToTarget();

// Get the SW version once
if(out.contains("release-") && !getSWString) {
SWver += "Version: " + out.substring(8, 10);
getSWString = true;
}

// Get the HW version once
if(out.contains("RPI") && !getHWString){
HWver += "; " + out;
getHWString = true;
}

// Display SW/HW version after getting all the String from above.
if(!alreadyExecutedDisplay && getSWString && getHWString) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
final ActionBar actionBar = mActivity.getActionBar();
if (null == actionBar) { return; }
Log.d(TAG, "actionBar.setSubtitle(subTitle) = " + SWver + HWver);
actionBar.setSubtitle(SWver + HWver);
}
});
//Set everything back to default state
alreadyExecutedDisplay = true;
}

// mEmulatorView.write(buffer, bytes);
// Send the obtained bytes to the UI Activity
//mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
HWver = "";
SWver = "";
connectionLost();
break;
}
Expand Down
Loading

0 comments on commit 8bd9967

Please sign in to comment.