diff options
11 files changed, 234 insertions, 46 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/SettingsAdapter.java b/app/src/main/java/github/daneren2005/dsub/adapter/SettingsAdapter.java index 1cb9c34e..ba4b11fc 100644 --- a/app/src/main/java/github/daneren2005/dsub/adapter/SettingsAdapter.java +++ b/app/src/main/java/github/daneren2005/dsub/adapter/SettingsAdapter.java @@ -23,11 +23,16 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.domain.User; import github.daneren2005.dsub.util.ImageLoader; +import github.daneren2005.dsub.util.UserUtil; +import github.daneren2005.dsub.view.BasicHeaderView; import github.daneren2005.dsub.view.RecyclingImageView; import github.daneren2005.dsub.view.SettingView; import github.daneren2005.dsub.view.UpdateView; @@ -37,26 +42,42 @@ import static github.daneren2005.dsub.domain.User.Setting; public class SettingsAdapter extends SectionAdapter<Setting> { private static final String TAG = SettingsAdapter.class.getSimpleName(); public final int VIEW_TYPE_SETTING = 1; + public final int VIEW_TYPE_SETTING_HEADER = 2; private final User user; private final boolean editable; private final ImageLoader imageLoader; - public SettingsAdapter(Context context, User user, ImageLoader imageLoader, boolean editable, OnItemClickedListener<Setting> onItemClickedListener) { - super(context, user.getSettings(), imageLoader != null); + public SettingsAdapter(Context context, User user, List<String> headers, List<List<User.Setting>> settingSections, ImageLoader imageLoader, boolean editable, OnItemClickedListener<Setting> onItemClickedListener) { + super(context, headers, settingSections, imageLoader != null); this.user = user; this.imageLoader = imageLoader; this.editable = editable; this.onItemClickedListener = onItemClickedListener; - List<Setting> settings = sections.get(0); - for(Setting setting: settings) { - if(setting.getValue()) { - addSelected(setting); + for(List<Setting> settings: sections) { + for (Setting setting : settings) { + if (setting.getValue()) { + addSelected(setting); + } } } } + @Override + public int getItemViewType(int position) { + int viewType = super.getItemViewType(position); + if(viewType == SectionAdapter.VIEW_TYPE_HEADER) { + if(position == 0 && imageLoader != null) { + return VIEW_TYPE_HEADER; + } else { + return VIEW_TYPE_SETTING_HEADER; + } + } else { + return viewType; + } + } + public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) { View header = LayoutInflater.from(context).inflate(R.layout.user_header, parent, false); return new UpdateView.UpdateViewHolder(header, false); @@ -65,28 +86,37 @@ public class SettingsAdapter extends SectionAdapter<Setting> { View header = holder.getView(); RecyclingImageView coverArtView = (RecyclingImageView) header.findViewById(R.id.user_avatar); - imageLoader.loadAvatar(context, coverArtView, user.getUsername()); - coverArtView.setOnInvalidated(new RecyclingImageView.OnInvalidated() { - @Override - public void onInvalidated(RecyclingImageView imageView) { - imageLoader.loadAvatar(context, imageView, user.getUsername()); + if(coverArtView != null) { + imageLoader.loadAvatar(context, coverArtView, user.getUsername()); + coverArtView.setOnInvalidated(new RecyclingImageView.OnInvalidated() { + @Override + public void onInvalidated(RecyclingImageView imageView) { + imageLoader.loadAvatar(context, imageView, user.getUsername()); + } + }); + + TextView usernameView = (TextView) header.findViewById(R.id.user_username); + usernameView.setText(user.getUsername()); + + final TextView emailView = (TextView) header.findViewById(R.id.user_email); + if (user.getEmail() != null) { + emailView.setText(user.getEmail()); + } else { + emailView.setVisibility(View.GONE); } - }); - - TextView usernameView = (TextView) header.findViewById(R.id.user_username); - usernameView.setText(user.getUsername()); - - final TextView emailView = (TextView) header.findViewById(R.id.user_email); - if(user.getEmail() != null) { - emailView.setText(user.getEmail()); } else { - emailView.setVisibility(View.GONE); + TextView nameView = (TextView) header.findViewById(R.id.item_name); + nameView.setText(description); } } @Override public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) { - return new UpdateView.UpdateViewHolder(new SettingView(context)); + if(viewType == VIEW_TYPE_SETTING_HEADER) { + return new UpdateView.UpdateViewHolder(new BasicHeaderView(context)); + } else { + return new UpdateView.UpdateViewHolder(new SettingView(context)); + } } @Override @@ -105,4 +135,21 @@ public class SettingsAdapter extends SectionAdapter<Setting> { updateView.setChecked(checked); } } + + public static SettingsAdapter getSettingsAdapter(Context context, User user, ImageLoader imageLoader, OnItemClickedListener<Setting> onItemClickedListener) { + return getSettingsAdapter(context, user, imageLoader, UserUtil.isCurrentAdmin() && ServerInfo.checkServerVersion(context, "1.10"), onItemClickedListener); + } + public static SettingsAdapter getSettingsAdapter(Context context, User user, ImageLoader imageLoader, boolean isEditable, OnItemClickedListener<Setting> onItemClickedListener) { + List<String> headers = new ArrayList<>(); + List<List<User.Setting>> settingsSections = new ArrayList<>(); + settingsSections.add(user.getSettings()); + + if(user.getMusicFolderSettings() != null) { + headers.add(context.getResources().getString(R.string.admin_permissions)); + headers.add(context.getResources().getString(R.string.admin_musicFolders)); + settingsSections.add(user.getMusicFolderSettings()); + } + + return new SettingsAdapter(context, user, headers, settingsSections, imageLoader, isEditable, onItemClickedListener); + } } diff --git a/app/src/main/java/github/daneren2005/dsub/domain/MusicFolder.java b/app/src/main/java/github/daneren2005/dsub/domain/MusicFolder.java index 99e86e23..37f76249 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/MusicFolder.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/MusicFolder.java @@ -18,7 +18,12 @@ */ package github.daneren2005.dsub.domain; +import android.util.Log; + import java.io.Serializable; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; /** * Represents a top level directory in which music or other media is stored. @@ -27,23 +32,49 @@ import java.io.Serializable; * @version $Id$ */ public class MusicFolder implements Serializable { - - private String id; - private String name; + private static final String TAG = MusicFolder.class.getSimpleName(); + private String id; + private String name; + private boolean enabled; public MusicFolder() { } - public MusicFolder(String id, String name) { - this.id = id; - this.name = name; - } + public MusicFolder(String id, String name) { + this.id = id; + this.name = name; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + public boolean getEnabled() { + return enabled; + } - public String getId() { - return id; - } + public static class MusicFolderComparator implements Comparator<MusicFolder> { + public int compare(MusicFolder lhsMusicFolder, MusicFolder rhsMusicFolder) { + if(lhsMusicFolder == rhsMusicFolder || lhsMusicFolder.getName().equals(rhsMusicFolder.getName())) { + return 0; + } else { + return lhsMusicFolder.getName().compareToIgnoreCase(rhsMusicFolder.getName()); + } + } + } - public String getName() { - return name; - } + public static void sort(List<MusicFolder> musicFolders) { + try { + Collections.sort(musicFolders, new MusicFolderComparator()); + } catch (Exception e) { + Log.w(TAG, "Failed to sort music folders", e); + } + } } diff --git a/app/src/main/java/github/daneren2005/dsub/domain/User.java b/app/src/main/java/github/daneren2005/dsub/domain/User.java index 797a1271..b5ee6188 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/User.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/User.java @@ -15,6 +15,8 @@ package github.daneren2005.dsub.domain; +import android.util.Pair; + import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -52,6 +54,7 @@ public class User implements Serializable { private String email; private List<Setting> settings = new ArrayList<Setting>(); + private List<Setting> musicFolders; public User() { @@ -92,9 +95,27 @@ public class User implements Serializable { settings.add(new Setting(name, value)); } + public void addMusicFolder(MusicFolder musicFolder) { + if(musicFolders == null) { + musicFolders = new ArrayList<>(); + } + + musicFolders.add(new MusicFolderSetting(musicFolder.getId(), musicFolder.getName(), false)); + } + public void addMusicFolder(MusicFolderSetting musicFolderSetting, boolean defaultValue) { + if(musicFolders == null) { + musicFolders = new ArrayList<>(); + } + + musicFolders.add(new MusicFolderSetting(musicFolderSetting.getName(), musicFolderSetting.getLabel(), defaultValue)); + } + public List<Setting> getMusicFolderSettings() { + return musicFolders; + } + public static class Setting implements Serializable { - String name; - Boolean value; + private String name; + private Boolean value; public Setting() { @@ -114,4 +135,20 @@ public class User implements Serializable { this.value = value; } } + + public static class MusicFolderSetting extends Setting { + private String label; + + public MusicFolderSetting() { + + } + public MusicFolderSetting(String name, String label, Boolean value) { + super(name, value); + this.label = label; + } + + public String getLabel() { + return label; + } + } } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/AdminFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/AdminFragment.java index 630acf2c..187f0d55 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/AdminFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/AdminFragment.java @@ -47,7 +47,7 @@ public class AdminFragment extends SelectRecyclerFragment<User> { switch (item.getItemId()) { case R.id.menu_add_user: - UserUtil.addNewUser(context, this); + UserUtil.addNewUser(context, this, objects.get(0)); break; } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/UserFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/UserFragment.java index dab104bd..dde76624 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/UserFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/UserFragment.java @@ -20,6 +20,8 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import github.daneren2005.dsub.R; @@ -78,7 +80,7 @@ public class UserFragment extends SelectRecyclerFragment<User.Setting>{ @Override public SectionAdapter<User.Setting> getAdapter(List<User.Setting> objs) { - return new SettingsAdapter(context, user, getImageLoader(), UserUtil.isCurrentAdmin() && ServerInfo.checkServerVersion(context, "1.10"), this); + return SettingsAdapter.getSettingsAdapter(context, user, getImageLoader(), this); } @Override diff --git a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java index 52268d82..3be04cff 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -130,6 +132,8 @@ public class CachedMusicService implements MusicService { result = musicService.getMusicFolders(refresh, context, progressListener); FileUtil.serialize(context, new ArrayList<MusicFolder>(result), getCacheName(context, "musicFolders")); } + + MusicFolder.sort(result); cachedMusicFolders.set(result); } return result; diff --git a/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java index d258ec15..75023ec0 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java @@ -1500,6 +1500,15 @@ public class RESTMusicService implements MusicService { values.add(setting.getValue()); } + if(user.getMusicFolderSettings() != null) { + for(User.Setting setting: user.getMusicFolderSettings()) { + if(setting.getValue()) { + names.add("musicFolderId"); + values.add(setting.getName()); + } + } + } + Reader reader = getReader(context, progressListener, "createUser", null, names, values); try { new ErrorParser(context, getInstance(context)).parse(reader); @@ -1525,6 +1534,15 @@ public class RESTMusicService implements MusicService { } } + if(user.getMusicFolderSettings() != null) { + for(User.Setting setting: user.getMusicFolderSettings()) { + if(setting.getValue()) { + names.add("musicFolderId"); + values.add(setting.getName()); + } + } + } + Reader reader = getReader(context, progressListener, "updateUser", null, names, values); try { new ErrorParser(context, getInstance(context)).parse(reader); diff --git a/app/src/main/java/github/daneren2005/dsub/service/parser/UserParser.java b/app/src/main/java/github/daneren2005/dsub/service/parser/UserParser.java index e20556c0..fc2ddd7e 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/parser/UserParser.java +++ b/app/src/main/java/github/daneren2005/dsub/service/parser/UserParser.java @@ -16,6 +16,7 @@ package github.daneren2005.dsub.service.parser; import android.content.Context; +import android.util.Log; import org.xmlpull.v1.XmlPullParser; @@ -23,10 +24,16 @@ import java.io.Reader; import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.domain.MusicFolder; import github.daneren2005.dsub.domain.User; +import github.daneren2005.dsub.domain.User.MusicFolderSetting; +import github.daneren2005.dsub.domain.User.Setting; +import github.daneren2005.dsub.service.MusicService; +import github.daneren2005.dsub.service.MusicServiceFactory; import github.daneren2005.dsub.util.ProgressListener; public class UserParser extends AbstractParser { + private static final String TAG = UserParser.class.getSimpleName(); public UserParser(Context context, int instance) { super(context, instance); @@ -35,14 +42,17 @@ public class UserParser extends AbstractParser { public List<User> parse(Reader reader, ProgressListener progressListener) throws Exception { init(reader); List<User> result = new ArrayList<User>(); + List<MusicFolder> musicFolders = null; + User user = null; int eventType; + String tagName = null; do { eventType = nextParseEvent(); if (eventType == XmlPullParser.START_TAG) { - String name = getElementName(); - if ("user".equals(name)) { - User user = new User(); + tagName = getElementName(); + if ("user".equals(tagName)) { + user = new User(); user.setUsername(get("username")); user.setEmail(get("email")); @@ -53,9 +63,31 @@ public class UserParser extends AbstractParser { parseSetting(user, User.LASTFM); result.add(user); - } else if ("error".equals(name)) { + } else if ("error".equals(tagName)) { handleError(); } + } else if(eventType == XmlPullParser.TEXT) { + if("folder".equals(tagName)) { + String id = getText(); + if(musicFolders == null) { + musicFolders = getMusicFolders(); + } + + if(user != null) { + if(user.getMusicFolderSettings() == null) { + for (MusicFolder musicFolder : musicFolders) { + user.addMusicFolder(musicFolder); + } + } + + for(Setting musicFolder: user.getMusicFolderSettings()) { + if(musicFolder.getName().equals(id)) { + musicFolder.setValue(true); + break; + } + } + } + } } } while (eventType != XmlPullParser.END_DOCUMENT); @@ -63,6 +95,11 @@ public class UserParser extends AbstractParser { return result; } + + private List<MusicFolder> getMusicFolders() throws Exception{ + MusicService musicService = MusicServiceFactory.getMusicService(context); + return musicService.getMusicFolders(false, context, null); + } private void parseSetting(User user, String name) { String value = get(name); diff --git a/app/src/main/java/github/daneren2005/dsub/util/UserUtil.java b/app/src/main/java/github/daneren2005/dsub/util/UserUtil.java index 6417dc81..3f16de21 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/UserUtil.java +++ b/app/src/main/java/github/daneren2005/dsub/util/UserUtil.java @@ -373,7 +373,7 @@ public final class UserUtil { }); } - public static void addNewUser(final Activity context, final SubsonicFragment fragment) { + public static void addNewUser(final Activity context, final SubsonicFragment fragment, User sampleUser) { final User user = new User(); for(String role: User.ROLES) { if(role.equals(User.SETTINGS) || role.equals(User.STREAM)) { @@ -383,6 +383,13 @@ public final class UserUtil { } } + if(sampleUser.getMusicFolderSettings() != null) { + for(User.Setting setting: sampleUser.getMusicFolderSettings()) { + User.MusicFolderSetting musicFolderSetting = (User.MusicFolderSetting) setting; + user.addMusicFolder(musicFolderSetting, true); + } + } + 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); @@ -391,7 +398,7 @@ public final class UserUtil { LinearLayoutManager layoutManager = new LinearLayoutManager(context); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(new SettingsAdapter(context, user, null, true, new SectionAdapter.OnItemClickedListener<User.Setting>() { + recyclerView.setAdapter(SettingsAdapter.getSettingsAdapter(context, user, null, true, new SectionAdapter.OnItemClickedListener<User.Setting>() { @Override public void onItemClicked(UpdateView<User.Setting> updateView, User.Setting item) { if(updateView.isCheckable()) { diff --git a/app/src/main/java/github/daneren2005/dsub/view/SettingView.java b/app/src/main/java/github/daneren2005/dsub/view/SettingView.java index d46dc5d2..efd8bc83 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/SettingView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/SettingView.java @@ -23,6 +23,7 @@ import android.widget.TextView; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.User; +import github.daneren2005.dsub.domain.User.MusicFolderSetting; import static github.daneren2005.dsub.domain.User.Setting; @@ -51,12 +52,14 @@ public class SettingView extends UpdateView2<Setting, Boolean> { protected void setObjectImpl(Setting setting, Boolean isEditable) { // Can't edit non-role parts String name = setting.getName(); - if(name.indexOf("Role") == -1) { + if(name.indexOf("Role") == -1 && !(setting instanceof MusicFolderSetting)) { item2 = false; } int res = -1; - if(User.SCROBBLING.equals(name)) { + if(setting instanceof MusicFolderSetting) { + titleView.setText(((MusicFolderSetting) setting).getLabel()); + } else if(User.SCROBBLING.equals(name)) { res = R.string.admin_scrobblingEnabled; } else if(User.ADMIN.equals(name)) { res = R.string.admin_role_admin; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 174e6588..6eac3c83 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -531,6 +531,8 @@ <string name="admin.delete_user_error">Failed to delete %1$s</string> <string name="admin.confirm_password">Confirm Password</string> <string name="admin.confirm_password_bad">Entered password is wrong</string> + <string name="admin.permissions">Permissions</string> + <string name="admin.musicFolders">Music Folders</string> <string name="admin.scrobblingEnabled">Scrobbling allowed</string> <string name="admin.role.admin">Administrator</string> |