Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Bug 1221929 - [TV][System] Track the last app before turning the device off. #33359

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions tv_apps/smart-system/js/app_usage_metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
const ENABLED = 'applicationenabled';
const DISABLED = 'applicationdisabled';
const UNDERLAYOPENED = 'homescreen-underlayopened';
const REQUESTSHUTDOWN = 'requestshutdown';
const BATTERYSHUTDOWN = 'batteryshutdown';

// This is the list of event types we register handlers for
const EVENT_TYPES = [
Expand All @@ -97,7 +99,9 @@
IACMETRICS,
ENABLED,
DISABLED,
UNDERLAYOPENED
UNDERLAYOPENED,
REQUESTSHUTDOWN,
BATTERYSHUTDOWN
];


Expand Down Expand Up @@ -386,6 +390,18 @@
// Register for idle events
navigator.addIdleObserver(self.idleObserver);

// Register for poweroff events
var mozPower = navigator.mozPower;
if (!mozPower.addPowerStateListener) {
// XXX: Mocked addPowerStateListener for testing.
mozPower = {
addPowerStateListener: function(cb) {
setTimeout(cb, 10000, 'on-powerstate');
setTimeout(cb, 30000, 'off-powerstate');
}
};
}
mozPower.addPowerStateListener(self.onPowerStateChange.bind(self));
if (done) {
done();
}
Expand Down Expand Up @@ -429,25 +445,31 @@
AUM.prototype.handleEvent = function handleEvent(e) {
var now = performance.now();
debug('got an event: ', e.type);
switch (e.type) {

case APPOPENED:
// The user has opened an app or switched apps.
// Record data about the app that was running and then
// update the currently running app.
if (this._appIsUnderlay) {
var self = this;
function recordUnderlayAndHome() {
if (self._appIsUnderlay) {
// In this case, user launched an app from Home app while Home is
// overlaying on an app. We increase invocation count of underlying app
// without recording any usage time (since the usage time before home
// covers on has already recorded in HOMESCREEN event).
this.metrics.recordInvocation(this.getCurrentApp(), 0);
this.metrics.recordUsageTime(homescreenWindowManager.getHomescreen(),
now - this.getCurrentStartTime());
this._appIsUnderlay = false;
self.metrics.recordInvocation(self.getCurrentApp(), 0);
self.metrics.recordUsageTime(homescreenWindowManager.getHomescreen(),
now - self.getCurrentStartTime());
self._appIsUnderlay = false;
} else {
this.metrics.recordInvocation(this.getCurrentApp(),
now - this.getCurrentStartTime());
self.metrics.recordInvocation(self.getCurrentApp(),
now - self.getCurrentStartTime());
}
}

switch (e.type) {

case APPOPENED:
// The user has opened an app or switched apps.
// Record data about the app that was running and then
// update the currently running app.
recordUnderlayAndHome();

this.attentionWindows = [];
this.currentApp = e.detail;
Expand Down Expand Up @@ -592,6 +614,14 @@
this.idle = true;
break;

case REQUESTSHUTDOWN:
case BATTERYSHUTDOWN:
recordUnderlayAndHome();

// Set idle to true to make sure calling metrics.save().
this.idle = true;
break;

case ACTIVE:
this.idle = false;
break;
Expand Down Expand Up @@ -760,6 +790,15 @@
}
};

AUM.prototype.onPowerStateChange = function onPowerStateChange(state) {
if (state === 'off-powerstate') {
this.handleEvent({
type: REQUESTSHUTDOWN,
detail: this
});
}
};

/*
* A helper class that holds (and persists) a batch of app usage data
*/
Expand Down