/[projects]/android/MockLocation/src/dk/thoerup/mocklocation/MockLocationView.java
ViewVC logotype

Contents of /android/MockLocation/src/dk/thoerup/mocklocation/MockLocationView.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 359 - (show annotations) (download)
Tue Sep 29 19:40:07 2009 UTC (14 years, 7 months ago) by torben
File size: 3991 byte(s)
Added a few locations
1 package dk.thoerup.mocklocation;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.ListActivity;
7 import android.content.Context;
8 import android.location.Criteria;
9 import android.location.Location;
10 import android.location.LocationManager;
11 import android.location.LocationProvider;
12 import android.os.Bundle;
13 import android.util.Log;
14 import android.view.View;
15 import android.widget.ArrayAdapter;
16 import android.widget.Button;
17 import android.widget.ListView;
18
19 public class MockLocationView extends ListActivity {
20 /** Called when the activity is first created. */
21
22
23 LocationManager locationManager;
24
25 boolean isEnabled;
26
27 Button btnEnable;
28 Button btnDisable;
29 Button btnSet;
30 ListView listView;
31
32 ArrayAdapter<LocationBean> adapter;
33
34 List<LocationBean> locations = new ArrayList<LocationBean>();
35
36
37 @Override
38 public void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
40 setContentView(R.layout.main);
41
42 buildLocations();
43 listView = getListView();
44 listView.setChoiceMode( ListView.CHOICE_MODE_SINGLE );
45
46 locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
47
48 adapter = new ArrayAdapter<LocationBean>(this, android.R.layout.simple_list_item_single_choice, locations);
49
50 setListAdapter(adapter);
51
52
53 btnEnable = (Button) findViewById( R.id.enable );
54 btnDisable = (Button) findViewById( R.id.disable );
55 btnSet = (Button) findViewById( R.id.set );
56
57 btnEnable.setOnClickListener(enableListener);
58 btnDisable.setOnClickListener(disableListener);
59 btnSet.setOnClickListener(setListener);
60
61 }
62
63
64
65 @Override
66 protected void onResume() {
67 super.onResume();
68 LocationProvider gps2 = locationManager.getProvider("gps2");
69 isEnabled = (gps2 != null);
70 enableButtons();
71 Log.e("onResume", "onResume");
72 }
73
74
75
76 private void enableButtons() {
77 btnEnable.setEnabled( !isEnabled );
78 btnDisable.setEnabled( isEnabled );
79 btnSet.setEnabled( isEnabled );
80 }
81
82 private void buildLocations() {
83
84 locations.add( new LocationBean("Odder", 55.976632, 10.16407) );
85 locations.add( new LocationBean("Kbh: Christiansborg", 55.675092, 12.578573) );
86 locations.add( new LocationBean("Bjerringbro", 56.380745, 9.655609) );
87 locations.add( new LocationBean("Hillerød", 55.929177, 12.308095) );
88 locations.add( new LocationBean("Aros, Århus", 56.153828, 10.200369) );
89 locations.add( new LocationBean("Gilleleje", 56.126901, 12.30271) );
90 locations.add( new LocationBean("Hamborg", 53.554444, 10.008488) );
91 locations.add( new LocationBean("New York", 40.718464, -74.001689) );
92 locations.add( new LocationBean("Lund, Sverige", 55.708768, 13.185968) );
93 }
94
95 View.OnClickListener enableListener = new View.OnClickListener() {
96 public void onClick(View v) {
97 locationManager.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
98 locationManager.setTestProviderEnabled("gps2", true);
99
100
101 isEnabled = true;
102 enableButtons();
103 }
104 };
105
106 View.OnClickListener disableListener = new View.OnClickListener() {
107 public void onClick(View v) {
108 locationManager.removeTestProvider("gps2");
109
110 isEnabled = false;
111 enableButtons();
112 }
113 };
114
115 View.OnClickListener setListener = new View.OnClickListener() {
116 public void onClick(View v) {
117 int selected = listView.getCheckedItemPosition();
118
119 if (selected >= 0) {
120
121 LocationBean bean = locations.get(selected);
122
123 Location loc = new Location("gps2");
124 loc.setLatitude( bean.getLatitude() );
125 loc.setLongitude( bean.getLongitude() );
126
127 locationManager.setTestProviderLocation("gps2", loc);
128 } else {
129 Log.e("mocklocation", "No item selected " + selected);
130 }
131 listView.setItemChecked(-1, true);
132
133 }
134 };
135
136
137
138
139 }

  ViewVC Help
Powered by ViewVC 1.1.20