Extract data folder dialog layout
This is in preparation to add a progress bar displaying the amount of used/free space in the dialog (#3049). Since we'll need a custom view to do it, this extracts the layout beforehand.
This commit is contained in:
parent
31adff0dcc
commit
683f7e46a2
|
@ -0,0 +1,131 @@
|
|||
package de.danoeh.antennapod.adapter;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.util.Converter;
|
||||
import de.danoeh.antennapod.core.util.StorageUtils;
|
||||
import de.danoeh.antennapod.dialog.ChooseDataFolderDialog;
|
||||
|
||||
|
||||
public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.ViewHolder> {
|
||||
private final ChooseDataFolderDialog.RunnableWithString selectionHandler;
|
||||
private final String currentPath;
|
||||
private final List<StoragePath> entries;
|
||||
private Dialog dialog;
|
||||
|
||||
public DataFolderAdapter(Context context, ChooseDataFolderDialog.RunnableWithString selectionHandler) {
|
||||
this.entries = getStorageEntries(context);
|
||||
this.currentPath = getCurrentPath();
|
||||
this.selectionHandler = selectionHandler;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||||
View entryView = inflater.inflate(R.layout.choose_data_folder_dialog_entry, parent, false);
|
||||
return new ViewHolder(entryView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
StoragePath storagePath = entries.get(position);
|
||||
holder.path.setText(storagePath.getShortPath());
|
||||
holder.size.setText(Converter.byteToString(storagePath.getAvailableSpace()));
|
||||
holder.root.setOnClickListener((View v) -> selectAndDismiss(storagePath));
|
||||
holder.radioButton.setOnClickListener((View v) -> selectAndDismiss(storagePath));
|
||||
if (storagePath.getFullPath().equals(currentPath)) {
|
||||
holder.radioButton.toggle();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
if (currentPath == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return entries.size();
|
||||
}
|
||||
}
|
||||
|
||||
public void setDialog(Dialog dialog) {
|
||||
this.dialog = dialog;
|
||||
}
|
||||
|
||||
private String getCurrentPath() {
|
||||
File dataFolder = UserPreferences.getDataFolder(null);
|
||||
if (dataFolder != null) return dataFolder.getAbsolutePath();
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<StoragePath> getStorageEntries(Context context) {
|
||||
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
|
||||
final List<StoragePath> entries = new ArrayList<>(mediaDirs.length);
|
||||
for (File dir : mediaDirs) {
|
||||
if (isNotWritable(dir)) continue;
|
||||
|
||||
entries.add(new StoragePath(dir.getAbsolutePath()));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
private boolean isNotWritable(File dir) {
|
||||
return dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite();
|
||||
}
|
||||
|
||||
private void selectAndDismiss(StoragePath storagePath) {
|
||||
selectionHandler.run(storagePath.getFullPath());
|
||||
dialog.dismiss();
|
||||
}
|
||||
|
||||
class ViewHolder extends RecyclerView.ViewHolder {
|
||||
private View root;
|
||||
private TextView path;
|
||||
private TextView size;
|
||||
private RadioButton radioButton;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
root = itemView.findViewById(R.id.root);
|
||||
path = itemView.findViewById(R.id.path);
|
||||
size = itemView.findViewById(R.id.size);
|
||||
radioButton = itemView.findViewById(R.id.radio_button);
|
||||
}
|
||||
}
|
||||
|
||||
class StoragePath {
|
||||
private final String path;
|
||||
|
||||
StoragePath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
String getShortPath() {
|
||||
int prefixIndex = path.indexOf("Android");
|
||||
return (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
|
||||
}
|
||||
|
||||
String getFullPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
long getAvailableSpace() {
|
||||
return StorageUtils.getFreeSpaceAvailable(path);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +1,11 @@
|
|||
package de.danoeh.antennapod.dialog;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.Html;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.util.Converter;
|
||||
import de.danoeh.antennapod.core.util.StorageUtils;
|
||||
import de.danoeh.antennapod.adapter.DataFolderAdapter;
|
||||
|
||||
public class ChooseDataFolderDialog {
|
||||
|
||||
|
@ -32,12 +22,9 @@ public class ChooseDataFolderDialog {
|
|||
private ChooseDataFolderDialog() {}
|
||||
|
||||
public static void showDialog(final Context context, RunnableWithString handlerFunc) {
|
||||
File dataFolder = UserPreferences.getDataFolder(null);
|
||||
HashMap<String, List<String>> options = getStorageOptions(context);
|
||||
final List<String> entries = options.get("entries");
|
||||
final List<String> folders = options.get("folders");
|
||||
DataFolderAdapter adapter = new DataFolderAdapter(context, handlerFunc);
|
||||
|
||||
if (dataFolder == null || entries.isEmpty()) {
|
||||
if (adapter.getItemCount() == 0) {
|
||||
new MaterialDialog.Builder(context)
|
||||
.title(R.string.error_label)
|
||||
.content(R.string.external_storage_error_msg)
|
||||
|
@ -46,66 +33,15 @@ public class ChooseDataFolderDialog {
|
|||
return;
|
||||
}
|
||||
|
||||
int selectedIndex = folders.indexOf(dataFolder.getAbsolutePath());
|
||||
MaterialDialog dialog = new MaterialDialog.Builder(context)
|
||||
.title(R.string.choose_data_directory)
|
||||
.content(R.string.choose_data_directory_message)
|
||||
.items(entries)
|
||||
.itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
|
||||
String folder = folders.get(which);
|
||||
handlerFunc.run(folder);
|
||||
return true;
|
||||
})
|
||||
.adapter(adapter, null)
|
||||
.negativeText(R.string.cancel_label)
|
||||
.cancelable(true)
|
||||
.build();
|
||||
adapter.setDialog(dialog);
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private static HashMap<String, List<String>> getStorageOptions(Context context) {
|
||||
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
|
||||
final List<String> folders = new ArrayList<>(mediaDirs.length);
|
||||
final List<String> entries = new ArrayList<>(mediaDirs.length);
|
||||
for (File dir : mediaDirs) {
|
||||
if (isNotWritable(dir)) continue;
|
||||
|
||||
String path = dir.getAbsolutePath();
|
||||
String location = getStorageLocation(path);
|
||||
String availableSpace = getAvailableSpace(path);
|
||||
folders.add(path);
|
||||
entries.add(storageEntry(location, availableSpace));
|
||||
}
|
||||
return new HashMap<String, List<String>>() {{
|
||||
put("folders", folders);
|
||||
put("entries", entries);
|
||||
}};
|
||||
}
|
||||
|
||||
private static String storageEntry(String location, String availableSpace) {
|
||||
String html = String.format("<small>%1$s [%2$s]</small>", location, availableSpace);
|
||||
return fromHtmlVersioned(html).toString();
|
||||
}
|
||||
|
||||
private static String getAvailableSpace(String path) {
|
||||
long spaceAvailable = StorageUtils.getFreeSpaceAvailable(path);
|
||||
return Converter.byteToString(spaceAvailable);
|
||||
}
|
||||
|
||||
private static String getStorageLocation(String path) {
|
||||
int prefixIndex = path.indexOf("Android");
|
||||
return (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
|
||||
}
|
||||
|
||||
private static boolean isNotWritable(File dir) {
|
||||
return dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static CharSequence fromHtmlVersioned(final String html) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
return Html.fromHtml(html);
|
||||
}
|
||||
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/root"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/diag_content_side_padding"
|
||||
android:background="?attr/selectableItemBackground">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/radio_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:padding="4dp" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:layout_toEndOf="@+id/radio_button"
|
||||
android:layout_toRightOf="@+id/radio_button"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/path"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="/storage/sdcard0" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/size"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="2 GB" />
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
Loading…
Reference in New Issue