Reworking data dir dialog

This commit is contained in:
Danial Klimkin 2017-10-15 09:31:11 +02:00
parent 26cdef1378
commit 6c8f9bc80e
3 changed files with 98 additions and 128 deletions

View File

@ -10,10 +10,8 @@ import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
@ -21,13 +19,11 @@ import android.widget.Button;
import com.afollestad.materialdialogs.MaterialDialog;
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;
/** Is show if there is now external storage available. */
public class StorageErrorActivity extends AppCompatActivity {
@ -117,65 +113,10 @@ public class StorageErrorActivity extends AppCompatActivity {
// see PreferenceController.showChooseDataFolderDialog()
private void showChooseDataFolderDialog() {
File dataFolder = UserPreferences.getDataFolder(null);
if(dataFolder == null) {
new MaterialDialog.Builder(this)
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
String dataFolderPath = dataFolder.getAbsolutePath();
int selectedIndex = -1;
File[] mediaDirs = ContextCompat.getExternalFilesDirs(this, null);
List<String> folders = new ArrayList<>(mediaDirs.length);
List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
for(int i=0; i < mediaDirs.length; i++) {
File dir = mediaDirs[i];
if(dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
continue;
}
String path = mediaDirs[i].getAbsolutePath();
folders.add(path);
if(dataFolderPath.equals(path)) {
selectedIndex = i;
}
int index = path.indexOf("Android");
String choice;
if(index >= 0) {
choice = path.substring(0, index);
} else {
choice = path;
}
long bytes = StorageUtils.getFreeSpaceAvailable(path);
String freeSpace = String.format(getString(R.string.free_space_label),
Converter.byteToString(bytes));
choices.add(Html.fromHtml("<html><small>" + choice + " [" + freeSpace + "]"
+ "</small></html>"));
}
if(choices.size() == 0) {
new MaterialDialog.Builder(this)
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
MaterialDialog dialog = new MaterialDialog.Builder(this)
.title(R.string.choose_data_directory)
.content(R.string.choose_data_directory_message)
.items(choices.toArray(new CharSequence[choices.size()]))
.itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
String folder = folders.get(which);
ChooseDataFolderDialog.showDialog(this, (folder) -> {
UserPreferences.setDataFolder(folder);
leaveErrorState();
return true;
})
.negativeText(R.string.cancel_label)
.cancelable(true)
.build();
dialog.show();
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {

View File

@ -0,0 +1,88 @@
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.List;
import java.util.function.Consumer;
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;
public class ChooseDataFolderDialog {
private ChooseDataFolderDialog() {}
public static void showDialog(final Context context, Consumer<String> handlerFunc) {
File dataFolder = UserPreferences.getDataFolder(null);
if (dataFolder == null) {
new MaterialDialog.Builder(context)
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
String dataFolderPath = dataFolder.getAbsolutePath();
int selectedIndex = -1;
int index = 0;
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
final List<String> folders = new ArrayList<>(mediaDirs.length);
final List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
for (File dir : mediaDirs) {
if(dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
continue;
}
String path = dir.getAbsolutePath();
folders.add(path);
if(dataFolderPath.equals(path)) {
selectedIndex = index;
}
int prefixIndex = path.indexOf("Android");
String choice = (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
long bytes = StorageUtils.getFreeSpaceAvailable(path);
String item = String.format(
"<small>%1$s [%2$s]</small>", choice, Converter.byteToString(bytes));
choices.add(fromHtmlVersioned(item));
index++;
}
if (choices.isEmpty()) {
new MaterialDialog.Builder(context)
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
MaterialDialog dialog = new MaterialDialog.Builder(context)
.title(R.string.choose_data_directory)
.content(R.string.choose_data_directory_message)
.items(choices)
.itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
String folder = folders.get(which);
handlerFunc.accept(folder);
return true;
})
.negativeText(R.string.cancel_label)
.cancelable(true)
.build();
dialog.show();
}
@SuppressWarnings("deprecation")
private static CharSequence fromHtmlVersioned(final String html) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
}
return Html.fromHtml(html);
}
}

View File

@ -23,7 +23,6 @@ import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.Html;
@ -65,11 +64,10 @@ import de.danoeh.antennapod.core.export.opml.OpmlWriter;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.GpodnetSyncService;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.StorageUtils;
import de.danoeh.antennapod.core.util.flattr.FlattrUtils;
import de.danoeh.antennapod.dialog.AuthenticationDialog;
import de.danoeh.antennapod.dialog.AutoFlattrPreferenceDialog;
import de.danoeh.antennapod.dialog.ChooseDataFolderDialog;
import de.danoeh.antennapod.dialog.GpodnetSetHostnameDialog;
import de.danoeh.antennapod.dialog.ProxyDialog;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
@ -918,67 +916,10 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
}
private void showChooseDataFolderDialog() {
Context context = ui.getActivity();
File dataFolder = UserPreferences.getDataFolder(null);
if(dataFolder == null) {
new MaterialDialog.Builder(ui.getActivity())
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
String dataFolderPath = dataFolder.getAbsolutePath();
int selectedIndex = -1;
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
List<String> folders = new ArrayList<>(mediaDirs.length);
List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
for(int i=0; i < mediaDirs.length; i++) {
File dir = mediaDirs[i];
if(dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
continue;
}
String path = mediaDirs[i].getAbsolutePath();
folders.add(path);
if(dataFolderPath.equals(path)) {
selectedIndex = i;
}
int index = path.indexOf("Android");
String choice;
if(index >= 0) {
choice = path.substring(0, index);
} else {
choice = path;
}
long bytes = StorageUtils.getFreeSpaceAvailable(path);
String freeSpace = String.format(context.getString(R.string.free_space_label),
Converter.byteToString(bytes));
choices.add(Html.fromHtml("<html><small>" + choice
+ " [" + freeSpace + "]" + "</small></html>"));
}
if(choices.size() == 0) {
new MaterialDialog.Builder(ui.getActivity())
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
MaterialDialog dialog = new MaterialDialog.Builder(ui.getActivity())
.title(R.string.choose_data_directory)
.content(R.string.choose_data_directory_message)
.items(choices.toArray(new CharSequence[choices.size()]))
.itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
String folder = folders.get(which);
Log.d(TAG, "data folder: " + folder);
ChooseDataFolderDialog.showDialog(ui.getActivity(), (folder) -> {
UserPreferences.setDataFolder(folder);
setDataFolderText();
return true;
})
.negativeText(R.string.cancel_label)
.cancelable(true)
.build();
dialog.show();
});
}
// UPDATE TIME/INTERVAL DIALOG