package dk.thoerup.mocklocation; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class MockLocationView extends ListActivity { /** Called when the activity is first created. */ private static final String PROVIDERNAME = "gps2"; LocationManager locationManager; boolean isEnabled; Button btnEnable; Button btnDisable; Button btnSet; ListView listView; ArrayAdapter adapter; List locations = new ArrayList(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buildLocations(); listView = getListView(); listView.setChoiceMode( ListView.CHOICE_MODE_SINGLE ); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, locations); setListAdapter(adapter); btnEnable = (Button) findViewById( R.id.enable ); btnDisable = (Button) findViewById( R.id.disable ); btnSet = (Button) findViewById( R.id.set ); btnEnable.setOnClickListener(enableListener); btnDisable.setOnClickListener(disableListener); btnSet.setOnClickListener(setListener); } @Override protected void onResume() { super.onResume(); LocationProvider provider = locationManager.getProvider(PROVIDERNAME); isEnabled = (provider != null); enableButtons(); Log.e("onResume", "onResume"); } private void enableButtons() { btnEnable.setEnabled( !isEnabled ); btnDisable.setEnabled( isEnabled ); btnSet.setEnabled( isEnabled ); } private void buildLocations() { locations.add( new LocationBean("Bjerringbro", 56.380745, 9.655609) ); locations.add( new LocationBean("Brønderslev", 57.272046, 9.944973) ); locations.add( new LocationBean("Hillerød", 55.929177, 12.308095) ); locations.add( new LocationBean("Gilleleje", 56.126901, 12.30271) ); locations.add( new LocationBean("København", 55.675092, 12.578573) ); locations.add( new LocationBean("Odder", 55.976632, 10.16407) ); locations.add( new LocationBean("Odense", 55.394839, 10.403166) ); locations.add( new LocationBean("Århus", 56.153828, 10.200369) ); locations.add( new LocationBean("Hamborg", 53.554444, 10.008488) ); locations.add( new LocationBean("Lund, Sverige", 55.708768, 13.185968) ); locations.add( new LocationBean("New York", 40.718464, -74.001689) ); } View.OnClickListener enableListener = new View.OnClickListener() { public void onClick(View v) { locationManager.addTestProvider(PROVIDERNAME, false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); locationManager.setTestProviderEnabled(PROVIDERNAME, true); isEnabled = true; enableButtons(); } }; View.OnClickListener disableListener = new View.OnClickListener() { public void onClick(View v) { locationManager.removeTestProvider(PROVIDERNAME); isEnabled = false; enableButtons(); } }; View.OnClickListener setListener = new View.OnClickListener() { public void onClick(View v) { int selected = listView.getCheckedItemPosition(); if (selected >= 0) { LocationBean bean = locations.get(selected); Location loc = new Location(PROVIDERNAME); loc.setLatitude( bean.getLatitude() ); loc.setLongitude( bean.getLongitude() ); locationManager.setTestProviderLocation(PROVIDERNAME, loc); } else { Log.e("mocklocation", "No item selected " + selected); } listView.setItemChecked(-1, true); } }; }