diff options
Diffstat (limited to 'src/github')
4 files changed, 126 insertions, 1 deletions
diff --git a/src/github/daneren2005/dsub/fragments/AdminFragment.java b/src/github/daneren2005/dsub/fragments/AdminFragment.java index 216ac000..19aa4b95 100644 --- a/src/github/daneren2005/dsub/fragments/AdminFragment.java +++ b/src/github/daneren2005/dsub/fragments/AdminFragment.java @@ -49,6 +49,21 @@ public class AdminFragment extends SelectListFragment<User> { private static String TAG = AdminFragment.class.getSimpleName();
@Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if(super.onOptionsItemSelected(item)) {
+ return true;
+ }
+
+ switch (item.getItemId()) {
+ case R.id.menu_add_user:
+ UserUtil.addNewUser(context, this);
+ break;
+ }
+
+ return false;
+ }
+
+ @Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 11431eaa..3299ec4b 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -1220,7 +1220,22 @@ public class RESTMusicService implements MusicService { @Override public void createUser(User user, Context context, ProgressListener progressListener) throws Exception { - Reader reader = getReader(context, progressListener, "createUser", null); + List<String> names = new ArrayList<String>(); + List<Object> values = new ArrayList<Object>(); + + names.add("username"); + values.add(user.getUsername()); + names.add("email"); + values.add(user.getEmail()); + names.add("password"); + values.add(user.getPassword()); + + for(User.Setting setting: user.getSettings()) { + names.add(setting.getName()); + values.add(setting.getValue()); + } + + Reader reader = getReader(context, progressListener, "createUser", null, names, values); try { new ErrorParser(context).parse(reader); } finally { diff --git a/src/github/daneren2005/dsub/util/UserUtil.java b/src/github/daneren2005/dsub/util/UserUtil.java index 60243731..3e6d9ce7 100644 --- a/src/github/daneren2005/dsub/util/UserUtil.java +++ b/src/github/daneren2005/dsub/util/UserUtil.java @@ -23,16 +23,19 @@ import android.content.SharedPreferences; 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 User currentUser;
@@ -254,4 +257,89 @@ public final class UserUtil { }
});
}
+
+ public static void addNewUser(final Activity context, final SubsonicFragment fragment) {
+ final User user = new User();
+ user.addSetting("adminRole", false);
+ user.addSetting("settingsRole", true);
+ user.addSetting("downloadRole", false);
+ user.addSetting("uploadRole", false);
+ user.addSetting("coverArtRole", false);
+ user.addSetting("commentRole", false);
+ user.addSetting("podcastRole", false);
+ user.addSetting("streamRole", true);
+ user.addSetting("jukeboxRole", false);
+ user.addSetting("shareRole", 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();
+ }
}
diff --git a/src/github/daneren2005/dsub/view/SettingsAdapter.java b/src/github/daneren2005/dsub/view/SettingsAdapter.java index 88019c00..ce6c12ed 100644 --- a/src/github/daneren2005/dsub/view/SettingsAdapter.java +++ b/src/github/daneren2005/dsub/view/SettingsAdapter.java @@ -23,6 +23,7 @@ import android.widget.ArrayAdapter; import java.util.List;
import github.daneren2005.dsub.R;
+import github.daneren2005.dsub.domain.User;
import static github.daneren2005.dsub.domain.User.Setting;
@@ -30,6 +31,12 @@ public class SettingsAdapter extends ArrayAdapter<Setting> { private final Context context;
private final boolean editable;
+ public SettingsAdapter(Context context, User user, boolean editable) {
+ super(context, R.layout.basic_list_item, user.getSettings());
+ this.context = context;
+ this.editable = editable;
+ }
+
public SettingsAdapter(Context context, List<Setting> settings, boolean editable) {
super(context, R.layout.basic_list_item, settings);
this.context = context;
|