Commit 1a6ebf04 authored by Sebastián Katzer's avatar Sebastián Katzer
Browse files

Initial windows support (not yet complete)

parent 5dc8b661
Loading
Loading
Loading
Loading
+21 −0
Changes for plugin.xml: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -199,4 +199,25 @@

    </platform>

    <!-- windows -->
    <platform name="windows">

        <js-module src="src/windows/LocalNotificationProxy.js" name="LocalNotification.Proxy" >
            <merges target="" />
        </js-module>

        <js-module src="src/windows/LocalNotificationCore.js" name="LocalNotification.Proxy.Core" >
            <merges target="" />
        </js-module>

        <js-module src="src/windows/LocalNotificationUtil.js" name="LocalNotification.Proxy.Util" >
            <merges target="" />
        </js-module>

        <!-- Platform Hooks -->
        <hook type="after_platform_add" src="scripts/windows/setToastCapable.js" />
        <hook type="after_plugin_install" src="scripts/windows/setToastCapable.js" />

    </platform>

</plugin>
+33 −0
Changes for scripts/windows/setToastCapable.js: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env node

// This Plugin-Hook sets ToastCapable on true to allow windows-platform
// cordova apps displaing local-notifications

var fs = require('fs'),
    rootdir = process.argv[2];

if (!rootdir)
    return;

function replace_string_in_file (filename, to_replace, replace_with) {
    var data = fs.readFileSync(filename, 'utf8'),
        result;

    if (data.indexOf(replace_with) > -1)
        return;

    result = data.replace(new RegExp(to_replace, 'g'), replace_with);

    fs.writeFileSync(filename, result, 'utf8');
}


var manifests = [
    'platforms/windows/package.phone.appxmanifest',
    'platforms/windows/package.windows.appxmanifest',
    'platforms/windows/package.windows80.appxmanifest'
];

for (var i = 0; i < manifests.length; i++) {
    replace_string_in_file(manifests[i], '<m3:VisualElements ', '<m3:VisualElements ToastCapable="true" ');
}
+382 −0
Changes for src/windows/LocalNotificationCore.js: 382 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
    Copyright 2013-2015 appPlant UG

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/


var Notifications = Windows.UI.Notifications,
    applicationData = Windows.Storage.ApplicationData.current,
    localSettings = applicationData.localSettings;


