Skip to content

Commit

Permalink
* Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kconger committed Jan 11, 2015
1 parent 87ce3c1 commit a3cae84
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 91 deletions.
88 changes: 37 additions & 51 deletions app/src/main/java/org/thecongers/itpms/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public class MainActivity extends ActionBarActivity {

static SensorIdDatabase sensorDB;
private LogData logger = null;
private Handler h;
ConnectThread mConnectThread;
private Handler sensorMessages;
private ConnectThread btConnectThread;

@SuppressLint("HandlerLeak")
@Override
Expand Down Expand Up @@ -137,7 +137,7 @@ public void onCreate(Bundle savedInstanceState) {
txtOutBackground = this.getResources().getDrawable(R.drawable.rectangle);
txtOutBackgroundDark = this.getResources().getDrawable(R.drawable.rectangle_dark);

// Draw gauges
// Update textViews with select units
String pressureFormat = sharedPrefs.getString("prefpressuref", "0");
String pressureUnit = "psi";
if (pressureFormat.contains("1")) {
Expand Down Expand Up @@ -170,7 +170,7 @@ public void onCreate(Bundle savedInstanceState) {
new AlertDialog.Builder(this).setTitle(R.string.alert_setup_title).setMessage(R.string.alert_setup_message).setNeutralButton(R.string.alert_setup_button, null).show();
}

h = new Handler() {
sensorMessages = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECEIVE_MESSAGE:
Expand Down Expand Up @@ -201,7 +201,7 @@ public void handleMessage(android.os.Message msg) {
// Add sensor ID to db
sensorDB.addID(sensorID.toString());
Toast.makeText(MainActivity.this,
R.string.toast_newSensor + " " + sensorID.toString(),
getResources().getString(R.string.toast_newSensor) + " " + sensorID.toString(),
Toast.LENGTH_LONG).show();
}
// Only parse message if there is one or more sensor mappings
Expand Down Expand Up @@ -417,8 +417,7 @@ public void onConfigurationChanged(Configuration newConfig) {
// Redraw Screen
final LinearLayout layoutFront = (LinearLayout) findViewById(R.id.layoutFront);
final LinearLayout layoutRear = (LinearLayout) findViewById(R.id.layoutRear);
// Gauges
// Text
// Restore Text
CharSequence currentTxtFrontPressure = txtFrontPressure.getText();
txtFrontPressure = (TextView) findViewById(R.id.txtFrontPressure);
txtFrontPressure.setText(currentTxtFrontPressure);
Expand Down Expand Up @@ -447,7 +446,7 @@ public void onConfigurationChanged(Configuration newConfig) {
txtOutput = (TextView) findViewById(R.id.txtOutput);
txtOutput.setText(currentTxt);

// Colors
// Restore Colors
if (!itsDark){
if (frontStatus > 0) {
layoutFront.setBackground(redBackground);
Expand Down Expand Up @@ -573,8 +572,8 @@ private boolean btConnect() {
if (address != null){
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
mConnectThread = new ConnectThread(device);
mConnectThread.start();
btConnectThread = new ConnectThread(device);
btConnectThread.start();
} else {
Toast.makeText(MainActivity.this,
getResources().getString(R.string.toast_noPaired),
Expand Down Expand Up @@ -636,65 +635,64 @@ private void checkBTState() {

// Bluetooth Connect Thread
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final BluetoothSocket btSocket;
private final BluetoothDevice btDevice;

public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
// because btSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
btDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = createBluetoothSocket(device);
} catch (IOException e) {
Log.d(TAG,"Bluetooth socket create failed: " + e.getMessage() + ".");
}
mmSocket = tmp;
btSocket = tmp;
}

public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.d(TAG, "Connecting to the iTPMSystem...");
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Log.d(TAG, "Connected to: " + mmDevice.getName() + " " + mmDevice.getAddress());
// Connect the device through the socket. This will block until it succeeds or
// throws an exception
btSocket.connect();
Log.d(TAG, "Connected to: " + btDevice.getName() + " " + btDevice.getAddress());
MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(MainActivity.this,
getResources().getString(R.string.toast_connectedTo) +
" " + mmDevice.getName() + " " + mmDevice.getAddress(),
" " + btDevice.getName() + " " + btDevice.getAddress(),
Toast.LENGTH_LONG).show();

}
});
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
// Unable to connect
Log.d(TAG, "Unable to connect to the iTPMSystem...");
try {
mmSocket.close();
btSocket.close();
} catch (IOException closeException) {
Log.d(TAG,"Unable to close socket during connection failure");
}
return;
}

// Do work to manage the connection (in a separate thread)
ConnectedThread mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
ConnectedThread btConnectedThread = new ConnectedThread(btSocket);
btConnectedThread.start();
}

// Cancel an in-progress connection, and close the socket
public void cancel() {
try {
mmSocket.close();
btSocket.close();
} catch (IOException e) {
Log.d(TAG, "Unable to close Bluetooth socket");
}
Expand All @@ -703,34 +701,33 @@ public void cancel() {

// Connected bluetooth thread
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final InputStream btInStream;

public ConnectedThread(BluetoothSocket socket) {
InputStream tmpInput = null;

// Get the input stream, using temp objects because
// member streams are final
// Get the input stream, using temp objects because member streams are final
try {
tmpInput = socket.getInputStream();
} catch (IOException e) {
Log.d(TAG, "IO Exception getting input stream");
}
mmInStream = tmpInput;
btInStream = tmpInput;
}

public void run() {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
byte[] buffer = new byte[256]; // Buffer store for the stream
int bytes; // Bytes returned from read()

// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
h.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
bytes = btInStream.read(buffer); // Get number of bytes and message in "buffer"
sensorMessages.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
} catch (IOException e) {
Log.d(TAG, "IO Exception while reading stream");
mConnectThread.cancel();
btConnectThread.cancel();
break;
}
}
Expand Down Expand Up @@ -796,17 +793,17 @@ public void onSensorChanged(SensorEvent event) {
int delay = (Integer.parseInt(sharedPrefs.getString("prefAutoNightModeDelay", "30")) * 1000);
if(event.sensor.getType()==Sensor.TYPE_LIGHT){
float currentReading = event.values[0];
// TODO: Proper light value for night & should I be using a timer instead of waiting for a sensor change?
if (currentReading < 20.0){
double darkThreshold = 20.0; // Light level to determine darkness
if (currentReading < darkThreshold){
lightTimer = 0;
if (darkTimer == 0){
darkTimer = System.currentTimeMillis();
} else {
long currentTime = System.currentTimeMillis();
long duration = (currentTime - darkTimer);
if ((duration >= delay) && (!itsDark)){ // divide be 1000 for secs
if ((duration >= delay) && (!itsDark)){
itsDark = true;
Log.d(TAG,"Its night");
Log.d(TAG,"Its dark");
// Redraw Screen
final LinearLayout layoutFront = (LinearLayout) findViewById(R.id.layoutFront);
final LinearLayout layoutRear = (LinearLayout) findViewById(R.id.layoutRear);
Expand All @@ -831,11 +828,6 @@ public void onSensorChanged(SensorEvent event) {
txtRearTemperature.setTextColor(getResources().getColor(android.R.color.white));
txtRearVoltage.setTextColor(getResources().getColor(android.R.color.white));
txtOutput.setTextColor(getResources().getColor(android.R.color.white));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(android.R.color.black));
}

}
}
} else {
Expand All @@ -847,7 +839,7 @@ public void onSensorChanged(SensorEvent event) {
long duration = (currentTime - lightTimer);
if ((duration >= delay) && (itsDark)){
itsDark = false;
Log.d(TAG,"Its day");
Log.d(TAG,"Its light");
// Redraw Screen
final LinearLayout layoutFront = (LinearLayout) findViewById(R.id.layoutFront);
final LinearLayout layoutRear = (LinearLayout) findViewById(R.id.layoutRear);
Expand All @@ -871,17 +863,11 @@ public void onSensorChanged(SensorEvent event) {
txtRearTemperature.setTextColor(getResources().getColor(android.R.color.black));
txtRearVoltage.setTextColor(getResources().getColor(android.R.color.black));
txtOutput.setTextColor(getResources().getColor(android.R.color.black));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark));
}
}
}
}
}

}
}

};
}
8 changes: 4 additions & 4 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
app:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_sensorReset"
android:orderInCategory="100"
android:showAsAction="never"
app:showAsAction="never"
android:title="@string/action_sensorReset"/>
<item
android:id="@+id/action_exit"
android:orderInCategory="100"
android:showAsAction="never"
app:showAsAction="never"
android:title="@string/action_exit"/>
</menu>
18 changes: 2 additions & 16 deletions app/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<resources>
<!--
Base application theme for API 21+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v21/styles.xml on API 21+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- API 21 theme customizations can go here. -->
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">@color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">@color/accent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="@android:panelFullBackground">@color/primary</item>
<item name="@android:panelColorBackground">@color/primary</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">@color/primary</item>
<item name="background">@color/primary</item>
</style>
</resources>
6 changes: 0 additions & 6 deletions app/src/main/res/values/colors.xml

This file was deleted.

7 changes: 0 additions & 7 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">iTPMS</string>
<string name="app_icon_desc">iTPMS icon</string>

<string name="front_wheel_desc">Front Wheel</string>
<string name="back_wheel_desc">Back Wheel</string>

<string name="action_settings">Settings</string>
<string name="action_sensorReset">Reset Sensor IDs</string>
Expand Down Expand Up @@ -37,7 +33,6 @@
<string name="pref_autoNightMode_summary">Enable Auto Night Mode</string>
<string name="pref_autoNightModeDelay_name">Auto Night Mode Delay In Seconds</string>
<string name="pref_autoNightModeDelay_summary">Set Auto Night Mode Delay In Seconds</string>

<string name="notification_settings_title">Notification Settings</string>
<string name="pref_sound_name">Sound</string>
<string name="pref_sound_summary">Notification Sound</string>
Expand All @@ -47,7 +42,6 @@
<string name="pref_notificationVibrate_summary">Enable Vibration</string>
<string name="pref_notificationLED_name">LED</string>
<string name="pref_notificationLED_summary">Enable LED</string>

<string name="front_wheel_settings_title">Front Wheel Settings</string>
<string name="rear_wheel_settings_title">Rear Wheel Settings</string>
<string name="pref_id_name">Wheel Sensor ID</string>
Expand All @@ -56,7 +50,6 @@
<string name="pref_low_pressure_summary">Low Pressure Alert Threshold</string>
<string name="pref_high_pressure_name">High Pressure</string>
<string name="pref_high_pressure_summary">High Pressure Alert Threshold</string>

<string name="advanced_settings_title">Advanced Settings</string>
<string name="pref_dataLogging_name">Data Logging</string>
<string name="pref_dataLogging_summary">Enable Data Logging To /sdcard/itpms/</string>
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Expand All @@ -11,14 +11,8 @@
backward-compatibility can go here.
-->
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">@color/primary</item>
<item name="background">@color/primary</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

0 comments on commit a3cae84

Please sign in to comment.