Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ android {
outputFileName = new File("remote-" + android.defaultConfig.versionCode + ".apk")
}
}
multiDexEnabled true

}
buildTypes {
release {
Expand All @@ -40,31 +42,34 @@ dependencies {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'
implementation 'com.mikepenz:materialdrawer:6.1.1'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.annotation:annotation:1.0.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.mikepenz:google-material-typeface:3.0.1.3.original@aar'
implementation 'com.mikepenz:fontawesome-typeface:5.3.1.1@aar'
implementation 'com.mikepenz:octicons-typeface:3.2.0.5@aar'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.mikepenz:itemanimators:1.1.0'
implementation 'com.mikepenz:crossfader:1.5.1@aar'
implementation 'com.mikepenz:crossfadedrawerlayout:1.0.1@aar'
implementation 'com.google.android:flexbox:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.preference:preference:1.1.0"
implementation "androidx.preference:preference:1.1.1"
testImplementation 'junit:junit:4.12'
implementation 'com.github.parse-community:ParseLiveQuery-Android:1.1.0'
implementation 'com.google.code.gson:gson:2.8.6'
androidTestImplementation 'androidx.test:runner:1.1.1'
implementation 'me.aflak.libraries:bluetooth:1.3.4'
androidTestImplementation 'androidx.test:runner:1.2.0'
implementation 'com.github.ivbaranov:rxbluetooth2:2.1.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.caverock:androidsvg-aar:1.4'
implementation 'com.android.support:multidex:1.0.3'



}
20 changes: 12 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACTION_PICK_WIFI_NETWORK" />
<uses-permission android:name="android.Manifest.permission.OVERRIDE_WIFI_CONFIG" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<!--<uses-permission android:name="android.permission.READ_PHONE_STATE" />-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Expand All @@ -37,15 +38,18 @@
<application
android:name=".MainApplication"
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:logo="@mipmap/ic_launcher_round"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:theme="@style/MyMaterialTheme.Base">
<activity android:name=".SplashScreenActivity"
android:screenOrientation="portrait">
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -56,9 +60,9 @@
android:name=".InitialActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/MyMaterialTheme.Base"
android:windowSoftInputMode="stateHidden|adjustResize">
</activity>
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize"
tools:ignore="LockedOrientationActivity" />

<service android:name=".utils.GPSService" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,20 @@
import io.treehouses.remote.R;
import io.treehouses.remote.adapter.RPIListAdapter;
import io.treehouses.remote.bases.BaseDialogFragment;
import io.treehouses.remote.callback.BluetoothDeviceCallback;
import io.treehouses.remote.callback.SetDisconnect;
import io.treehouses.remote.pojo.DeviceInfo;

import static android.widget.Toast.LENGTH_LONG;

public class RPIDialogFragment extends BaseDialogFragment {

private static BluetoothChatService mChatService = null;
public class RPIDialogFragment extends BaseDialogFragment implements BluetoothDeviceCallback {

private static RPIDialogFragment instance = null;
private List<BluetoothDevice> raspberry_devices = new ArrayList<BluetoothDevice>(), all_devices = new ArrayList<BluetoothDevice>();
private Set<BluetoothDevice> pairedDevices;
private static BluetoothDevice mainDevice = null;
private ListView listView;
private ArrayAdapter mArrayAdapter;
private BluetoothAdapter mBluetoothAdapter;
private SetDisconnect checkConnectionState;
private Context context;
private Switch mDiscoverRaspberry;
Expand All @@ -73,25 +71,20 @@ public static androidx.fragment.app.DialogFragment newInstance(int num) {
public Dialog onCreateDialog(Bundle savedInstanceState) {
instance = this;
context = getContext();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

bluetoothCheck();
if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); }
mBluetoothAdapter.startDiscovery();
LayoutInflater inflater = getActivity().getLayoutInflater();
final View mView = inflater.inflate(R.layout.activity_rpi_dialog_fragment, null);
initDialog(mView);

if (mChatService == null) { mChatService = new BluetoothChatService(mHandler, getActivity().getApplicationContext()); }

pairedDevices = mBluetoothAdapter.getBondedDevices();
mChatService.startDiscovery(this);
pairedDevices = mChatService.getPairedDevices();
setAdapterNotNull(raspberryDevicesText);
for (BluetoothDevice d : pairedDevices) {
if (checkPiAddress(d.getAddress())) {
addToDialog(d, raspberryDevicesText, raspberry_devices, false);
progressBar.setVisibility(View.INVISIBLE); }
}
intentFilter();

return mDialog;
}

Expand All @@ -105,7 +98,7 @@ private void initDialog(View mView) {
mCloseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetoothCheck("unregister");
mChatService.disconnect();
dismiss();
}
});
Expand All @@ -115,23 +108,15 @@ public void onClick(View v) {
progressBar = mView.findViewById(R.id.progressBar);
}

private void intentFilter() {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
getActivity().registerReceiver(mReceiver, filter);
}

private void listViewOnClickListener(View mView) {
listView.setOnItemClickListener((parent, view, position, id) -> {
mChatService = new BluetoothChatService(mHandler, getActivity().getApplicationContext());
mChatService.updateHandler(mHandler);
List<BluetoothDevice> deviceList;
if (mDiscoverRaspberry.isChecked()) deviceList = raspberry_devices;
else deviceList = all_devices;
if (checkPiAddress(deviceList.get(position).getAddress())) {
mainDevice = deviceList.get(position);
mChatService.connect(deviceList.get(position),true);
mChatService.connectToDevice(deviceList.get(position));
int status = mChatService.getState();
mDialog.cancel();
finish(status, mView);
Expand Down Expand Up @@ -178,48 +163,29 @@ private void finish(int status, View mView) {
@Override
public void onDestroy() {
super.onDestroy();
try { if (mBluetoothAdapter == null) context.unregisterReceiver(mReceiver); } catch (Exception e) { e.printStackTrace(); }
}

public AlertDialog getAlertDialog(View mView, Context context, Boolean wifi) {
return new AlertDialog.Builder(context).setView(mView).setIcon(R.drawable.dialog_icon).create();
}

public void bluetoothCheck(String... args) {
if (mBluetoothAdapter == null) {
public void bluetoothCheck() {
if (!mChatService.isBluetoothSupported()) {
Toast.makeText(getActivity(), "Your Bluetooth Is Not Enabled or Not Supported", LENGTH_LONG).show();
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
context.unregisterReceiver(mReceiver);
// getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
dismiss();
}
if (args.length >= 1) {
if (args[0].equals("unregister")) {
context.unregisterReceiver(mReceiver);
Intent intent = new Intent();
intent.putExtra("mChatService", mChatService);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
}
else if (!mChatService.isBluetoothEnabled()) {
Toast.makeText(context, "Please enable bluetooth", Toast.LENGTH_SHORT).show();
}

}

private void setAdapterNotNull(List<DeviceInfo> listVal) {
mArrayAdapter = new RPIListAdapter(getContext(), listVal);
listView.setAdapter(mArrayAdapter);
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (checkPiAddress(device.getAddress())) {
addToDialog(device, raspberryDevicesText, raspberry_devices, true);
progressBar.setVisibility(View.INVISIBLE);
}
addToDialog(device, allDevicesText, all_devices, true);
Log.e("Broadcast BT", device.getName() + "\n" + device.getAddress());
}
}
};

private void addToDialog(BluetoothDevice device, List<DeviceInfo> textList, List<BluetoothDevice> mDevices, Boolean inRange) {
if (!mDevices.contains(device)){
mDevices.add(device);
Expand Down Expand Up @@ -251,9 +217,7 @@ public void handleMessage(Message msg) {
case Constants.STATE_CONNECTED:
Log.e("RPIDialogFragment", "Bluetooth Connection Status Change: State Listen");
pDialog.dismiss();
listener.setChatService(mChatService);
checkConnectionState.checkConnectionState();
mBluetoothAdapter.cancelDiscovery();
Toast.makeText(context, "Bluetooth Connected", LENGTH_LONG).show();
break;
case Constants.STATE_NONE:
Expand All @@ -271,4 +235,14 @@ public void handleMessage(Message msg) {
};

public Handler getmHandler() { return mHandler; }

@Override
public void onDeviceFound(BluetoothDevice device) {
Log.e("RPIDIALOG", "onDeviceFound: "+ device.getName());
if (checkPiAddress(device.getAddress())) {
addToDialog(device, raspberryDevicesText, raspberry_devices, true);
progressBar.setVisibility(View.INVISIBLE);
}
addToDialog(device, allDevicesText, all_devices, true);
}
}
31 changes: 8 additions & 23 deletions app/src/main/java/io/treehouses/remote/Fragments/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class HomeFragment extends BaseHomeFragment implements SetDisconnect {
private ExpandableListView network_profiles;

private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothChatService mChatService = null;
private Button connectRpi, getStarted, testConnection;
private Boolean connectionState = false;
private Boolean result = false;
Expand All @@ -78,8 +77,8 @@ public class HomeFragment extends BaseHomeFragment implements SetDisconnect {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_home_fragment, container, false);
mChatService = listener.getChatService();
connectRpi = view.findViewById(R.id.btn_connect);
getStarted = view.findViewById(R.id.btn_getStarted);
preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Expand Down Expand Up @@ -168,8 +167,7 @@ public void connectRpiListener() {
@Override
public void onClick(View v) {
if (connectionState) {
RPIDialogFragment.getInstance().bluetoothCheck("unregister");
mChatService.stop();
mChatService.disconnect();
connectionState = false;
checkConnectionState();
return;
Expand All @@ -192,27 +190,26 @@ public void testConnectionListener() {
List<String> options = Arrays.asList(getResources().getStringArray(R.array.led_options));
String[] options_code = getResources().getStringArray(R.array.led_options_commands);
selected_LED = options.indexOf(preference);
writeToRPI(options_code[selected_LED]);
mChatService.write(options_code[selected_LED]);
testConnectionDialog = showTestConnectionDialog(false, "Testing Connection...", R.string.test_connection_message, selected_LED);
testConnectionDialog.show();
result = false;
});
}

public void checkConnectionState() {
mChatService = listener.getChatService();
if (mChatService.getState() == Constants.STATE_CONNECTED) {
showLogDialog(preferences);
transitionOnConnected();
connectionState = true;
checkVersionSent = true;
writeToRPI("treehouses remote version " + BuildConfig.VERSION_CODE + "\n");
mChatService.updateHandler(mHandler);
mChatService.write("treehouses remote version " + BuildConfig.VERSION_CODE + "\n");

} else {
transitionDisconnected();
connectionState = false;
}
mChatService.updateHandler(mHandler);
}

private void transitionOnConnected() {
Expand Down Expand Up @@ -247,10 +244,6 @@ private void dismissTestConnection() {
}
}

private void writeToRPI(String ping) {
mChatService.write(ping.getBytes());
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
Expand All @@ -267,7 +260,7 @@ private void checkVersion(String output) {
showUpgradeCLI();
}
else if(BuildConfig.VERSION_CODE == 2 || output.contains("true")) {
writeToRPI("treehouses remote check\n");
mChatService.write("treehouses remote check\n");
}
else if (output.contains("false")){
AlertDialog alertDialog = new AlertDialog.Builder(getContext())
Expand Down Expand Up @@ -305,7 +298,7 @@ private void readMessage(String output) {
internetSent = false;
if (output.trim().contains("true")) internetstatus.setImageDrawable(getResources().getDrawable(R.drawable.circle_green));
else internetstatus.setImageDrawable(getResources().getDrawable(R.drawable.circle));
writeToRPI("treehouses upgrade --check\n");
mChatService.write("treehouses upgrade --check\n");
}
else {
moreActions(output);
Expand Down Expand Up @@ -333,6 +326,7 @@ private void moreActions(String output) {
}
}


/**
* The Handler that gets information back from the BluetoothChatService
*/
Expand All @@ -351,13 +345,4 @@ public void handleMessage(Message msg) {
}
}
};

@Override
public void onResume() {
super.onResume();
if (mChatService.getState() == Constants.STATE_CONNECTED) {
checkVersionSent = true;
writeToRPI("treehouses remote version " + BuildConfig.VERSION_CODE + "\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void updateNetworkText(String mode) {

private void updateNetworkMode() {
String s = "treehouses networkmode";
mChatService.write(s.getBytes());
mChatService.write(s);
Toast.makeText(getContext(), "Network Mode updated", Toast.LENGTH_LONG).show();
}

Expand Down
Loading