/
MainActivity.java
251 lines (187 loc) · 6.26 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.example.spidey;
import java.io.IOException;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import com.example.spidey.cloud.OpenCellIdLookup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
public class MainActivity extends Activity {
private final static String TAG = "Spidey";
private final static int DEFAULT_MAP_ZOOM = 17;
private MapFragment mMapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
mMapFragment = new MapFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, mMapFragment).commit();
}
// Use this to test simple database CRUD
// Intent intent = new Intent(this, DatabaseActivity.class);
// startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id == R.id.action_scan) {
runScan ();
}
else if (id == R.id.action_about) {
showAbout ();
}
return super.onOptionsItemSelected(item);
}
private void showAbout ()
{
Intent intent = new Intent(this,AboutActivity.class);
startActivity(intent);
}
private void runScan ()
{
startService(new Intent(this, ScanService.class));
for (int i = 1; i < 6; i++)
mMapFragment.addResult("Tower Scan " + i + "\nresult info");
doCellLookup ();
}
private void doCellLookup ()
{
new CellLookupOperation().execute("");
}
private class CellLookupOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
OpenCellIdLookup.findMatchingCell("310","4384","65","2578");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class MapFragment extends Fragment implements LocationListener {
MapView mMapView;
Activity mActivity;
LinearLayout mViewResults;
public MapFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
setupMap("0","0",DEFAULT_MAP_ZOOM,rootView);
mViewResults = (LinearLayout)rootView.findViewById(R.id.results);
mActivity = this.getActivity();
return rootView;
}
public void addResult (String text)
{
/**
* <Button
android:text="Scan Result 1"
android:textSize="24sp"
android:textColor="#000000"
android:background="#cc999999"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_margin="6dp"
*/
Button btn = new Button(mActivity);
btn.setText(text);
btn.setTextSize(24);
btn.setTextColor(Color.BLACK);
btn.setPadding(3, 3, 3, 3);
btn.setBackgroundColor(Color.parseColor("#CC999999"));
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(6, 6, 6, 6);
mViewResults.addView(btn,lp);
}
protected void setupMap (String lat, String lon, int zoomLevel, View view)
{
mMapView = (MapView)view.findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.getController().setZoom(zoomLevel);
mMapView.getController().setCenter(GeoPoint.fromDoubleString(lat + ',' + lon,','));
doLocation();
}
private LocationClient locationclient;
private LocationRequest locationrequest;
private void doLocation ()
{
locationclient = new LocationClient(this.getActivity().getApplicationContext(),new ConnectionCallbacks (){
@Override
public void onConnected(Bundle arg0) {
locationrequest = LocationRequest.create();
locationrequest.setInterval(100);
locationclient.requestLocationUpdates(locationrequest, (LocationListener) MapFragment.this);
}
@Override
public void onDisconnected() {
}}, new OnConnectionFailedListener (){
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Log.d(TAG, "Could not connect to Google Play Services (location provider)");
}});
locationclient.connect();
}
@Override
public void onLocationChanged(Location loc) {
if (mMapView != null)
mMapView.getController().setCenter(new GeoPoint(loc.getLatitude(),loc.getLongitude()));
}
}
}