1
0
mirror of https://github.com/bspeice/itcs4180 synced 2025-07-03 23:06:05 -04:00

Added Compass capability, QR scanning (requires zxing to be installed),

and main search screen.
Added comments at the top of all files.
TODO: Fix formatting, add locations to server, add photos of locations,
implement database functionality, fix all other TODO's.
This commit is contained in:
tokugawa
2014-04-19 05:09:09 -04:00
parent 31935908b2
commit 995919644b
22 changed files with 736 additions and 7 deletions

View File

@ -0,0 +1,161 @@
package edu.uncc.scavenger;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* CompassActivity.java
*/
/* References:
* stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android
* stackoverflow.com/questions/5479753/using-orientation-sensor-to-point-towards-a-specific-location
*/
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CompassActivity extends Activity implements SensorEventListener
{
ImageView compass, arrowView;
Button backButton;
SensorManager sManager;
Sensor aSensor, mSensor;
float[] compassValues = new float[3];
float[] rotation = new float[16];
float[] rotated = new float[16];
float[] inclination = new float[16];
float[] gravity = new float[3];
float[] magneticField = new float[3];
float[] coordinates = new float[3];
LocationManager locationManager;
DirectionListener locationListener;
Location searchLocation;
Location testLocation = new Location(LocationManager.NETWORK_PROVIDER);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compass);
arrowView = (ImageView)findViewById(R.id.arrowView);
compass = (ImageView)findViewById(R.id.compassRose);
/*Test Values
testLocation.setLatitude(35.30719258);//woodward eagle
testLocation.setLongitude(-80.73505447);
testLocation.setLatitude(35.310043);//Bottom of Student Union bridge
testLocation.setLongitude(-80.733734);*/
searchLocation = new Location(LocationManager.NETWORK_PROVIDER);
searchLocation.setLatitude(getIntent().getDoubleExtra("searchLat", 0));
searchLocation.setLongitude(getIntent().getDoubleExtra("searchLong", 0));
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new DirectionListener(searchLocation);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
sManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
aSensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensor = sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
backButton = (Button)findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
protected void onResume()
{
super.onResume();
sManager.registerListener(this, aSensor, SensorManager.SENSOR_DELAY_GAME);
sManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause()
{
super.onPause();
sManager.unregisterListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent event)
{
//Check sensor that changed
int type = event.sensor.getType();
if(type == Sensor.TYPE_ACCELEROMETER)
{
gravity = event.values;
}
else if(type == Sensor.TYPE_MAGNETIC_FIELD)
{
magneticField = event.values;
}
else
{
return;
}
//Compute compass orientation values
SensorManager.getInclination(inclination);
SensorManager.getRotationMatrix(rotation, inclination, gravity, magneticField);
SensorManager.getOrientation(rotation, compassValues);
//Calculate true north and angle to desired location
//float azimuth = (float) (Math.toDegrees(compassValues[0]));
float trueHeading = (float)(Math.toDegrees(compassValues[0]) + locationListener.getDeclination());
//Calculate bearing to search location
float rotateArrow = (float) (trueHeading - locationListener.getBearing());
//Rotate compass and arrow. Rotations must be opposite to counteract device movement
compass.setRotation((long)(-1 * trueHeading));
arrowView.setRotation((long)(-1 * rotateArrow));
//TODO
//If within 5 to 10 meters, display image of search
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,82 @@
package edu.uncc.scavenger;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* DirectionListener.java
*/
import android.hardware.GeomagneticField;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
public class DirectionListener implements LocationListener
{
GeomagneticField geoField;
Location toGo;
float bearing;
float distance;
public DirectionListener(Location toGo)
{
this.toGo = toGo;
this.bearing = 0;
this.distance = 0;
}
@Override
public void onLocationChanged(Location location)
{
//String lat = ""+location.getLatitude();
//String lon = ""+location.getLongitude();
geoField = new GeomagneticField(
(float)location.getLatitude(),
(float)location.getLongitude(),
(float)location.getAltitude(),
System.currentTimeMillis()
);
bearing = location.bearingTo(toGo);
distance = location.distanceTo(toGo);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public float getBearing()
{
return bearing;
}
public float getDeclination()
{
if(geoField!=null)
return geoField.getDeclination();
else
return 0;
}
public float getDistance()
{
return distance;
}
}

View File

@ -0,0 +1,78 @@
package edu.uncc.scavenger;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* FoundActivity.java
*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FoundActivity extends Activity {
TextView numberFoundText;
Button seeMoreButton, tryMoreButton;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_found);
numberFoundText = (TextView)findViewById(R.id.numberFoundText);
seeMoreButton = (Button)findViewById(R.id.seeMoreButton);
tryMoreButton = (Button)findViewById(R.id.tryMoreButton);
seeMoreButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//TODO
//Get more information address and open web browser
}
});
tryMoreButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
});
//TODO
//Add found location to database
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.found, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -1,12 +1,22 @@
package edu.uncc.scavenger;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* MainActivity.java
*/
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
@ -18,18 +28,43 @@ import edu.uncc.scavenger.rest.RestLocation;
public class MainActivity extends Activity {
ListView locationList;
List<RestLocation> locations;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Test Code
RestLocation location = new RestLocation();
location.setId(1);
location.setName("Bridge");
location.setRiddle("Riddle");
location.setLocationLong(-80.733734);
location.setLocationLat(35.310043);
location.setKey("Key");
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.putExtra("restLocation", location);
startActivity(intent);
finish();
/*End Test Code*/
// Get our list of events loaded
locationList = (ListView)findViewById(R.id.listLocations);
List<RestLocation> locations = LocationDatabaseHelper.getInstance(this).fetchAll();
locations = LocationDatabaseHelper.getInstance(this).fetchAll();
if (locations != null && locations.size() > 0) {
LocationAdapter mLocationAdapter = new LocationAdapter(locations);
locationList.setAdapter(mLocationAdapter);
locationList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.putExtra("restLocation", locations.get(position));
startActivity(intent);
}
});
} else {
// We don't yet have any locations...
((TextView)findViewById(R.id.txtNoLocations)).setVisibility(View.VISIBLE);
@ -54,6 +89,7 @@ public class MainActivity extends Activity {
LocationDatabaseHelper.getInstance(MainActivity.this).persistAll(result);
}
}.execute();
}
@Override

