aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgeo.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/cgeo.java')
-rw-r--r--main/src/cgeo/geocaching/cgeo.java288
1 files changed, 102 insertions, 186 deletions
diff --git a/main/src/cgeo/geocaching/cgeo.java b/main/src/cgeo/geocaching/cgeo.java
index c80ec6f..0091d7c 100644
--- a/main/src/cgeo/geocaching/cgeo.java
+++ b/main/src/cgeo/geocaching/cgeo.java
@@ -8,17 +8,17 @@ import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Units;
import cgeo.geocaching.maps.CGeoMap;
-import cgeo.geocaching.network.StatusUpdater.Status;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.GeoDirHandler;
-import cgeo.geocaching.utils.IObserver;
import cgeo.geocaching.utils.Log;
+import cgeo.geocaching.utils.RunnableWithArgument;
import cgeo.geocaching.utils.Version;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import android.app.AlertDialog;
+import android.app.AlertDialog.Builder;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
@@ -27,36 +27,28 @@ import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.location.Address;
import android.location.Geocoder;
-import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
-import android.view.ContextMenu;
import android.view.Menu;
+import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
-import android.view.ViewGroup;
-import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class cgeo extends AbstractActivity {
private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN";
- private static final int MENU_ABOUT = 0;
- private static final int MENU_HELPERS = 1;
- private static final int MENU_SETTINGS = 2;
- private static final int MENU_HISTORY = 3;
- private static final int MENU_SCAN = 4;
private static final int SCAN_REQUEST_CODE = 1;
- private static final int MENU_OPEN_LIST = 100;
-
public static final int SEARCH_REQUEST_CODE = 2;
private int version = 0;
@@ -172,74 +164,11 @@ public class cgeo extends AbstractActivity {
showToast(res.getString(reason == StatusCode.MAINTENANCE ? reason.getErrorString() : R.string.err_login_failed_toast));
}
} catch (Exception e) {
- Log.w("cgeo.firstLoginHander: " + e.toString());
+ Log.w("cgeo.firstLoginHander", e);
}
}
};
- private class StatusHandler extends Handler implements IObserver<Status> {
-
- @Override
- public void update(final Status data) {
- obtainMessage(0, data).sendToTarget();
- }
-
- @Override
- public void handleMessage(final Message msg) {
- final Status data = (Status) msg.obj;
- updateDisplay(data != null && data.message != null ? data : null);
- }
-
- private void updateDisplay(final Status data) {
- final ViewGroup status = (ViewGroup) findViewById(R.id.status);
- final ImageView statusIcon = (ImageView) findViewById(R.id.status_icon);
- final TextView statusMessage = (TextView) findViewById(R.id.status_message);
-
- if (data == null) {
- status.setVisibility(View.GONE);
- return;
- }
-
- if (data.icon != null) {
- final int iconId = res.getIdentifier(data.icon, "drawable", getPackageName());
- if (iconId != 0) {
- statusIcon.setImageResource(iconId);
- statusIcon.setVisibility(View.VISIBLE);
- } else {
- Log.e("StatusHandler: could not find icon corresponding to @drawable/" + data.icon);
- statusIcon.setVisibility(View.GONE);
- }
- } else {
- statusIcon.setVisibility(View.GONE);
- }
-
- String message = data.message;
- if (data.messageId != null) {
- final int messageId = res.getIdentifier(data.messageId, "string", getPackageName());
- if (messageId != 0) {
- message = res.getString(messageId);
- }
- }
-
- statusMessage.setText(message);
- status.setVisibility(View.VISIBLE);
-
- if (data.url != null) {
- status.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(final View v) {
- startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(data.url)));
- }
- });
- } else {
- status.setClickable(false);
- }
- }
-
- }
-
- private StatusHandler statusHandler = new StatusHandler();
-
public cgeo() {
super("c:geo-main-screen");
}
@@ -248,6 +177,12 @@ public class cgeo extends AbstractActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
+ // If we had been open already, start from the last used activity.
+ finish();
+ return;
+ }
+
setContentView(R.layout.main);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); // type to search
@@ -288,7 +223,6 @@ public class cgeo extends AbstractActivity {
@Override
public void onResume() {
super.onResume();
- app.getStatusUpdater().addObserver(statusHandler);
locationUpdater.startGeo();
satellitesHandler.startGeo();
updateUserInfoHandler.sendEmptyMessage(-1);
@@ -312,7 +246,6 @@ public class cgeo extends AbstractActivity {
@Override
public void onPause() {
initialized = false;
- app.getStatusUpdater().deleteObserver(statusHandler);
locationUpdater.stopGeo();
satellitesHandler.stopGeo();
super.onPause();
@@ -320,18 +253,15 @@ public class cgeo extends AbstractActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, MENU_SETTINGS, 0, res.getString(R.string.menu_settings)).setIcon(R.drawable.ic_menu_preferences);
- menu.add(0, MENU_HISTORY, 0, res.getString(R.string.menu_history)).setIcon(R.drawable.ic_menu_recent_history);
- menu.add(0, MENU_HELPERS, 0, res.getString(R.string.menu_helpers)).setIcon(R.drawable.ic_menu_shopping);
- menu.add(0, MENU_SCAN, 0, res.getString(R.string.menu_scan_geo)).setIcon(R.drawable.ic_menu_barcode);
- menu.add(0, MENU_ABOUT, 0, res.getString(R.string.menu_about)).setIcon(R.drawable.ic_menu_info_details);
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.main_options, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
- MenuItem item = menu.findItem(MENU_SCAN);
+ MenuItem item = menu.findItem(R.id.menu_scan);
if (item != null) {
item.setEnabled(isIntentAvailable(SCAN_INTENT));
}
@@ -350,26 +280,25 @@ public class cgeo extends AbstractActivity {
public boolean onOptionsItemSelected(MenuItem item) {
final int id = item.getItemId();
switch (id) {
- case MENU_ABOUT:
+ case R.id.menu_about:
showAbout(null);
return true;
- case MENU_HELPERS:
+ case R.id.menu_helpers:
startActivity(new Intent(this, UsefulAppsActivity.class));
return true;
- case MENU_SETTINGS:
+ case R.id.menu_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
- case MENU_HISTORY:
+ case R.id.menu_history:
cgeocaches.startActivityHistory(this);
return true;
- case MENU_SCAN:
+ case R.id.menu_scan:
startScannerApplication();
return true;
default:
- break;
+ return super.onOptionsItemSelected(item);
}
- return false;
}
private void startScannerApplication() {
@@ -408,90 +337,6 @@ public class cgeo extends AbstractActivity {
}
}
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
-
- // context menu for offline button
- if (v.getId() == R.id.search_offline) {
- menu.setHeaderTitle(res.getString(R.string.list_title));
- for (final StoredList list : app.getLists()) {
- menu.add(Menu.NONE, MENU_OPEN_LIST + list.id, Menu.NONE, list.getTitleAndCount());
- }
- return;
- }
-
- // standard context menu
- menu.setHeaderTitle(res.getString(R.string.menu_filter));
-
- //first add the most used types
- menu.add(1, 0, 0, CacheType.ALL.getL10n());
- menu.add(1, 1, 0, CacheType.TRADITIONAL.getL10n());
- menu.add(1, 2, 0, CacheType.MULTI.getL10n());
- menu.add(1, 3, 0, CacheType.MYSTERY.getL10n());
-
- // then add all other cache types sorted alphabetically
- List<String> sorted = new ArrayList<String>();
- for (CacheType ct : CacheType.values()) {
- if (ct == CacheType.ALL ||
- ct == CacheType.TRADITIONAL ||
- ct == CacheType.MULTI ||
- ct == CacheType.MYSTERY) {
- continue;
- }
- sorted.add(ct.getL10n());
- }
- Collections.sort(sorted);
- for (String choice : sorted) {
- menu.add(1, menu.size(), 0, choice);
- }
-
- // mark current filter as checked
- menu.setGroupCheckable(1, true, true);
- boolean foundItem = false;
- int itemCount = menu.size();
- String typeTitle = Settings.getCacheType().getL10n();
- for (int i = 0; i < itemCount; i++) {
- if (menu.getItem(i).getTitle().equals(typeTitle)) {
- menu.getItem(i).setChecked(true);
- foundItem = true;
- break;
- }
- }
- if (!foundItem) {
- menu.getItem(0).setChecked(true);
- }
- }
-
- @Override
- public boolean onContextItemSelected(final MenuItem item) {
- final int id = item.getItemId();
- if (id < 0) {
- return false;
- }
-
- if (id == 0) {
- Settings.setCacheType(CacheType.ALL);
- setFilterTitle();
- } else if (id > MENU_OPEN_LIST) {
- Settings.saveLastList(id - MENU_OPEN_LIST);
- cgeocaches.startActivityOffline(this);
- } else {
- final String itemTitle = item.getTitle().toString();
- CacheType cacheType = CacheType.ALL;
- for (final CacheType ct : CacheType.values()) {
- if (ct.getL10n().equalsIgnoreCase(itemTitle)) {
- cacheType = ct;
- break;
- }
- }
- Settings.setCacheType(cacheType);
- setFilterTitle();
- }
-
- return true;
- }
-
private void setFilterTitle() {
if (filterTitle == null) {
filterTitle = (TextView) findViewById(R.id.filter_button_title);
@@ -530,7 +375,22 @@ public class cgeo extends AbstractActivity {
cgeoFindByOffline(v);
}
});
- registerForContextMenu(findByOffline);
+ findByOffline.setOnLongClickListener(new View.OnLongClickListener() {
+
+ @Override
+ public boolean onLongClick(View v) {
+ new StoredList.UserInterface(cgeo.this).promptForListSelection(R.string.list_title, new RunnableWithArgument<Integer>() {
+
+ @Override
+ public void run(Integer selectedListId) {
+ Settings.saveLastList(selectedListId);
+ cgeocaches.startActivityOffline(cgeo.this);
+ }
+ });
+ return true;
+ }
+ });
+ findByOffline.setLongClickable(true);
final View advanced = findViewById(R.id.advanced_button);
advanced.setClickable(true);
@@ -552,11 +412,18 @@ public class cgeo extends AbstractActivity {
final View filter = findViewById(R.id.filter_button);
filter.setClickable(true);
- registerForContextMenu(filter);
filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- openContextMenu(v);
+ selectGlobalTypeFilter();
+ }
+ });
+ filter.setOnLongClickListener(new View.OnLongClickListener() {
+
+ @Override
+ public boolean onLongClick(View v) {
+ selectGlobalTypeFilter();
+ return true;
}
});
@@ -567,12 +434,62 @@ public class cgeo extends AbstractActivity {
(new cleanDatabase()).start();
}
- private void updateCacheCounter() {
+ protected void selectGlobalTypeFilter() {
+ final List<CacheType> cacheTypes = new ArrayList<CacheType>();
+
+ //first add the most used types
+ cacheTypes.add(CacheType.ALL);
+ cacheTypes.add(CacheType.TRADITIONAL);
+ cacheTypes.add(CacheType.MULTI);
+ cacheTypes.add(CacheType.MYSTERY);
+
+ // then add all other cache types sorted alphabetically
+ List<CacheType> sorted = new ArrayList<CacheType>();
+ sorted.addAll(Arrays.asList(CacheType.values()));
+ sorted.removeAll(cacheTypes);
+
+ Collections.sort(sorted, new Comparator<CacheType>() {
+
+ @Override
+ public int compare(CacheType left, CacheType right) {
+ return left.getL10n().compareToIgnoreCase(right.getL10n());
+ }
+ });
+
+ cacheTypes.addAll(sorted);
+
+ int checkedItem = cacheTypes.indexOf(Settings.getCacheType());
+ if (checkedItem < 0) {
+ checkedItem = 0;
+ }
+
+ String[] items = new String[cacheTypes.size()];
+ for (int i = 0; i < cacheTypes.size(); i++) {
+ items[i] = cacheTypes.get(i).getL10n();
+ }
+
+ Builder builder = new AlertDialog.Builder(this);
+ builder.setTitle(R.string.menu_filter);
+ builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
+
+ @Override
+ public void onClick(DialogInterface dialog, int position) {
+ CacheType cacheType = cacheTypes.get(position);
+ Settings.setCacheType(cacheType);
+ setFilterTitle();
+ dialog.dismiss();
+ }
+
+ });
+ builder.create().show();
+ }
+
+ void updateCacheCounter() {
(new CountBubbleUpdateThread()).start();
}
private void checkRestore() {
- if (!cgData.isNewlyCreatedDatebase() || null == cgData.isRestoreFile()) {
+ if (!cgData.isNewlyCreatedDatebase() || null == cgData.getRestoreFile()) {
return;
}
new AlertDialog.Builder(this)
@@ -585,7 +502,6 @@ public class cgeo extends AbstractActivity {
dialog.dismiss();
cgData.resetNewlyCreatedDatabase();
app.restoreDatabase(cgeo.this);
- updateCacheCounter();
}
})
.setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
@@ -747,7 +663,7 @@ public class cgeo extends AbstractActivity {
countBubble.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
- Log.w("cgeo.countBubbleHander: " + e.toString());
+ Log.w("cgeo.countBubbleHander", e);
}
}
};
@@ -759,7 +675,7 @@ public class cgeo extends AbstractActivity {
}
int checks = 0;
- while (!app.storageStatus()) {
+ while (!cgData.isInitialized()) {
try {
wait(500);
checks++;
@@ -772,7 +688,7 @@ public class cgeo extends AbstractActivity {
}
}
- countBubbleCnt = app.getAllStoredCachesCount(true, CacheType.ALL);
+ countBubbleCnt = cgData.getAllCachesCount();
countBubbleHandler.sendEmptyMessage(0);
}
@@ -797,7 +713,7 @@ public class cgeo extends AbstractActivity {
}
cleanupRunning = true;
- app.cleanDatabase(more);
+ cgData.clean(more);
cleanupRunning = false;
if (version > 0) {