2014-04-19 05:09:09 -04:00
|
|
|
package edu.uncc.scavenger;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bradlee Speice, Brandon Rodenmayer
|
|
|
|
* ITIS 4180
|
|
|
|
* UNCCScavenger (NinerFinder)
|
|
|
|
* SearchActivity.java
|
|
|
|
*/
|
|
|
|
|
2014-04-19 16:37:08 -04:00
|
|
|
import java.net.URL;
|
|
|
|
|
2014-04-19 05:09:09 -04:00
|
|
|
import edu.uncc.scavenger.rest.RestLocation;
|
|
|
|
import android.app.Activity;
|
2014-04-19 17:31:33 -04:00
|
|
|
import android.app.AlertDialog;
|
2014-04-19 05:09:09 -04:00
|
|
|
import android.content.ActivityNotFoundException;
|
2014-04-19 17:31:33 -04:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
2014-04-19 05:09:09 -04:00
|
|
|
import android.content.Intent;
|
2014-04-19 16:37:08 -04:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2014-04-19 17:31:33 -04:00
|
|
|
import android.location.LocationManager;
|
2014-04-19 05:09:09 -04:00
|
|
|
import android.net.Uri;
|
2014-04-19 16:37:08 -04:00
|
|
|
import android.os.AsyncTask;
|
2014-04-19 05:09:09 -04:00
|
|
|
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;
|
2014-04-19 16:37:08 -04:00
|
|
|
TextView locationText, riddleView;
|
2014-04-19 05:09:09 -04:00
|
|
|
Intent intent;
|
|
|
|
RestLocation restLocation;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_search);
|
|
|
|
|
2014-04-19 16:37:08 -04:00
|
|
|
locationText = (TextView)findViewById(R.id.locationText);
|
2014-04-19 05:09:09 -04:00
|
|
|
locationImage = (ImageView)findViewById(R.id.locationImage);
|
|
|
|
compassButton = (Button)findViewById(R.id.compassButton);
|
|
|
|
scanButton = (Button)findViewById(R.id.scanButton);
|
|
|
|
riddleView = (TextView)findViewById(R.id.riddleView);
|
|
|
|
|
2014-04-19 16:37:08 -04:00
|
|
|
restLocation = (RestLocation)(getIntent().getSerializableExtra("restLocation"));
|
|
|
|
Log.d("restLocation", restLocation.getName());
|
|
|
|
|
|
|
|
locationText.setText(restLocation.getName());
|
|
|
|
riddleView.setText(restLocation.getRiddle());
|
|
|
|
|
2014-04-19 05:09:09 -04:00
|
|
|
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){
|
2014-04-19 17:31:33 -04:00
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
|
|
|
|
builder.setMessage(getString(R.string.zxing_missing_message));
|
|
|
|
builder.setPositiveButton(getString(R.string.dialog_ok_text), new DialogInterface.OnClickListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
//Does not work on an emulator because there is no access to the market
|
|
|
|
Uri marketUri = Uri.parse(getString(R.string.zxing_market_uri));
|
|
|
|
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
|
|
|
|
startActivity(marketIntent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(getString(R.string.dialog_cancel_text), new DialogInterface.OnClickListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
AlertDialog alert = builder.create();
|
|
|
|
alert.show();
|
2014-04-19 05:09:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
compassButton.setOnClickListener(new OnClickListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-04-19 17:31:33 -04:00
|
|
|
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
|
|
|
|
|
|
|
|
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
|
|
|
|
{
|
|
|
|
intent = new Intent(getApplicationContext(), CompassActivity.class);
|
2014-04-19 23:02:52 -04:00
|
|
|
intent.putExtra("restLocation", restLocation);
|
2014-04-19 17:31:33 -04:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
|
|
|
|
builder.setMessage(getString(R.string.gps_disabled_message));
|
|
|
|
builder.setPositiveButton(getString(R.string.dialog_ok_text), new DialogInterface.OnClickListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(getString(R.string.dialog_cancel_text), new DialogInterface.OnClickListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
AlertDialog alert = builder.create();
|
|
|
|
alert.show();
|
|
|
|
}
|
2014-04-19 05:09:09 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-19 17:31:33 -04:00
|
|
|
//Toast.makeText(getApplicationContext(), restLocation.getLocationImageURL(), Toast.LENGTH_SHORT).show();
|
2014-04-19 23:02:52 -04:00
|
|
|
Bitmap locationPicture = BitmapAccess.loadBitmap(getApplicationContext(), restLocation.getName());
|
|
|
|
if(locationPicture != null)
|
|
|
|
{
|
|
|
|
locationImage.setImageBitmap(locationPicture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-19 23:39:53 -04:00
|
|
|
new ImageDownloader().execute(restLocation.getRiddleImageURL());
|
2014-04-19 23:02:52 -04:00
|
|
|
}
|
2014-04-19 05:09:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
{
|
2014-04-19 17:31:33 -04:00
|
|
|
Toast.makeText(getApplicationContext(), "Incorrect place found: "+contents, Toast.LENGTH_SHORT).show();
|
2014-04-19 05:09:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (resultCode == RESULT_CANCELED)
|
|
|
|
{
|
|
|
|
Toast.makeText(getApplicationContext(), "Error scanning code", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-19 16:37:08 -04:00
|
|
|
|
|
|
|
private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(String... params) {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
URL imageUrl = new URL(params[0]);
|
|
|
|
return BitmapFactory.decodeStream(imageUrl.openStream());
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap result) {
|
|
|
|
|
2014-04-19 17:31:33 -04:00
|
|
|
if(result!=null)
|
|
|
|
{
|
|
|
|
locationImage.setImageBitmap(result);
|
2014-04-19 23:02:52 -04:00
|
|
|
BitmapAccess.saveBitmap(getApplicationContext(), result, restLocation.getName());
|
2014-04-19 17:31:33 -04:00
|
|
|
}
|
2014-04-19 16:37:08 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-19 05:09:09 -04:00
|
|
|
}
|