Android Open Source - myAndroidApps Where Parked Activity






From Project

Back to project page myAndroidApps.

License

The source code is released under:

This is my first GitHub try. Be gentle :)

If you think the Android project myAndroidApps 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 gidy.carpark;
/*from  www  .j a va2  s.  c o  m*/
import java.util.Locale;

import gidy.carpark.utils.GPSTracker;
import gidy.carpark.utils.LocationUtils;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class WhereParkedActivity extends Activity implements SensorEventListener{

  private boolean _isGeoRelevant = false;
  private Location curParkingLocationLoc = new Location("");
  private ParkingLocation _curParkingLoc;

  // Used for getting GPS location
  private GPSTracker _gpsTracker;

  // Used for compass
  final float alpha = 0.97f;

  private SensorManager _sensorManager;
  private float _currentAzimuth = 0f;
  private float _azimuth = 0f;
  private float[] _gravityMatrix = new float[3]; 
  private float[] _magneticsMatrix = new float[3]; 

  private ImageView arrowImage;
  private Button showOnMapButton;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_where_parked);

    _gpsTracker = new GPSTracker(WhereParkedActivity.this);

    arrowImage = (ImageView) findViewById(R.id.arrowImage);
    showOnMapButton  = (Button) findViewById(R.id.ShowOnMapButton);

    _sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    initGeo();

    setText();
    
    if (_isGeoRelevant){
      _sensorManager.registerListener(this, 
          _sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
          SensorManager.SENSOR_DELAY_GAME); 
      _sensorManager.registerListener(this, 
          _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
          SensorManager.SENSOR_DELAY_GAME);
      _sensorManager.registerListener(this,
          _sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION),
          SensorManager.SENSOR_DELAY_GAME);
    }

    else{
      arrowImage.setVisibility(View.GONE);
      showOnMapButton.setVisibility(View.GONE);
    }
  }


  private void initGeo(){

    _curParkingLoc = LocationUtils.getCurrentLocation();

    if (_curParkingLoc != null){
      
      if (_curParkingLoc.hasLocation() && _gpsTracker.canGetLocation()){
        _isGeoRelevant = true;
        
        curParkingLocationLoc.setLatitude(_curParkingLoc.getLat());
        curParkingLocationLoc.setLongitude(_curParkingLoc.getLong());
      }
    }
  }

  private void setText() {
    TextView tv = (TextView)findViewById(R.id.whereParkedText);

    String parkedAtStr = "?? ???? ???? ????";

    ParkingLocation curParkLoc = LocationUtils.getCurrentLocation();

    if (curParkLoc != null){

      parkedAtStr = "???? ?" + LocationUtils.getCurrentLocation().getLocationString();

      if (_isGeoRelevant){
        float distance = getDistanceToParking();
        
        if (distance < 1000){
          parkedAtStr += ", ????? " + Math.round(distance) +  " ?" + "\'";
        }
        else{
          parkedAtStr += ", ????? " + String.format("%.02f", (distance/1000)) + " ?" + "\"" + "?";
        }        
      }
    }

    tv.setText(parkedAtStr);
  }

  private float getDistanceToParking() {

    Location currentLocation = new Location("");
    currentLocation.setLatitude(_gpsTracker.getLatitude());
    currentLocation.setLongitude(_gpsTracker.getLongitude());

    return currentLocation.distanceTo(curParkingLocationLoc);
  }


  private float getBearingToParking(){

    Location currentLocation = new Location("");
    currentLocation.setLatitude(_gpsTracker.getLatitude());
    currentLocation.setLongitude(_gpsTracker.getLongitude());    

    return currentLocation.bearingTo(curParkingLocationLoc);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.where_parked, 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 onStop(){
    _gpsTracker.stopUsingGPS();
    _sensorManager.unregisterListener(this);
    super.onStop();
  }

  @Override
  public void onSensorChanged(SensorEvent event){

    if (event.sensor.getType() == Sensor.TYPE_SIGNIFICANT_MOTION){
      setText();
    }
    else{
      if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        _gravityMatrix[0] = alpha * _gravityMatrix[0] + (1 - alpha)
            * event.values[0];
        _gravityMatrix[1] = alpha * _gravityMatrix[1] + (1 - alpha)
            * event.values[1];
        _gravityMatrix[2] = alpha * _gravityMatrix[2] + (1 - alpha)
            * event.values[2];
      }
      if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        _magneticsMatrix[0] = alpha * _magneticsMatrix[0] + (1 - alpha)
            * event.values[0];
        _magneticsMatrix[1] = alpha * _magneticsMatrix[1] + (1 - alpha)
            * event.values[1];
        _magneticsMatrix[2] = alpha * _magneticsMatrix[2] + (1 - alpha)
            * event.values[2];
      }

      float R[] = new float[9];
      float I[] = new float[9];

      boolean success = SensorManager.getRotationMatrix(R, I, _gravityMatrix,  _magneticsMatrix);
      if (success) {
        // Compute Azimuth
        float orientation[] = new float[3];
        SensorManager.getOrientation(R, orientation);
        _azimuth = (float) Math.toDegrees(orientation[0]); 
        _azimuth = (_azimuth + 360) % 360;

        // Compute Bearing
        float bearing = getBearingToParking(); 

        // Change bearing from (-180)-180 to 0-360
        bearing = (bearing + 360) % 360;    
                
        // Point to the lcoation instead of North
        _azimuth += 360 - bearing;
        
        _azimuth = _azimuth % 360;
        
        adjustArrow();
      }
    }
  }


  private void adjustArrow(){

    float azimuthDiff = Math.abs(_azimuth - _currentAzimuth); 
    
    if (azimuthDiff > 10){
      
      Animation an = new RotateAnimation(-_currentAzimuth, -_azimuth,
          Animation.RELATIVE_TO_SELF, 0.5f, 
          Animation.RELATIVE_TO_SELF, 0.5f);

      _currentAzimuth = _azimuth;

      an.setDuration(500);
      an.setFillAfter(true);

      arrowImage.startAnimation(an);
    }
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // do nothing
  }

  @Override
  protected void onResume(){
    _sensorManager.registerListener(this, 
        _sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
        SensorManager.SENSOR_DELAY_UI); 
    _sensorManager.registerListener(this, 
        _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
        SensorManager.SENSOR_DELAY_UI); 
    _sensorManager.registerListener(this,
        _sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION),
        SensorManager.SENSOR_DELAY_GAME);
    super.onResume();
  }

  @Override
  protected void onPause(){
    _sensorManager.unregisterListener(this);
    super.onPause();
  }
  
  public void showOnMap(View view){
    
    String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)",
        _curParkingLoc.getLat(),
        _curParkingLoc.getLong(),
        _curParkingLoc.getLocationString());
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    startActivity(intent);
  }
}




