1 |
torben |
356 |
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 |
|
|
} |
90 |
|
|
|
91 |
|
|
View.OnClickListener enableListener = new View.OnClickListener() { |
92 |
|
|
public void onClick(View v) { |
93 |
|
|
locationManager.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |
94 |
|
|
locationManager.setTestProviderEnabled("gps2", true); |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
isEnabled = true; |
98 |
|
|
enableButtons(); |
99 |
|
|
} |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
View.OnClickListener disableListener = new View.OnClickListener() { |
103 |
|
|
public void onClick(View v) { |
104 |
|
|
locationManager.removeTestProvider("gps2"); |
105 |
|
|
|
106 |
|
|
isEnabled = false; |
107 |
|
|
enableButtons(); |
108 |
|
|
} |
109 |
|
|
}; |
110 |
|
|
|
111 |
|
|
View.OnClickListener setListener = new View.OnClickListener() { |
112 |
|
|
public void onClick(View v) { |
113 |
|
|
int selected = listView.getCheckedItemPosition(); |
114 |
|
|
|
115 |
|
|
if (selected >= 0) { |
116 |
|
|
|
117 |
|
|
LocationBean bean = locations.get(selected); |
118 |
|
|
|
119 |
|
|
Location loc = new Location("gps2"); |
120 |
|
|
loc.setLatitude( bean.getLatitude() ); |
121 |
|
|
loc.setLongitude( bean.getLongitude() ); |
122 |
|
|
|
123 |
|
|
locationManager.setTestProviderLocation("gps2", loc); |
124 |
|
|
} else { |
125 |
|
|
Log.e("mocklocation", "No item selected " + selected); |
126 |
|
|
} |
127 |
|
|
listView.setItemChecked(-1, true); |
128 |
|
|
|
129 |
|
|
} |
130 |
|
|
}; |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
} |