aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/activity/ActivityMixin.java
blob: 9a50662f0a03a211d0cdc0894d2c38348fa6b48d (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
package cgeo.geocaching.activity;

import gnu.android.app.appmanualclient.AppManualReaderClient;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.Menu;
import android.view.SubMenu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cgeo.geocaching.R;
import cgeo.geocaching.cgBase;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgSettings;
import cgeo.geocaching.cgeo;

public final class ActivityMixin {
	private static final int MENU_ICON_LOG_VISIT = android.R.drawable.ic_menu_agenda;

	public final static void goHome(final Activity fromActivity) {
		final Intent intent = new Intent(fromActivity, cgeo.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		fromActivity.startActivity(intent);
		fromActivity.finish();
	}

	public final static void goManual(final Context context, final String helpTopic) {
		if (helpTopic == null || helpTopic.length() == 0) {
			return;
		}
		try {
			AppManualReaderClient.openManual(
					"c-geo",
					helpTopic,
					context,
					"http://manual.cgeo.org/");
		} catch (Exception e) {
			// nothing
		}
	}

	public final static void setTitle(final Activity activity, final String text) {
		if (text == null) {
			return;
		}

		final TextView title = (TextView) activity.findViewById(R.id.actionbar_title);
		if (title != null) {
			title.setText(text);
		}
	}

	public final static void showProgress(final Activity activity, final boolean show) {
		if (activity == null) {
			return;
		}

		final ProgressBar progress = (ProgressBar) activity.findViewById(R.id.actionbar_progress);
		if (show) {
			progress.setVisibility(View.VISIBLE);
		} else {
			progress.setVisibility(View.GONE);
		}
	}

	public final static void setTheme(final Activity activity) {
		cgSettings settings = new cgSettings(activity, activity.getSharedPreferences(cgSettings.preferences, Context.MODE_PRIVATE));
		if (settings.skin == 1) {
			activity.setTheme(R.style.light);
		} else {
			activity.setTheme(R.style.dark);
		}
	}

	public final static void showToast(final Activity activity, final String text) {
		if (text.length() > 0) {
			Toast toast = Toast.makeText(activity, text, Toast.LENGTH_LONG);

			toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 100);
			toast.show();
		}
	}

	public final static void showShortToast(final Activity activity, final String text) {
		if (text.length() > 0) {
			Toast toast = Toast.makeText(activity, text, Toast.LENGTH_SHORT);

			toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 100);
			toast.show();
		}
	}

	public static final void helpDialog(final Activity activity, final String title, final String message) {
		if (message == null || message.length() == 0) {
			return;
		}

		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		dialog.setTitle(title);
		dialog.setMessage(message);
		dialog.setCancelable(true);
		dialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });

	   AlertDialog alert = dialog.create();
	   alert.show();
	}

	protected static void addVisitMenu(IAbstractActivity activity, Menu menu, cgCache cache) {
		if (cache == null) {
			return;
		}
		if (!cache.supportsLogging()) {
			return;
		}
		cgSettings settings = activity.getSettings();
		Resources res = ((Activity)activity).getResources();
		if (settings.isLogin()) {
			if (settings.getLogOffline()) {
				SubMenu logMenu = menu.addSubMenu(1, IAbstractActivity.MENU_LOG_VISIT_OFFLINE, 0, res.getString(R.string.cache_menu_visit_offline)).setIcon(MENU_ICON_LOG_VISIT);
				ArrayList<Integer> logTypes = cache.getPossibleLogTypes(settings);
				for (Integer logType : logTypes) {
					String label = cgBase.logTypes2.get(logType);
					logMenu.add(1, IAbstractActivity.MENU_LOG_VISIT_OFFLINE + logType, 0, label);
				}
				logMenu.add(1, IAbstractActivity.MENU_LOG_VISIT, 0, res.getString(R.string.cache_menu_visit));
			}
			else {
				menu.add(1, IAbstractActivity.MENU_LOG_VISIT, 0, res.getString(R.string.cache_menu_visit)).setIcon(MENU_ICON_LOG_VISIT);
			}
		}
	}

}