Android Open Source - on-the-hook Fishing Activity






From Project

Back to project page on-the-hook.

License

The source code is released under:

MIT License

If you think the Android project on-the-hook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yoandinkov.onthehook;
//  w  ww.  ja v a2 s .  com

import java.sql.SQLException;

import org.apache.http.HttpStatus;

import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.support.DatabaseConnection;
import com.yoandinkov.onthehook.crypt.JsonConverter;
import com.yoandinkov.onthehook.db.DatabaseManager;
import com.yoandinkov.onthehook.models.Coordinates;
import com.yoandinkov.onthehook.models.Fishing;
import com.yoandinkov.onthehook.models.HttpResponseWrapper;
import com.yoandinkov.onthehook.models.NewFishing;
import com.yoandinkov.onthehook.models.RequestPurpose;
import com.yoandinkov.onthehook.models.Response;
import com.yoandinkov.onthehook.models.WaterLocation;

import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class FishingActivity extends RequestActivity {

  private LocationManager myLocationManager;
  private MyLocationListener myLocationListner;
  private ProgressBar ProgressDialogPlaceLoading;
  private ProgressBar ProgressBarFishesLoading;
  private TextView TextViewLocationText;
  private Button ButtonFinishFishing;
  private Button ButtonCatchFish;
  private Button ButtonViewDraught;
  private int createdFishingId;
  private LinearLayout LinearLayoutButtonHolder;
  private WaterLocation currentLocation;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fishing);
    
    this.setTextViewPlace((TextView)findViewById(R.id.fishingTextViewLocationText));
    this.setProgressBarFishesLoading((ProgressBar)findViewById(R.id.fishingProgressBarFishesLoading));
    this.setProgressDialogPlaceLoading((ProgressBar)findViewById(R.id.fishingProgressBarPlaceLoading));
    this.setButtonCatchFish((Button)findViewById(R.id.fishingButtonCatchFish));
    this.setButtonFinishFishing((Button)findViewById(R.id.fishingButtonFinish));
    this.setLinearLayoutButtonHolder((LinearLayout)findViewById(R.id.fishingLinearLayoutButtonHolder));
    this.setMyLocationListner(new MyLocationListener());
    this.setButtonViewDraught((Button)findViewById(R.id.fishingButtonViewDraugth));


    
    this.getButtonCatchFish().setOnClickListener(this);
    this.getButtonFinishFishing().setOnClickListener(this);
    this.getButtonViewDraught().setOnClickListener(this);
    

  }

  
  @Override
  protected void onResume() {
    super.onResume();
    
    int fishingId = this.getFishingPreferences();
    
    if (fishingId == -1) {
      this.getLocation();
    } else {
      this.displayButtons();
    }
  }
  
  private void getLocation() {
    this.setMyLocationManager((LocationManager) getSystemService(LOCATION_SERVICE));

    this.getMyLocationManager().requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 1000, 100,
        this.getMyLocationListner());
  }

  @Override
  public void onClick(View clickedView) {
    int clickedViewId = clickedView.getId();
    
    switch(clickedViewId) {
    case R.id.fishingButtonCatchFish:
      this.buttonCatchFishClick(clickedView);
      break;
    case R.id.fishingButtonFinish:
      this.buttonFinishClick(clickedView);
      break;
    case R.id.fishingButtonViewDraugth:
      this.startDraughtActivity(clickedView);
    }
  }
  
  private void startDraughtActivity(View currentView) {
    Intent startDraughtIntent = new Intent(this, DraughtActivity.class);
    
    startActivity(startDraughtIntent);
  }
  
  private void setFishingPreferences(WaterLocation location) {
    SharedPreferences fishing = getSharedPreferences("fishing", MODE_PRIVATE);
    SharedPreferences.Editor editor = fishing.edit();
    editor.putInt("locationId", location.getLocationId());
    editor.putInt("placeId", location.getPlaceId());
    editor.putString("placeName", location.getPlaceName());
    
    // put distance
    
    editor.commit();
  }
  
  private void setFishingId(int id) {
    SharedPreferences fishing = getSharedPreferences("fishing", MODE_PRIVATE);
    SharedPreferences.Editor editor = fishing.edit();
    
    editor.putInt("id", this.createdFishingId);
    editor.commit();
  }
  
  private int getFishingPreferences() {
    SharedPreferences fishing = getSharedPreferences("fishing", MODE_PRIVATE);
    
    int result = fishing.getInt("id", -1);
      
    return result;
  }
  
  private void buttonCatchFishClick(View currentView) {
    Intent startFishActivityIntent = new Intent(this, FishActivity.class);
        
    startFishActivityIntent.putExtra("fishingId", this.getFishingPreferences());
    
    startActivity(startFishActivityIntent);    
  }
  
  private void buttonFinishClick(View currentView) {
    this.finishFishing();
    
  }
  
  private void finishFishing() {
    HttpPutRequester currentRequest = new HttpPutRequester();
    
    currentRequest.setCurrentPurpose(RequestPurpose.FINISH_FISHING);
    
    Fishing finishingFishing = new Fishing(this.getFishingPreferences());
    
    String json = JsonConverter.toJson(finishingFishing);
    
    String url = String.format("%s/fishings/finish", this.getApiUrl());
    
    currentRequest.execute(url, json);
  }
  
  private void handleFinishedFishing(Response response) {
    if(response.getStatusCode() == HttpStatus.SC_OK) {
      this.removeFishes();
      this.removeCurrentFishing();
      
      Intent startMainMenuActivityIntent = new Intent(this, MainMenuActivity.class);
      
      startActivity(startMainMenuActivityIntent);
      
      finish();
    } else {
      Toast.makeText(this, response.getBody(), Toast.LENGTH_LONG).show();
    }
  }
  
  private void removeFishes() {
    DatabaseManager.init(this);
    DatabaseManager.getInstance().clearFishesList();
  }
  
  private void removeCurrentFishing() {
    SharedPreferences fishing = getSharedPreferences("fishing", MODE_PRIVATE);
    SharedPreferences.Editor editor = fishing.edit();
    
    editor.clear();
    editor.commit();
  }
  
  private void getClosestPlace(Coordinates currentCoordinates) {
    String coordinatesAsJson = JsonConverter.toJson(currentCoordinates);
    String url = this.getApiUrl() + "/places/closest";
    
    HttpPutRequester currentRequest = new HttpPutRequester();
    
    currentRequest.setCurrentPurpose(RequestPurpose.GET_LOCATION);
    
    currentRequest.execute(url, coordinatesAsJson);
  }

  // Auto-generated methods
  public LocationManager getMyLocationManager() {
    return myLocationManager;
  }

  public void setMyLocationManager(LocationManager myLocationManager) {
    this.myLocationManager = myLocationManager;
  }

  public MyLocationListener getMyLocationListner() {
    return myLocationListner;
  }

  public void setMyLocationListner(MyLocationListener myLocationListner) {
    this.myLocationListner = myLocationListner;
  }

  public ProgressBar getProgressDialogPlaceLoading() {
    return ProgressDialogPlaceLoading;
  }

  public void setProgressDialogPlaceLoading(ProgressBar progressDialogPlaceLoading) {
    ProgressDialogPlaceLoading = progressDialogPlaceLoading;
  }

  public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
      
      Coordinates currentLocation = new Coordinates(location.getLatitude(), location.getLongitude());
      
      FishingActivity.this.getClosestPlace(currentLocation);
      
      FishingActivity.this.getMyLocationManager().removeUpdates(FishingActivity.this.getMyLocationListner());
    }

    @Override
    public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
      // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub

    }
  }
  
  public ProgressBar getProgressBarFishesLoading() {
    return ProgressBarFishesLoading;
  }

  public void setProgressBarFishesLoading(ProgressBar progressBarFishesLoading) {
    ProgressBarFishesLoading = progressBarFishesLoading;
  }

  public TextView getTextViewPlace() {
    return TextViewLocationText;
  }

  public void setTextViewPlace(TextView textViewPlace) {
    TextViewLocationText = textViewPlace;
  }

  private void handleLocaltionResponse(Response response) {

      if (response.getStatusCode() == HttpStatus.SC_OK) {  
        this.getProgressDialogPlaceLoading().setVisibility(View.INVISIBLE);
        
        WaterLocation currentLocation = JsonConverter.fromJson(response.getBody(), WaterLocation.class);
        
        this.setCurrentLocation(currentLocation);
        
        this.getTextViewPlace().setText("You are near " + currentLocation.getPlaceName());
        
        this.getFishesByPlace(currentLocation.getPlaceId());
        
        this.createNewFishing(currentLocation.getPlaceId());
        
      } else {
        Log.d("OnTheHook", response.getBody());
        Log.d("OnTheHook", String.valueOf(response.getStatusCode()));
      }
    }
  
  private void createNewFishing(int placeId) {
    NewFishing currentFishing = new NewFishing();
    
    currentFishing.setPlaceId(placeId);
    
    String fishingAsJson = JsonConverter.toJson(currentFishing);
    String url = this.getApiUrl() + "/fishings/start";
    
    HttpPostRequester currentRequest = new HttpPostRequester();
    
    currentRequest.setCurrentPurpose(RequestPurpose.CREATE_FISHING);
    
    currentRequest.execute(url, fishingAsJson);
  }
  
  private void getFishesByPlace(int placeId){
    HttpGetRequester currentRequest = new HttpGetRequester();

    currentRequest.setCurrentPurpose(RequestPurpose.GET_FISHES_BY_PLACE);
    
    String url = this.getApiUrl() + "/places/" + String.valueOf(placeId) + "/fishes";

    currentRequest.execute(url);
  }
  
  private void displayButtons() {
    this.getProgressBarFishesLoading().setVisibility(View.GONE);
    this.getProgressDialogPlaceLoading().setVisibility(View.GONE);
    this.getLinearLayoutButtonHolder().setVisibility(View.VISIBLE);
    this.getTextViewPlace().setVisibility(View.VISIBLE);
  }
  
  @Override
  protected void handleResponse(HttpResponseWrapper response) {
  
    RequestPurpose purpose = response.getPurpose();
    Response currentResponse = response.getResponse();
      
    switch(purpose) {
    case CREATE_FISHING:
      this.handleCreateFishing(currentResponse);
      break;
    case LOGOUT:
      break;
    case GET_LOCATION:
      this.handleLocaltionResponse(currentResponse);
      break;
    case FINISH_FISHING:
      this.handleFinishedFishing(currentResponse);
      break;
    default:
      break;
    }
  }

  private void handleCreateFishing(Response currentResponse) {
    
    
    if (currentResponse.getStatusCode() == HttpStatus.SC_OK) {
      this.setCreatedFishingId(Integer.parseInt(currentResponse.getBody()));
      this.setFishingId(this.getCreatedFishingId());
      this.getProgressBarFishesLoading().setVisibility(View.INVISIBLE);
      this.getLinearLayoutButtonHolder().setVisibility(View.VISIBLE);
      
      this.setFishingPreferences(this.getCurrentLocation());
      //this.setSharedPreferencesFishingId - get fishing id
    } else if (currentResponse.getStatusCode() == HttpStatus.SC_FORBIDDEN) {
      // logout
    } else {
      // dialog
    }
    
  }

  public Button getButtonFinishFishing() {
    return ButtonFinishFishing;
  }

  public void setButtonFinishFishing(Button buttonFinishFishing) {
    ButtonFinishFishing = buttonFinishFishing;
  }

  public Button getButtonCatchFish() {
    return ButtonCatchFish;
  }

  public void setButtonCatchFish(Button buttonCatchFish) {
    ButtonCatchFish = buttonCatchFish;
  }

  
  
  public int getCreatedFishingId() {
    return createdFishingId;
  }

  public void setCreatedFishingId(int createdFishingId) {
    this.createdFishingId = createdFishingId;
  }

  private LinearLayout getLinearLayoutButtonHolder() {
    return LinearLayoutButtonHolder;
  }

  private void setLinearLayoutButtonHolder(LinearLayout linearLayoutButtonHolder) {
    LinearLayoutButtonHolder = linearLayoutButtonHolder;
  }

  public Button getButtonViewDraught() {
    return ButtonViewDraught;
  }

  public void setButtonViewDraught(Button buttonViewDraught) {
    ButtonViewDraught = buttonViewDraught;
  }

  public WaterLocation getCurrentLocation() {
    return currentLocation;
  }

  public void setCurrentLocation(WaterLocation currentLocation) {
    this.currentLocation = currentLocation;
  }
}




