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 = "gps"; |
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("Odder", 55.976632, 10.16407) ); |
86 |
locations.add( new LocationBean("Kbh: Christiansborg", 55.675092, 12.578573) ); |
87 |
locations.add( new LocationBean("Bjerringbro", 56.380745, 9.655609) ); |
88 |
locations.add( new LocationBean("Hillerød", 55.929177, 12.308095) ); |
89 |
locations.add( new LocationBean("Aros, Århus", 56.153828, 10.200369) ); |
90 |
locations.add( new LocationBean("Gilleleje", 56.126901, 12.30271) ); |
91 |
locations.add( new LocationBean("Hamborg", 53.554444, 10.008488) ); |
92 |
locations.add( new LocationBean("New York", 40.718464, -74.001689) ); |
93 |
locations.add( new LocationBean("Lund, Sverige", 55.708768, 13.185968) ); |
94 |
} |
95 |
|
96 |
View.OnClickListener enableListener = new View.OnClickListener() { |
97 |
public void onClick(View v) { |
98 |
locationManager.addTestProvider(PROVIDERNAME, false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |
99 |
locationManager.setTestProviderEnabled(PROVIDERNAME, true); |
100 |
|
101 |
|
102 |
isEnabled = true; |
103 |
enableButtons(); |
104 |
} |
105 |
}; |
106 |
|
107 |
View.OnClickListener disableListener = new View.OnClickListener() { |
108 |
public void onClick(View v) { |
109 |
locationManager.removeTestProvider(PROVIDERNAME); |
110 |
|
111 |
isEnabled = false; |
112 |
enableButtons(); |
113 |
} |
114 |
}; |
115 |
|
116 |
View.OnClickListener setListener = new View.OnClickListener() { |
117 |
public void onClick(View v) { |
118 |
int selected = listView.getCheckedItemPosition(); |
119 |
|
120 |
if (selected >= 0) { |
121 |
|
122 |
LocationBean bean = locations.get(selected); |
123 |
|
124 |
Location loc = new Location(PROVIDERNAME); |
125 |
loc.setLatitude( bean.getLatitude() ); |
126 |
loc.setLongitude( bean.getLongitude() ); |
127 |
|
128 |
locationManager.setTestProviderLocation(PROVIDERNAME, loc); |
129 |
} else { |
130 |
Log.e("mocklocation", "No item selected " + selected); |
131 |
} |
132 |
listView.setItemChecked(-1, true); |
133 |
|
134 |
} |
135 |
}; |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
} |