[Android] Pass context to CustomDirectories util
Habib Kazemi
git at videolan.org
Tue Aug 14 18:32:32 CEST 2018
vlc-android | branch: master | Habib Kazemi <kazemihabib1996 at gmail.com> | Sat Aug 11 12:41:19 2018 +0430| [ca4b147112e08cc0cd0ced30427a9653e3c98ea2] | committer: Geoffrey Métais
Pass context to CustomDirectories util
Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>
> https://code.videolan.org/videolan/vlc-android/commit/ca4b147112e08cc0cd0ced30427a9653e3c98ea2
---
.../vlc/gui/browser/StorageBrowserAdapter.java | 7 +++---
.../vlc/gui/browser/StorageBrowserFragment.java | 10 ++++-----
.../org/videolan/vlc/providers/StorageProvider.kt | 2 +-
.../org/videolan/vlc/util/CustomDirectories.java | 25 ++++++++--------------
4 files changed, 19 insertions(+), 25 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
index 88e4c4c60..716e78bfa 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
@@ -23,6 +23,7 @@
package org.videolan.vlc.gui.browser;
+import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
@@ -49,7 +50,7 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
StorageBrowserAdapter(BaseBrowserFragment fragment) {
super(fragment);
- updateMediaDirs();
+ updateMediaDirs(fragment.requireContext());
}
@Override
@@ -78,14 +79,14 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
return false;
}
- void updateMediaDirs() {
+ void updateMediaDirs(Context context) {
if (mMediaDirsLocation != null) mMediaDirsLocation.clear();
final String folders[] = VLCApplication.getMLInstance().getFoldersList();
mMediaDirsLocation = new ArrayList<>(folders.length);
for (String folder : folders) {
mMediaDirsLocation.add(Uri.decode(folder.startsWith("file://") ? folder.substring(7) : folder));
}
- mCustomDirsLocation = new ArrayList<>(Arrays.asList(CustomDirectories.getCustomDirectories()));
+ mCustomDirsLocation = new ArrayList<>(Arrays.asList(CustomDirectories.getCustomDirectories(context)));
}
protected void checkBoxAction(View v, String mrl) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java
index a6d3c42fb..f097aa031 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java
@@ -151,7 +151,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
public void onCtxClick(View v, int position, MediaLibraryItem item) {
if (isRootDirectory()) {
final Storage storage = (Storage) getAdapter().getItem(position);
- boolean isCustom = CustomDirectories.contains(storage.getUri().getPath());
+ boolean isCustom = CustomDirectories.contains(storage.getUri().getPath(), requireContext());
if (isCustom) ContextSheetKt.showContext(requireActivity(), this, position, item.getTitle(), Constants.CTX_CUSTOM_REMOVE);
}
}
@@ -159,7 +159,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
@Override
public void onCtxAction(int position, int option) {
final Storage storage = (Storage) getAdapter().getItem(position);
- CustomDirectories.removeCustomDirectory(storage.getUri().getPath());
+ CustomDirectories.removeCustomDirectory(storage.getUri().getPath(), requireContext());
viewModel.remove(storage);
((AudioPlayerContainerActivity)getActivity()).updateLib();
}
@@ -198,7 +198,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
public void run() {
cb.setEnabled(true);
if (success) {
- ((StorageBrowserAdapter) getAdapter()).updateMediaDirs();
+ ((StorageBrowserAdapter) getAdapter()).updateMediaDirs(requireContext());
getAdapter().notifyDataSetChanged();
} else cb.setChecked(true);
}
@@ -224,7 +224,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
mProcessingFolders.get(finalPath).setEnabled(true);
}
});
- ((StorageBrowserAdapter) getAdapter()).updateMediaDirs();
+ ((StorageBrowserAdapter) getAdapter()).updateMediaDirs(requireContext());
}
}
@@ -251,7 +251,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
}
try {
- CustomDirectories.addCustomDirectory(f.getCanonicalPath());
+ CustomDirectories.addCustomDirectory(f.getCanonicalPath(), requireContext());
((AudioPlayerContainerActivity)getActivity()).updateLib();
} catch (IOException ignored) {}
}
diff --git a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
index b7923bfb2..dd041bde8 100644
--- a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
@@ -37,7 +37,7 @@ class StorageProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
override fun browseRoot() {
val storages = AndroidDevices.getMediaDirectories()
- val customDirectories = CustomDirectories.getCustomDirectories()
+ val customDirectories = CustomDirectories.getCustomDirectories(context)
var storage: Storage
val storagesList = ArrayList<MediaLibraryItem>()
for (mediaDirLocation in storages) {
diff --git a/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java b/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
index 81807a22b..8d20056b6 100644
--- a/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
+++ b/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
@@ -23,20 +23,17 @@ package org.videolan.vlc.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
-
-import org.videolan.vlc.VLCApplication;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CustomDirectories {
- public static void addCustomDirectory(String path) {
- SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
+ public static void addCustomDirectory(String path, Context context) {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
List<String> dirs = new ArrayList<String>(
- Arrays.asList(CustomDirectories.getCustomDirectories()));
+ Arrays.asList(CustomDirectories.getCustomDirectories(context)));
dirs.add(path);
StringBuilder builder = new StringBuilder();
builder.append(dirs.remove(0));
@@ -49,8 +46,8 @@ public class CustomDirectories {
editor.apply();
}
- public static void removeCustomDirectory(String path) {
- SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
+ public static void removeCustomDirectory(String path, Context context) {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (!preferences.getString("custom_paths", "").contains(path))
return;
List<String> dirs = new ArrayList<String>(
@@ -74,12 +71,8 @@ public class CustomDirectories {
editor.apply();
}
- public static String[] getCustomDirectories() {
- return getCustomDirectories(VLCApplication.getAppContext());
- }
-
- public static String[] getCustomDirectories(Context ctx) {
- final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
+ public static String[] getCustomDirectories(Context context) {
+ final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final String custom_paths = preferences.getString("custom_paths", "");
if (custom_paths.equals(""))
return new String[0];
@@ -87,7 +80,7 @@ public class CustomDirectories {
return custom_paths.split(":");
}
- public static boolean contains(String directory) {
- return Strings.INSTANCE.stringArrayContains(getCustomDirectories(), directory.trim());
+ public static boolean contains(String directory, Context context) {
+ return Strings.INSTANCE.stringArrayContains(getCustomDirectories(context), directory.trim());
}
}
More information about the Android
mailing list