Java Source Code List

com.yoandinkov.onthehook.DraughtActivity.java
com.yoandinkov.onthehook.FishActivity.java
com.yoandinkov.onthehook.FishingActivity.java
com.yoandinkov.onthehook.LoginActivity.java
com.yoandinkov.onthehook.MainMenuActivity.java
com.yoandinkov.onthehook.RegisterActivity.java
com.yoandinkov.onthehook.RequestActivity.java
com.yoandinkov.onthehook.adapters.FishAdapter.java
com.yoandinkov.onthehook.crypt.JsonConverter.java
com.yoandinkov.onthehook.crypt.SHA1Converter.java
com.yoandinkov.onthehook.db.DatabaseHelper.java
com.yoandinkov.onthehook.db.DatabaseManager.java
com.yoandinkov.onthehook.db.models.FishDbModel.java
com.yoandinkov.onthehook.models.Coordinates.java
com.yoandinkov.onthehook.models.Credentials.java
com.yoandinkov.onthehook.models.FishButton.java
com.yoandinkov.onthehook.models.FishSpecies.java
com.yoandinkov.onthehook.models.Fish.java
com.yoandinkov.onthehook.models.Fishing.java
com.yoandinkov.onthehook.models.HttpResponseWrapper.java
com.yoandinkov.onthehook.models.HttpType.java
com.yoandinkov.onthehook.models.Login.java
com.yoandinkov.onthehook.models.NewFishing.java
com.yoandinkov.onthehook.models.Registration.java
com.yoandinkov.onthehook.models.RequestPurpose.java
com.yoandinkov.onthehook.models.Response.java
com.yoandinkov.onthehook.models.WaterLocation.java