source: other-projects/tipple-android/trunk/src/org/greenstone/android/tipple/TippleActivity.java@ 23977

Last change on this file since 23977 was 23977, checked in by davidb, 13 years ago

Initial set of files for the Tipple project, to be run on an Android device

File size: 13.0 KB
Line 
1package org.greenstone.android.tipple;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Locale;
6
7import android.location.LocationManager;
8import android.os.Bundle;
9import android.os.Environment;
10import android.preference.PreferenceManager;
11import android.speech.tts.TextToSpeech;
12import android.speech.tts.TextToSpeech.OnInitListener;
13import android.text.method.ScrollingMovementMethod;
14import android.util.Log;
15import android.view.Gravity;
16import android.view.Menu;
17import android.view.MenuItem;
18import android.view.View;
19import android.view.ViewGroup;
20import android.content.Context;
21import android.content.Intent;
22import android.content.SharedPreferences;
23import android.location.LocationListener;
24import android.widget.FrameLayout;
25import android.widget.LinearLayout;
26import android.widget.LinearLayout.LayoutParams;
27import android.widget.TextView;
28
29import org.mapsforge.android.maps.MapActivity;
30import org.mapsforge.android.maps.MapView;
31
32
33public class TippleActivity extends MapActivity implements OnInitListener
34{
35 enum TextAudioModeEnum { TEXT_ONLY, TEXT_PLUS_AUDIO, AUDIO_ONLY, AUDIO_PLUS_TEXT };
36
37 // File storage
38 public static String dataDirectory;
39 public static String logDirectory;
40 public static String audioDirectory;
41
42 // Preferences
43 public static SharedPreferences sharedPreferences;
44 public static boolean centreOnGPSLocation;
45 public static boolean showScaleBar;
46 public static boolean showSights;
47 public static boolean showUserTrail;
48 public static TextAudioModeEnum textAudioMode;
49
50 // Logging
51 public static TippleLog log;
52
53 // Views
54 protected TextView longlat_view_;
55 protected MapView map_view_;
56 protected TextView text_view_;
57 protected FrameLayout text_map_composite_;
58
59 protected AudioPlayerView audio_player_view_;
60
61 protected TipLocationManager tip_location_manager_;
62
63 protected ArrayList<TipLocation> locations_ = null;
64 protected ArrayList<TipLocation> user_trail_ = null;
65 protected String user_trail_filename_ = null;
66
67 // Text to Speech
68 protected TextToSpeech tts_ = null;
69
70 protected LocationManager location_manager_ = null;
71 protected LocationListener location_listener_ = null;
72
73 // Request codes
74 protected final int TTS_DATA_CHECK = 0;
75 private static final int SELECT_LOG_FILE = 1;
76
77
78 protected TextView createLongLatView()
79 {
80 // Lat,Long line at top
81 TextView longlat_view = new TextView(this);
82 longlat_view.setText("(Long,Lat): ");
83
84 LayoutParams longlat_view_layout
85 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 35, 0);
86 longlat_view.setLayoutParams(longlat_view_layout);
87
88 return longlat_view;
89 }
90
91
92
93
94 protected MapView createMapView(String map_filename)
95 {
96 // Map view in middle
97 MapView map_view = new MapView(this);
98 map_view.setClickable(true);
99 map_view.setBuiltInZoomControls(true);
100 String full_map_filename = dataDirectory + map_filename;
101 map_view.setMapFile(full_map_filename);
102
103 LayoutParams map_view_layout
104 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
105 ViewGroup.LayoutParams.WRAP_CONTENT, 1);
106 map_view.setLayoutParams(map_view_layout);
107
108
109 return map_view;
110 }
111
112 protected TextView createTextView()
113 {
114 // Text view in middle (ultimately on top of map view)
115 TextView text_view = new TextView(this);
116
117 LayoutParams text_view_layout
118 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
119 ViewGroup.LayoutParams.WRAP_CONTENT, 1);
120 text_view.setLayoutParams(text_view_layout);
121
122 text_view.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
123
124 text_view.setPadding(12, 12, 12, 12);
125 text_view.setTextColor(0xffffffff);
126 text_view.setBackgroundColor(0xCC000000);
127 text_view.setTextSize(20.0f);
128
129 text_view.setVerticalScrollBarEnabled(true);
130 text_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
131
132 text_view.setMovementMethod(new ScrollingMovementMethod());
133
134 text_view.setVisibility(View.INVISIBLE);
135
136 return text_view;
137 }
138
139 protected FrameLayout compositTextViewOnMapView(MapView map_view, TextView text_view)
140 {
141 FrameLayout frame_layout = new FrameLayout(this);
142
143 LayoutParams view_layout
144 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
145 ViewGroup.LayoutParams.WRAP_CONTENT, 1);
146
147 frame_layout.setLayoutParams(view_layout);
148 frame_layout.addView(map_view);
149 frame_layout.addView(text_view);
150
151 return frame_layout;
152 }
153
154 protected AudioPlayerView createAudioPlayerView()
155 {
156 // Player at the bottom
157 AudioPlayerView audio_player_view = new AudioPlayerView(this, text_view_);
158
159 LayoutParams audio_player_layout
160 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100, 0);
161 audio_player_view.setLayoutParams(audio_player_layout);
162
163 return audio_player_view;
164 }
165
166
167
168 protected void refreshPreferences()
169 {
170 // Restore preferences
171 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
172
173 //shared_preferences_ = getPreferences(MODE_PRIVATE);
174 showScaleBar = sharedPreferences.getBoolean("showScaleBar", false);
175 centreOnGPSLocation = sharedPreferences.getBoolean("centreOnGPSLoc", false);
176
177 showSights = sharedPreferences.getBoolean("showSights", true);
178 showUserTrail = sharedPreferences.getBoolean("showUserTrail", false);
179
180 String textAudioModeStr = sharedPreferences.getString("textAudioMode", "AUDIO_PLUS_TEXT");
181 textAudioMode = Enum.valueOf(TextAudioModeEnum.class, textAudioModeStr);
182 }
183
184
185 @Override
186 protected void onCreate(Bundle savedInstanceState)
187 {
188 System.err.println("*** TippleActivity::onCreate()");
189 super.onCreate(savedInstanceState);
190
191 // Locate map data directory
192 File external_root_file = Environment.getExternalStorageDirectory();
193 String external_root = external_root_file.getAbsolutePath();
194 dataDirectory = external_root + "/tipple-store/geodata/";
195 logDirectory = external_root + "/tipple-store/logs/";
196 audioDirectory = external_root + "/tipple-store/audio/";
197
198 // Set up logging
199 log = new TippleLog(this,logDirectory);
200 log.optStartLog();
201
202 // "refresh" preferences here so views can be configured correctly
203 // and ensure showUserTrail is reset to off (as not log file has been selected yet)
204 refreshPreferences();
205 SharedPreferences.Editor editor = sharedPreferences.edit();
206 editor.putBoolean("showUserTrail",false);
207
208 // Set up Text to Speech
209 Intent checkIntent = new Intent();
210 checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
211 startActivityForResult(checkIntent, TTS_DATA_CHECK);
212
213 //startActivityForResult(new Intent(this, LogFileBrowser.class), SELECT_LOG_FILE);
214
215
216 // Screen dived into:
217 // Top: GPS location
218 // Middle: Map view
219 // Bottom: Media player
220
221 LinearLayout linearLayout = new LinearLayout(this);
222 linearLayout.setOrientation(LinearLayout.VERTICAL);
223
224 longlat_view_ = createLongLatView();
225 map_view_ = createMapView("new-zealand.map");
226 text_view_ = createTextView();
227 text_map_composite_ = compositTextViewOnMapView(map_view_,text_view_);
228
229 audio_player_view_ = createAudioPlayerView();
230
231 linearLayout.addView(longlat_view_);
232 linearLayout.addView(text_map_composite_);
233 linearLayout.addView(audio_player_view_);
234
235 setContentView(linearLayout);
236 }
237
238
239 @Override
240 protected void onResume()
241 {
242 System.err.println("*** TippleActivity::onResume()");
243
244 super.onResume();
245
246 refreshPreferences();
247 audio_player_view_.refreshView();
248
249 map_view_.setScaleBar(showScaleBar);
250
251 if (tip_location_manager_ != null) {
252 if (showSights) {
253 //tip_location_manager_.createTourLocationsOverlay(map_view_,locations_);
254 tip_location_manager_.addTourLocations(map_view_);
255 }
256 else {
257 tip_location_manager_.removeTourLocations(map_view_);
258 }
259
260 if (user_trail_filename_ != null) {
261
262 if (showUserTrail) {
263 //tip_location_manager_.createLogRouteLocationsOverlay(map_view_,user_trail_);
264 tip_location_manager_.addLogRouteLocations(map_view_);
265 }
266 else {
267 tip_location_manager_.removeLogRouteLocations(map_view_);
268 }
269 }
270 }
271
272 }
273
274
275
276 protected void onActivityResult(int requestCode, int resultCode, Intent data)
277 {
278 System.err.println("*** TippleActivity::onActivityResult(), requestCode: " + requestCode);
279
280 if (requestCode == TTS_DATA_CHECK) {
281 if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
282 // success, create the TTS instance
283 tts_ = new TextToSpeech(this, (TextToSpeech.OnInitListener) this);
284 tts_.setLanguage(Locale.UK);
285
286 // now TTS initialized, init TTS aware place-locations
287 String loc_filename = dataDirectory + "hamilton.loc";
288 tip_location_manager_ = new TipLocationManager(this,audio_player_view_,tts_);
289 locations_ = tip_location_manager_.loadTour(loc_filename);
290 tip_location_manager_.createTourLocationsOverlay(map_view_,locations_);
291
292 // Set up a callback method for handling GPS location updates connected with our place-locations
293 location_manager_ = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
294 location_listener_
295 = new TippleLocationListener(this,map_view_,longlat_view_,tip_location_manager_,
296 locations_);
297
298 location_manager_.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, location_listener_);
299
300 } else {
301 // missing data, install it
302 Intent installIntent = new Intent();
303 installIntent.setAction(
304 TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
305 startActivity(installIntent);
306 }
307 }
308
309 else if (requestCode == SELECT_LOG_FILE) {
310
311 if (resultCode == RESULT_OK) {
312 if (data != null && data.getStringExtra("logFile") != null) {
313 if (user_trail_filename_ != null) {
314 // have a previously loaded route => scrub it
315 tip_location_manager_.removeLogRouteLocations(map_view_);
316 user_trail_filename_ = null;
317 }
318 // set up route overlay
319 user_trail_filename_ = data.getStringExtra("logFile");
320 user_trail_ = tip_location_manager_.loadUserTrail(user_trail_filename_);
321 tip_location_manager_.createLogRouteLocationsOverlay(map_view_,user_trail_);
322 }
323 } else if (resultCode == RESULT_CANCELED) {
324 // nothing to do
325 }
326
327 }
328
329 }
330
331
332 @Override
333 public void onInit(int status)
334 {
335 System.err.println("*** TippleActivity::onInit(), status (zero == good): " + status);
336
337 if (status == TextToSpeech.SUCCESS) {
338 Log.d("TippleActivity::onInit()","Text-to-speech engine initialized");
339 }
340 else if (status == TextToSpeech.ERROR){
341 Log.e("TippleActivity::onInit()","Text-to-speech engine failed initialized");
342 System.err.println("Error occured while initializing text-to-speech engine");
343 }
344
345 }
346
347 @Override
348 public boolean onCreateOptionsMenu(Menu menu)
349 {
350 getMenuInflater().inflate(R.menu.options_menu, menu);
351 return true;
352 }
353
354 @Override
355 public boolean onOptionsItemSelected(MenuItem item)
356 {
357 switch (item.getItemId()) {
358 case R.id.menu_info:
359 startActivity(new Intent(this, InfoView.class));
360 return true;
361
362 case R.id.menu_preferences:
363 startActivity(new Intent(this, EditPreferences.class));
364 return true;
365
366 case R.id.menu_logfile:
367 startActivityForResult(new Intent(this, TippleLogFileBrowser.class), SELECT_LOG_FILE);
368 return true;
369
370 default:
371 return false;
372 }
373 }
374
375
376
377 @Override
378 protected void onStop()
379 {
380 System.err.println("*** TippleActivity::onStop()");
381 super.onStop();
382
383 // Is this needed?
384 // Go through an Editor object to make preference changes stick
385 SharedPreferences.Editor editor = sharedPreferences.edit();
386 editor.commit();
387
388 }
389
390 @Override
391 protected void onDestroy()
392 {
393 System.err.println("*** TippleActivity::onDestroy()");
394 super.onDestroy();
395
396 tts_.shutdown();
397 tts_ = null;
398
399 // Switch off location updates
400 if (location_manager_ != null) {
401
402 if (location_listener_ != null) {
403
404 location_manager_.removeUpdates(location_listener_);
405 location_listener_ = null;
406 }
407 location_manager_ = null;
408
409 user_trail_filename_ = null;
410 }
411
412
413 TipLocationManager.stopOverlayThreads();
414
415
416 log.optStopLog();
417 }
418
419}
Note: See TracBrowser for help on using the repository browser.