View File

@ -0,0 +1,126 @@
package edu.uncc.scavenger;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* SearchActivity.java
*/
import edu.uncc.scavenger.rest.RestLocation;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class SearchActivity extends Activity {
ImageView locationImage;
Button compassButton, scanButton;
TextView riddleView;
Intent intent;
RestLocation restLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
locationImage = (ImageView)findViewById(R.id.locationImage);
compassButton = (Button)findViewById(R.id.compassButton);
scanButton = (Button)findViewById(R.id.scanButton);
riddleView = (TextView)findViewById(R.id.riddleView);
//TODO
//Load picture
//Load riddle
//Load hints
scanButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
try{
intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "SCAN_MODE");
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);
}
catch(ActivityNotFoundException e){
//Does not work on an emulator because there is no access to the market
Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
startActivity(marketIntent);
}
}
});
compassButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), CompassActivity.class);
intent.putExtra("searchLat", restLocation.getLocationLat());
intent.putExtra("searchLong", restLocation.getLocationLong());
startActivity(intent);
}
});
restLocation = (RestLocation)(getIntent().getSerializableExtra("restLocation"));
Log.d("restLocation", restLocation.getName());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK)
{
String contents = data.getStringExtra("SCAN_RESULT");
if(contents.equals(restLocation.getName()))
{
intent = new Intent(getApplicationContext(), FoundActivity.class);
startActivity(intent);
finish();
}
else
{
Toast.makeText(getApplicationContext(), "Incorrect url found: "+contents, Toast.LENGTH_SHORT).show();
}
}
else if (resultCode == RESULT_CANCELED)
{
Toast.makeText(getApplicationContext(), "Error scanning code", Toast.LENGTH_SHORT).show();
}
}
}
}

View File

@ -1,5 +1,12 @@
package edu.uncc.scavenger.database;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* LocationDatabaseHelper.java
*/
// Design pattern from: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html
import java.util.ArrayList;

View File

@ -1,5 +1,12 @@
package edu.uncc.scavenger.rest;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* LocationClient.java
*/
import java.util.HashMap;
import java.util.List;
import java.util.Map;

View File

@ -1,5 +1,12 @@
package edu.uncc.scavenger.rest;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* LocationService.java
*/
import java.util.List;
import java.util.Map;

View File

@ -1,8 +1,17 @@
package edu.uncc.scavenger.rest;
/*
* Bradlee Speice, Brandon Rodenmayer
* ITIS 4180
* UNCCScavenger (NinerFinder)
* RestLocation.java
*/
import java.io.Serializable;
import android.location.Location;
public class RestLocation {
public class RestLocation implements Serializable{
private int id;
private String name;