Created
December 30, 2012 02:31
-
-
Save eWizardII/4410665 to your computer and use it in GitHub Desktop.
Main Java Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.tigerblood; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Date; | |
import com.parse.LogInCallback; | |
import com.parse.Parse; | |
import com.parse.ParseAnonymousUtils; | |
import com.parse.ParseException; | |
import com.parse.ParseFile; | |
import com.parse.ParseUser; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.os.SystemClock; | |
import android.app.Activity; | |
import android.app.AlarmManager; | |
import android.app.PendingIntent; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends Activity implements SensorEventListener { | |
Button btnShowLocation; | |
// gpstracker class | |
gpstracker gps; | |
private TextView userid; | |
private SensorManager sensorManager; | |
double ax, ay, az; // these are the acceleration in x,y and z axis | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Calls the EULA Script | |
new SimpleEula(this).show(); | |
btnShowLocation = (Button) findViewById(R.id.btnShowLocation); | |
userid = (TextView) findViewById(R.id.textView2); | |
// Create Information for PARSE | |
Parse.initialize(this, "null", | |
"null"); | |
// Generate ANON user | |
ParseAnonymousUtils.logIn(new LogInCallback() { | |
@Override | |
public void done(ParseUser user, ParseException e) { | |
if (e != null) { | |
Log.d("Node", "Anonymous login failed."); | |
user = new ParseUser(); | |
user.setUsername("null user"); | |
user.setPassword("null pass"); | |
} else { | |
Log.d("Node", "Anonymous user logged in."); | |
} | |
// Create the PARSE file save the user ID into it as the header | |
// String currentUser = user.getUsername(); | |
// String header = "user: " + currentUser; | |
// byte[] data = header.getBytes(); | |
// ParseFile file = new ParseFile("data.txt", data); | |
} | |
}); | |
// Create data file for local storage | |
String filenameline = "CREATING DATA FILE \n"; | |
FileOutputStream fos = null; | |
try { | |
fos = openFileOutput("data.txt", Context.MODE_WORLD_READABLE); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
fos.write(filenameline.getBytes()); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
fos.close(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
// Schedule repeating alarm | |
Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class); | |
PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, | |
intent, 0); | |
// We want the alarm to go off 30 seconds from now. | |
long firstTime = SystemClock.elapsedRealtime(); | |
firstTime += 15 * 1000; | |
// Schedule the alarm! | |
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); | |
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, | |
15 * 1000, sender); | |
// Tell the user about what we did. | |
Toast.makeText( | |
getApplicationContext(), | |
"Scheduled", Toast.LENGTH_LONG) | |
.show(); | |
// Set up generic sensor managers | |
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |
// Set to read the sensor every 60 seconds | |
sensorManager.registerListener(this, | |
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), | |
60000000); | |
// Continuously grab data | |
final class MyLocationListener implements LocationListener { | |
@Override | |
public void onLocationChanged(Location locFromGps) { | |
// called when the listener is notified with a location update | |
// from the GPS | |
gps = new gpstracker(MainActivity.this); | |
// check if GPS enabled | |
if (gps.canGetLocation()) { | |
gps.getLocation(); | |
long timestamp = new Date().getTime(); | |
Toast.makeText( | |
getApplicationContext(), | |
"Sensor Readings \nLat: " + gps.latitude | |
+ "\nLong: " + gps.longitude + "\nax: " | |
+ ax + "\nay: " + ay + "\naz: " + az | |
+ "\ntime: " + timestamp, Toast.LENGTH_LONG) | |
.show(); | |
// data_string containing all the sensor data | |
String data_string = gps.latitude + "," + gps.longitude | |
+ "," + ax + "," + ay + "," + az + "\n"; | |
// Save data into a text file | |
// byte[] data = data_string.getBytes(); | |
// ParseFile file = new ParseFile("data.txt", data); | |
// Save data locally | |
FileOutputStream fos = null; | |
try { | |
fos = openFileOutput("data.txt", Context.MODE_APPEND); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
fos.write(data_string.getBytes()); | |
Toast.makeText( | |
getApplicationContext(), | |
"File Written", Toast.LENGTH_LONG) | |
.show(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
fos.close(); | |
Toast.makeText( | |
getApplicationContext(), | |
"File Close", Toast.LENGTH_LONG) | |
.show(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} else { | |
// can't get location | |
Toast.makeText(getApplicationContext(), "No GPS!", | |
Toast.LENGTH_LONG).show(); | |
gps.showSettingsAlert(); | |
} | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
// called when the GPS provider is turned off (user turning off | |
// the GPS on the phone) | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
// called when the GPS provider is turned on (user turning on | |
// the GPS on the phone) | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, | |
Bundle extras) { | |
// called when the status of the GPS provider changes | |
} | |
} | |
// GPS Reader | |
LocationListener locationListener = new MyLocationListener(); | |
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, | |
locationListener); | |
// Save data into a text file | |
// show location button click event | |
btnShowLocation.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
// Lock the user choices and display user name | |
// String currentUser = user.getUsername(); | |
// userid.setText(currentUser); | |
btnShowLocation.setEnabled(false); | |
} | |
}); | |
} | |
@Override | |
public void onAccuracyChanged(Sensor arg0, int arg1) { | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | |
ax = event.values[0]; | |
ay = event.values[1]; | |
az = event.values[2]; | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
} | |
//Schedule alarm for data upload | |
class RepeatingAlarm extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Toast.makeText( | |
context, | |
"ALARMED!", Toast.LENGTH_LONG) | |
.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment