package com.brink.main.Fragments; import java.io.ByteArrayOutputStream; import java.io.Console; import java.io.File; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.Intent; 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.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; 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.Spinner; import android.widget.SpinnerAdapter; import android.widget.TextView; import android.widget.Toast; import android.widget.SeekBar.OnSeekBarChangeListener; import android.graphics.BitmapFactory; import android.graphics.Bitmap.CompressFormat; import com.brink.main.IncomingAPI; import com.brink.main.OutgoingAPI; import com.brink.main.R; import com.brink.main.Stored; import com.brink.main.Fragments.Rate.RateFragment; import com.brink.main.api.BarApiManager; import com.brink.main.api.CheckinApiManager; import com.brink.main.api.RestClientManager; import com.brink.main.contracts.ISecondTaskNotifier; import com.brink.main.contracts.ITaskNotifier; import com.brink.main.contracts.IThirdTaskNotifier; import com.brink.main.models.Bar; import com.brink.main.models.Checkin; public class Rate extends FragmentActivity{ static Spinner barSelectionSpinner; static SeekBar seekbar; static ImageButton DoneButton; public static ImageButton btnCaptureImage; public static ImageView image; private static int usersRating; 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; private static String barSelected; private static ITaskNotifier<Bar> barCallback; private static ISecondTaskNotifier<List<Bar>> barsCallback; private static IThirdTaskNotifier<Checkin> checkinCallback; private static Boolean ratingCheck = false; private static Boolean uploadToFacebook = false; public static Context _Context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentManager fm = getSupportFragmentManager(); if(fm.findFragmentById(android.R.id.content) == null) { RateFragment mRateFragment = new RateFragment(); fm.beginTransaction().add(android.R.id.content, mRateFragment).commit(); } } public static class RateFragment extends Fragment implements ITaskNotifier<Bar>, ISecondTaskNotifier<List<Bar>>, IThirdTaskNotifier<Checkin> { private Uri imageUri; private String resultUrl; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.barpopuprate, container, false); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { switch (requestCode) { case ACTION_TAKE_PHOTO_S: Uri u = imageUri; Bitmap photo = BitmapFactory.decodeFile(u.getPath()); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(CompressFormat.JPEG, 75, stream); byte[] data = stream.toByteArray(); RestClientManager manager = new RestClientManager(); String accessUrl = manager.PostImage("uploadimage", data); resultUrl = accessUrl; } } public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //Get a Handle on all the individual XML Objects DoneButton = (ImageButton) this.getView().findViewById(R.id.donebutton); image = ( ImageView ) this.getView().findViewById( R.id.picturetaken ); btnCaptureImage = (ImageButton ) this.getView().findViewById( R.id.addphoto ); barSelectionSpinner = (Spinner) this.getView().findViewById(R.id.dropdownbars); final TextView ratingValue = (TextView) this.getView().findViewById(R.id.ratingvalue); seekbar = (SeekBar) this.getView().findViewById(R.id.ratingseek); image.setVisibility(View.INVISIBLE); setRetainInstance(true); _Context = getActivity(); checkinCallback = this; barCallback = this; barsCallback = this; AsyncTask<String, Void, List<Bar>> getBars = BarApiManager.GetBarsByRegionUtility(barsCallback); getBars.execute("Baton Rouge"); btnCaptureImage.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //TODO add the ability to capture large images then scale them down rather than small images Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); startActivityForResult(intent, ACTION_TAKE_PHOTO_S); } }); //For the SeekBar seekbar.setMax(5); seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser) { usersRating = progress; ratingValue.setText("" + progress); } public void onStartTrackingTouch(SeekBar seekBar) {} public void onStopTrackingTouch(SeekBar seekBar) { //Error Check so if the user Clicks Done we make sure we have a value. if(usersRating != 0) { ratingCheck = true; } else { ratingCheck = false; } } }); DoneButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(ratingCheck == true) { if(Stored.pictureTaken != null && 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 = mRate.new SendBarRatingTask(); //sendRating.execute(Stored.ratingPicked); } else if(Stored.pictureTaken != null && 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"); AsyncTask<String, Void, Bar> barTask = BarApiManager.GetBarByName(barCallback); barTask.execute(barSelected); } } } }); } //CallBacks for running Tasks public void OnTaskComplete(Bar results) { AsyncTask<Integer, Void, Checkin> sendRatingTask = CheckinApiManager.SendCheckin(checkinCallback, results); sendRatingTask.execute(usersRating); } public void onSecondTaskComplete(final List<Bar> result) { BarsSpinnerAdapter adapter = new BarsSpinnerAdapter(result, _Context); barSelectionSpinner.setAdapter(adapter); barSelectionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Bar mBar = result.get(pos); barSelected = mBar.Name; Toast.makeText(_Context, "You have selected item : " + barSelected, Toast.LENGTH_SHORT).show(); } public void onNothingSelected(AdapterView parent) {} }); } public void onThirdTaskComplete(Checkin result) { //Alert that the Checkin was sent successfully Toast.makeText(_Context, "Rating Sent Successfully", Toast.LENGTH_LONG).show(); } } } /* * Description: BarsSpinnerAdapter is a Custom adapter for the spinner on the RateBar page. */ class BarsSpinnerAdapter implements SpinnerAdapter { List<Bar> data = new ArrayList<Bar>(); Context _Context; public BarsSpinnerAdapter(List<Bar> result, Context Context){ _Context = Context; this.data = result; } 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(_Context); v.setTextColor(Color.BLACK); Bar mBar = (Bar) getItem(position); v.setText(mBar.Name); 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) _Context.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); Bar newBar = (Bar) getItem(position); textView.setText(newBar.Name); return convertView; } }