Display notification when opml export is over

This commit is contained in:
Shinokuni 2019-11-09 16:38:30 +01:00
parent 3bef12e7b5
commit d2bca6f8e8
5 changed files with 52 additions and 25 deletions

View File

@ -2,6 +2,8 @@ package com.readrops.app.fragments.settings;
import android.Manifest; import android.Manifest;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
@ -9,10 +11,11 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders; import androidx.lifecycle.ViewModelProviders;
@ -26,6 +29,7 @@ import com.readrops.app.activities.ManageFeedsFoldersActivity;
import com.readrops.app.database.entities.account.Account; import com.readrops.app.database.entities.account.Account;
import com.readrops.app.database.entities.account.AccountType; import com.readrops.app.database.entities.account.AccountType;
import com.readrops.app.utils.OPMLMatcher; import com.readrops.app.utils.OPMLMatcher;
import com.readrops.app.utils.ReadropsApp;
import com.readrops.app.utils.Utils; import com.readrops.app.utils.Utils;
import com.readrops.app.viewmodels.AccountViewModel; import com.readrops.app.viewmodels.AccountViewModel;
import com.readrops.readropslibrary.opml.OPMLParser; import com.readrops.readropslibrary.opml.OPMLParser;
@ -153,13 +157,13 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show(); Utils.showSnackbar(getView(), e.getMessage());
} }
}))) })))
.show(); .show();
} }
// region opml parsing // region opml import
private void openOPMLFile() { private void openOPMLFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
@ -207,6 +211,14 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
}); });
} }
private void displayErrorMessage() {
new MaterialDialog.Builder(getActivity())
.title(R.string.processing_file_failed)
.neutralText(R.string.cancel)
.iconRes(R.drawable.ic_error)
.show();
}
//endregion //endregion
//region opml export //region opml export
@ -232,28 +244,44 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
displayErrorMessage(); Utils.showSnackbar(getView(), e.getMessage());
} }
}) })
.subscribe(new DisposableCompletableObserver() { .subscribe(new DisposableCompletableObserver() {
@Override @Override
public void onComplete() { public void onComplete() {
Log.d(TAG, "onComplete: "); displayNotification(file);
} }
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Utils.showSnackbar(getView(), e.getMessage());
} }
}); });
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
Utils.showSnackbar(getView(), e.getMessage());
displayErrorMessage();
} }
} }
private void displayNotification(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(file.getAbsolutePath()));
Notification notification = new NotificationCompat.Builder(getContext(), ReadropsApp.OPML_EXPORT_CHANNEL_ID)
.setContentTitle(getString(R.string.opml_export))
.setContentText(file.getName())
.setSmallIcon(R.drawable.ic_readrops)
.setContentIntent(PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setDeleteIntent(PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getContext());
manager.notify(2, notification);
}
private boolean isExternalStoragePermissionGranted() { private boolean isExternalStoragePermissionGranted() {
return ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; return ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
} }
@ -288,12 +316,4 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
} }
//endregion //endregion
private void displayErrorMessage() {
new MaterialDialog.Builder(getActivity())
.title(R.string.processing_file_failed)
.neutralText(R.string.cancel)
.iconRes(R.drawable.ic_error)
.show();
}
} }

View File

@ -14,28 +14,35 @@ import io.reactivex.plugins.RxJavaPlugins;
public class ReadropsApp extends Application { public class ReadropsApp extends Application {
public static final String FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel"; public static final String FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel";
public static final String OPML_EXPORT_CHANNEL_ID = "opmlExportChannel";
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
RxJavaPlugins.setErrorHandler(e -> { }); RxJavaPlugins.setErrorHandler(e -> {
});
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this); Stetho.initializeWithDefaults(this);
} }
createNotificationChannel(); createNotificationChannels();
} }
private void createNotificationChannel() { private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel feedsColorsChannel = new NotificationChannel(FEEDS_COLORS_CHANNEL_ID, NotificationChannel feedsColorsChannel = new NotificationChannel(FEEDS_COLORS_CHANNEL_ID,
getString(R.string.feeds_colors), NotificationManager.IMPORTANCE_DEFAULT); getString(R.string.feeds_colors), NotificationManager.IMPORTANCE_DEFAULT);
feedsColorsChannel.setDescription(getString(R.string.get_feeds_colors)); feedsColorsChannel.setDescription(getString(R.string.get_feeds_colors));
NotificationChannel opmlExportChannel = new NotificationChannel(OPML_EXPORT_CHANNEL_ID,
getString(R.string.opml_export), NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class); NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(feedsColorsChannel); manager.createNotificationChannel(feedsColorsChannel);
manager.createNotificationChannel(opmlExportChannel);
} }
} }
} }

View File

@ -97,8 +97,8 @@
<string name="opml_processing">Traitement du fichier OPML</string> <string name="opml_processing">Traitement du fichier OPML</string>
<string name="operation_takes_time">Cette opération peut prendre un certain temps car il faut interroger chaque flux.</string> <string name="operation_takes_time">Cette opération peut prendre un certain temps car il faut interroger chaque flux.</string>
<string name="processing_file_failed">Une erreur s\'est produite lors du traitement du fichier</string> <string name="processing_file_failed">Une erreur s\'est produite lors du traitement du fichier</string>
<string name="import_opml">Importer un fichier OPML</string> <string name="opml_import">Import OPML</string>
<string name="export_feeds_folders_opml">Exporter les flux et les dossiers dans un fichier OPML</string> <string name="opml_export">Export OPML</string>
<string name="external_storage_opml_export">L\'export des soubscriptions nécessite l\'accès au stockage</string> <string name="external_storage_opml_export">L\'export des soubscriptions nécessite l\'accès au stockage</string>
<string name="try_again">Réessayer</string> <string name="try_again">Réessayer</string>
<string name="permissions">Permissions</string> <string name="permissions">Permissions</string>

View File

@ -6,8 +6,8 @@
</string-array> </string-array>
<string-array name="opml_import_export"> <string-array name="opml_import_export">
<item>@string/import_opml</item> <item>@string/opml_import</item>
<item>@string/export_feeds_folders_opml</item> <item>@string/opml_export</item>
</string-array> </string-array>
<string-array name="items_per_feed_numbers_values"> <string-array name="items_per_feed_numbers_values">

View File

@ -105,8 +105,8 @@
<string name="opml_processing">Processing OPML file</string> <string name="opml_processing">Processing OPML file</string>
<string name="operation_takes_time">This operation can take a significant time as each feed needs to be queried.</string> <string name="operation_takes_time">This operation can take a significant time as each feed needs to be queried.</string>
<string name="processing_file_failed">An error occurred during the file processing</string> <string name="processing_file_failed">An error occurred during the file processing</string>
<string name="import_opml">Import OPML file</string> <string name="opml_import">OPML import</string>
<string name="export_feeds_folders_opml">Export feeds and folders to an OPML file</string> <string name="opml_export">OPML export</string>
<string name="subscriptions" translatable="false">Subscriptions</string> <string name="subscriptions" translatable="false">Subscriptions</string>
<string name="external_storage_opml_export">Subscriptions export needs external storage permission</string> <string name="external_storage_opml_export">Subscriptions export needs external storage permission</string>
<string name="try_again">Try again</string> <string name="try_again">Try again</string>