Coder Social home page Coder Social logo

capstoneandroidapp's Introduction

CapstoneAndroidApp

Multi-threaded Android application for ceg4912's Capstone Project. Navigable entirely through voice command.

capstoneandroidapp's People

Contributors

akiraro avatar hbina avatar

Watchers

 avatar

capstoneandroidapp's Issues

unsolicited suggestion to simplify

@hbina @akiraro

Sorry for busting in with unsolicited suggestion but I was reading the code and saw the TODO comment to simplify the code. I figured I might be able to provide some general suggestions.

Change from

@Override
public void run() {
super.run();
// TODO: Rewrite this...make it a lot simpler than this...
while (isRunning.get()) {
try {
if (BluetoothConnector.getInputStream() != null) {
int availableBytes = BluetoothConnector.getInputStream().available();
if (availableBytes > 0) {
Log.d(TAG, String.format("Received something from Bluetooth of size:%d", availableBytes));
byte[] rawBytes = new byte[availableBytes];
final int receivedLength = BluetoothConnector.getInputStream().read(rawBytes);
final String receivedString = filterBluetooth((new String(rawBytes, StandardCharsets.UTF_8)));
if (receivedLength > 1) {
Log.d(TAG, "receivedString:" + receivedString);
currentLocation = receivedString;
if (nodesInPath.isEmpty()) {
SpeechServices.addText("Please enter a destination...");
} else if (nodesInPath.size() == 1) {
SpeechServices.addText("Congratulation, you have arrived at " + nodesInPath.poll());
} else {
if (currentLocation.equals(decodeNodeNameToId(nodesInPath.peekFirst()))) {
SpeechServices.addText("You have arrived at " + encodeIdToName(receivedString));
nodesInPath.removeFirst();
} else {
Log.d(TAG, String.format("currentLocation:%s peekFirst:%s", currentLocation, nodesInPath.peekFirst()));
}
BearingRequest bearingRequest = new BearingRequest(currentLocation, nodesInPath.peekFirst(), BearingListener.getBearing());
RequestServices.addBearingRequest(bearingRequest);
}
} else {
Log.d(TAG, "Delta string recevied => " + receivedString);
}
callerActivity.TEXT_VIEW_PATH.setText(nodesInPath.toString());
}
}
} catch (
IOException exception) {
Log.e(TAG, exception.getMessage());
}
}
Log.d(TAG, "Thread have finished running");
}

To

@Override
public void run() {
    super.run();
    // TODO: Rewrite this...make it a lot simpler than this...
    while (isRunning.get()) {
        try {
            if (BluetoothConnector.getInputStream() == null) {
                continue;
            }
            
            int availableBytes = BluetoothConnector.getInputStream().available();

            if (availableBytes <= 0) {
                continue;
            }

            Log.d(TAG, String.format("Received something from Bluetooth of size:%d", availableBytes));
            byte[] rawBytes = new byte[availableBytes];
            final int receivedLength = BluetoothConnector.getInputStream().read(rawBytes);
            final String receivedString = filterBluetooth((new String(rawBytes, StandardCharsets.UTF_8)));

            if (receivedLength <= 1) {
                Log.d(TAG, "Delta string recevied => " + receivedString);
                continue;
            }

            Log.d(TAG, "receivedString:" + receivedString);
            currentLocation = receivedString;

            if (nodesInPath.isEmpty()) {
                SpeechServices.addText("Please enter a destination...");
                callerActivity.TEXT_VIEW_PATH.setText(nodesInPath.toString());
                continue;
            }
            
            if (nodesInPath.size() == 1) {
                SpeechServices.addText("Congratulation, you have arrived at " + nodesInPath.poll());
                callerActivity.TEXT_VIEW_PATH.setText(nodesInPath.toString());
                continue;
            }
            
            if (!currentLocation.equals(decodeNodeNameToId(nodesInPath.peekFirst()))) {
                Log.d(TAG, String.format("currentLocation:%s peekFirst:%s", currentLocation, nodesInPath.peekFirst()));
                continue;
            }
            
            SpeechServices.addText("You have arrived at " + encodeIdToName(receivedString));
            nodesInPath.removeFirst();
            BearingRequest bearingRequest = new BearingRequest(currentLocation, nodesInPath.peekFirst(), BearingListener.getBearing());
            RequestServices.addBearingRequest(bearingRequest);

            callerActivity.TEXT_VIEW_PATH.setText(nodesInPath.toString());

        } catch (
                IOException exception) {
            Log.e(TAG, exception.getMessage());
        }
    }
    Log.d(TAG, "Thread have finished running");
}

The logic is exactly the same but the idea here is to reorganize the main branch to as left as possible to avoid nested conditionals. Nested conditionals are generally more difficult to understand.

Hope this is useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.