Android Open Source - mock-location-test-android Mock Location Test Activity






From Project

Back to project page mock-location-test-android.

License

The source code is released under:

Apache License ? 2.0 TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by...

If you think the Android project mock-location-test-android 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.esri.template;
/* w  w  w  . ja v  a  2 s  .  c om*/
import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.quickstart.EsriQuickStart;
import com.esri.quickstart.EsriQuickStart.MapType;


public class MockLocationTestActivity extends Activity {
  
  EsriQuickStart _map = null;
  Button _mockLocationButton = null;
  MockLocationTest _locationTest;
  LocationListener _locationListener;
  EditText _latitudeText;
  EditText _longitudeText;
  boolean _allowed = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    _map = new EsriQuickStart(this,R.id.map);
    _map.addLayer(MapType.STREETS, null, null, null,true);
    
    _mockLocationButton = (Button) findViewById(R.id.button1);
    _latitudeText = (EditText) findViewById(R.id.latitude);
    _latitudeText.setText("38.89");
    _longitudeText = (EditText) findViewById(R.id.longitude);
    _longitudeText.setText("-77.03");
    
      _locationTest = new MockLocationTest();
    
    setButtonListeners();
    setLocationListener();
    setLocationManager();
    }
    
    private void setButtonListeners(){
      _mockLocationButton.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        createMockLocation();
      }
    });
    }
    
    private void setLocationManager(){
      _allowed = _locationTest.addTestProvider(this, LocationManager.GPS_PROVIDER, false, false, false, false, false, false, false, 0, android.location.Criteria.ACCURACY_FINE);
      LocationManager locationManager = _locationTest.getLocationManager();
    locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 10000, 10, _locationListener);
    }
    
    private void setLocationListener(){
      _locationListener = new LocationListener() {

      @Override
      public void onLocationChanged(Location location) {
          _map.centerAt(location.getLatitude(), location.getLongitude(), true);
          _map.addGraphicLatLon(location.getLatitude(), location.getLongitude(), null, SimpleMarkerSymbol.STYLE.CIRCLE,Color.BLUE,10);
      }

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

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

      @Override
      public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub
        
      }
        
      };
    }
    
    private void createMockLocation(){
      
      double latitude = 0;
      double longitude = 0;
    
    String lat = _latitudeText.getText().toString();
    String lon = _longitudeText.getText().toString();
    
    if(_locationTest.getLocationManager().getProvider(LocationManager.GPS_PROVIDER) != null){
      _locationTest.getLocationManager().removeTestProvider(LocationManager.GPS_PROVIDER);
      setLocationManager();
      _map.clearAllGraphics();
      _locationTest.clearLocation();
    }
      
    
    if(!lat.equals("") && !lon.equals("") && _allowed == true){
      latitude = Double.parseDouble(lat);
      longitude = Double.parseDouble(lon);
      _locationTest.addNewLocation(latitude,longitude, 3, 0, 0, 0, System.currentTimeMillis());
    }
    else{
      Toast toast = Toast.makeText(getApplicationContext(), "Invalid locations.Try again.", Toast.LENGTH_LONG);
      toast.show();
    }
    
    }

  @Override 
  protected void onDestroy() { 
    super.onDestroy();
  }
  
  @Override
  protected void onPause() {
    _map.pause();
    super.onPause();
  }
  
  @Override   protected void onResume() {
    super.onResume(); 
    _map.unpause();
  }

}




Java Source Code List

com.esri.template.MockLocationTestActivity.java
com.esri.template.MockLocationTest.java