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
|
package cgeo.geocaching.ui;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.settings.Settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;
public class LoggingUI extends AbstractUIFactory {
public static class LogTypeEntry {
private final LogType logType;
private final SpecialLogType specialLogType;
private final boolean isActive;
public LogTypeEntry(final LogType logType, final SpecialLogType specialLogType, final boolean isActive) {
this.logType = logType;
this.specialLogType = specialLogType;
this.isActive = isActive;
}
@Override
public String toString() {
if (logType == null) {
return specialLogType.getL10n();
}
String text = logType.getL10n();
if (isActive) {
text += " ✔";
}
return text;
}
}
private enum SpecialLogType {
LOG_CACHE(R.string.cache_menu_visit),
CLEAR_LOG(R.string.log_clear);
private final int stringId;
SpecialLogType(final int stringId) {
this.stringId = stringId;
}
public final String getL10n() {
return res.getString(stringId);
}
}
public static boolean onMenuItemSelected(final MenuItem item, final Activity activity, final Geocache cache) {
switch (item.getItemId()) {
case R.id.menu_log_visit:
cache.logVisit(activity);
return true;
case R.id.menu_log_visit_offline:
showOfflineMenu(cache, activity);
return true;
default:
return false;
}
}
private static void showOfflineMenu(final Geocache cache, final Activity activity) {
final LogEntry currentLog = DataStore.loadLogOffline(cache.getGeocode());
final LogType currentLogType = currentLog == null ? null : currentLog.type;
final List<LogType> logTypes = cache.getPossibleLogTypes();
final ArrayList<LogTypeEntry> list = new ArrayList<>();
for (LogType logType : logTypes) {
list.add(new LogTypeEntry(logType, null, logType == currentLogType));
}
if (cache.isLogOffline()) {
list.add(new LogTypeEntry(null, SpecialLogType.CLEAR_LOG, false));
}
list.add(new LogTypeEntry(null, SpecialLogType.LOG_CACHE, false));
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.cache_menu_visit_offline);
final ArrayAdapter<LogTypeEntry> adapter = new ArrayAdapter<>(activity, android.R.layout.select_dialog_item, list);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
final LogTypeEntry logTypeEntry = adapter.getItem(item);
if (logTypeEntry.logType == null) {
switch (logTypeEntry.specialLogType) {
case LOG_CACHE:
cache.logVisit(activity);
break;
case CLEAR_LOG:
cache.clearOfflineLog();
break;
}
} else {
cache.logOffline(activity, logTypeEntry.logType);
}
}
});
builder.create().show();
}
public static void onPrepareOptionsMenu(Menu menu, Geocache cache) {
if (cache == null) {
return;
}
final MenuItem itemLog = menu.findItem(R.id.menu_log_visit);
itemLog.setVisible(cache.supportsLogging() && !Settings.getLogOffline());
final MenuItem itemOffline = menu.findItem(R.id.menu_log_visit_offline);
itemOffline.setVisible(cache.supportsLogging() && Settings.getLogOffline());
}
public static void addMenuItems(Activity activity, Menu menu, Geocache cache) {
activity.getMenuInflater().inflate(R.menu.logging_ui, menu);
onPrepareOptionsMenu(menu, cache);
}
}
|