diff options
author | Martin Fietz <martin.fietz@gmail.com> | 2018-01-21 11:10:20 +0100 |
---|---|---|
committer | Martin Fietz <martin.fietz@gmail.com> | 2018-01-21 11:10:20 +0100 |
commit | d3491d1858bf33f894f07652169254b5821b0e9d (patch) | |
tree | 74449084cd33cdcdf9a2c04291ba5c9d36defffb /app/src | |
parent | e4770e7edce8116f764fdb748cb3d4252c91b8dd (diff) | |
parent | f7c048e5b4717a997c26be42332395dc3b0f6f02 (diff) | |
download | AntennaPod-d3491d1858bf33f894f07652169254b5821b0e9d.zip |
Merge branch 'develop' into refactor
# Conflicts:
# app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java
# core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java
Diffstat (limited to 'app/src')
11 files changed, 238 insertions, 6 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6be5bd1b3..8c3c86358 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -168,6 +168,13 @@ android:value="de.danoeh.antennapod.activity.PreferenceActivity"/> </activity> <activity + android:name=".activity.ImportExportActivity" + android:label="@string/import_export"> + <meta-data + android:name="android.support.PARENT_ACTIVITY" + android:value="de.danoeh.antennapod.activity.PreferenceActivity"/> + </activity> + <activity android:name=".activity.OpmlImportFromPathActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:label="@string/opml_import_label"> diff --git a/app/src/main/java/de/danoeh/antennapod/activity/FeedInfoActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/FeedInfoActivity.java index 7580becdc..8dea41b7c 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/FeedInfoActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/FeedInfoActivity.java @@ -379,7 +379,7 @@ public class FeedInfoActivity extends AppCompatActivity { } } - private class ApplyToEpisodesDialog extends ConfirmationDialog { + private static class ApplyToEpisodesDialog extends ConfirmationDialog { private final Feed feed; private final boolean autoDownload; diff --git a/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java new file mode 100644 index 000000000..6a97adcc3 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/activity/ImportExportActivity.java @@ -0,0 +1,185 @@ +package de.danoeh.antennapod.activity; + +import android.content.ComponentName; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Environment; +import android.os.ParcelFileDescriptor; +import android.support.design.widget.Snackbar; +import android.support.v4.content.IntentCompat; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.MenuItem; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.preferences.UserPreferences; +import de.danoeh.antennapod.core.storage.PodDBAdapter; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.channels.FileChannel; + +/** + * Displays the 'import/export' screen + */ +public class ImportExportActivity extends AppCompatActivity { + private static final int REQUEST_CODE_RESTORE = 43; + private static final int REQUEST_CODE_BACKUP_DOCUMENT = 44; + private static final String EXPORT_FILENAME = "AntennaPodBackup.db"; + private static final String TAG = ImportExportActivity.class.getSimpleName(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + setTheme(UserPreferences.getTheme()); + super.onCreate(savedInstanceState); + getSupportActionBar().setDisplayShowHomeEnabled(true); + setContentView(R.layout.import_export_activity); + + findViewById(R.id.button_export).setOnClickListener(view -> backup()); + findViewById(R.id.button_import).setOnClickListener(view -> restore()); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } else { + return super.onOptionsItemSelected(item); + } + } + + private void backup() { + if (Build.VERSION.SDK_INT >= 19) { + Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("application/x-sqlite3") + .putExtra(Intent.EXTRA_TITLE, EXPORT_FILENAME); + + startActivityForResult(intent, REQUEST_CODE_BACKUP_DOCUMENT); + } else { + try { + File sd = Environment.getExternalStorageDirectory(); + File backupDB = new File(sd, EXPORT_FILENAME); + writeBackupTo(new FileOutputStream(backupDB)); + } catch (IOException e) { + Log.e(TAG, Log.getStackTraceString(e)); + Snackbar.make(findViewById(R.id.import_export_layout), e.getLocalizedMessage(), Snackbar.LENGTH_SHORT).show(); + } + } + } + + private void restore() { + if (Build.VERSION.SDK_INT >= 19) { + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + intent.setType("*/*"); + startActivityForResult(intent, REQUEST_CODE_RESTORE); + } else { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("*/*"); + startActivityForResult(Intent.createChooser(intent, + getString(R.string.import_select_file)), REQUEST_CODE_RESTORE); + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent resultData) { + if (resultCode != RESULT_OK || resultData == null) { + return; + } + Uri uri = resultData.getData(); + + if (requestCode == REQUEST_CODE_RESTORE) { + restoreFrom(uri); + } else if (requestCode == REQUEST_CODE_BACKUP_DOCUMENT) { + backupToDocument(uri); + } + } + + private void restoreFrom(Uri inputUri) { + File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME); + InputStream inputStream = null; + try { + inputStream = getContentResolver().openInputStream(inputUri); + FileUtils.copyInputStreamToFile(inputStream, currentDB); + displayImportSuccessDialog(); + } catch (IOException e) { + Log.e(TAG, Log.getStackTraceString(e)); + Snackbar.make(findViewById(R.id.import_export_layout), e.getLocalizedMessage(), Snackbar.LENGTH_SHORT).show(); + } finally { + IOUtils.closeQuietly(inputStream); + } + } + + private void displayImportSuccessDialog() { + AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this); + d.setMessage(R.string.import_ok); + d.setCancelable(false); + d.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { + Intent intent = new Intent(getApplicationContext(), SplashActivity.class); + ComponentName cn = intent.getComponent(); + Intent mainIntent = IntentCompat.makeRestartActivityTask(cn); + startActivity(mainIntent); + }); + d.show(); + } + + private void backupToDocument(Uri uri) { + ParcelFileDescriptor pfd = null; + FileOutputStream fileOutputStream = null; + try { + pfd = getContentResolver().openFileDescriptor(uri, "w"); + fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); + writeBackupTo(fileOutputStream); + + Snackbar.make(findViewById(R.id.import_export_layout), + R.string.export_ok, Snackbar.LENGTH_SHORT).show(); + } catch (IOException e) { + Log.e(TAG, Log.getStackTraceString(e)); + Snackbar.make(findViewById(R.id.import_export_layout), e.getLocalizedMessage(), Snackbar.LENGTH_SHORT).show(); + } finally { + IOUtils.closeQuietly(fileOutputStream); + + if (pfd != null) { + try { + pfd.close(); + } catch (IOException e) { + Log.d(TAG, "Unable to close ParcelFileDescriptor"); + } + } + } + } + + private void writeBackupTo(FileOutputStream outFileStream) { + FileChannel src = null; + FileChannel dst = null; + try { + File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME); + + if (currentDB.exists()) { + src = new FileInputStream(currentDB).getChannel(); + dst = outFileStream.getChannel(); + dst.transferFrom(src, 0, src.size()); + + Snackbar.make(findViewById(R.id.import_export_layout), + R.string.export_ok, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(findViewById(R.id.import_export_layout), + "Can not access current database", Snackbar.LENGTH_SHORT).show(); + } + } catch (IOException e) { + Log.e(TAG, Log.getStackTraceString(e)); + Snackbar.make(findViewById(R.id.import_export_layout), e.getLocalizedMessage(), Snackbar.LENGTH_SHORT).show(); + } finally { + IOUtils.closeQuietly(src); + IOUtils.closeQuietly(dst); + } + } +} diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java index ae7040e0e..5e182d39e 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java @@ -72,12 +72,13 @@ import rx.schedulers.Schedulers; */ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implements NavDrawerActivity { + private static final String TAG = "MediaplayerInfoActivity"; + private static final int POS_COVER = 0; private static final int POS_DESCR = 1; private static final int POS_CHAPTERS = 2; private static final int NUM_CONTENT_FRAGMENTS = 3; - private final String TAG = "MediaplayerInfoActivity"; private static final String PREFS = "AudioPlayerActivityPreferences"; private static final String PREF_KEY_SELECTED_FRAGMENT_POSITION = "selectedFragmentPosition"; diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/DownloadLogAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/DownloadLogAdapter.java index 2c6aa8f72..7c0be47b6 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/DownloadLogAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/DownloadLogAdapter.java @@ -29,7 +29,7 @@ import de.danoeh.antennapod.core.storage.DownloadRequestException; /** Displays a list of DownloadStatus entries. */ public class DownloadLogAdapter extends BaseAdapter { - private final String TAG = "DownloadLogAdapter"; + private static final String TAG = "DownloadLogAdapter"; private final Context context; diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/itunes/ItunesAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/itunes/ItunesAdapter.java index 10b5330c1..e493a2ecc 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/itunes/ItunesAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/itunes/ItunesAdapter.java @@ -162,7 +162,7 @@ public class ItunesAdapter extends ArrayAdapter<ItunesAdapter.Podcast> { /** * View holder object for the GridView */ - class PodcastViewHolder { + static class PodcastViewHolder { /** * ImageView holding the Podcast image diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/EpisodesApplyActionFragment.java b/app/src/main/java/de/danoeh/antennapod/dialog/EpisodesApplyActionFragment.java index 04d4ae247..10ce12771 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/EpisodesApplyActionFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/EpisodesApplyActionFragment.java @@ -32,7 +32,7 @@ import de.danoeh.antennapod.core.util.LongList; public class EpisodesApplyActionFragment extends Fragment { - public String TAG = "EpisodeActionFragment"; + public static final String TAG = "EpisodeActionFragment"; private static final int ACTION_QUEUE = 1; private static final int ACTION_MARK_PLAYED = 2; diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/DownloadsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/DownloadsFragment.java index 0e9e36df0..9c4d00e31 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/DownloadsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/DownloadsFragment.java @@ -78,7 +78,7 @@ public class DownloadsFragment extends Fragment { viewPager.setCurrentItem(lastPosition); } - public class DownloadsPagerAdapter extends FragmentPagerAdapter { + public static class DownloadsPagerAdapter extends FragmentPagerAdapter { final Resources resources; diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index 8c2ece735..b5e0f1e99 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -38,6 +38,7 @@ import android.widget.Toast; import com.afollestad.materialdialogs.MaterialDialog; +import de.danoeh.antennapod.activity.ImportExportActivity; import org.apache.commons.lang3.ArrayUtils; import java.io.File; @@ -90,6 +91,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc private static final String PREF_OPML_EXPORT = "prefOpmlExport"; private static final String PREF_HTML_EXPORT = "prefHtmlExport"; private static final String STATISTICS = "statistics"; + private static final String IMPORT_EXPORT = "importExport"; private static final String PREF_ABOUT = "prefAbout"; private static final String PREF_CHOOSE_DATA_DIR = "prefChooseDataDir"; private static final String AUTO_DL_PREF_SCREEN = "prefAutoDownloadSettings"; @@ -173,6 +175,12 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc return true; } ); + ui.findPreference(PreferenceController.IMPORT_EXPORT).setOnPreferenceClickListener( + preference -> { + activity.startActivity(new Intent(activity, ImportExportActivity.class)); + return true; + } + ); ui.findPreference(PreferenceController.PREF_OPML_EXPORT).setOnPreferenceClickListener( preference -> export(new OpmlWriter())); ui.findPreference(PreferenceController.PREF_HTML_EXPORT).setOnPreferenceClickListener( diff --git a/app/src/main/res/layout/import_export_activity.xml b/app/src/main/res/layout/import_export_activity.xml new file mode 100644 index 000000000..6614a8710 --- /dev/null +++ b/app/src/main/res/layout/import_export_activity.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + android:id="@+id/import_export_layout" + android:padding="8dp"> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/import_export_warning" + android:gravity="center_horizontal"/> + + <Button + android:text="@string/label_export" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/button_export" + android:layout_marginTop="24dp"/> + + <Button + android:text="@string/label_import" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/button_import"/> + +</LinearLayout> diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index e81115627..8ed7da731 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -316,6 +316,9 @@ android:key="prefHtmlExport" android:title="@string/html_export_label"/> <Preference + android:key="importExport" + android:title="@string/import_export"/> + <Preference android:key="statistics" android:title="@string/statistics_label"/> </PreferenceCategory> |