Use NotificationChannel and prevent crash in API 26 (#1835)

This commit is contained in:
Yusuke Matsubara 2018-08-19 19:19:00 +09:00 committed by neslihanturan
parent 7a43c59c80
commit 6842420aeb
4 changed files with 44 additions and 16 deletions

View file

@ -1,8 +1,12 @@
package fr.free.nrw.commons;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.multidex.MultiDexApplication;
import com.facebook.drawee.backends.pipeline.Fresco;
@ -60,6 +64,8 @@ public class CommonsApplication extends MultiDexApplication {
public static final String LOGS_PRIVATE_EMAIL_SUBJECT = "Commons Android App (%s) Logs";
public static final String NOTIFICATION_CHANNEL_ID_ALL = "CommonsNotificationAll";
private RefWatcher refWatcher;
@ -92,10 +98,23 @@ public class CommonsApplication extends MultiDexApplication {
Stetho.initializeWithDefaults(this);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
createNotificationChannel();
}
// Fire progress callbacks for every 3% of uploaded content
System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0");
}
@RequiresApi(26)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID_ALL,
getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_NONE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
}
/**
* Helps in setting up LeakCanary library

View file

@ -14,6 +14,7 @@ import java.util.Locale;
import javax.inject.Inject;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.SessionManager;
@ -44,14 +45,17 @@ public class DeleteTask extends AsyncTask<Void, Integer, Boolean> {
}
@Override
protected void onPreExecute(){
protected void onPreExecute() {
ApplicationlessInjection
.getInstance(context.getApplicationContext())
.getCommonsApplicationComponent()
.inject(this);
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(context);
notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(
context,
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER,0,0);
toast = Toast.makeText(context,"Trying to nominate "+media.getDisplayTitle()+ " for deletion",Toast.LENGTH_SHORT);

View file

@ -10,7 +10,6 @@ import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
@ -27,6 +26,7 @@ import java.util.regex.Pattern;
import javax.inject.Inject;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.HandlerService;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
@ -35,7 +35,6 @@ import fr.free.nrw.commons.contributions.Contribution;
import fr.free.nrw.commons.contributions.ContributionDao;
import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.mwapi.UploadResult;
import fr.free.nrw.commons.wikidata.WikidataEditService;
@ -182,6 +181,19 @@ public class UploadService extends HandlerService<Contribution> {
}
@SuppressLint("StringFormatInvalid")
private NotificationCompat.Builder getNotificationBuilder(Contribution contribution, String channelId) {
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
.setOngoing(true)
.setProgress(100, 0, true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ContributionsActivity.class), 0))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
}
private void uploadContribution(Contribution contribution) {
InputStream fileInputStream;
@ -207,17 +219,9 @@ public class UploadService extends HandlerService<Contribution> {
}
Timber.d("Before execution!");
curProgressNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
.setOngoing(true)
.setProgress(100, 0, true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ContributionsActivity.class), 0))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
curProgressNotification = getNotificationBuilder(
contribution,
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
String filename = null;

View file

@ -349,4 +349,5 @@
<string name="images_uploaded_explanation">The number of images you have uploaded to Commons, via any upload software</string>
<string name="images_reverted_explanation">The percentage of images you have uploaded to Commons that were not deleted</string>
<string name="images_used_explanation">The number of images you have uploaded to Commons that were used in Wikimedia articles</string>
<string name="notifications_channel_name_all">Commons Notification</string>
</resources>