Merge pull request #2439 from dklimkin/htmlpatch

Reworking data dir dialog
This commit is contained in:
Martin Fietz 2017-10-20 20:39:38 +02:00 committed by GitHub
commit 33ea125489
3 changed files with 115 additions and 127 deletions

View File

@ -10,10 +10,8 @@ import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Button; import android.widget.Button;
@ -21,13 +19,11 @@ import android.widget.Button;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences; 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.core.util.StorageUtils;
import de.danoeh.antennapod.dialog.ChooseDataFolderDialog;
/** Is show if there is now external storage available. */ /** Is show if there is now external storage available. */
public class StorageErrorActivity extends AppCompatActivity { public class StorageErrorActivity extends AppCompatActivity {
@ -117,65 +113,14 @@ public class StorageErrorActivity extends AppCompatActivity {
// see PreferenceController.showChooseDataFolderDialog() // see PreferenceController.showChooseDataFolderDialog()
private void showChooseDataFolderDialog() { private void showChooseDataFolderDialog() {
File dataFolder = UserPreferences.getDataFolder(null); ChooseDataFolderDialog.showDialog(
if(dataFolder == null) { this, new ChooseDataFolderDialog.RunnableWithString() {
new MaterialDialog.Builder(this) @Override
.title(R.string.error_label) public void run(final String folder) {
.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);
UserPreferences.setDataFolder(folder); UserPreferences.setDataFolder(folder);
leaveErrorState(); leaveErrorState();
return true; }
}) });
.negativeText(R.string.cancel_label)
.cancelable(true)
.build();
dialog.show();
} }
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {

View File

@ -0,0 +1,97 @@
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 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 {
public static abstract class RunnableWithString implements Runnable {
public RunnableWithString() {
super();
}
public abstract void run(final String arg);
@Override public void run() {
throw new IllegalArgumentException("Expect one String parameter.");
}
}
private ChooseDataFolderDialog() {}
public static void showDialog(final Context context, RunnableWithString 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.run(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);
}
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
}
}

View File

@ -68,11 +68,10 @@ import de.danoeh.antennapod.core.export.opml.OpmlWriter;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences; import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.GpodnetSyncService; 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.core.util.flattr.FlattrUtils;
import de.danoeh.antennapod.dialog.AuthenticationDialog; import de.danoeh.antennapod.dialog.AuthenticationDialog;
import de.danoeh.antennapod.dialog.AutoFlattrPreferenceDialog; import de.danoeh.antennapod.dialog.AutoFlattrPreferenceDialog;
import de.danoeh.antennapod.dialog.ChooseDataFolderDialog;
import de.danoeh.antennapod.dialog.GpodnetSetHostnameDialog; import de.danoeh.antennapod.dialog.GpodnetSetHostnameDialog;
import de.danoeh.antennapod.dialog.ProxyDialog; import de.danoeh.antennapod.dialog.ProxyDialog;
import de.danoeh.antennapod.dialog.VariableSpeedDialog; import de.danoeh.antennapod.dialog.VariableSpeedDialog;
@ -945,67 +944,14 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
} }
private void showChooseDataFolderDialog() { private void showChooseDataFolderDialog() {
Context context = ui.getActivity(); ChooseDataFolderDialog.showDialog(
File dataFolder = UserPreferences.getDataFolder(null); ui.getActivity(), new ChooseDataFolderDialog.RunnableWithString() {
if(dataFolder == null) { @Override
new MaterialDialog.Builder(ui.getActivity()) public void run(final String folder) {
.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);
UserPreferences.setDataFolder(folder); UserPreferences.setDataFolder(folder);
setDataFolderText(); setDataFolderText();
return true; }
}) });
.negativeText(R.string.cancel_label)
.cancelable(true)
.build();
dialog.show();
} }
// UPDATE TIME/INTERVAL DIALOG // UPDATE TIME/INTERVAL DIALOG