Bug 476254 - Send a notification once a day when the user goes idle for some time. r=roc, sr=roc

This commit is contained in:
Edward Lee 2009-01-31 22:58:34 -06:00
Родитель 8d15eb61af
Коммит e4c58c88f8
2 изменённых файлов: 54 добавлений и 7 удалений

Просмотреть файл

@ -41,6 +41,9 @@
#include "nsIdleService.h"
#include "nsString.h"
#include "nsIObserverService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsIServiceManager.h"
#include "nsDebug.h"
#include "nsCOMArray.h"
@ -48,9 +51,12 @@
// observer topics used:
#define OBSERVER_TOPIC_IDLE "idle"
#define OBSERVER_TOPIC_BACK "back"
#define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
// interval in milliseconds between internal idle time requests
#define MIN_IDLE_POLL_INTERVAL 5000
#define MAX_IDLE_POLL_INTERVAL 300000
// Pref for last time (seconds since epoch) daily notification was sent
#define PREF_LAST_DAILY "idle.lastDailyNotification"
// Use this to find previously added observers in our array:
class IdleListenerComparator
@ -65,13 +71,14 @@ public:
nsIdleService::nsIdleService()
{
// Immediately create a timer to handle the daily notification
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
StartTimer(MAX_IDLE_POLL_INTERVAL);
}
nsIdleService::~nsIdleService()
{
if (mTimer) {
mTimer->Cancel();
}
StopTimer();
}
NS_IMETHODIMP
@ -109,8 +116,8 @@ nsIdleService::RemoveIdleObserver(nsIObserver* aObserver, PRUint32 aTime)
// Find the entry and remove it:
IdleListenerComparator c;
if (mArrayListeners.RemoveElement(listener, c)) {
if (mTimer && mArrayListeners.IsEmpty()) {
mTimer->Cancel();
if (mArrayListeners.IsEmpty()) {
StopTimer();
}
return NS_OK;
}
@ -187,10 +194,48 @@ nsIdleService::CheckAwayState()
hereListeners[i]->Observe(this, OBSERVER_TOPIC_BACK, timeStr.get());
}
// The user has been idle for a while, so try sending the daily idle
if (idleTime >= MAX_IDLE_POLL_INTERVAL) {
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (pref) {
// Get the current number of seconds since epoch
PRUint32 nowSec = PR_Now() / PR_USEC_PER_SEC;
// Get the last notification time; default to 0 for the first time
PRInt32 lastDaily = 0;
pref->GetIntPref(PREF_LAST_DAILY, &lastDaily);
// Has it been a day (24*60*60 seconds) since the last notification
if (nowSec - lastDaily > 86400) {
nsCOMPtr<nsIObserverService> observerService =
do_GetService("@mozilla.org/observer-service;1");
observerService->NotifyObservers(nsnull,
OBSERVER_TOPIC_IDLE_DAILY,
nsnull);
pref->SetIntPref(PREF_LAST_DAILY, nowSec);
}
}
}
// Restart the timer with the dynamically optimized poll time
StartTimer(nextPoll);
}
void
nsIdleService::StartTimer(PRUint32 aDelay)
{
if (mTimer) {
mTimer->Cancel();
mTimer->InitWithFuncCallback(IdleTimerCallback, this, nextPoll,
StopTimer();
mTimer->InitWithFuncCallback(IdleTimerCallback, this, aDelay,
nsITimer::TYPE_ONE_SHOT);
}
}
void
nsIdleService::StopTimer()
{
if (mTimer) {
mTimer->Cancel();
}
}

Просмотреть файл

@ -77,6 +77,8 @@ protected:
~nsIdleService();
private:
void StartTimer(PRUint32 aDelay);
void StopTimer();
nsCOMPtr<nsITimer> mTimer;
nsTArray<IdleListener> mArrayListeners;
};