diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-06-10 20:17:37 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-06-10 20:17:37 -0700 |
commit | 2b079541a39f08be590cb2b37e2d401edcb4f13e (patch) | |
tree | 7b1e2b1cf90dd845a47d9d78f6906a2d9473878b /src/github/daneren2005 | |
parent | f064378307cf651c1f0cb82a36b81099b4db7401 (diff) | |
download | dsub-2b079541a39f08be590cb2b37e2d401edcb4f13e.tar.gz dsub-2b079541a39f08be590cb2b37e2d401edcb4f13e.tar.bz2 dsub-2b079541a39f08be590cb2b37e2d401edcb4f13e.zip |
#171 Add ability to change email, email/password to UserFragment
Diffstat (limited to 'src/github/daneren2005')
7 files changed, 237 insertions, 129 deletions
diff --git a/src/github/daneren2005/dsub/fragments/AdminFragment.java b/src/github/daneren2005/dsub/fragments/AdminFragment.java index b7f09532..e0989e43 100644 --- a/src/github/daneren2005/dsub/fragments/AdminFragment.java +++ b/src/github/daneren2005/dsub/fragments/AdminFragment.java @@ -53,7 +53,7 @@ public class AdminFragment extends SelectListFragment<User> { super.onCreateContextMenu(menu, view, menuInfo);
MenuInflater inflater = context.getMenuInflater();
- if(UserUtil.isCurrentAdmin(context) && Util.checkServerVersion(context, "1.10")) {
+ if(UserUtil.isCurrentAdmin()) {
inflater.inflate(R.menu.admin_context, menu);
} else {
inflater.inflate(R.menu.admin_context_user, menu);
@@ -66,11 +66,14 @@ public class AdminFragment extends SelectListFragment<User> { User user = objects.get(info.position);
switch(menuItem.getItemId()) {
+ case R.id.admin_change_email:
+ UserUtil.changeEmail(context, user);
+ break;
case R.id.admin_change_password:
- changePassword(user);
+ UserUtil.changePassword(context, user);
break;
case R.id.admin_delete_user:
- deleteUser(user);
+ UserUtil.deleteUser(context, user, adapter);
break;
}
@@ -79,7 +82,7 @@ public class AdminFragment extends SelectListFragment<User> { @Override
public int getOptionsMenu() {
- if(UserUtil.isCurrentAdmin(context)) {
+ if(UserUtil.isCurrentAdmin()) {
return R.menu.admin;
} else {
return R.menu.empty;
@@ -125,91 +128,4 @@ public class AdminFragment extends SelectListFragment<User> { replaceFragment(fragment);
}
-
- private void changePassword(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();
- }
-
- private void deleteUser(final User user) {
- 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) {
- 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();
- }
- });
- }
}
diff --git a/src/github/daneren2005/dsub/fragments/UserFragment.java b/src/github/daneren2005/dsub/fragments/UserFragment.java index f2af85fb..6423ebcd 100644 --- a/src/github/daneren2005/dsub/fragments/UserFragment.java +++ b/src/github/daneren2005/dsub/fragments/UserFragment.java @@ -15,6 +15,9 @@ package github.daneren2005.dsub.fragments;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
@@ -24,8 +27,10 @@ import android.view.MenuItem; import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
+import android.widget.TextView;
import github.daneren2005.dsub.R;
+import github.daneren2005.dsub.activity.SubsonicActivity;
import github.daneren2005.dsub.domain.User;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
@@ -39,12 +44,10 @@ import github.daneren2005.dsub.view.SettingsAdapter; public class UserFragment extends SubsonicFragment{
private ListView listView;
- private LayoutInflater inflater;
private User user;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
- this.inflater = inflater;
rootView = inflater.inflate(R.layout.abstract_list_fragment, container, false);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
@@ -54,7 +57,7 @@ public class UserFragment extends SubsonicFragment{ user = (User) args.getSerializable(Constants.INTENT_EXTRA_NAME_ID);
listView = (ListView)rootView.findViewById(R.id.fragment_list);
- listView.setAdapter(new SettingsAdapter(context, user.getSettings(), UserUtil.isCurrentAdmin(context)));
+ listView.setAdapter(new SettingsAdapter(context, user.getSettings(), UserUtil.isCurrentAdmin()));
setTitle(user.getUsername());
@@ -62,15 +65,22 @@ public class UserFragment extends SubsonicFragment{ }
@Override
+ public void onAttach(Activity activity) {
+ super.onAttach(activity);
+ activity.invalidateOptionsMenu();
+ }
+
+ @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
- if(!primaryFragment) {
+ // For some reason this is called before onAttach
+ if(!primaryFragment || context == null) {
return;
}
- if(UserUtil.isCurrentAdmin(context)) {
+ if(UserUtil.isCurrentAdmin() && Util.checkServerVersion(context, "1.10")) {
menuInflater.inflate(R.menu.user, menu);
} else {
- menuInflater.inflate(R.menu.empty, menu);
+ menuInflater.inflate(R.menu.user_user, menu);
}
}
@@ -82,38 +92,16 @@ public class UserFragment extends SubsonicFragment{ switch (item.getItemId()) {
case R.id.menu_update_permissions:
- updateSettings();
+ UserUtil.updateSettings(context, user);
+ return true;
+ case R.id.menu_change_password:
+ UserUtil.changePassword(context, user);
+ return true;
+ case R.id.menu_change_email:
+ UserUtil.changeEmail(context, user);
return true;
}
return false;
}
-
- private void updateSettings() {
- 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();
- }
}
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 86289511..0049e0ac 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -563,6 +563,15 @@ public class CachedMusicService implements MusicService { } @Override + public void changeEmail(String username, String email, Context context, ProgressListener progressListener) throws Exception { + musicService.changeEmail(username, email, context, progressListener); + + // Delete cached users if any have been removed from list + File file = new File(context.getCacheDir(), getCacheName(context, "users")); + file.delete(); + } + + @Override public void changePassword(String username, String password, Context context, ProgressListener progressListener) throws Exception { musicService.changePassword(username, password, context, progressListener); } diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 6f58603c..dc760f86 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -172,6 +172,8 @@ public interface MusicService { void deleteUser(String username, Context context, ProgressListener progressListener) throws Exception; + void changeEmail(String username, String email, Context context, ProgressListener progressListener) throws Exception; + void changePassword(String username, String password, Context context, ProgressListener progressListener) throws Exception; Bitmap getAvatar(String username, Context context, ProgressListener progressListener) throws Exception; diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index 9e2b9e13..75a090c6 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -716,6 +716,11 @@ public class OfflineMusicService extends RESTMusicService { } @Override + public void changeEmail(String username, String email, Context context, ProgressListener progressListener) throws Exception { + throw new OfflineException("Changing email not available in offline mode"); + } + + @Override public void changePassword(String username, String password, Context context, ProgressListener progressListener) throws Exception { throw new OfflineException("Changing passwords not available in offline mode"); } diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index c2f64dad..371e309c 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -1264,6 +1264,16 @@ public class RESTMusicService implements MusicService { } @Override + public void changeEmail(String username, String email, Context context, ProgressListener progressListener) throws Exception { + Reader reader = getReader(context, progressListener, "updateUser", null, Arrays.asList("username", "email"), Arrays.<Object>asList(username, email)); + try { + new ErrorParser(context).parse(reader); + } finally { + Util.close(reader); + } + } + + @Override public void changePassword(String username, String password, Context context, ProgressListener progressListener) throws Exception { Reader reader = getReader(context, progressListener, "changePassword", null, Arrays.asList("username", "password"), Arrays.<Object>asList(username, password)); try { diff --git a/src/github/daneren2005/dsub/util/UserUtil.java b/src/github/daneren2005/dsub/util/UserUtil.java index 5158829c..60243731 100644 --- a/src/github/daneren2005/dsub/util/UserUtil.java +++ b/src/github/daneren2005/dsub/util/UserUtil.java @@ -15,14 +15,24 @@ 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.view.View;
+import android.widget.Adapter;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
import java.io.File;
import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.User;
+import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
+import github.daneren2005.dsub.service.OfflineException;
+import github.daneren2005.dsub.service.ServerTooOldException;
public final class UserUtil {
private static User currentUser;
@@ -55,15 +65,15 @@ public final class UserUtil { return getCurrentUsername(context, Util.getActiveServer(context));
}
- public static boolean isCurrentAdmin(Context context) {
+ public static boolean isCurrentAdmin() {
if(currentUser == null) {
return false;
} else {
- return isCurrentRole(context, "adminRole");
+ return isCurrentRole("adminRole");
}
}
- public static boolean isCurrentRole(Context context, String role) {
+ public static boolean isCurrentRole(String role) {
if(currentUser == null) {
return false;
}
@@ -76,4 +86,172 @@ public final class UserUtil { 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();
+ }
+ });
+ }
}
|