Commit 1723625e authored by Geoffrey Métais's avatar Geoffrey Métais

Use 3-states checkboxes for Medialibrary settings

parent c98b28db
......@@ -100,9 +100,15 @@ public class Medialibrary {
nativeDiscover(Tools.encodeVLCMrl(path));
}
public void removeFolder(@NonNull String path) {
if (mIsInitiated)
nativeRemoveEntryPoint(Tools.encodeVLCMrl(path));
public void removeFolder(@NonNull String mrl) {
if (!mIsInitiated)
return;
String[] folders = getFoldersList();
for (String folder : folders) {
if (!folder.equals(mrl) && folder.contains(mrl))
removeFolder(folder);
}
nativeRemoveEntryPoint(Tools.encodeVLCMrl(mrl));
}
public String[] getFoldersList() {
......
......@@ -18,9 +18,6 @@
<variable
name="checkEnabled"
type="boolean"/>
<variable
name="checked"
type="boolean"/>
<variable
name="protocol"
type="String"/>
......@@ -39,11 +36,10 @@
android:onClick="@{holder::onClick}"
android:background="@drawable/background_item">
<CheckBox
<org.videolan.vlc.gui.helpers.ThreeStatesCheckbox
android:id="@+id/browser_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{checked, default=false}"
android:enabled="@{checkEnabled}"
android:focusable="false"
android:onClick="@{holder::onCheckBoxClick}"
......
......@@ -149,7 +149,6 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
binding.browserCheckbox.toggle();
onCheckBoxClick(binding.browserCheckbox);
return true;
}
......@@ -168,7 +167,7 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
public void onCheckBoxClick(View v) {
if (getItem(getLayoutPosition()).getItemType() == TYPE_STORAGE)
checkBoxAction(v, ((Storage) getItem(getLayoutPosition())).getUri().getPath());
checkBoxAction(v, ((Storage) getItem(getLayoutPosition())).getUri().toString());
}
@Override
......@@ -331,7 +330,7 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
return media.getUri().getScheme();
}
protected void checkBoxAction(View v, String path){}
protected void checkBoxAction(View v, String mrl){}
ArrayList<MediaWrapper> getSelection() {
ArrayList<MediaWrapper> selection = new ArrayList<>();
......
......@@ -23,15 +23,15 @@
package org.videolan.vlc.gui.browser;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.medialibrary.media.Storage;
import org.videolan.vlc.MediaParsingService;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.helpers.MedialibraryUtils;
import org.videolan.vlc.gui.helpers.ThreeStatesCheckbox;
import org.videolan.vlc.util.CustomDirectories;
import java.util.ArrayList;
......@@ -39,8 +39,8 @@ import java.util.Arrays;
class StorageBrowserAdapter extends BaseBrowserAdapter {
private ArrayList<String> mMediaDirsLocation;
private ArrayList<String> mCustomDirsLocation;
private static ArrayList<String> mMediaDirsLocation;
private static ArrayList<String> mCustomDirsLocation;
StorageBrowserAdapter(BaseBrowserFragment fragment) {
super(fragment);
......@@ -51,43 +51,42 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
public void onBindViewHolder(final ViewHolder holder, int position) {
final MediaViewHolder vh = (MediaViewHolder) holder;
MediaLibraryItem storage = getItem(position);
if (storage.getItemType() == MediaLibraryItem.TYPE_MEDIA)
storage = new Storage(((MediaWrapper)storage).getUri());
String storagePath = ((Storage)storage).getUri().getPath();
if (!storagePath.endsWith("/"))
storagePath += "/";
boolean hasContextMenu = mCustomDirsLocation.contains(storagePath);
boolean checked = ((StorageBrowserFragment) fragment).mScannedDirectory || mMediaDirsLocation.contains(storagePath);
vh.binding.setItem(storage);
vh.binding.setHasContextMenu(hasContextMenu);
vh.binding.setChecked(((StorageBrowserFragment) fragment).mScannedDirectory || mMediaDirsLocation.contains(storagePath));
if (checked)
vh.binding.browserCheckbox.setState(ThreeStatesCheckbox.STATE_CHECKED);
else if (hasDiscoveredChildren(storagePath))
vh.binding.browserCheckbox.setState(ThreeStatesCheckbox.STATE_PARTIAL);
else
vh.binding.browserCheckbox.setState(ThreeStatesCheckbox.STATE_UNCHECKED);
vh.binding.setCheckEnabled(!((StorageBrowserFragment) fragment).mScannedDirectory);
if (hasContextMenu)
vh.setContextMenuListener();
}
private boolean hasDiscoveredChildren(String path) {
for (String directory : mMediaDirsLocation)
if (directory.startsWith(path))
return true;
return false;
}
public void addItem(MediaLibraryItem item, boolean top, int position) {
if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA)
item = new Storage(((MediaWrapper)item).getUri());
item = new Storage(((MediaWrapper)item).getUri());
else if (item.getItemType() != MediaLibraryItem.TYPE_STORAGE)
return;
super.addItem(item, top, position);
}
private void removeDir(final String path) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
VLCApplication.getMLInstance().removeFolder(path);
}
});
}
private void addDir(final String path) {
Intent intent = new Intent(MediaParsingService.ACTION_DISCOVER, null, VLCApplication.getAppContext(), MediaParsingService.class);
intent.putExtra(MediaParsingService.EXTRA_PATH, path);
VLCApplication.getAppContext().startService(intent);
}
void updateMediaDirs() {
if (mMediaDirsLocation != null)
mMediaDirsLocation.clear();
......@@ -99,18 +98,13 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
mCustomDirsLocation = new ArrayList<>(Arrays.asList(CustomDirectories.getCustomDirectories()));
}
protected void openMediaFromView(MediaViewHolder holder, View v) {
MediaWrapper mw = new MediaWrapper(((Storage) getItem(holder.getLayoutPosition())).getUri());
mw.setType(MediaWrapper.TYPE_DIR);
fragment.browse(mw, holder.getLayoutPosition(), holder.binding.browserCheckbox.isChecked());
}
protected void checkBoxAction(View v, String path){
boolean isChecked = ((CheckBox) v).isChecked();
if (isChecked)
addDir(path);
else
removeDir(path);
((StorageBrowserFragment)fragment).processEvent((CheckBox) v, path);
protected void checkBoxAction(View v, String mrl) {
ThreeStatesCheckbox tscb = (ThreeStatesCheckbox) v;
int state = tscb.getState();
if (state == ThreeStatesCheckbox.STATE_CHECKED)
MedialibraryUtils.addDir(mrl);
else
MedialibraryUtils.removeDir(mrl);
((StorageBrowserFragment)fragment).processEvent((CheckBox) v, mrl);
}
}
......@@ -43,6 +43,7 @@ import org.videolan.medialibrary.media.Storage;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.BrowserItemBinding;
import org.videolan.vlc.gui.helpers.ThreeStatesCheckbox;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.CustomDirectories;
......@@ -178,7 +179,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
public void onClick(View v, int position, MediaLibraryItem item) {
MediaWrapper mw = new MediaWrapper(((Storage) item).getUri());
mw.setType(MediaWrapper.TYPE_DIR);
browse(mw, position, ((BrowserItemBinding)DataBindingUtil.findBinding(v)).browserCheckbox.isChecked());
browse(mw, position, ((BrowserItemBinding)DataBindingUtil.findBinding(v)).browserCheckbox.getState() == ThreeStatesCheckbox.STATE_CHECKED);
}
@Override
......@@ -188,9 +189,9 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
}
}
void processEvent(CheckBox cbp, String path) {
void processEvent(CheckBox cbp, String mrl) {
cbp.setEnabled(false);
mProcessingFolders.put(path, cbp);
mProcessingFolders.put(mrl, cbp);
}
@Override
......@@ -206,20 +207,21 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
@Override
public void onEntryPointRemoved(String entryPoint, final boolean success) {
String path = Uri.parse(entryPoint).getPath();
if (path.endsWith("/"))
path = path.substring(0, path.length()-1);
if (mProcessingFolders.containsKey(path)) {
final String finalPath = path;
if (entryPoint.endsWith("/"))
entryPoint = entryPoint.substring(0, entryPoint.length()-1);
if (mProcessingFolders.containsKey(entryPoint)) {
final String finalMrl = entryPoint;
mHandler.post(new Runnable() {
@Override
public void run() {
mProcessingFolders.get(finalPath).setEnabled(true);
if (!success)
mProcessingFolders.get(finalPath).setChecked(false);
mProcessingFolders.get(finalMrl).setEnabled(true);
if (success) {
((StorageBrowserAdapter)mAdapter).updateMediaDirs();
mAdapter.notifyDataSetChanged();
} else
mProcessingFolders.get(finalMrl).setChecked(false);
}
});
((StorageBrowserAdapter)mAdapter).updateMediaDirs();
}
}
......@@ -231,7 +233,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
@Override
public void onDiscoveryCompleted(String entryPoint) {
String path = Uri.parse(entryPoint).getPath();
String path = entryPoint;
if (path.endsWith("/"))
path = path.substring(0, path.length()-1);
if (mProcessingFolders.containsKey(path)) {
......
package org.videolan.vlc.gui.helpers;
import android.content.Intent;
import org.videolan.vlc.MediaParsingService;
import org.videolan.vlc.VLCApplication;
public class MedialibraryUtils {
public static void removeDir(final String path) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
VLCApplication.getMLInstance().removeFolder(path);
}
});
}
public static void addDir(final String path) {
Intent intent = new Intent(MediaParsingService.ACTION_DISCOVER, null, VLCApplication.getAppContext(), MediaParsingService.class);
intent.putExtra(MediaParsingService.EXTRA_PATH, path);
VLCApplication.getAppContext().startService(intent);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment