aboutsummaryrefslogtreecommitdiff
path: root/src/github/daneren2005/dsub/util/UserUtil.java
blob: ccda1070b6e42d3d9f07886131c1c2c52a5442cb (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
  This file is part of Subsonic.
	Subsonic is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	Subsonic is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
	Copyright 2014 (C) Scott Jackson
*/

package github.daneren2005.dsub.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.User;
import github.daneren2005.dsub.fragments.SubsonicFragment;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
import github.daneren2005.dsub.service.OfflineException;
import github.daneren2005.dsub.service.ServerTooOldException;
import github.daneren2005.dsub.view.SettingsAdapter;

public final class UserUtil {
	private static final String TAG = UserUtil.class.getSimpleName();
	private static int instance = -1;
	private static User currentUser;

	public static void seedCurrentUser(final Context context) {
		// Only try to seed if online
		if(Util.isOffline(context)) {
			return;
		}
		
		final int instance = Util.getActiveServer(context);
		if(this.instance == instance && currentUser != null) {
			return;
		} else {
			this.instance = instance;
		}

		new SilentBackgroundTask<Void>(context) {
			@Override
			protected Void doInBackground() throws Throwable {
				currentUser = MusicServiceFactory.getMusicService(context).getUser(false, getCurrentUsername(context, instance), context, null);
				return null;
			}

			@Override
			protected void error(Throwable error) {
				// Don't do anything, supposed to be background pull
				Log.e(TAG, "Failed to seed user information");
			}
		}.execute();
	}

	public static User getCurrentUser() {
		return currentUser;
	}

	public static String getCurrentUsername(Context context, int instance) {
		SharedPreferences prefs = Util.getPreferences(context);
		return prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null);
	}

	public static String getCurrentUsername(Context context) {
		return getCurrentUsername(context, Util.getActiveServer(context));
	}

	public static boolean isCurrentAdmin() {
		return isCurrentRole("adminRole");
	}
	
	public static boolean canPodcast() {
		return isCurrentRole("podcastRole");
	}
	public static boolean canShare() {
		return isCurrentRole("shareRole");
	}
	public static boolean canJukebox() {
		return isCurrentRole("jukeboxRole");
	}

	public static boolean isCurrentRole(String role) {
		if(currentUser == null) {
			return false;
		}

		for(User.Setting setting: currentUser.getSettings()) {
			if(setting.getName().equals(role)) {
				return setting.getValue() == true;
			}
		}

		return false;
	}

	public static void changePassword(final Activity context, final User user) {
		View layout = context.getLayoutInflater().inflate(R.layout.change_password, null);
		final TextView passwordView = (TextView) layout.findViewById(R.id.new_password);

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.admin_change_password)
				.setView(layout)
				.setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						final String password = passwordView.getText().toString();
						// Don't allow blank passwords
						if ("".equals(password)) {
							Util.toast(context, R.string.admin_change_password_invalid);
							return;
						}

						new SilentBackgroundTask<Void>(context) {
							@Override
							protected Void doInBackground() throws Throwable {
								MusicService musicService = MusicServiceFactory.getMusicService(context);
								musicService.changePassword(user.getUsername(), password, context, null);
								return null;
							}

							@Override
							protected void done(Void v) {
								Util.toast(context, context.getResources().getString(R.string.admin_change_password_success, user.getUsername()));
							}

							@Override
							protected void error(Throwable error) {
								String msg;
								if (error instanceof OfflineException || error instanceof ServerTooOldException) {
									msg = getErrorMessage(error);
								} else {
									msg = context.getResources().getString(R.string.admin_change_password_error, user.getUsername());
								}

								Util.toast(context, msg);
							}
						}.execute();
					}
				})
				.setNegativeButton(R.string.common_cancel, null)
				.setCancelable(true);

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

	public static void updateSettings(final Context context, final User user) {
		new SilentBackgroundTask<Void>(context) {
			@Override
			protected Void doInBackground() throws Throwable {
				MusicService musicService = MusicServiceFactory.getMusicService(context);
				musicService.updateUser(user, context, null);
				return null;
			}

			@Override
			protected void done(Void v) {
				Util.toast(context, context.getResources().getString(R.string.admin_update_permissions_success, user.getUsername()));
			}

			@Override
			protected void error(Throwable error) {
				String msg;
				if (error instanceof OfflineException || error instanceof ServerTooOldException) {
					msg = getErrorMessage(error);
				} else {
					msg = context.getResources().getString(R.string.admin_update_permissions_error, user.getUsername());
				}

				Util.toast(context, msg);
			}
		}.execute();
	}

	public static void changeEmail(final Activity context, final User user) {
		View layout = context.getLayoutInflater().inflate(R.layout.change_email, null);
		final TextView emailView = (TextView) layout.findViewById(R.id.new_email);

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.admin_change_email)
				.setView(layout)
				.setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						final String email = emailView.getText().toString();
						// Don't allow blank emails
						if ("".equals(email)) {
							Util.toast(context, R.string.admin_change_email_invalid);
							return;
						}

						new SilentBackgroundTask<Void>(context) {
							@Override
							protected Void doInBackground() throws Throwable {
								MusicService musicService = MusicServiceFactory.getMusicService(context);
								musicService.changeEmail(user.getUsername(), email, context, null);
								return null;
							}

							@Override
							protected void done(Void v) {
								Util.toast(context, context.getResources().getString(R.string.admin_change_email_success, user.getUsername()));
							}

							@Override
							protected void error(Throwable error) {
								String msg;
								if (error instanceof OfflineException || error instanceof ServerTooOldException) {
									msg = getErrorMessage(error);
								} else {
									msg = context.getResources().getString(R.string.admin_change_email_error, user.getUsername());
								}

								Util.toast(context, msg);
							}
						}.execute();
					}
				})
				.setNegativeButton(R.string.common_cancel, null)
				.setCancelable(true);

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

	public static void deleteUser(final Context context, final User user, final ArrayAdapter adapter) {
		Util.confirmDialog(context, R.string.common_delete, user.getUsername(), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				new SilentBackgroundTask<Void>(context) {
					@Override
					protected Void doInBackground() throws Throwable {
						MusicService musicService = MusicServiceFactory.getMusicService(context);
						musicService.deleteUser(user.getUsername(), context, null);
						return null;
					}

					@Override
					protected void done(Void v) {
						if(adapter != null) {
							adapter.remove(user);
							adapter.notifyDataSetChanged();
						}

						Util.toast(context, context.getResources().getString(R.string.admin_delete_user_success, user.getUsername()));
					}

					@Override
					protected void error(Throwable error) {
						String msg;
						if (error instanceof OfflineException || error instanceof ServerTooOldException) {
							msg = getErrorMessage(error);
						} else {
							msg = context.getResources().getString(R.string.admin_delete_user_error, user.getUsername());
						}

						Util.toast(context, msg);
					}
				}.execute();
			}
		});
	}

	public static void addNewUser(final Activity context, final SubsonicFragment fragment) {
		final User user = new User();
		for(String role: User.ROLES) {
			if(role.equals(User.SETTINGS) || role.equals(User.STREAM)) {
				user.addSetting(role, true);
			} else {
				user.addSetting(role, false);
			}
		}

		View layout = context.getLayoutInflater().inflate(R.layout.create_user, null);
		final TextView usernameView = (TextView) layout.findViewById(R.id.username);
		final TextView emailView = (TextView) layout.findViewById(R.id.email);
		final TextView passwordView = (TextView) layout.findViewById(R.id.password);
		final ListView listView = (ListView) layout.findViewById(R.id.settings_list);
		listView.setAdapter(new SettingsAdapter(context, user, true));

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.menu_add_user)
				.setView(layout)
				.setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						final String username = usernameView.getText().toString();
						// Don't allow blank emails
						if ("".equals(username)) {
							Util.toast(context, R.string.admin_change_username_invalid);
							return;
						}

						final String email = emailView.getText().toString();
						// Don't allow blank emails
						if ("".equals(email)) {
							Util.toast(context, R.string.admin_change_email_invalid);
							return;
						}

						final String password = passwordView.getText().toString();
						if ("".equals(password)) {
							Util.toast(context, R.string.admin_change_password_invalid);
							return;
						}

						user.setUsername(username);
						user.setEmail(email);
						user.setPassword(password);

						new SilentBackgroundTask<Void>(context) {
							@Override
							protected Void doInBackground() throws Throwable {
								MusicService musicService = MusicServiceFactory.getMusicService(context);
								musicService.createUser(user, context, null);
								return null;
							}

							@Override
							protected void done(Void v) {
								fragment.onRefresh();
								Util.toast(context, context.getResources().getString(R.string.admin_create_user_success));
							}

							@Override
							protected void error(Throwable error) {
								String msg;
								if (error instanceof OfflineException || error instanceof ServerTooOldException) {
									msg = getErrorMessage(error);
								} else {
									msg = context.getResources().getString(R.string.admin_create_user_error);
								}

								Util.toast(context, msg);
							}
						}.execute();
					}
				})
				.setNegativeButton(R.string.common_cancel, null)
				.setCancelable(true);

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