aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--res/layout/create_user.xml77
-rw-r--r--res/values/strings.xml6
-rw-r--r--src/github/daneren2005/dsub/fragments/AdminFragment.java15
-rw-r--r--src/github/daneren2005/dsub/service/RESTMusicService.java17
-rw-r--r--src/github/daneren2005/dsub/util/UserUtil.java88
-rw-r--r--src/github/daneren2005/dsub/view/SettingsAdapter.java7
6 files changed, 209 insertions, 1 deletions
diff --git a/res/layout/create_user.xml b/res/layout/create_user.xml
new file mode 100644
index 00000000..4d918fc9
--- /dev/null
+++ b/res/layout/create_user.xml
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content">
+
+ <TextView
+ android:id="@+id/username_label"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="4dp"
+ android:textSize="20dp"
+ android:text="@string/admin.add_user_username" />
+ <EditText
+ android:id="@+id/username"
+ android:inputType="text"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:layout_marginLeft="4dp" />
+ </LinearLayout>
+
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content">
+
+ <TextView
+ android:id="@+id/email_label"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="4dp"
+ android:textSize="20dp"
+ android:text="@string/admin.add_user_email" />
+ <EditText
+ android:id="@+id/email"
+ android:inputType="textEmailAddress"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:layout_marginLeft="4dp" />
+ </LinearLayout>
+
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="horizontal"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content">
+
+ <TextView
+ android:id="@+id/password_label"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="4dp"
+ android:textSize="20dp"
+ android:text="@string/admin.add_user_password" />
+ <EditText
+ android:id="@+id/password"
+ android:inputType="textPassword"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:layout_marginLeft="4dp" />
+ </LinearLayout>
+
+ <ListView
+ android:id="@+id/settings_list"
+ android:layout_width="fill_parent"
+ android:layout_height="0dip"
+ android:layout_weight="1.0"
+ android:fastScrollEnabled="true"/>
+
+</LinearLayout> \ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d43d8c7b..4957587b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -447,6 +447,12 @@
<string name="share.via">Share via</string>
<string name="share.delete">Delete Share</string>
+ <string name="admin.add_user_username">Username:</string>
+ <string name="admin.add_user_email">Email:</string>
+ <string name="admin.add_user_password">Password:</string>
+ <string name="admin.create_user_success">Successfully created new user</string>
+ <string name="admin.create_user_error">Failed to create new user</string>
+ <string name="admin.change_username_invalid">Enter a valid username</string>
<string name="admin.update_permissions">Update Permissions</string>
<string name="admin.update_permissions_success">Successfully updated permission for %1$s</string>
<string name="admin.update_permissions_error">Failed to update permissions for %1$s</string>
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;