mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Fix to make notifications not crash in 2.3
This removes the progressbar from the notifications on 2.3, which is much less than ideal. However after blindly debugging for about a week, I couldn't really figure out a way to make custom views in Notifications work across both platforms. So this will have to do until I figure a proper way out.
This commit is contained in:
parent
5396541f4e
commit
1ec9be01f8
2 changed files with 20 additions and 89 deletions
|
|
@ -1,61 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:orientation="horizontal"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="fill_parent"
|
|
||||||
android:padding="10dp" >
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/uploadNotificationIcon"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="fill_parent"
|
|
||||||
android:src="@drawable/ic_launcher"
|
|
||||||
android:layout_marginRight="10dp" />
|
|
||||||
|
|
||||||
<LinearLayout android:orientation="vertical"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/uploadNotificationTitle"
|
|
||||||
style="@style/NotificationTitle"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="Uploading image to commons"
|
|
||||||
android:singleLine="true"
|
|
||||||
android:marqueeRepeatLimit="marquee_forever"
|
|
||||||
android:scrollHorizontally="true"
|
|
||||||
android:focusable="true"
|
|
||||||
android:focusableInTouchMode="true"
|
|
||||||
android:ellipsize="marquee"/>
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="horizontal"
|
|
||||||
>
|
|
||||||
<ProgressBar
|
|
||||||
android:id="@+id/uploadNotificationProgress"
|
|
||||||
android:layout_width="0px"
|
|
||||||
android:layout_height="fill_parent"
|
|
||||||
android:indeterminate="false"
|
|
||||||
android:indeterminateOnly="false"
|
|
||||||
android:layout_weight="1"
|
|
||||||
style="@style/NotificationProgress" >
|
|
||||||
|
|
||||||
</ProgressBar>
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/uploadNotificationsCount"
|
|
||||||
android:layout_height="fill_parent"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:singleLine="true"
|
|
||||||
android:focusable="true"
|
|
||||||
android:focusableInTouchMode="true"
|
|
||||||
style="@style/NotificationText"
|
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
/>
|
|
||||||
</LinearLayout>
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
@ -3,6 +3,7 @@ package org.wikimedia.commons;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
import org.mediawiki.api.*;
|
import org.mediawiki.api.*;
|
||||||
import org.wikimedia.commons.contributions.Contribution;
|
import org.wikimedia.commons.contributions.Contribution;
|
||||||
import org.wikimedia.commons.contributions.ContributionsActivity;
|
import org.wikimedia.commons.contributions.ContributionsActivity;
|
||||||
|
|
@ -34,7 +35,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
private ContentProviderClient contributionsProviderClient;
|
private ContentProviderClient contributionsProviderClient;
|
||||||
private CommonsApplication app;
|
private CommonsApplication app;
|
||||||
|
|
||||||
private Notification curProgressNotification;
|
private NotificationCompat.Builder curProgressNotification;
|
||||||
|
|
||||||
private int toUpload;
|
private int toUpload;
|
||||||
|
|
||||||
|
|
@ -47,7 +48,6 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
|
|
||||||
private class NotificationUpdateProgressListener implements ProgressListener {
|
private class NotificationUpdateProgressListener implements ProgressListener {
|
||||||
|
|
||||||
Notification curNotification;
|
|
||||||
String notificationTag;
|
String notificationTag;
|
||||||
boolean notificationTitleChanged;
|
boolean notificationTitleChanged;
|
||||||
Contribution contribution;
|
Contribution contribution;
|
||||||
|
|
@ -55,8 +55,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
String notificationProgressTitle;
|
String notificationProgressTitle;
|
||||||
String notificationFinishingTitle;
|
String notificationFinishingTitle;
|
||||||
|
|
||||||
public NotificationUpdateProgressListener(Notification curNotification, String notificationTag, String notificationProgressTitle, String notificationFinishingTitle, Contribution contribution) {
|
public NotificationUpdateProgressListener(String notificationTag, String notificationProgressTitle, String notificationFinishingTitle, Contribution contribution) {
|
||||||
this.curNotification = curNotification;
|
|
||||||
this.notificationTag = notificationTag;
|
this.notificationTag = notificationTag;
|
||||||
this.notificationProgressTitle = notificationProgressTitle;
|
this.notificationProgressTitle = notificationProgressTitle;
|
||||||
this.notificationFinishingTitle = notificationFinishingTitle;
|
this.notificationFinishingTitle = notificationFinishingTitle;
|
||||||
|
|
@ -66,24 +65,20 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
@Override
|
@Override
|
||||||
public void onProgress(long transferred, long total) {
|
public void onProgress(long transferred, long total) {
|
||||||
Log.d("Commons", String.format("Uploaded %d of %d", transferred, total));
|
Log.d("Commons", String.format("Uploaded %d of %d", transferred, total));
|
||||||
RemoteViews curView = curNotification.contentView;
|
|
||||||
if(!notificationTitleChanged) {
|
if(!notificationTitleChanged) {
|
||||||
curView.setTextViewText(R.id.uploadNotificationTitle, notificationProgressTitle);
|
curProgressNotification.setContentTitle(notificationProgressTitle);
|
||||||
if(toUpload != 1) {
|
Log.d("Commons", String.format("%d uploads left", toUpload));
|
||||||
curView.setTextViewText(R.id.uploadNotificationsCount, String.format(getString(R.string.uploads_pending_notification_indicator), toUpload));
|
|
||||||
Log.d("Commons", String.format("%d uploads left", toUpload));
|
|
||||||
}
|
|
||||||
notificationTitleChanged = true;
|
notificationTitleChanged = true;
|
||||||
contribution.setState(Contribution.STATE_IN_PROGRESS);
|
contribution.setState(Contribution.STATE_IN_PROGRESS);
|
||||||
}
|
}
|
||||||
if(transferred == total) {
|
if(transferred == total) {
|
||||||
// Completed!
|
// Completed!
|
||||||
curView.setTextViewText(R.id.uploadNotificationTitle, notificationFinishingTitle);
|
curProgressNotification.setContentTitle(notificationFinishingTitle);
|
||||||
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification);
|
curProgressNotification.setProgress(0, 100, true);
|
||||||
} else {
|
} else {
|
||||||
curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, (int) (((double) transferred / (double) total) * 100), false);
|
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
|
||||||
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curNotification);
|
|
||||||
}
|
}
|
||||||
|
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
||||||
contribution.setTransferred(transferred);
|
contribution.setTransferred(transferred);
|
||||||
contribution.save();
|
contribution.save();
|
||||||
}
|
}
|
||||||
|
|
@ -129,9 +124,9 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
contribution.save();
|
contribution.save();
|
||||||
toUpload++;
|
toUpload++;
|
||||||
if (curProgressNotification != null && toUpload != 1) {
|
if (curProgressNotification != null && toUpload != 1) {
|
||||||
curProgressNotification.contentView.setTextViewText(R.id.uploadNotificationsCount, String.format(getString(R.string.uploads_pending_notification_indicator), toUpload));
|
curProgressNotification.setContentText(String.format(getString(R.string.uploads_pending_notification_indicator), toUpload));
|
||||||
Log.d("Commons", String.format("%d uploads left", toUpload));
|
Log.d("Commons", String.format("%d uploads left", toUpload));
|
||||||
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
|
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
super.queue(what, contribution);
|
super.queue(what, contribution);
|
||||||
|
|
@ -166,7 +161,6 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
MWApi api = app.getApi();
|
MWApi api = app.getApi();
|
||||||
|
|
||||||
ApiResult result;
|
ApiResult result;
|
||||||
RemoteViews notificationView;
|
|
||||||
InputStream file = null;
|
InputStream file = null;
|
||||||
|
|
||||||
String notificationTag = contribution.getLocalUri().toString();
|
String notificationTag = contribution.getLocalUri().toString();
|
||||||
|
|
@ -178,21 +172,19 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationView = new RemoteViews(getPackageName(), R.layout.layout_upload_progress);
|
|
||||||
notificationView.setTextViewText(R.id.uploadNotificationTitle, String.format(getString(R.string.upload_progress_notification_title_start), contribution.getFilename()));
|
|
||||||
notificationView.setProgressBar(R.id.uploadNotificationProgress, 100, 0, false);
|
|
||||||
|
|
||||||
Log.d("Commons", "Before execution!");
|
Log.d("Commons", "Before execution!");
|
||||||
curProgressNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
|
curProgressNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
.setSmallIcon(R.drawable.ic_launcher)
|
||||||
|
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
.setContent(notificationView)
|
.setContentTitle(String.format(getString(R.string.upload_progress_notification_title_start), contribution.getFilename()))
|
||||||
|
.setContentText(String.format(getString(R.string.uploads_pending_notification_indicator), toUpload))
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ContributionsActivity.class), 0))
|
.setProgress(100, 0, true)
|
||||||
.setTicker(String.format(getString(R.string.upload_progress_notification_title_in_progress), contribution.getFilename()))
|
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(this, ContributionsActivity.class), 0))
|
||||||
.getNotification();
|
.setTicker(String.format(getString(R.string.upload_progress_notification_title_in_progress), contribution.getFilename()));
|
||||||
|
|
||||||
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
|
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(!api.validateLogin()) {
|
if(!api.validateLogin()) {
|
||||||
|
|
@ -208,7 +200,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(curProgressNotification, notificationTag,
|
NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(notificationTag,
|
||||||
String.format(getString(R.string.upload_progress_notification_title_in_progress), contribution.getFilename()),
|
String.format(getString(R.string.upload_progress_notification_title_in_progress), contribution.getFilename()),
|
||||||
String.format(getString(R.string.upload_progress_notification_title_finishing), contribution.getFilename()),
|
String.format(getString(R.string.upload_progress_notification_title_finishing), contribution.getFilename()),
|
||||||
contribution
|
contribution
|
||||||
|
|
@ -266,7 +258,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
.setTicker(String.format(getString(R.string.upload_failed_notification_title), contribution.getFilename()))
|
.setTicker(String.format(getString(R.string.upload_failed_notification_title), contribution.getFilename()))
|
||||||
.setContentTitle(String.format(getString(R.string.upload_failed_notification_title), contribution.getFilename()))
|
.setContentTitle(String.format(getString(R.string.upload_failed_notification_title), contribution.getFilename()))
|
||||||
.setContentText(getString(R.string.upload_failed_notification_subtitle))
|
.setContentText(getString(R.string.upload_failed_notification_subtitle))
|
||||||
.getNotification();
|
.build();
|
||||||
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
|
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
|
||||||
|
|
||||||
contribution.setState(Contribution.STATE_FAILED);
|
contribution.setState(Contribution.STATE_FAILED);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue