From 680c9db075ffb2afafed936dc23a5562db0a7b63 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Mon, 30 Mar 2015 12:18:16 +0200 Subject: [PATCH] "Add feed" facelift, OPML import with better explanations and usage of external apps --- .../activity/OpmlImportFromPathActivity.java | 191 +++++++++--------- app/src/main/res/layout/addfeed.xml | 180 ++++++++--------- app/src/main/res/layout/opml_import.xml | 82 +++++++- core/src/main/res/values/strings.xml | 12 +- core/src/main/res/values/styles.xml | 1 - 5 files changed, 268 insertions(+), 198 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java index 162a8f2e5..c1bbb7e52 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportFromPathActivity.java @@ -1,7 +1,9 @@ package de.danoeh.antennapod.activity; -import android.app.AlertDialog; -import android.content.DialogInterface; +import android.content.ActivityNotFoundException; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Menu; @@ -10,23 +12,31 @@ import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; -import android.widget.Toast; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.List; + import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.LangUtils; import de.danoeh.antennapod.core.util.StorageUtils; -import java.io.*; - /** * Lets the user start the OPML-import process from a path */ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { + private static final String TAG = "OpmlImportFromPathActivity"; - private TextView txtvPath; - private Button butStart; - private String importPath; + + private static final int CHOOSE_OPML_FILE = 1; + + private Intent intentPickAction; + private Intent intentGetContentAction; @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,47 +46,74 @@ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.opml_import); - txtvPath = (TextView) findViewById(R.id.txtvPath); - butStart = (Button) findViewById(R.id.butStartImport); + final TextView txtvHeaderExplanation1 = (TextView) findViewById(R.id.txtvHeadingExplanation1); + final TextView txtvExplanation1 = (TextView) findViewById(R.id.txtvExplanation1); + final TextView txtvHeaderExplanation2 = (TextView) findViewById(R.id.txtvHeadingExplanation2); + final TextView txtvExplanation2 = (TextView) findViewById(R.id.txtvExplanation2); + final TextView txtvHeaderExplanation3 = (TextView) findViewById(R.id.txtvHeadingExplanation3); - butStart.setOnClickListener(new OnClickListener() { + Button butChooseFilesystem = (Button) findViewById(R.id.butChooseFileFromFilesystem); + butChooseFilesystem.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - checkFolderForFiles(); + chooseFileFromFilesystem(); } }); + + Button butChooseExternal = (Button) findViewById(R.id.butChooseFileFromExternal); + butChooseExternal.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + chooseFileFromExternal(); + } + }); + + int nextOption = 1; + intentPickAction = new Intent(Intent.ACTION_PICK); + intentPickAction.setData(Uri.parse("file://")); + List intentActivities = getPackageManager() + .queryIntentActivities(intentPickAction, CHOOSE_OPML_FILE); + if(intentActivities.size() == 0) { + intentPickAction.setData(null); + intentActivities = getPackageManager() + .queryIntentActivities(intentPickAction, CHOOSE_OPML_FILE); + if(intentActivities.size() == 0) { + txtvHeaderExplanation1.setVisibility(View.GONE); + txtvExplanation1.setVisibility(View.GONE); + findViewById(R.id.divider1).setVisibility(View.GONE); + butChooseFilesystem.setVisibility(View.GONE); + } + } + if(txtvExplanation1.getVisibility() == View.VISIBLE) { + txtvHeaderExplanation1.setText("Option " + nextOption); + nextOption++; + } + + intentGetContentAction = new Intent(Intent.ACTION_GET_CONTENT); + intentGetContentAction.addCategory(Intent.CATEGORY_OPENABLE); + intentGetContentAction.setType("*/*"); + intentActivities = getPackageManager() + .queryIntentActivities(intentGetContentAction, CHOOSE_OPML_FILE); + if(intentActivities.size() == 0) { + txtvHeaderExplanation2.setVisibility(View.GONE); + txtvExplanation2.setVisibility(View.GONE); + findViewById(R.id.divider2).setVisibility(View.GONE); + butChooseExternal.setVisibility(View.GONE); + } else { + txtvHeaderExplanation2.setText("Option " + nextOption); + nextOption++; + } + + txtvHeaderExplanation3.setText("Option " + nextOption); } @Override protected void onResume() { super.onResume(); StorageUtils.checkStorageAvailability(this); - setImportPath(); } - /** - * Sets the importPath variable and makes txtvPath display the import - * directory. - */ - private void setImportPath() { - File importDir = UserPreferences.getDataFolder(this, UserPreferences.IMPORT_DIR); - boolean success = true; - if (!importDir.exists()) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Import directory doesn't exist. Creating..."); - success = importDir.mkdir(); - if (!success) { - Log.e(TAG, "Could not create directory"); - } - } - if (success) { - txtvPath.setText(importDir.toString()); - importPath = importDir.toString(); - } else { - txtvPath.setText(R.string.opml_directory_error); - } - } @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -95,32 +132,6 @@ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { } } - /** - * Looks at the contents of the import directory and decides what to do. If - * more than one file is in the directory, a dialog will be created to let - * the user choose which item to import - */ - private void checkFolderForFiles() { - File dir = new File(importPath); - if (dir.isDirectory()) { - File[] fileList = dir.listFiles(); - if (fileList.length == 1) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Found one file, choosing that one."); - startImport(fileList[0]); - } else if (fileList.length > 1) { - Log.w(TAG, "Import directory contains more than one file."); - askForFile(dir); - } else { - Log.e(TAG, "Import directory is empty"); - Toast toast = Toast - .makeText(this, R.string.opml_import_error_dir_empty, - Toast.LENGTH_LONG); - toast.show(); - } - } - } - private void startImport(File file) { Reader mReader = null; try { @@ -134,38 +145,36 @@ public class OpmlImportFromPathActivity extends OpmlImportBaseActivity { } } - /** - * Asks the user to choose from a list of files in a directory and returns - * his choice. + /* + * Creates an implicit intent to launch a file manager which lets + * the user choose a specific OPML-file to import from. */ - private void askForFile(File dir) { - final File[] fileList = dir.listFiles(); - String[] fileNames = dir.list(); - - AlertDialog.Builder dialog = new AlertDialog.Builder(this); - dialog.setTitle(R.string.choose_file_to_import_label); - dialog.setNeutralButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Dialog was cancelled"); - dialog.dismiss(); - } - }); - dialog.setItems(fileNames, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (BuildConfig.DEBUG) - Log.d(TAG, "File at index " + which + " was chosen"); - dialog.dismiss(); - startImport(fileList[which]); - } - }); - dialog.create().show(); + private void chooseFileFromFilesystem() { + try { + startActivityForResult(intentPickAction, CHOOSE_OPML_FILE); + } catch (ActivityNotFoundException e) { + Log.e(TAG, "No activity found. Should never happen..."); + } } + private void chooseFileFromExternal() { + try { + startActivityForResult(intentGetContentAction, CHOOSE_OPML_FILE); + } catch (ActivityNotFoundException e) { + Log.e(TAG, "No activity found. Should never happen..."); + } + } + + /** + * Gets the path of the file chosen with chooseFileToImport() + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == RESULT_OK && requestCode == CHOOSE_OPML_FILE) { + String filename = data.getData().getPath(); + startImport(new File(filename)); + } + } } diff --git a/app/src/main/res/layout/addfeed.xml b/app/src/main/res/layout/addfeed.xml index a740d88cf..b7babbafa 100644 --- a/app/src/main/res/layout/addfeed.xml +++ b/app/src/main/res/layout/addfeed.xml @@ -1,107 +1,107 @@ - + - + android:paddingTop="8dp" + android:paddingLeft="16dp" + android:paddingRight="16dp" + android:paddingBottom="8dp" + android:orientation="vertical"> - + android:layout_height="wrap_content" + style="@style/AntennaPod.TextView.Heading" + android:text="@string/txtvfeedurl_label"/> - + - +