source: other-projects/tipple-android/src/org/tipple/mapDisplay/tippleActivity.java@ 26523

Last change on this file since 26523 was 26523, checked in by davidb, 11 years ago

A version of Tipple with the mods Casey made for her Masters work

File size: 17.4 KB
Line 
1package org.tipple.mapDisplay;
2
3import java.io.File;
4import java.io.FileOutputStream;
5import java.io.InputStream;
6import java.util.ArrayList;
7import java.util.Locale;
8
9import org.mapsforge.android.maps.MapActivity;
10import org.mapsforge.android.maps.MapView;
11
12import android.app.Activity;
13import android.content.Context;
14import android.content.Intent;
15import android.content.SharedPreferences;
16import android.content.res.AssetManager;
17import android.graphics.drawable.Drawable;
18import android.location.LocationListener;
19import android.location.LocationManager;
20import android.os.Bundle;
21import android.os.Environment;
22import android.preference.PreferenceManager;
23import android.speech.tts.TextToSpeech;
24import android.speech.tts.TextToSpeech.OnInitListener;
25import android.text.method.ScrollingMovementMethod;
26import android.util.Log;
27import android.view.Gravity;
28import android.view.Menu;
29import android.view.MenuItem;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.Button;
33import android.widget.FrameLayout;
34import android.widget.LinearLayout;
35import android.widget.LinearLayout.LayoutParams;
36import android.widget.TextView;
37
38public class tippleActivity extends MapActivity implements OnInitListener {// OnSharedPreferenceChangeListener
39 enum TextAudioModeEnum {
40 TEXT_ONLY, TEXT_PLUS_AUDIO, AUDIO_ONLY, AUDIO_PLUS_TEXT
41 };
42
43 enum bookModeEnum {
44 BOOK_A, BOOK_B, BOOK_AB
45 };
46
47 // image loader
48 public ImageLoader loader;
49 public static TippleLog log;
50 public static bookModeEnum current_book_mode;
51 protected tippleActivity.bookModeEnum current_book_mode_;
52 // File storage to run on the emulator initially
53 public static String geodataDirectory = "/sdcard/";
54 public static String logDirectory = "/sdcard/";
55 public static String audioDirectory = "/sdcard/";
56 public static String imageDirectory = "/sdcard/";
57 // File storage to run on the real device
58 // public static String geodataDirectory;
59 // public static String logDirectory;
60 // public static String audioDirectory;
61
62 // Views
63 protected TextView longlat_view_;
64 protected MapView mapview;
65 protected TextView text_view_;
66 protected FrameLayout text_map_composite_;
67
68 protected ArrayList<tippleLocation> locations = null;
69 protected ArrayList<tippleLocation> user_trail_ = null;
70 protected String user_trail_filename_ = null;
71 protected TippleLocationManagerVersion2 tip_location_manager_,
72 my_location_manager;
73 // Preferences
74 public static SharedPreferences sharedPreferences;
75 public static boolean centreOnGPSLocation;
76 public static boolean showScaleBar;
77 public static boolean showSights;
78 public static boolean showUserTrail;
79 public static TextAudioModeEnum textAudioMode;
80 public static bookModeEnum bookMode;
81 public static boolean bookA;
82 public static boolean bookB;
83 public static String bookModeStr;
84 public static Boolean currentShowSights = false;
85 public static Boolean currentShowUserTrail = false;
86
87 // Text to Speech
88 protected TextToSpeech tts_ = null;
89 // Request codes
90 protected final int TTS_DATA_CHECK = 0;
91 private static final int SELECT_LOG_FILE = 1;
92
93 protected LocationListener mylocationListener = null;
94 public static Drawable highlightMarker1;
95 public static Drawable highlightMarker2;
96
97 // Text to Speech
98 private final String MY_DEBUG_TAG = "DL xml data retreive";
99 private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
100 // Meters
101 private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in
102 // miniseconds
103 protected AudioPlayerView audio_player_view_;
104 protected LocationManager locationManager;
105 protected Button retrieveLocationButton;
106 protected double currentLongitude;
107 protected double currentLantitude;
108 protected double currentNodeId;
109
110 /** Called when the activity is first created. */
111 @Override
112 public void onCreate(Bundle savedInstanceState) {
113 super.onCreate(savedInstanceState);
114 // Set up logging
115 log = new TippleLog(this, logDirectory);
116 log.optStartLog();
117
118 refreshPreferences();
119 SharedPreferences.Editor editor = sharedPreferences.edit();
120 editor.putBoolean("showUserTrail", false);
121
122 // Set up Text to Speech
123 Intent checkIntent = new Intent();
124 checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
125 startActivityForResult(checkIntent, TTS_DATA_CHECK);
126
127 mapview = createMapView("/sdcard/hamilton.map");
128 // For running on the real phone,change all file to tipple-store
129 // and asset folder
130 // mapview = createMapViewFromAsset("hamilton.map");
131
132 longlat_view_ = createLongLatView();
133 LinearLayout linearLayout = new LinearLayout(this);
134 linearLayout.setOrientation(LinearLayout.VERTICAL);
135 text_view_ = createTextView();
136 text_map_composite_ = compositTextViewOnMapView(mapview, text_view_);
137 audio_player_view_ = createAudioPlayerView();
138
139 linearLayout.addView(longlat_view_);
140 linearLayout.addView(text_map_composite_);
141 linearLayout.addView(audio_player_view_);
142 setContentView(linearLayout);
143
144 // load image in background and store in sdcard
145 load(this);
146 }
147
148 protected void load(Activity activity) {
149
150 try {
151 for (int i = 0; i < ImageURL.URLS.length; i++) {
152 loader = new ImageLoader(activity);
153 loader.download(ImageURL.URLS[i]);
154 }
155 } catch (Exception ex) {
156 System.out.println("tippleActivity load... error loading");
157 }
158
159 }
160
161 protected boolean assetToExternalStorage(String asset_filename,
162 String external_storage_filename) {
163 boolean status = true;
164
165 try {
166
167 File external_storage_file = new File(external_storage_filename);
168 if (!external_storage_file.exists()) {
169 // Need to create the /sdcard version
170
171 AssetManager assetManager = getAssets();
172
173 InputStream ais = assetManager.open(asset_filename); // ais =
174 // asset
175 // input
176 // stream
177 FileOutputStream fos = new FileOutputStream(
178 external_storage_file); // fos = file output stream
179
180 byte[] buffer = new byte[1024];
181
182 int buflen;
183 while ((buflen = ais.read(buffer)) > 0) {
184 fos.write(buffer, 0, buflen);
185 }
186
187 ais.close();
188 fos.close();
189 }
190
191 } catch (Exception e) {
192 e.printStackTrace();
193 status = false;
194 }
195
196 return status;
197 }
198
199 protected boolean initTippleStore(String location) {
200
201 // Locate map data directory
202 File external_root_file = Environment.getExternalStorageDirectory();
203 String external_root = external_root_file.getAbsolutePath();
204 String tipple_store_dir = external_root + "/tipple-store";
205
206 geodataDirectory = tipple_store_dir + "/geodata/";
207 // logDirectory = tipple_store_dir + "/logs/";
208 audioDirectory = tipple_store_dir + "/audio/";
209
210 File tipple_store_file = new File(tipple_store_dir);
211 if (!tipple_store_file.exists()) {
212 if (!tipple_store_file.mkdir()) {
213 System.err.println("Failed to create directory: "
214 + tipple_store_dir);
215 return false;
216 }
217 }
218
219 File geodataDirFile = new File(geodataDirectory);
220 if (!geodataDirFile.exists()) {
221 if (!geodataDirFile.mkdir()) {
222 System.err.println("Failed to create directory: "
223 + geodataDirectory);
224 return false;
225 }
226 }
227
228 File audioDirFile = new File(audioDirectory);
229 if (!audioDirFile.exists()) {
230 if (!audioDirFile.mkdir()) {
231 System.err.println("Failed to create directory: "
232 + audioDirectory);
233 return false;
234 }
235 }
236
237 // dig out the audio files
238 String[] audio_filenames = { "BeepMorseSonar.wav", "Ding.wav",
239 "DingExtended.wav" };
240 for (String audio_filename : audio_filenames) {
241 String audio_asset_filename = "audio/" + audio_filename;
242 String audio_exstore_filename = audioDirectory + audio_filename;
243
244 if (!assetToExternalStorage(audio_asset_filename,
245 audio_exstore_filename)) {
246 System.err.println("Failed to transfer asset "
247 + audio_asset_filename + " to "
248 + audio_exstore_filename);
249 return false;
250 }
251 }
252
253 if ((location != null) && (!location.equals(""))) {
254 // get out map and XML location file
255
256 String map_asset_filename = "geodata/" + location + ".map";
257 String map_exstore_filename = geodataDirectory + location + ".map";
258
259 if (!assetToExternalStorage(map_asset_filename,
260 map_exstore_filename)) {
261 System.err.println("Failed to transfer asset "
262 + map_asset_filename + " to " + map_exstore_filename);
263 return false;
264 }
265
266 }
267
268 return true;
269 }
270
271 protected boolean initTippleStore() {
272 return initTippleStore(null);
273 }
274
275 protected FrameLayout compositTextViewOnMapView(MapView map_view,
276 TextView text_view) {
277 FrameLayout frame_layout = new FrameLayout(this);
278
279 LayoutParams view_layout = new LinearLayout.LayoutParams(
280 ViewGroup.LayoutParams.WRAP_CONTENT,
281 ViewGroup.LayoutParams.WRAP_CONTENT, 1);
282
283 frame_layout.setLayoutParams(view_layout);
284 frame_layout.addView(map_view);
285 frame_layout.addView(text_view);
286
287 return frame_layout;
288 }
289
290 protected TextView createLongLatView() {
291 // Lat,Long line at top
292 TextView longlat_view = new TextView(this);
293 longlat_view.setText("(Long,Lat): ");
294
295 LayoutParams longlat_view_layout = new LinearLayout.LayoutParams(
296 ViewGroup.LayoutParams.MATCH_PARENT, 35, 0);
297 longlat_view.setLayoutParams(longlat_view_layout);
298
299 return longlat_view;
300 }
301
302 protected TextView createTextView() {
303 // Text view in middle (ultimately on top of map view)
304 TextView text_view = new TextView(this);
305
306 LayoutParams text_view_layout = new LinearLayout.LayoutParams(
307 ViewGroup.LayoutParams.MATCH_PARENT,
308 ViewGroup.LayoutParams.WRAP_CONTENT, 1);
309 text_view.setLayoutParams(text_view_layout);
310
311 text_view.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
312
313 text_view.setPadding(12, 12, 12, 12);
314 text_view.setTextColor(0xffffffff);
315 text_view.setBackgroundColor(0xCC000000);
316 text_view.setTextSize(20.0f);
317
318 text_view.setVerticalScrollBarEnabled(true);
319 text_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
320
321 text_view.setMovementMethod(new ScrollingMovementMethod());
322
323 text_view.setVisibility(View.INVISIBLE);
324
325 return text_view;
326 }
327
328 protected MapView createMapView(String map_filename) {
329 // Map view in middle
330 MapView mapview = new MapView(this);
331 mapview.setClickable(true);
332 mapview.setBuiltInZoomControls(true);
333 // String full_map_filename = geodataDirectory + map_filename;
334 // mapview.setMapFile(full_map_filename);
335 mapview.setMapFile(map_filename);
336 LayoutParams map_view_layout = new LinearLayout.LayoutParams(
337 ViewGroup.LayoutParams.WRAP_CONTENT,
338 ViewGroup.LayoutParams.WRAP_CONTENT, 1);
339 mapview.setLayoutParams(map_view_layout);
340 return mapview;
341
342 }
343
344 protected MapView createMapViewFromAsset(String map_filename) {
345 // Transfer asset to file in dataDirectory if it does not already exist
346 String asset_filename = "geodata" + File.separator + map_filename;
347 String external_storage_filename = geodataDirectory + map_filename;
348
349 assetToExternalStorage(asset_filename, external_storage_filename);
350
351 return createMapView(map_filename);
352
353 }
354
355 protected AudioPlayerView createAudioPlayerView() {
356 // Player at the bottom
357 AudioPlayerView audio_player_view = new AudioPlayerView(this,
358 text_view_);
359
360 LayoutParams audio_player_layout = new LinearLayout.LayoutParams(
361 ViewGroup.LayoutParams.MATCH_PARENT, 100, 0);
362 audio_player_view.setLayoutParams(audio_player_layout);
363
364 return audio_player_view;
365 }
366
367 protected void refreshPreferences() {
368 // Restore preferences
369 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
370
371 showScaleBar = sharedPreferences.getBoolean("showScaleBar", false);
372 centreOnGPSLocation = sharedPreferences.getBoolean("centreOnGPSLoc",
373 false);
374
375 showSights = sharedPreferences.getBoolean("showSights", true);
376 showUserTrail = sharedPreferences.getBoolean("showUserTrail", false);
377
378 String textAudioModeStr = sharedPreferences.getString("textAudioMode",
379 "AUDIO_PLUS_TEXT");
380 textAudioMode = Enum.valueOf(TextAudioModeEnum.class, textAudioModeStr);
381
382 // change book selection from radio button to check box
383 bookA = sharedPreferences.getBoolean("showBookA", false);
384 bookB = sharedPreferences.getBoolean("showBookB", false);
385
386 if (bookA) {
387 if (!bookB)
388 bookModeStr = "BOOK_A";
389 else
390 bookModeStr = "BOOK_AB";
391 } else {
392 if (!bookB)
393 bookModeStr = "BOOK_B";// by default, show one books
394 else
395 bookModeStr = "BOOK_B";
396
397 }
398
399 bookMode = Enum.valueOf(bookModeEnum.class, bookModeStr);
400 }
401
402 protected void displayLocationOnMap() {
403 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
404 tip_location_manager_ = new TippleLocationManagerVersion2(this,
405 mapview, longlat_view_, locationManager, audio_player_view_,
406 tts_);
407
408 }
409
410 Boolean isActive = true;
411 Boolean showPath = true;
412
413 @Override
414 protected void onResume() {
415 System.err.println("*** TippleActivity::onResume()");
416 super.onResume();
417 refreshPreferences();
418 audio_player_view_.refreshView();// try this way can we get locations
419 mapview.setScaleBar(showScaleBar);
420 if (tip_location_manager_ != null) {
421 refreshMapView();
422
423 }
424
425 }
426
427 @Override
428 protected void onStop() {
429 System.err.println("*** TippleActivity::onStop()");
430 super.onStop();
431
432 SharedPreferences.Editor editor = sharedPreferences.edit();
433 editor.commit();
434
435 }
436
437 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
438 System.err
439 .println("*** TippleActivity::onActivityResult(), requestCode: "
440 + requestCode);
441
442 if (requestCode == TTS_DATA_CHECK) {
443 if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
444 // success, create the TTS instance
445 tts_ = new TextToSpeech(this,
446 (TextToSpeech.OnInitListener) this);
447 tts_.setLanguage(Locale.UK);
448 displayLocationOnMap();
449 load(this);
450
451 } else {
452 // missing data, install it
453 Intent installIntent = new Intent();
454 installIntent
455 .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
456 startActivity(installIntent);
457 }
458 }
459
460 else if (requestCode == SELECT_LOG_FILE) {
461
462 }
463
464 }
465
466 public void onInit(int status) {
467 System.err
468 .println("*** TippleActivity::onInit(), status (zero == good): "
469 + status);
470
471 if (status == TextToSpeech.SUCCESS) {
472 Log.d("TippleActivity::onInit()",
473 "Text-to-speech engine initialized");
474 } else if (status == TextToSpeech.ERROR) {
475 Log.e("TippleActivity::onInit()",
476 "Text-to-speech engine failed initialized");
477 System.err
478 .println("Error occured while initializing text-to-speech engine");
479 }
480
481 }
482
483 protected void onDestroy() {
484 System.err.println("*** TippleActivity::onDestroy()");
485 super.onDestroy();
486
487 tts_.shutdown();
488 tts_ = null;
489
490 // Switch off location updates
491 if (locationManager != null) {
492
493 if (mylocationListener != null) {
494
495 locationManager.removeUpdates(mylocationListener);
496 mylocationListener = null;
497 }
498 mylocationListener = null;
499
500 }
501
502 tip_location_manager_.stopOverlayThreads();
503
504 }
505
506 @Override
507 public boolean onCreateOptionsMenu(Menu menu) {
508 getMenuInflater().inflate(R.menu.options_menu, menu);
509 return true;
510 }
511
512 @Override
513 public boolean onOptionsItemSelected(MenuItem item) {
514 switch (item.getItemId()) {
515
516 case R.id.menu_preferences:
517 startActivity(new Intent(this, EditPreferences.class));
518 return true;
519
520 default:
521 return false;
522 }
523 }
524
525 // @Override
526 public void refreshMapView() {
527
528 if (current_book_mode_ != tippleActivity.bookMode) {
529 // book mode has beeen changed
530 tip_location_manager_.resetOverlayThreads();// reset overlay item
531 System.out.println("Clear all arraylist");
532 if (tippleActivity.bookMode == tippleActivity.bookModeEnum.BOOK_A) {
533
534 locations = tip_location_manager_.loadTour(true);
535
536 }
537
538 else if (tippleActivity.bookMode == tippleActivity.bookModeEnum.BOOK_B) {
539 locations = tip_location_manager_.loadTour(false);
540
541 } else if (tippleActivity.bookMode == tippleActivity.bookModeEnum.BOOK_AB) {
542 locations = tip_location_manager_.loadTourDescription();
543
544 }
545 current_book_mode_ = bookMode;
546 displayLocationOnMapDynamically(locations);
547
548 }
549 showTour();
550 System.out.println("refreshMapView when mode not changed ");
551 }
552
553 protected void displayLocationOnMapDynamically(
554 ArrayList<tippleLocation> locations) {
555
556 if (mylocationListener != null) {
557 ((locationListener) mylocationListener)
558 .resetOverlayThreadsOnListener();
559 locationManager.removeUpdates(mylocationListener);
560
561 }
562 mylocationListener = null;
563 System.gc();
564 audio_player_view_.resetAudioView();
565 audio_player_view_.refreshView();
566
567 tip_location_manager_.createTourLocationsOverlay(mapview, locations);
568 mylocationListener = new locationListener(this, mapview, longlat_view_,
569 locationManager, tip_location_manager_, locations);
570 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
571 MINIMUM_TIME_BETWEEN_UPDATES,
572 MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
573 System.out.println("displayLocationOnmapDy created listener");
574
575 if (showSights) {
576 if (showUserTrail)
577 tip_location_manager_.addTourLocations(mapview, true);
578 else {
579 tip_location_manager_.resetRouteOverlay();
580 tip_location_manager_.addTourLocations(mapview, false);
581 }
582 } else {
583 tip_location_manager_.removeTourLocations(mapview);
584 }
585
586 }
587
588 /*
589 * when showSignts or showUserTrail has been changed will call this method
590 */
591 protected void showTour() {
592 if ((currentShowSights != showSights)
593 || (currentShowUserTrail != showUserTrail)) {
594 if (showSights) {
595 if (showUserTrail)
596 tip_location_manager_.addTourLocations(mapview, true);
597 else {
598 tip_location_manager_.resetRouteOverlay();
599 tip_location_manager_.addTourLocations(mapview, false);
600 }
601 } else {
602 tip_location_manager_.removeTourLocations(mapview);
603 }
604 currentShowSights = showSights;
605 currentShowUserTrail = showUserTrail;
606 }
607 }
608
609}
Note: See TracBrowser for help on using the repository browser.