exports.core = {

    // Array of unactivated notifications
    unactivatedIds: [],

    /**
     * Schedules new local notifications.
     *
     * @param {Object[]} notifications
     *      Array of local notifications
     * @param {String} event
     *      'schedule' or 'update'
     */
    schedule: function (notifications) {

        var triggerFn = function (notification) {
            this.fireEvent('trigger', notification);
        };

        for (var i = 0; i < notifications.length; i++) {
            var options = notifications[i],
                notification = this.build(options);

            this.cancelLocalNotification(options.id);

            this.scheduleLocalNotification(notification, options);
            this.scheduleBackupNotification(notification, options);

            this.fireEvent('schedule', options);
            this.callOnTrigger(options, triggerFn);
        }
    },

    /**
     * Schedules a single local notification.
     *
     * @param {Windows.Data.Xml.Dom.XmlDocument} notification
     *      The local notification
     * @param {Object} options
     *      Local notification properties
     */
    scheduleLocalNotification: function (notification, options) {
        var interval = this.getRepeatInterval(options.every),
            triggerTime = new Date((options.at * 1000)),
            now = new Date().getTime(),
            toast;

        if (triggerTime <= now) {
            triggerTime = new Date(now + 10);
        }

        try {
            if (interval !== 0 && interval < 360001 && interval > 59999) {
                toast = new Notifications.ScheduledToastNotification(
                    notification, triggerTime, interval, 5);
            } else {
                toast = new Notifications.ScheduledToastNotification(
                    notification, triggerTime);
            }
        } catch (e) {
            console.error(e);
            return;
        }

        toast.id = options.id;
        toast.tag = 'Toast' + toast.id;

        Notifications.ToastNotificationManager
            .createToastNotifier()
            .addToSchedule(toast);
    },

    /**
     * Schedules a backup local notification 10 years later.
     *
     * @param {Object} notification
     *      The local notification
     */
    scheduleBackupNotification: function (notification, options) {
        var properties = Object.create(options);

        properties.id = options.id + '-2';
        properties.at = options.at + 315360000; // 10 years later

        this.scheduleLocalNotification(notification, properties);
    },

    /**
     * Updates a single local notification.
     *
     * @param {Object} notification
     *      The local notification
     * @param {Object} updates
     *      Updated properties
     */
    updateLocalNotification: function (notification, updates) {

    },

    /**
     * Cancels the local notification with the given ID.
     *
     * @param {String} id
     *      Local notification ID
     */
    cancelLocalNotification: function (id) {
        var notifier = this.getToastNotifier(),
            history = this.getToastHistory(),
            toasts = this.getScheduledToast();

        history.remove('Toast' + id);

        for (var i = 0; i < toasts.length; i++) {
            var toast = toasts[i];

            if (toast.id == id || toast.id == id + '-2') {
                notifier.removeFromSchedule(toast);
            }
        }
    },

    /**
     * Clears the local notification with the given ID.
     *
     * @param {String} id
     *      Local notification ID
     */
    clearLocalNotification: function (id) {
        this.getToastHistory().remove('Toast' + id);

        if (this.isTriggered(id) && !this.isScheduled(id)) {
            this.cancelLocalNotification(id);
        }
    },

    /**
     * Updates existing notifications specified by IDs in options.
     *
     * @param {Object[]} notifications
     *      Array of local notifications
     */
    update: function (notifications) {
        for (var i = 0; i < notifications.length; i++) {
            var updates = notifications[i],
                options = getAll(updates.id || '0')[0];

            this.updateLocalNotification(options, updates);
            this.fireEvent('update', options);
        }
    },

    /**
     * Clears the specified notifications.
     *
     * @param {int[]} ids
     *      List of local notification IDs
     */
    clear: function (ids) {
        for (var i = 0; i < ids.length; i++) {
            var notification = this.getAll([id])[0];

            this.clearLocalNotification(ids[i]);
            this.fireEvent('clear', notification);
        }
    },

    /**
     * Clears all notifications.
     */
    clearAll: function () {
        var ids = this.getTriggeredIds();

        for (var i = 0; i < ids.length; i++) {
            this.clearLocalNotification(ids[i]);
        }

        this.getToastHistory().clear();
        this.fireEvent('clearall');
    },

    /**
     * Cancels all specified notifications.
     *
     * @param {int[]} ids
     *      List of local notification IDs
     */
    cancel: function (ids) {
        for (var i = 0; i < ids.length; i++) {
            var notification = this.getAll([id])[0];

            this.cancelLocalNotification(ids[i]);
            this.fireEvent('cancel', notification);
        }
    },

    /**
     * Cancels all notifications.
     */
    cancelAll: function () {
        var ids = this.getAllIds();

        for (var i = 0; i < ids.length; i++) {
            this.cancelLocalNotification(ids[i]);
        }

        this.getToastHistory().clear();
        this.fireEvent('cancelall');
    },

    /**
     * Checks if a notification with an ID is present.
     *
     * @param {int} id
     *      Local notification ID
     */
    isPresent: function (id) {
        return this.getAllIds().indexOf(id) > -1;
    },

    /**
     * Checks if a notification with an ID was scheduled.
     *
     * @param {int} id
     *      Local notification ID
     */
    isScheduled: function (id) {
        return this.getScheduledIds().indexOf(id) > -1;
    },

    /**
     * Checks if a notification with an ID was triggered.
     *
     * @param {int} id
     *      Local notification ID
     */
    isTriggered: function (id) {
        return this.getTriggeredIds().indexOf(id) > -1;
    },

    /**
     * Lists all local notification IDs.
     */
    getAllIds: function () {
        var toasts = this.getScheduledToast(),
            ids = [];

        for (var i = 0; i < toasts.length; i++) {
            var toast = toasts[i];

            ids.push(this.getToastId(toast));
        }

        return ids;
    },

    /**
     * Lists all scheduled notification IDs.
     */
    getScheduledIds: function () {
        var toasts = this.getScheduledToast(),
            ids = [];

        for (var i = 0; i < toasts.length; i++) {
            var toast = toasts[i];

            if (!this.isToastScheduled(toast))
                continue;

            ids.push(this.getToastId(toast));
        }

        return ids;
    },

    /**
     * Lists all scheduled notification IDs.
     */
    getTriggeredIds: function () {
        var toasts = this.getScheduledToast(),
            ids = [];

        for (var i = 0; i < toasts.length; i++) {
            var toast = toasts[i];

            if (!this.isToastTriggered(toast))
                continue;

            ids.push(this.getToastId(toast));
        }

        return ids;
    },

    /**
     * Property list for given notifications.
     * If called without IDs, all notification will be returned.
     *
     * @param {int[]} ids
     *      List of local notification IDs.
     * @param {String?} type
     *      Local notification life cycle type
     */
    getAll: function (ids, type) {
        var toasts = this.getScheduledToast(),
            notifications = [];

        if (ids.length === 0) {
            ids = getAllIds();
        }

        for (var index = 0; index < ids.length; index++) {
            var id = ids[index],
                toast = this.findToastById(id);

            if (!toast || type && this.getToastType(toast) != type)
                continue;

            var json = toast.content.lastChild.lastChild.innerText;

            notifications.push(JSON.parse(json));
        }

        return notifications;
    },

    /**
     * Property list for given notifications.
     * If called without IDs, all notification will be returned.
     *
     * @param {int[]} ids
     *      List of local notification IDs
     */
    getScheduled: function (ids) {
        if (ids.length === 0) {
            ids = this.getAllIds();
        }

        return this.getAll(ids, 'scheduled');
    },

    /**
     * Property list for given notifications.
     * If called without IDs, all notification will be returned.
     *
     * @param {int[]} ids
     *      List of local notification IDs
     */
    getTriggered: function (ids) {
        if (ids.length === 0) {
            ids = this.getAllIds();
        }

        return this.getAll(ids, 'triggered');
    },
};
+269 −0
Changes for src/windows/LocalNotificationProxy.js: 269 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
    Copyright 2013-2015 appPlant UG

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/

/**
 * Executes all queued events.
 */
exports.deviceready  = function () {

};

/**
 * Schedule a new local notification.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {Object[]} notifications
 *      Array of local notifications
 */
exports.schedule = function (success, error, notifications) {
    exports.core.schedule(notifications, 'schedule');

    success();
};

/**
 * Update existing notifications specified by IDs in options.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {Object[]} notifications
 *      Array of local notifications
 */
exports.update = function (success, error, notifications) {
    exports.core.update(notifications);

    success();
};

/**
 * Clear the specified notification.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int[]} ids
 *      List of local notification IDs
 */
exports.clear = function (success, error, ids) {
    exports.core.clear(ids, true);

    success();
};

/**
 * Clear all previously sheduled notifications.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 */
exports.clearAll = function (success, error) {
    exports.core.clearAll();

    success();
};

/**
 * Cancel the specified notifications.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int[]} ids
 *      List of local notification IDs
 */
exports.cancel = function (success, error, ids) {
    exports.core.cancel(ids, true);

    success();
};

/**
 * Remove all previously registered notifications.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 */
exports.cancelAll = function (success, error) {
    exports.core.cancelAll();

    success();
};

/**
 * Check if a notification with an ID is present.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int} id
 *      Local notification ID
 */
exports.isPresent = function (success, error, args) {
    var found = exports.core.isPresent(args[0]);

    success(found);
};

/**
 * Check if a notification with an ID is scheduled.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int} id
 *      Local notification ID
 */
exports.isScheduled = function (success, error, args) {
    var found = exports.core.isScheduled(args[0]);

    success(found);
};

/**
 * Check if a notification with an ID was triggered.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int} id
 *      Local notification ID
 */
exports.isTriggered = function (success, error, args) {
    var found = exports.core.isTriggered(args[0]);

    success(found);
};

/**
 * List all local notification IDs.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 */
exports.getAllIds = function (success, error) {
    var ids = exports.core.getAllIds();

    success(ids);
};

/**
 * List all scheduled notification IDs.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 */
exports.getScheduledIds = function (success, error) {
    var ids = exports.core.getScheduledIds();

    success(ids);
};

/**
 * List all triggered notification IDs.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 */
exports.getTriggeredIds = function (success, error) {
    var ids = exports.core.getTriggeredIds();

    success(ids);
};

/**
 * Property list for given notifications.
 * If called without IDs, all notification will be returned.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int[]} ids
 *      List of local notification IDs
 */
exports.getAll = function (success, error, ids) {
    var notifications = exports.core.getAll(ids);

    success(notifications);
};

/**
 * Property list for given triggered notifications.
 * If called without IDs, all notification will be returned.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int[]} ids
 *      List of local notification IDs
 */
exports.getScheduled = function (success, error, ids) {
    var notifications = exports.core.getScheduled(ids);

    success(notifications);
};

/**
 * Property list for given triggered notifications.
 * If called without IDs, all notification will be returned.
 *
 * @param {Function} success
 *      Success callback
 * @param {Function} error
 *      Error callback
 * @param {int[]} ids
 *      List of local notification IDs
 */
exports.getTriggered = function (success, error, ids) {
    var notifications = exports.core.getTriggered(ids);

    success(notifications);
};

/**
 * Required for core module to access the proxy.
 */
exports.exports = function () {
    return exports;
};

cordova.commandProxy.add('LocalNotification', exports);
+314 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading