package com.brink.main;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class RateBar extends Activity {
Spinner s1;
SeekBar seekbar;
ImageButton DoneButton;
ImageButton CancelButton;
public static ImageButton button;
public static ImageView image;
private static final int ACTION_TAKE_PHOTO_B = 1;
private static final int ACTION_TAKE_PHOTO_S = 2;
private static final String JPEG_FILE_PREFIX = "IMG_";
private static final String JPEG_FILE_SUFFIX = ".jpg";
private String mCurrentPhotoPath;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Set the layout file
setContentView(R.layout.barpopuprate);
//Get a Handle on all the individual XML Objects
DoneButton = (ImageButton) findViewById(R.id.donebutton);
CancelButton = (ImageButton) findViewById(R.id.cancelbutton);
image = ( ImageView ) findViewById( R.id.picturetaken );
button = (ImageButton ) findViewById( R.id.addphoto );
image.setVisibility(View.INVISIBLE);
RetrieveBarsTask getBars = new RetrieveBarsTask(this);
getBars.execute(Stored.UserInformation.retrieveUsersCity());
//Add the picture
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//File file = new File(get)
//Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, ACTION_TAKE_PHOTO_S);
}
});
//For the SeekBar
final TextView value = (TextView) findViewById(R.id.ratingvalue);
seekbar = (SeekBar)findViewById(R.id.ratingseek);
seekbar.setMax(5);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(
SeekBar seekBar,
int progress,
boolean fromUser)
{
Stored.ratingPicked = progress;
value.setText("" + progress);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
//Error Check so if the user Clicks Done we make sure we have a value.
if(Stored.ratingPicked != 0)
{
Stored.ratingCorrect = true;
}
else
{
Stored.ratingCorrect = false;
}
}
});
DoneButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(Stored.ratingCorrect == true)
{
if(Stored.pictureTaken != null && Stored.uploadToFacebook == false)
{
//Launch 2 tasks, Upload picture to our server pool, Upload Rating to server.
Log.w("Launching 2 Tasks for Rating, and Server Upload", "RateBar.java");
SendBarRatingTask sendRating = new SendBarRatingTask();
sendRating.execute(Stored.ratingPicked);
finish();
}
else if(Stored.pictureTaken != null && Stored.uploadToFacebook == true)
{
//Launch 3 tasks, Upload picture to facebook, Upload picture to our server pool, Upload Rating to server.
Log.w("Launching 3 tasks for Rating, Facebook Upload, and Server Upload", "RateBar.Java");
}
else
{
//Launch 1 task for Uploading rating to the server.
Log.w("Launching 1 tasks for Rating", "RateBar.Java");
SendBarRatingTask sendRating = new SendBarRatingTask();
sendRating.execute(Stored.ratingPicked);
}
}
}
});
CancelButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.w("User Has called Cancel button", "Closing out RateBar Activity");
finish();
}
});
}
@Override
public void onStart()
{
super.onStart();
}
@Override
public void onPause()
{
super.onPause();
}
@Override
public void onResume()
{
super.onResume();
Log.v("RateBar - onResume", "Entering onResume method");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case ACTION_TAKE_PHOTO_B: {
if (resultCode == RESULT_OK) {
Log.w("RateBar - onActivityResult", "Intent was passed back");
//handleBigCameraPhoto();
}
break;
} // ACTION_TAKE_PHOTO_B
case ACTION_TAKE_PHOTO_S:
{
if (resultCode == RESULT_OK) {
Log.w("RateBar - onActivityResult", "Intent was passed back - Result OK");
handleSmallCameraPhoto(data);
}
else
{
Log.w("RateBar - onActivityResult", "Intent was passed back - Result BAD");
}
} // ACTION_TAKE_PHOTO_S
}
}
private void handleSmallCameraPhoto(Intent intent)
{
Bundle extras = intent.getExtras();
Stored.pictureTaken = (Bitmap) extras.get("data");
image.setImageBitmap(Stored.pictureTaken);
image.setVisibility(View.VISIBLE);
button.setVisibility(View.INVISIBLE);
}
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
private class RetrieveBarsTask extends AsyncTask<String, Integer, ArrayList<Stored.BarList>>
{
private RateBar _page;
protected RetrieveBarsTask(RateBar page)
{
_page = page;
}
protected ArrayList<Stored.BarList> doInBackground(String... params)
{
return IncomingAPI.populateBarPick("TigerLand");
}
protected void onPostExecute(ArrayList<Stored.BarList> result)
{
s1 = (Spinner) findViewById(R.id.dropdownbars);
BarsSpinnerAdapter adapter = new BarsSpinnerAdapter(result);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Stored.BarList b = (Stored.BarList) parent.getItemAtPosition(pos);
Toast.makeText(RateBar.this, "You have selected item : " + b.retrieveBarsName(), Toast.LENGTH_SHORT).show();
Stored.barToCheckIn = b.retrieveBarsName();
Log.w("BAR SELECTED FOR CHECK", Stored.barToCheckIn);
}
public void onNothingSelected(AdapterView parent) {}
});
}
}
/*
* Description: BarsSpinnerAdapter is a Custom adapter for the spinner on the RateBar page.
*/
private class BarsSpinnerAdapter implements SpinnerAdapter
{
ArrayList<Stored.BarList> data;
public BarsSpinnerAdapter(ArrayList<Stored.BarList> data){
this.data = data;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public int getItemViewType(int position) {
return android.R.layout.simple_spinner_dropdown_item;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(getApplicationContext());
v.setTextColor(Color.BLACK);
v.setText(data.get(position).retrieveBarsName());
return v;
}
public int getViewTypeCount() {
return 1;
}
public boolean hasStableIds() {
return false;
}
public boolean isEmpty() {
return false;
}
public void registerDataSetObserver(DataSetObserver arg0) {
}
public void unregisterDataSetObserver(DataSetObserver arg0) {
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
}
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setText(data.get(position).retrieveBarsName());
return convertView;
}
}
private class SendBarRatingTask extends AsyncTask<Integer, Void, Boolean>
{
//doInBackground should make the network call to send the Rating to our server
@Override
protected Boolean doInBackground(Integer... params)
{
return OutgoingAPI.SendBarRating(params[0], Stored.barToCheckIn);
}
//onPostExecute should be sent confirmation via a Boolean that the rating was sent successfully to the server.
@Override
protected void onPostExecute(Boolean result)
{
if(result)
{
Toast.makeText(RateBar.this, "Rating Has Been Sent Successfully", Toast.LENGTH_LONG).show();
Log.w("Rating was sent Successfully", "SendBarRatingTask - RateBar.java");
finish();
}
else
{
Toast.makeText(RateBar.this, "Problem sending Bar Rating", Toast.LENGTH_LONG).show();
Log.w("Problem Sending Bar Rating", "SendBarRatingTask - RateBar.java");
}
}
}
}