Java Source Code List

gidy.carpark.HomeScreen.java
gidy.carpark.JustParkedActivity.java
gidy.carpark.ParkingLocation.java
gidy.carpark.WhereParkedActivity.java
gidy.carpark.utils.CommonUtils.java
gidy.carpark.utils.FileUtils.java
gidy.carpark.utils.GPSTracker.java
gidy.carpark.utils.LocationUtils.java
gidy.carpark.utils.LocationsAdapter.java
gidy.medappg.CallAmbulanceActivity.java
gidy.medappg.HomeMenu.java
gidy.medappg.MedAppLogger.java
gidy.medappg.activities.AllergicReactionActivity.java
gidy.medappg.activities.AsthmaActivity.java
gidy.medappg.activities.BleedingActivity.java
gidy.medappg.activities.BrokenBoneActivity.java
gidy.medappg.activities.BurnActivity.java
gidy.medappg.activities.ChokeActivity.java
gidy.medappg.activities.DiabeticActivity.java
gidy.medappg.activities.DistressActivity.java
gidy.medappg.activities.EpilepsyActivity.java
gidy.medappg.activities.HeadInjuryActivity.java
gidy.medappg.activities.HeartAttackActivity.java
gidy.medappg.activities.HypothermiaActivity.java
gidy.medappg.activities.MedicalIssueActivity.java
gidy.medappg.activities.MeningitisActivity.java
gidy.medappg.activities.PoisoningActivity.java
gidy.medappg.activities.StrokeActivity.java
gidy.medappg.activities.UnconsciousBreatingActivity.java
gidy.medappg.activities.UnconsciousNotBreathingActivity.java
gidy.medappg.videoActivities.AsthmaVideoActivity.java
gidy.medappg.videoActivities.BleedingVideoActivity.java
gidy.medappg.videoActivities.BrokenBoneVideoActivity.java
gidy.medappg.videoActivities.BurnVideoActivity.java
gidy.medappg.videoActivities.ChokeActivityVideo.java
gidy.medappg.videoActivities.DiabeticVideoActivity.java
gidy.medappg.videoActivities.DistressVideoActivity.java
gidy.medappg.videoActivities.EpilepsyVideoActivity.java
gidy.medappg.videoActivities.HeadInjuryVideoActivity.java
gidy.medappg.videoActivities.HeartAttackVideoActivity.java
gidy.medappg.videoActivities.PoisoningVideoActivity.java
gidy.medappg.videoActivities.StrokeVideoActivity.java
gidy.medappg.videoActivities.UnconsciousBreatingVideoActivity.java
gidy.medappg.videoActivities.UnconsciousNotBreathingVideoActivity.java
gidy.medappg.videoActivities.VideoActivity.java
gidy.medappnem.CallAmbulanceActivity.java
gidy.medappnem.HomeMenu2Activity.java
gidy.medappnem.HomeMenu3Activity.java
gidy.medappnem.HomeMenu4Activity.java
gidy.medappnem.HomeMenu5Activity.java
gidy.medappnem.HomeMenu.java
gidy.medappnem.MedAppLogger.java
gidy.medappnem.activities.AllergicReactionActivity.java
gidy.medappnem.activities.AsthmaActivity.java
gidy.medappnem.activities.BleedingActivity.java
gidy.medappnem.activities.BrokenBoneActivity.java
gidy.medappnem.activities.BurnActivity.java
gidy.medappnem.activities.ChokeActivity.java
gidy.medappnem.activities.DiabeticActivity.java
gidy.medappnem.activities.DistressActivity.java
gidy.medappnem.activities.EpilepsyActivity.java
gidy.medappnem.activities.HeadInjuryActivity.java
gidy.medappnem.activities.HeartAttackActivity.java
gidy.medappnem.activities.HypothermiaActivity.java
gidy.medappnem.activities.MedicalIssueActivity.java
gidy.medappnem.activities.MeningitisActivity.java
gidy.medappnem.activities.PoisoningActivity.java
gidy.medappnem.activities.StrokeActivity.java
gidy.medappnem.activities.UnconsciousBreatingActivity.java
gidy.medappnem.activities.UnconsciousNotBreathingActivity.java
gidy.medappnem.videoActivities.AsthmaVideoActivity.java
gidy.medappnem.videoActivities.BleedingVideoActivity.java
gidy.medappnem.videoActivities.BrokenBoneVideoActivity.java
gidy.medappnem.videoActivities.BurnVideoActivity.java
gidy.medappnem.videoActivities.ChokeActivityVideo.java
gidy.medappnem.videoActivities.DiabeticVideoActivity.java
gidy.medappnem.videoActivities.DistressVideoActivity.java
gidy.medappnem.videoActivities.EpilepsyVideoActivity.java
gidy.medappnem.videoActivities.HeadInjuryVideoActivity.java
gidy.medappnem.videoActivities.HeartAttackVideoActivity.java
gidy.medappnem.videoActivities.PoisoningVideoActivity.java
gidy.medappnem.videoActivities.StrokeVideoActivity.java
gidy.medappnem.videoActivities.UnconsciousBreatingVideoActivity.java
gidy.medappnem.videoActivities.UnconsciousNotBreathingVideoActivity.java
gidy.medappnem.videoActivities.VideoActivity.java