Hello I have attached the Patch for Bug #138 <http://trac.hohndel.org/ticket/138> - Add a map picker for subsurface companion android application.
In order to save locations, a map picker is included in the add option. This feature is very handy for people with android devices. One could chose the location from map as well as the time offset in minutes of the dive (i.e how many minutes before the dive was performed). The location could be sent to server automatically or saved locally as per user preferences. Following is the list of changes made: 1. Added map picker option on add button in actionbar. 2. The new strings are included in languages German, French, Russian and Romanian. 3. Changed if else ladder in HomeActivity.java to switch case for better code readability Please review the code and send feedback for improvements. --- *Venkatesh Shukla B Tech ( Electrical Engineering )III YearIndian Institute of TechnologyBanaras Hindu UniversityPh No. +91 8960 579 122* *Email: [email protected] <[email protected]> *
From e82194023126d8d5a60d3657f68221757867ffed Mon Sep 17 00:00:00 2001 From: Venkatesh Shukla <[email protected]> Date: Mon, 3 Mar 2014 17:07:28 +0530 Subject: [PATCH] Fix for bug #138 - Add map picker to android app For storing dive location, a map picker is added. One could choose location of dive as well as time offset in minutes of the dive and store the information on server or locally as per preferences. For doing this, click the 'plus' symbol in the action bar and chose 'Use map' option. Pick location on map and press 'send' icon on the actionbar. Chose name of the dive and time offset in minutes. Pressing okay will save locally or send to server as per your preference. Changes made: 1. Added map picker option on add button in actionbar. a. Modified HomeActivity.java activity and dives.xml menu b. Added PickLocation.java activity and dive_picklocation.xml layout and dive_loc_menu.xml menu 2. The new strings are included in languages German, French, Russian and Romanian. a. Added strings in strings.xml of each language 3. Changed if else ladder in HomeActivity.java to switch case for better code readability a.Modified HomeActivity.java Reported-by: henrik Signed-off-by: Venkatesh Shukla <[email protected]> --- .gitignore | 1 + AndroidManifest.xml | 7 +- project.properties | 6 +- res/layout/dive_picklocation.xml | 31 +++ res/menu/dive_loc_menu.xml | 9 + res/menu/dives.xml | 11 +- res/values-de/strings.xml | 6 + res/values-fr/strings.xml | 6 + res/values-ro/strings.xml | 6 + res/values-ru/strings.xml | 6 + res/values/strings.xml | 6 + src/org/subsurface/HomeActivity.java | 414 +++++++++++++++++++++------------- src/org/subsurface/PickLocation.java | 83 +++++++ 13 files changed, 432 insertions(+), 160 deletions(-) create mode 100644 res/layout/dive_picklocation.xml create mode 100644 res/menu/dive_loc_menu.xml create mode 100644 src/org/subsurface/PickLocation.java diff --git a/.gitignore b/.gitignore index d7e2f0a..d55c959 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /bin /gen *.class +*~ # Package Files # *.jar diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 327e423..cd72546 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5,7 +5,7 @@ <uses-sdk android:minSdkVersion="9" - android:targetSdkVersion="15" /> + android:targetSdkVersion="19" /> <permission android:name="org.subsurface.mapfragment.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> @@ -43,13 +43,14 @@ <activity android:name="DiveDetailActivity" android:theme="@style/AppTheme.SubScreen" /> <activity android:name="Preferences" android:theme="@style/AppTheme" android:label="@string/menu_settings" android:excludeFromRecents="true" /> <activity android:name="MapActivity" android:theme="@style/AppTheme.SubScreen"></activity> + <activity android:name="PickLocation" android:theme="@style/AppTheme.SubScreen"></activity> <meta-data android:name="com.google.android.maps.v2.API_KEY" - android:value="AIzaSyAwDyRHLxnFRtECtjCU3etsPyD8y2DEtdo" /> + android:value="AIzaSyDT6q4u6abJkH_VOFWqAS8MV3MhXsaQS3E" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> -</manifest> \ No newline at end of file +</manifest> diff --git a/project.properties b/project.properties index 4554f77..607a736 100644 --- a/project.properties +++ b/project.properties @@ -11,6 +11,6 @@ #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. -target=android-15 -android.library.reference.1=..\\..\\Libs\\JakeWharton-ActionBarSherlock-e5c2d1c\\library -android.library.reference.2=..\\google-play-services_lib +target=android-19 +android.library.reference.1=../google-play-services_lib +android.library.reference.2=../actionbarsherlock diff --git a/res/layout/dive_picklocation.xml b/res/layout/dive_picklocation.xml new file mode 100644 index 0000000..bc4a913 --- /dev/null +++ b/res/layout/dive_picklocation.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" > + + <fragment + android:id="@+id/map" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:padding="3dp" + class="com.google.android.gms.maps.SupportMapFragment" /> + + <LinearLayout + android:id="@+id/linearLayout1" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:background="#000" + android:orientation="vertical" > + + <TextView + android:id="@+id/locinfo" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:padding="1dp" + android:textColor="#eee" + android:textSize="20sp" + android:gravity="center" + android:text="@string/instruction_pickloc" + /> + </LinearLayout> +</RelativeLayout> \ No newline at end of file diff --git a/res/menu/dive_loc_menu.xml b/res/menu/dive_loc_menu.xml new file mode 100644 index 0000000..da50d04 --- /dev/null +++ b/res/menu/dive_loc_menu.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" > + <item + android:id="@+id/dive_loc_finish" + android:title="@string/menu_save" + android:icon="@drawable/ic_menu_send_now" + android:showAsAction="always" + /> +</menu> diff --git a/res/menu/dives.xml b/res/menu/dives.xml index ff45fd3..0e47fa8 100644 --- a/res/menu/dives.xml +++ b/res/menu/dives.xml @@ -15,7 +15,16 @@ android:id="@+id/menu_new" android:icon="@drawable/ic_menu_new" android:showAsAction="always" - android:title="@string/menu_new"/> + android:title="@string/menu_new"> + <menu> + <item + android:id="@+id/menu_new_current" + android:title="@string/menu_new_current"/> + <item + android:id="@+id/menu_new_map" + android:title="@string/menu_new_map"/> + </menu> + </item> <item android:id="@+id/menu_start_background_service" android:icon="@android:drawable/ic_menu_mylocation" diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml index b0270d0..c12a10b 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -120,4 +120,10 @@ <string name="settings_other_website_summary">Subsurface Webseite anzeigen</string> <string name="settings_other_version">Version</string> <string name="settings_other_version_summary">Aktuelle Version: %s</string> + + <string name="menu_new_current">Strom</string> + <string name="menu_new_map">Verwenden Karte</string> + <string name="instruction_pickloc">Wählen Sie das Tauchpunkt</string> + <string name="hint_dive_offset">Minuten nach dem letzten Tauchgang</string> + <string name="confirmation_dive_picked_sent">Dive %s hinzugefügt wurde und gesendet</string> </resources> diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml index db39c10..07ce5cc 100644 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -120,4 +120,10 @@ <string name="settings_other_website_summary">Ouvrir la page du projet</string> <string name="settings_other_version">Version</string> <string name="settings_other_version_summary">Version actuelle : %s</string> + + <string name="menu_new_current">courant</string> + <string name="menu_new_map">Utilisez la carte</string> + <string name="instruction_pickloc">Sélectionnez le point de plongée</string> + <string name="hint_dive_offset">Minutes passé après la plongée</string> + <string name="confirmation_dive_picked_sent">Dive %s a été ajouté et envoyé</string> </resources> diff --git a/res/values-ro/strings.xml b/res/values-ro/strings.xml index 191bc79..dcb1166 100644 --- a/res/values-ro/strings.xml +++ b/res/values-ro/strings.xml @@ -120,4 +120,10 @@ <string name="settings_other_website_summary">Deschide pagina proiectului</string> <string name="settings_other_version">Versiune</string> <string name="settings_other_version_summary">Versiune actuală: %s</string> + + <string name="menu_new_current">curent</string> + <string name="menu_new_map">Folosiți harta</string> + <string name="instruction_pickloc">Selectați punctul de scufundare</string> + <string name="hint_dive_offset">Minute trecut după scufundare</string> + <string name="confirmation_dive_picked_sent">Dive %s a fost adăugat și a trimis</string> </resources> diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index 3e3f762..f47eed2 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -121,4 +121,10 @@ <string name="settings_other_website_summary">Открыть сайт проекта</string> <string name="settings_other_version">Версия</string> <string name="settings_other_version_summary">Текущая версия: %s</string> + + <string name="menu_new_current">ток</string> + <string name="menu_new_map">Используйте карту</string> + <string name="instruction_pickloc">Выберите точку погружения</string> + <string name="hint_dive_offset">Протокол мимо после погружения</string> + <string name="confirmation_dive_picked_sent">Была добавлена Dive %s и послал</string> </resources> diff --git a/res/values/strings.xml b/res/values/strings.xml index ea97133..da661b4 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -122,4 +122,10 @@ <string name="settings_other_website_summary">Open project website</string> <string name="settings_other_version">Version</string> <string name="settings_other_version_summary">Current version: %s</string> + + <string name="menu_new_current">Current</string> + <string name="menu_new_map">Use map</string> + <string name="instruction_pickloc">Select the dive point</string> + <string name="hint_dive_offset">Minutes past after dive</string> + <string name="confirmation_dive_picked_sent">Dive %s has been added and sent</string> </resources> diff --git a/src/org/subsurface/HomeActivity.java b/src/org/subsurface/HomeActivity.java index 4d301a1..ee8ed1b 100644 --- a/src/org/subsurface/HomeActivity.java +++ b/src/org/subsurface/HomeActivity.java @@ -21,6 +21,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; +import android.content.res.Resources; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; @@ -30,12 +31,14 @@ import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; +import android.preference.PreferenceManager; import android.support.v4.app.ActivityCompat; import android.text.InputType; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.Toast; @@ -45,6 +48,7 @@ import com.actionbarsherlock.app.SherlockListActivity; import com.actionbarsherlock.view.ActionMode; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; +import com.google.android.gms.maps.model.LatLng; public class HomeActivity extends SherlockListActivity implements com.actionbarsherlock.view.ActionMode.Callback, SelectionListener, OnNavigationListener { @@ -52,12 +56,12 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars private IBinder service = null; private final ServiceConnection connection = new ServiceConnection() { - + @Override public void onServiceDisconnected(ComponentName name) { service = null; } - + @Override public void onServiceConnected(ComponentName name, IBinder service) { HomeActivity.this.service = service; @@ -95,23 +99,23 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars .setCancelable(true) .setNegativeButton(android.R.string.no, null) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); - } - }).create().show(); + @Override + public void onClick(DialogInterface dialog, int which) { + startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); + } + }).create().show(); } private boolean isBackgroundLocationServiceStarted() { boolean started = false; ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); - for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { - if (BackgroundLocationService.class.getName().equals(service.service.getClassName())) { - started = true; - break; - } - } - return started; + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (BackgroundLocationService.class.getName().equals(service.service.getClassName())) { + started = true; + break; + } + } + return started; } private void refresh() { @@ -131,10 +135,11 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars } return message; } + @Override protected void onPostExecute(Integer success) { ((DiveArrayAdapter) getListAdapter()).notifyDataSetChanged(); - Toast.makeText(HomeActivity.this, success, Toast.LENGTH_SHORT).show(); + Toast.makeText(HomeActivity.this, success,Toast.LENGTH_SHORT).show(); refreshItem.setActionView(null); } }.execute(); @@ -160,15 +165,15 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars Toast.makeText(HomeActivity.this, R.string.error_no_settings, Toast.LENGTH_SHORT).show(); } else { // Send locations final Handler handler = new Handler() { - @Override - public void handleMessage(Message msg) { - int total = msg.arg1; - dialog.setProgress(total); - if (total >= dives.size()) { // OK, close dialog - dialog.dismiss(); - } - } - }; + @Override + public void handleMessage(Message msg) { + int total = msg.arg1; + dialog.setProgress(total); + if (total >= dives.size()) {// OK, close dialog + dialog.dismiss(); + } + } + }; new Thread(new Runnable() { public void run() { int success = 0; @@ -186,7 +191,7 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars handler.sendMessage(msg); } - // 100 % + // 100 % Message msg = handler.obtainMessage(); msg.arg1 = dives.size(); handler.sendMessage(msg); @@ -196,7 +201,7 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars runOnUiThread(new Runnable() { public void run() { ((DiveArrayAdapter) getListAdapter()).notifyDataSetChanged(); - Toast.makeText(HomeActivity.this, getString(R.string.confirmation_locations_sent, successCount, totalCount), Toast.LENGTH_SHORT).show(); + Toast.makeText(HomeActivity.this, getString(R.string.confirmation_locations_sent, successCount, totalCount),Toast.LENGTH_SHORT).show(); } }); } @@ -204,14 +209,53 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars } } + //Send location picked from map to the server and update the list + public void sendMapDiveLog(String name, LatLng latlng, long timestamp) { + if(name.contentEquals("")) + { + name = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("background_service_name", + getResources().getString(R.string.default_dive_name)); + } + DiveLocationLog locationlog = new DiveLocationLog(); + locationlog.setName(name); + locationlog.setLatitude(latlng.latitude); + locationlog.setLongitude(latlng.longitude); + locationlog.setTimestamp(timestamp); + if (UserController.instance.autoSend()) { + try { + DiveController.instance.sendDiveLog(locationlog); + Toast.makeText(HomeActivity.this, getString(R.string.confirmation_dive_picked_sent, name), Toast.LENGTH_SHORT).show(); + } catch (final WsException e) { + runOnUiThread(new Runnable() { + public void run() { + Toast.makeText(HomeActivity.this, e.getCode(), Toast.LENGTH_SHORT).show(); + } + }); + } catch (Exception e) { + Log.d(TAG, "Could not send dive " + locationlog.getName(), e); + runOnUiThread(new Runnable() { + public void run() { + Toast.makeText(HomeActivity.this, R.string.error_send, Toast.LENGTH_SHORT).show(); + } + }); + } + } else { + DiveController.instance.updateDiveLog(locationlog); + Toast.makeText(HomeActivity.this, getString(R.string.confirmation_location_picked, name), Toast.LENGTH_SHORT).show(); + } + runOnUiThread(new Runnable() { + public void run() { + ((DiveArrayAdapter) getListAdapter()).notifyDataSetChanged(); + } + }); + } + private void sendDiveLog(String name) { final DiveLocationLog locationLog = new DiveLocationLog(); locationLog.setName(name); final AtomicBoolean cancel = new AtomicBoolean(false); - final ProgressDialog waitDialog = ProgressDialog.show( - HomeActivity.this, - "", getString(R.string.dialog_wait), - true, true, new DialogInterface.OnCancelListener() { + final ProgressDialog waitDialog = ProgressDialog.show(HomeActivity.this, "", getString(R.string.dialog_wait), true, + true, new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { cancel.set(true); @@ -223,7 +267,7 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars } }); this.locationListener = new LocationListener() { - + @Override public void onStatusChanged(String provider, int status, Bundle extras) { if (!cancel.get()) { @@ -245,12 +289,13 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars Toast.makeText(HomeActivity.this, R.string.error_location, Toast.LENGTH_SHORT).show(); } } - + @Override public void onLocationChanged(final Location location) { if (!cancel.get()) { waitDialog.dismiss(); - Toast.makeText(HomeActivity.this, getString(R.string.confirmation_location_picked, locationLog.getName()), Toast.LENGTH_SHORT).show(); + Toast.makeText(HomeActivity.this, getString(R.string.confirmation_location_picked, + locationLog.getName()), Toast.LENGTH_SHORT).show(); new Thread(new Runnable() { public void run() { locationLog.setLocation(location); @@ -290,9 +335,9 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - UserController.instance.setContext(this); - try { + super.onCreate(savedInstanceState); + UserController.instance.setContext(this); + try { DiveController.instance.setContext(this); } catch (Exception e) { new AlertDialog.Builder(this) @@ -301,31 +346,29 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override - public void onClick(DialogInterface dialog, - int which) { + public void onClick(DialogInterface dialog, int which) { finish(); } }).setCancelable(false).show(); } - ArrayAdapter<CharSequence> listAdapter = ArrayAdapter.createFromResource( - this, R.array.list_menu_choices, - R.layout.sherlock_spinner_item); + ArrayAdapter<CharSequence> listAdapter = ArrayAdapter.createFromResource(this, R.array.list_menu_choices, + R.layout.sherlock_spinner_item); listAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item); getSupportActionBar().setListNavigationCallbacks(listAdapter, this); - getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); - // Retrieve location service - this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - setContentView(R.layout.dive_list); - DiveArrayAdapter adapter = new DiveArrayAdapter(this); - adapter.setListener(this); - setListAdapter(adapter); - getListView().setItemsCanFocus(true); - - // Register for dive service updates - if (isBackgroundLocationServiceStarted()) { - bindService(new Intent(this, BackgroundLocationService.class), connection, Context.BIND_NOT_FOREGROUND); - } + getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + // Retrieve location service + this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + setContentView(R.layout.dive_list); + DiveArrayAdapter adapter = new DiveArrayAdapter(this); + adapter.setListener(this); + setListAdapter(adapter); + getListView().setItemsCanFocus(true); + + // Register for dive service updates + if (isBackgroundLocationServiceStarted()) { + bindService(new Intent(this, BackgroundLocationService.class), connection, Context.BIND_NOT_FOREGROUND); + } } @Override @@ -367,7 +410,7 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars } @Override - public boolean onCreateOptionsMenu(Menu menu) { + public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.dives, menu); refreshItem = menu.findItem(R.id.menu_refresh); if (isBackgroundLocationServiceStarted()) { @@ -375,8 +418,8 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars } else { menu.findItem(R.id.menu_start_background_service).setTitle(getString(R.string.menu_start_background_service)); } - return true; - } + return true; + } @Override protected void onListItemClick(ListView l, View v, int position, long id) { @@ -385,70 +428,130 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars startActivity(detailIntent); } + + private static final int pickreq_code = 999; + static int lat; + static int lon; + //Receive result from map activity for map picker event + public void onActivityResult(int requestcode, int resultCode, Intent data) { + if (pickreq_code == requestcode && resultCode == RESULT_OK && data != null) { + Bundle rec_bundle = data.getExtras(); + final LatLng loc = (LatLng) rec_bundle.get("location"); + if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + + LinearLayout linlay = new LinearLayout(this); + linlay.setOrientation(LinearLayout.VERTICAL); + final EditText divename = new EditText(this); + divename.setHint(R.string.hint_dive_name); + divename.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); + final EditText diveoff = new EditText(this); + diveoff.setHint(R.string.hint_dive_offset); + diveoff.setInputType(InputType.TYPE_CLASS_NUMBER); + linlay.addView(divename); + linlay.addView(diveoff); + + builder.setView(linlay); + builder.setNegativeButton(android.R.string.cancel, null); + builder.setTitle(getString(R.string.dialog_location_name)) + .setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + long minuteoff; + String mins = diveoff.getText().toString(); + if(mins.contentEquals("")) + minuteoff = 0; + else + minuteoff = Integer.valueOf(mins); + // Dateutils.getFakeUtcDate returns timestamp in milliseconds. To include offset multiply by 60000 + sendMapDiveLog(divename.getText().toString(), loc, DateUtils.getFakeUtcDate() - minuteoff * 60000); + } + }).create().show(); + } else { + showGpsWarning(); + } + } else { + Toast.makeText(this, "No location detected", Toast.LENGTH_SHORT).show(); + } + } + @Override - public boolean onMenuItemSelected(int featureId, MenuItem item) { - if (item.getItemId() == R.id.menu_settings) { // Settings - startActivity(new Intent(this, Preferences.class)); - return true; - } else if (item.getItemId() == R.id.menu_new) { // Locate has been clicked - if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { - final EditText edit = new EditText(this); - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setView(edit); - builder.setNegativeButton(android.R.string.cancel, null); - edit.setHint(getString(R.string.hint_dive_name)); - edit.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); - builder.setTitle(getString(R.string.dialog_location_name)) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - sendDiveLog(edit.getText().toString()); - } - }).create().show(); - } else { - showGpsWarning(); - } + public boolean onMenuItemSelected(int featureId, MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_settings: + // Settings + startActivity(new Intent(this, Preferences.class)); return true; - } else if (item.getItemId() == R.id.menu_send_all) { // Send has been clicked - sendDives(DiveController.instance.getPendingLogs()); - return true; - } else if (item.getItemId() == R.id.menu_logoff) { - new AlertDialog.Builder(this) - .setTitle(R.string.menu_logoff) - .setIcon(android.R.drawable.ic_dialog_alert) - .setMessage(R.string.confirm_disconnect) - .setNegativeButton(android.R.string.cancel, null) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - DiveController.instance.deleteAll(); - UserController.instance.setUser(null); - startActivity(new Intent(HomeActivity.this, AccountLinkActivity.class)); - finish(); - } + case R.id.menu_new_map: + //Pick a location from map + Intent picklocation = new Intent(this, PickLocation.class); + startActivityForResult(picklocation, pickreq_code); + return true; + case R.id.menu_new_current: + // Locate has been clicked + if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + final EditText edit = new EditText(this); + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setView(edit); + builder.setNegativeButton(android.R.string.cancel, null); + edit.setHint(getString(R.string.hint_dive_name)); + edit.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); + builder.setTitle(getString(R.string.dialog_location_name)) + .setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + sendDiveLog(edit.getText().toString()); + } + }).create().show(); + } else { + showGpsWarning(); + } + return true; + case R.id.menu_send_all: + // Send has been clicked + sendDives(DiveController.instance.getPendingLogs()); + return true; + case R.id.menu_logoff: + new AlertDialog.Builder(this) + .setTitle(R.string.menu_logoff) + .setIcon(android.R.drawable.ic_dialog_alert) + .setMessage(R.string.confirm_disconnect) + .setNegativeButton(android.R.string.cancel, null) + .setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + DiveController.instance.deleteAll(); + UserController.instance.setUser(null); + startActivity(new Intent(HomeActivity.this, AccountLinkActivity.class)); + finish(); + } }).create().show(); - return true; - } else if (item.getItemId() == R.id.menu_refresh) { - refresh(); - return true; - } else if (item.getItemId() == R.id.menu_search) { - return onSearchRequested(); - } else if (item.getItemId() == R.id.menu_start_background_service) { - if (isBackgroundLocationServiceStarted()) { // Stop service - if (!stopService(new Intent(this, BackgroundLocationService.class))) { - Toast.makeText(this, R.string.error_background_service_unstoppable, Toast.LENGTH_SHORT).show(); - } - } else { // Start service - if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { - startService(new Intent(this, BackgroundLocationService.class)); - bindService(new Intent(this, BackgroundLocationService.class), connection, Context.BIND_NOT_FOREGROUND); - } else { - showGpsWarning(); - } - } - ActivityCompat.invalidateOptionsMenu(this); - } - return super.onMenuItemSelected(featureId, item); + return true; + case R.id.menu_refresh: + refresh(); + return true; + case R.id.menu_search: + return onSearchRequested(); + case R.id.menu_start_background_service: + if (isBackgroundLocationServiceStarted()) { // Stop service + if (!stopService(new Intent(this, BackgroundLocationService.class))) { + Toast.makeText(this, R.string.error_background_service_unstoppable, Toast.LENGTH_SHORT).show(); + } + } else { // Start service + if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + startService(new Intent(this, BackgroundLocationService.class)); + bindService(new Intent(this, BackgroundLocationService.class), connection, Context.BIND_NOT_FOREGROUND); + } else { + showGpsWarning(); + } + } + ActivityCompat.invalidateOptionsMenu(this); + break; + } + return super.onMenuItemSelected(featureId, item); } @Override @@ -479,7 +582,8 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { final List<DiveLocationLog> dives = ((DiveArrayAdapter) getListAdapter()).getSelectedDives(); - if (item.getItemId() == R.id.menu_send) { + switch (item.getItemId()) { + case R.id.menu_send: ArrayList<DiveLocationLog> copy = new ArrayList<DiveLocationLog>(); for (DiveLocationLog log : dives) { if (!log.isSent()) { @@ -487,46 +591,49 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars } } sendDives(copy); - } else if (item.getItemId() == R.id.menu_delete) { + break; + case R.id.menu_delete: if (!dives.isEmpty()) { new AlertDialog.Builder(this) - .setTitle(R.string.menu_delete) - .setMessage(R.string.confirm_delete_dives) - .setNegativeButton(android.R.string.cancel, null) - .setCancelable(true) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new Thread(new Runnable() { - public void run() { - int messageCode = R.string.error_delete_dives; - try { - for (DiveLocationLog log : dives) { - DiveController.instance.deleteDiveLog(log); - } - messageCode = -1; - } catch (WsException e) { - messageCode = e.getCode(); - } catch (Exception e) { - Log.d(TAG, "Could not delete dives", e); - } - final String message = messageCode == -1 ? null : getString(messageCode); - runOnUiThread(new Runnable() { - public void run() { - ((DiveArrayAdapter) getListAdapter()).notifyDataSetChanged(); - if (message != null) { - Toast.makeText(HomeActivity.this, message, Toast.LENGTH_SHORT).show(); + .setTitle(R.string.menu_delete) + .setMessage(R.string.confirm_delete_dives) + .setNegativeButton(android.R.string.cancel, null) + .setCancelable(true) + .setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new Thread(new Runnable() { + public void run() { + int messageCode = R.string.error_delete_dives; + try { + for (DiveLocationLog log : dives) { + DiveController.instance.deleteDiveLog(log); + } + messageCode = -1; + } catch (WsException e) { + messageCode = e.getCode(); + } catch (Exception e) { + Log.d(TAG, "Could not delete dives", e); + } + final String message = messageCode == -1 ? null : getString(messageCode); + runOnUiThread(new Runnable() { + public void run() { + ((DiveArrayAdapter) getListAdapter()).notifyDataSetChanged(); + if (message != null) { + Toast.makeText(HomeActivity.this, message, Toast.LENGTH_SHORT).show(); + } + } + }); } - } - }); - } - }).start(); - } - }).create().show(); + }).start(); + } + }).create().show(); } + break; } actionMode.finish(); - return true; + return true; } @Override @@ -555,3 +662,4 @@ public class HomeActivity extends SherlockListActivity implements com.actionbars return handled; } } + diff --git a/src/org/subsurface/PickLocation.java b/src/org/subsurface/PickLocation.java new file mode 100644 index 0000000..fb4c7b7 --- /dev/null +++ b/src/org/subsurface/PickLocation.java @@ -0,0 +1,83 @@ +package org.subsurface; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; + +import com.actionbarsherlock.app.SherlockFragmentActivity; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuItem; +import com.google.android.gms.maps.GoogleMap; +import com.google.android.gms.maps.GoogleMap.OnMapClickListener; +import com.google.android.gms.maps.SupportMapFragment; +import com.google.android.gms.maps.model.LatLng; +import com.google.android.gms.maps.model.MarkerOptions; + +public class PickLocation extends SherlockFragmentActivity implements OnMapClickListener +{ + + private GoogleMap mMap; + public static LatLng latlng; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.dive_picklocation); + setUpMapIfNeeded(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) + { + getSupportMenuInflater().inflate(R.menu.dive_loc_menu, menu); + return super.onCreateOptionsMenu(menu); + } + + + @Override + public boolean onOptionsItemSelected(MenuItem item) + { + switch(item.getItemId()) + { + case R.id.dive_loc_finish: + Intent resultIntent = new Intent(); + resultIntent.putExtra("location", latlng); + setResult(Activity.RESULT_OK, resultIntent); + finish(); + } + return super.onOptionsItemSelected(item); + } + + @Override + protected void onResume() { + super.onResume(); + setUpMapIfNeeded(); + } + + private void setUpMapIfNeeded() + { + if (mMap == null) + { + mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); + if (mMap != null) + { + setUpMap(); + } + } + } + + private void setUpMap() + { + mMap.setOnMapClickListener(this); + } + + @Override + public void onMapClick(LatLng loc) + { + mMap.clear(); + mMap.addMarker(new MarkerOptions().position(loc).title("Dive Spot")); + latlng = loc; + } + +} -- 1.7.9.5
_______________________________________________ subsurface mailing list [email protected] http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface
