Merge remote-tracking branch 'origin/tizen_3.0' into tizen_4.0
[platform/core/api/webapi-plugins.git] / src / application / application_api.js
index 6613b4b..70586ee 100755 (executable)
@@ -26,6 +26,11 @@ var ApplicationControlLaunchMode = {
     GROUP: 'GROUP'
 };
 
+var ApplicationUsageMode = {
+    RECENTLY: 'RECENTLY',
+    FREQUENTLY: 'FREQUENTLY'
+};
+
 //  TODO: Please uncomment below lines when system events is ready
 //var SystemEvent = {
 //  BATTERY_CHARGER_STATUS: 'BATTERY_CHARGER_STATUS',
@@ -508,6 +513,170 @@ ApplicationManager.prototype.getAppMetaData = function() {
     }
 };
 
+ApplicationManager.prototype.getBatteryUsageInfo = function() {
+    var args = AV.validateMethod(arguments, [
+        {
+            name: 'successCallback',
+            type: AV.Types.FUNCTION
+        },
+        {
+            name: 'errorCallback',
+            type: AV.Types.FUNCTION,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'days',
+            type: AV.Types.LONG,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'limit',
+            type: AV.Types.LONG,
+            optional: true,
+            nullable: true
+        }
+    ]);
+
+    var callArgs = {};
+
+    if (!T.isNullOrUndefined(args.days)) {
+        callArgs.days = args.days;
+    }
+
+    if (!T.isNullOrUndefined(args.limit)) {
+        callArgs.limit = args.limit;
+    }
+
+    var callback = function(result) {
+        if (native.isFailure(result)) {
+            native.callIfPossible(args.errorCallback, native.getErrorObject(result));
+        } else {
+            var data = native.getResultObject(result);
+            var resultArray = [];
+            data.forEach(function(i) {
+                resultArray.push(new ApplicationBatteryUsage(i));
+            });
+            args.successCallback(resultArray);
+        }
+    };
+
+    var result = native.call(
+        'ApplicationManager_getBatteryUsageInfo',
+        callArgs,
+        callback
+    );
+    if (native.isFailure(result)) {
+        throw native.getErrorObject(result);
+    }
+};
+
+ApplicationManager.prototype.getAppsUsageInfo = function() {
+    var args = AV.validateMethod(arguments, [
+        {
+            name: 'successCallback',
+            type: AV.Types.FUNCTION
+        },
+        {
+            name: 'errorCallback',
+            type: AV.Types.FUNCTION,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'mode',
+            type: AV.Types.ENUM,
+            values: T.getValues(ApplicationUsageMode),
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'filter',
+            type: AV.Types.DICTIONARY,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'limit',
+            type: AV.Types.LONG,
+            optional: true,
+            nullable: true
+        }
+    ]);
+
+    var callArgs = {};
+
+    if (!T.isNullOrUndefined(args.mode)) {
+        callArgs.mode = args.mode;
+    }
+
+    if (!T.isNullOrUndefined(args.filter) && typeof args.filter !== 'object') {
+        setTimeout(function() {
+            native.callIfPossible(
+                args.errorCallback,
+                new WebAPIException(
+                    WebAPIException.INVALID_VALUES_ERR,
+                    'filter must be an object.'
+                )
+            );
+        }, 0);
+        return;
+    }
+
+    callArgs.filter = {};
+    if (!T.isNullOrUndefined(args.filter)) {
+        var filter = args.filter;
+        if (!T.isNullOrUndefined(filter.timeSpan)) {
+            callArgs.filter.timeSpan = Converter.toLong(filter.timeSpan);
+        } else {
+            if (!T.isNullOrUndefined(filter.startTime)) {
+                if (filter.startTime instanceof Date) {
+                    callArgs.filter.startTime = filter.startTime.getTime() / 1000;
+                } else {
+                    throw new WebAPIException(
+                        WebAPIException.TYPE_MISMATCH_ERR,
+                        'startTime given with invalid type.'
+                    );
+                }
+            }
+
+            if (!T.isNullOrUndefined(filter.endTime)) {
+                if (filter.endTime instanceof Date) {
+                    callArgs.filter.endTime = filter.endTime.getTime() / 1000;
+                } else {
+                    throw new WebAPIException(
+                        WebAPIException.TYPE_MISMATCH_ERR,
+                        'endTime given with invalid type.'
+                    );
+                }
+            }
+        }
+    }
+
+    if (!T.isNullOrUndefined(args.limit)) {
+        callArgs.limit = args.limit;
+    }
+
+    var callback = function(result) {
+        if (native.isFailure(result)) {
+            native.callIfPossible(args.errorCallback, native.getErrorObject(result));
+        } else {
+            var data = native.getResultObject(result);
+            var resultArray = [];
+            data.forEach(function(i) {
+                resultArray.push(new ApplicationUsage(i));
+            });
+            args.successCallback(resultArray);
+        }
+    };
+
+    var result = native.call('ApplicationManager_getAppsUsageInfo', callArgs, callback);
+    if (native.isFailure(result)) {
+        throw native.getErrorObject(result);
+    }
+};
+
 function ListenerManager(native, listenerName) {
     this.listeners = {};
     this.nextId = 1;
@@ -617,6 +786,120 @@ ApplicationManager.prototype.removeAppInfoEventListener = function() {
     applicationEventListener.removeListener(args.watchId);
 };
 
+function StatusListenerManager(native, listenerName) {
+    this.listeners = {};
+    this.listenersCount = 0;
+    this.nextId = 1;
+    this.nativeSet = false;
+    this.native = native;
+    this.listenerName = listenerName;
+}
+
+StatusListenerManager.prototype.onListenerCalled = function(msg) {
+    var statusType = msg.statusType;
+    var app_id = msg.appId;
+
+    for (var watchId in this.listeners) {
+        if (this.listeners.hasOwnProperty(watchId)) {
+            var listener = this.listeners[watchId];
+            if (!listener.appId || listener.appId === app_id) {
+                listener.callback(app_id, statusType);
+            }
+        }
+    }
+};
+
+StatusListenerManager.prototype.addListener = function(callback, appId) {
+    if (!this.nativeSet) {
+        var result = this.native.callSync(
+            'ApplicationManager_addAppStatusChangeListener'
+        );
+        if (this.native.isFailure(result)) {
+            throw this.native.getErrorObject(result);
+        }
+
+        this.native.addListener(this.listenerName, this.onListenerCalled.bind(this));
+        this.nativeSet = true;
+    }
+
+    var listener = {
+        callback: callback,
+        appId: appId
+    };
+
+    var id = this.nextId++;
+    this.listeners[id] = listener;
+    this.listenersCount++;
+
+    return id;
+};
+
+StatusListenerManager.prototype.removeListener = function(watchId) {
+    if (this.listeners.hasOwnProperty(watchId)) {
+        if (this.listenersCount > 1) {
+            delete this.listeners[watchId];
+            this.listenersCount--;
+            return;
+        }
+
+        if (this.nativeSet) {
+            var result = this.native.callSync(
+                'ApplicationManager_removeStatusChangeListener'
+            );
+            if (this.native.isFailure(result)) {
+                throw this.native.getErrorObject(result);
+            }
+
+            delete this.listeners[watchId];
+            this.listenersCount--;
+
+            this.native.removeListener(this.listenerName);
+            this.nativeSet = false;
+        }
+    }
+};
+
+var APP_STATUS_CHANGE_LISTENER = 'AppStatusChangeListener';
+var appStatusChangeListener = new StatusListenerManager(
+    native,
+    APP_STATUS_CHANGE_LISTENER
+);
+
+ApplicationManager.prototype.addAppStatusChangeListener = function() {
+    var args = AV.validateMethod(arguments, [
+        {
+            name: 'statusChangeListener',
+            type: AV.Types.FUNCTION
+        },
+        {
+            name: 'appId',
+            type: AV.Types.STRING,
+            optional: true,
+            nullable: true
+        }
+    ]);
+
+    if (args.appId !== undefined && args.appId !== null && !args.appId.length) {
+        throw new WebAPIException(
+            WebAPIException.INVALID_VALUES_ERR,
+            'Application id is empty'
+        );
+    }
+
+    return appStatusChangeListener.addListener(args.statusChangeListener, args.appId);
+};
+
+ApplicationManager.prototype.removeAppStatusChangeListener = function() {
+    var args = AV.validateMethod(arguments, [
+        {
+            name: 'watchId',
+            type: AV.Types.LONG
+        }
+    ]);
+
+    appStatusChangeListener.removeListener(args.watchId);
+};
+
 // class Application ////////////////////////////////////////////////////
 
 function Application(data) {
@@ -1135,7 +1418,7 @@ function ApplicationCertificate(data) {
     });
 }
 
-// class ApplicationMetaData ////////////////////////////////////////////////////
+// class ApplicationMetaData ///////////////////////////////////////////////////////
 function ApplicationMetaData(data) {
     Object.defineProperties(this, {
         key: {
@@ -1151,5 +1434,47 @@ function ApplicationMetaData(data) {
     });
 }
 
+//class ApplicationBatteryUsage ////////////////////////////////////////////////////
+function ApplicationBatteryUsage(data) {
+    Object.defineProperties(this, {
+        appId: {
+            value: data.appId,
+            writable: false,
+            enumerable: true
+        },
+        batteryUsage: {
+            value: data.batteryUsage,
+            writable: false,
+            enumerable: true
+        }
+    });
+}
+
+//class ApplicationUsage ////////////////////////////////////////////////////////
+function ApplicationUsage(data) {
+    Object.defineProperties(this, {
+        appId: {
+            value: data.appId,
+            writable: false,
+            enumerable: true
+        },
+        totalCount: {
+            value: data.totalCount,
+            writable: false,
+            enumerable: true
+        },
+        totalDuration: {
+            value: data.totalDuration,
+            writable: false,
+            enumerable: true
+        },
+        lastTime: {
+            value: new Date(data.lastTime * 1000),
+            writable: false,
+            enumerable: true
+        }
+    });
+}
+
 // exports ////////////////////////////////////////////////////
 exports = new ApplicationManager();