Skip to content

Commit

Permalink
Delay notification updates to 500 ms (fix #224)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Feb 22, 2015
1 parent 4ab8056 commit 8e3b8cd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
11 changes: 7 additions & 4 deletions app/src/main/java/org/kontalk/service/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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;
Expand Down Expand Up @@ -237,6 +241,7 @@ public void stopForeground() {

@Override
public void start(String url, File destination, long length) {
mUpdateTimer.reset();
startForeground(length);
}

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/org/kontalk/service/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -258,6 +262,7 @@ public void stopForeground() {

@Override
public void start(UploadConnection conn) {
mUpdateTimer.reset();
startForeground(mTotalBytes);
}

Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/kontalk/ui/MessagingNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions app/src/main/java/org/kontalk/util/StepTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Kontalk Android client
* Copyright (C) 2014 Kontalk Devteam <[email protected]>
* 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 <http://www.gnu.org/licenses/>.
*/

package org.kontalk.util;


import java.util.concurrent.TimeUnit;

/**
* Step timer mainly used to slow down notification updates.<br>
* 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;
}
}

0 comments on commit 8e3b8cd

Please sign in to comment.