/[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 379 - (show annotations) (download)
Thu Oct 1 09:20:21 2009 UTC (14 years, 7 months ago) by torben
File size: 4227 byte(s)
A few more 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 private static final String PROVIDERNAME = "gps2";
23
24 LocationManager locationManager;
25
26 boolean isEnabled;
27
28 Button btnEnable;
29 Button btnDisable;
30 Button btnSet;
31 ListView listView;
32
33 ArrayAdapter<LocationBean> adapter;
34
35 List<LocationBean> locations = new ArrayList<LocationBean>();
36
37
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42
43 buildLocations();
44 listView = getListView();
45 listView.setChoiceMode( ListView.CHOICE_MODE_SINGLE );
46
47 locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
48
49 adapter = new ArrayAdapter<LocationBean>(this, android.R.layout.simple_list_item_single_choice, locations);
50
51 setListAdapter(adapter);
52
53
54 btnEnable = (Button) findViewById( R.id.enable );
55 btnDisable = (Button) findViewById( R.id.disable );
56 btnSet = (Button) findViewById( R.id.set );
57
58 btnEnable.setOnClickListener(enableListener);
59 btnDisable.setOnClickListener(disableListener);
60 btnSet.setOnClickListener(setListener);
61
62 }
63
64
65
66 @Override
67 protected void onResume() {
68 super.onResume();
69 LocationProvider provider = locationManager.getProvider(PROVIDERNAME);
70 isEnabled = (provider != null);
71 enableButtons();
72 Log.e("onResume", "onResume");
73 }
74
75
76
77 private void enableButtons() {
78 btnEnable.setEnabled( !isEnabled );
79 btnDisable.setEnabled( isEnabled );
80 btnSet.setEnabled( isEnabled );
81 }
82
83 private void buildLocations() {
84
85 locations.add( new LocationBean("Bjerringbro", 56.380745, 9.655609) );
86 locations.add( new LocationBean("Brønderslev", 57.272046, 9.944973) );
87 locations.add( new LocationBean("Hillerød", 55.929177, 12.308095) );
88 locations.add( new LocationBean("Gilleleje", 56.126901, 12.30271) );
89 locations.add( new LocationBean("København", 55.675092, 12.578573) );
90 locations.add( new LocationBean("Odder", 55.976632, 10.16407) );
91 locations.add( new LocationBean("Odense", 55.394839, 10.403166) );
92 locations.add( new LocationBean("Århus", 56.153828, 10.200369) );
93
94
95
96 locations.add( new LocationBean("Hamborg", 53.554444, 10.008488) );
97 locations.add( new LocationBean("Lund, Sverige", 55.708768, 13.185968) );
98 locations.add( new LocationBean("New York", 40.718464, -74.001689) );
99
100 }
101
102 View.OnClickListener enableListener = new View.OnClickListener() {
103 public void onClick(View v) {
104 locationManager.addTestProvider(PROVIDERNAME, false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
105 locationManager.setTestProviderEnabled(PROVIDERNAME, true);
106
107
108 isEnabled = true;
109 enableButtons();
110 }
111 };
112
113 View.OnClickListener disableListener = new View.OnClickListener() {
114 public void onClick(View v) {
115 locationManager.removeTestProvider(PROVIDERNAME);
116
117 isEnabled = false;
118 enableButtons();
119 }
120 };
121
122 View.OnClickListener setListener = new View.OnClickListener() {
123 public void onClick(View v) {
124 int selected = listView.getCheckedItemPosition();
125
126 if (selected >= 0) {
127
128 LocationBean bean = locations.get(selected);
129
130 Location loc = new Location(PROVIDERNAME);
131 loc.setLatitude( bean.getLatitude() );
132 loc.setLongitude( bean.getLongitude() );
133
134 locationManager.setTestProviderLocation(PROVIDERNAME, loc);
135 } else {
136 Log.e("mocklocation", "No item selected " + selected);
137 }
138 listView.setItemChecked(-1, true);
139
140 }
141 };
142
143
144
145
146 }

  ViewVC Help
Powered by ViewVC 1.1.20