Adding view of current data

This commit is contained in:
Chet Henry
2014-11-27 02:37:43 -07:00
parent 470441671f
commit 7b84ea2355
4 changed files with 163 additions and 22 deletions

View File

@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE AndroidXML>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:layout_height="0dp"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_weight="1.0"/>
</LinearLayout>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="4"
android:orientation="horizontal"
tools:context=".GridXMLActivity" >
</GridLayout>

View File

@@ -10,7 +10,6 @@
<string name="sms_period_dialog_title">Emergency Contact SMS Period</string>
<string name="sms_period_dialog_note">Period in Minutes:</string>
<string name="crash_detection_dialog_title">Crash Detection</string>
<string name="crash_detection_dialog_note">Should a text messesage be sent on crash detction to your emergency contact?</string>

View File

@@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@@ -35,8 +36,13 @@ import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
@@ -66,7 +72,36 @@ public class RideService extends Service
public Boolean snoop = false; //should we log others ant+ devices
Set<String> pairedAnts; //list of ant devices to pair with
private Timer timer; //timer class to control the periodic messages
private Timer timerUI; //timer class to control the periodic messages
public String emergencyNumbuer; //the number to send the messages to
final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler.
Messenger replyTo;
class IncomingHandler extends Handler { // Handler of incoming messages from clients.
@Override
public void handleMessage(Message msg) {
if(msg.replyTo != null && timerUI != null) {
replyTo = msg.replyTo;
timerUI.scheduleAtFixedRate(
new TimerTask() {
@Override
public void run() {
Message msg = Message.obtain(null, 2, 0, 0);
Bundle bundle = new Bundle();
bundle.putSerializable("currentValues", (Serializable) currentValues);
msg.setData(bundle);
try {
replyTo.send(msg);
} catch (RemoteException e) { }
}
},
1000,
1000
); //every second update the screen
}
}
}
/**
* starts the ride on service start
@@ -77,12 +112,14 @@ public class RideService extends Service
return Service.START_NOT_STICKY;
}
/**
* sets android service settings
*/
@Override
public IBinder onBind(Intent arg0) {
return null;
timerUI = new Timer();
return mMessenger.getBinder();
}
@@ -91,6 +128,10 @@ public class RideService extends Service
*/
@Override
public boolean onUnbind (Intent intent) {
if(timerUI != null) {
timerUI.cancel();
}
return true;
}
@@ -261,6 +302,7 @@ public class RideService extends Service
} else {
body = body + getString(R.string.crash_unknow_location);
}
body = body + "\n " + getString(R.string.crash_magnitude) + ": " + String.valueOf(mag);
smsHome(body);
}
@@ -276,6 +318,7 @@ public class RideService extends Service
} else {
body = body + getString(R.string.crash_unknow_location);
}
smsHome(body);
}
@@ -345,6 +388,10 @@ public class RideService extends Service
timer.cancel();
}
if(timerUI != null) {
timerUI.cancel();
}
try {
buf.write("]}}");
buf.close();

View File

@@ -1,8 +1,15 @@
package com.ridelogger;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Map.Entry;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceType;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
@@ -12,16 +19,26 @@ import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchRe
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.FragmentActivity;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;
public class StartActivity extends FragmentActivity
@@ -35,8 +52,44 @@ public class StartActivity extends FragmentActivity
public static final String PAIRED_ANTS = "PairedAnts";
public static final String PHONE_HOME_PERIOD = "PhoneHomePeriod";
SharedPreferences settings;
MultiDeviceSearch mSearch;
GridLayout grid;
public Map<String, TextView> textViews;
private SharedPreferences settings;
private MultiDeviceSearch mSearch;
private RunningServiceInfo service;
Messenger mService = null;
boolean mIsBound;
final Messenger mMessenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
updateValues(msg.getData());
}
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = new Messenger(service);
try {
Message msg = Message.obtain();
msg.replyTo = mMessenger;
mService.send(msg);
}
catch (RemoteException e) {
// In this case the service has crashed before we could even do anything with it
}
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been unexpectedly disconnected - process crashed.
mService = null;
}
};
/**
* start up our class
@@ -55,6 +108,12 @@ public class StartActivity extends FragmentActivity
}
}
BroadcastReceiver mReceiver;
/**
* setup the settings for the user
*/
public void setupSettings() {
final Runnable askAntRunnable = new Runnable() {
@Override
@@ -417,6 +476,13 @@ public class StartActivity extends FragmentActivity
@Override
protected void onDestroy() {
if(mSearch != null) mSearch.close();
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
super.onDestroy();
finish();
}
@@ -436,7 +502,8 @@ public class StartActivity extends FragmentActivity
* start or stop ride
*/
protected void toggleRide() {
if(!isServiceRunning(RideService.class)) {
service = getServiceRunning(RideService.class);
if(service == null) {
final Runnable startAndCloseRunnable = new Runnable() {
@Override
public void run() {
@@ -461,9 +528,16 @@ public class StartActivity extends FragmentActivity
getString(R.string.edit_settings)
);
} else {
final StartActivity that = this;
final Runnable viewRunnable = new Runnable() {
@Override
public void run() {}
public void run() {
bindService(new Intent(that, RideService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
setContentView(R.layout.activity_dashboard);
textViews = new HashMap<String, TextView>();
grid = (GridLayout)findViewById(R.id.GridLayout1);
}
};
final Runnable stopRunnable = new Runnable() {
@@ -485,14 +559,35 @@ public class StartActivity extends FragmentActivity
}
}
/**
* update the text fields with current values
* @param bundle
*/
public void updateValues(Bundle bundle) {
@SuppressWarnings("unchecked")
Map<String, String> currentValues = (Map<String, String>) bundle.getSerializable("currentValues");
TextView tv;
Iterator<Entry<String, String>> it = currentValues.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
String key = entry.getKey();
if(!textViews.containsKey(key)) {
tv = new TextView(this);
textViews.put(key, tv);
grid.addView(tv);
}
textViews.get(key).setText(entry.getValue() + " " + key);
}
}
/**
* start the ride and notify the user of success
*/
private void startRide() {
if(!isServiceRunning(RideService.class)) {
this.startService(rsi);
}
this.startService(rsi);
Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.starting_ride), Toast.LENGTH_LONG);
toast.show();
}
@@ -503,13 +598,13 @@ public class StartActivity extends FragmentActivity
* @param serviceClass
* @return
*/
private boolean isServiceRunning(Class<?> serviceClass) {
private RunningServiceInfo getServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
return service;
}
}
return false;
return null;
}
}