diff --git a/app/src/main/java/org/kontalk/service/DownloadService.java b/app/src/main/java/org/kontalk/service/DownloadService.java index cf48480a3..b62c0cbf5 100644 --- a/app/src/main/java/org/kontalk/service/DownloadService.java +++ b/app/src/main/java/org/kontalk/service/DownloadService.java @@ -63,10 +63,12 @@ import org.kontalk.ui.ProgressNotificationBuilder; import org.kontalk.util.MediaStorage; import org.kontalk.util.Preferences; +import org.kontalk.util.StepTimer; import static org.kontalk.ui.MessagingNotification.NOTIFICATION_ID_DOWNLOADING; import static org.kontalk.ui.MessagingNotification.NOTIFICATION_ID_DOWNLOAD_ERROR; import static org.kontalk.ui.MessagingNotification.NOTIFICATION_ID_DOWNLOAD_OK; +import static org.kontalk.ui.MessagingNotification.NOTIFICATION_UPDATE_DELAY; /** @@ -89,6 +91,8 @@ public class DownloadService extends IntentService implements DownloadListener { // data about the download currently being processed private Notification mCurrentNotification; private long mTotalBytes; + /** Step timer for notification updates. */ + private StepTimer mUpdateTimer = new StepTimer(NOTIFICATION_UPDATE_DELAY); private long mMessageId; private String mPeer; @@ -237,6 +241,7 @@ public void stopForeground() { @Override public void start(String url, File destination, long length) { + mUpdateTimer.reset(); startForeground(length); } @@ -374,14 +379,12 @@ private void errorNotification(String ticker, String text) { @Override public void progress(String url, File destination, long bytes) { - if (mCurrentNotification != null) { - int progress = (int)((100 * bytes) / mTotalBytes); + if (mCurrentNotification != null && (bytes >= mTotalBytes || mUpdateTimer.isStep())) { + int progress = (int) ((100 * bytes) / mTotalBytes); foregroundNotification(progress); // send the updates to the notification manager mNotificationManager.notify(NOTIFICATION_ID_DOWNLOADING, mCurrentNotification); } - - Thread.yield(); } public static boolean isQueued(String url) { diff --git a/app/src/main/java/org/kontalk/service/UploadService.java b/app/src/main/java/org/kontalk/service/UploadService.java index 96a31c5cb..30ed52f35 100644 --- a/app/src/main/java/org/kontalk/service/UploadService.java +++ b/app/src/main/java/org/kontalk/service/UploadService.java @@ -24,6 +24,7 @@ */ import static org.kontalk.ui.MessagingNotification.NOTIFICATION_ID_UPLOADING; import static org.kontalk.ui.MessagingNotification.NOTIFICATION_ID_UPLOAD_ERROR; +import static org.kontalk.ui.MessagingNotification.NOTIFICATION_UPDATE_DELAY; import java.io.File; import java.util.LinkedHashMap; @@ -39,6 +40,7 @@ import org.kontalk.upload.KontalkBoxUploadConnection; import org.kontalk.upload.UploadConnection; import org.kontalk.util.MediaStorage; +import org.kontalk.util.StepTimer; import android.app.IntentService; import android.app.Notification; @@ -89,6 +91,8 @@ public class UploadService extends IntentService implements ProgressListener { // data about the upload currently being processed private Notification mCurrentNotification; private long mTotalBytes; + /** Step timer for notification updates. */ + private StepTimer mUpdateTimer = new StepTimer(NOTIFICATION_UPDATE_DELAY); private long mMessageId; private UploadConnection mConn; @@ -258,6 +262,7 @@ public void stopForeground() { @Override public void start(UploadConnection conn) { + mUpdateTimer.reset(); startForeground(mTotalBytes); } @@ -306,15 +311,12 @@ public void progress(UploadConnection conn, long bytes) { mCanceled = true; } - //Log.v(TAG, "bytes = " + bytes); - if (mCurrentNotification != null) { + if (mCurrentNotification != null && (bytes >= mTotalBytes || mUpdateTimer.isStep())) { int progress = (int)((100 * bytes) / mTotalBytes); foregroundNotification(progress); // send the updates to the notification manager mNotificationManager.notify(NOTIFICATION_ID_UPLOADING, mCurrentNotification); } - - Thread.yield(); } public static boolean isQueued(String url) { diff --git a/app/src/main/java/org/kontalk/ui/MessagingNotification.java b/app/src/main/java/org/kontalk/ui/MessagingNotification.java index dfebb9295..7f24ac323 100644 --- a/app/src/main/java/org/kontalk/ui/MessagingNotification.java +++ b/app/src/main/java/org/kontalk/ui/MessagingNotification.java @@ -70,6 +70,9 @@ public class MessagingNotification { public static final int NOTIFICATION_ID_INVITATION = 109; public static final int NOTIFICATION_ID_AUTH_ERROR = 110; + /** Minimum delay for progress notification updates. */ + public static final int NOTIFICATION_UPDATE_DELAY = 500; + private static final String[] MESSAGES_UNREAD_PROJECTION = { Messages.THREAD_ID, diff --git a/app/src/main/java/org/kontalk/util/StepTimer.java b/app/src/main/java/org/kontalk/util/StepTimer.java new file mode 100644 index 000000000..8fdd6b8d2 --- /dev/null +++ b/app/src/main/java/org/kontalk/util/StepTimer.java @@ -0,0 +1,55 @@ +/* + * Kontalk Android client + * Copyright (C) 2014 Kontalk Devteam + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kontalk.util; + + +import java.util.concurrent.TimeUnit; + +/** + * Step timer mainly used to slow down notification updates.
+ * Uses {@link System#nanoTime()}. + * @author Daniele Ricci + */ +public class StepTimer { + + private final long mMinDelay; + + /** Marked timestamp. */ + private long mTimestamp; + /** Delay accumulator. */ + private long mDelay; + + public StepTimer(long delay) { + mMinDelay = delay; + } + + public void reset() { + mTimestamp = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); + } + + /** Returns true if the defined step delay has passed. */ + public boolean isStep() { + final long now = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); + long delay = now - mTimestamp; + if (delay < 1) delay = 1; + mDelay += delay; + mTimestamp = now; + return mDelay >= mMinDelay; + } +}