Source: cordova-plugin-bbd-push/assets/www/android/GDPush.js

/*
 * (c) 2020 BlackBerry Limited. All rights reserved.
 */

;
(function() {
    var cordovaExec = require('cordova/exec'),
        GDPushChannel = require('cordova-plugin-bbd-push.GDPushChannel');

    /**
     * @class GDPushConnection
     * 
     * @classdesc The Push Connection is the container and conduit for the device's Push Channels. An
     * application may open multiple Push Channels; all will be managed within a single Push Connection.
     * The Push Connection is automatically established during BlackBerry Dynamics authorization processing,
     * and then maintained by the BlackBerry Dynamics run-time under application control. The application can
     * instruct the run-time to switch the Push Connection off and on.  When instructed to switch off,
     * the GD run-time will terminate the Push Connection, and suspend its maintenance. When instructed
     * to switch back on, the GD run-time will re-establish the Push Connection, and resume maintenance.
     * Switching off the Push Connection might be an option that the application offers to the end user,
     * for example, allowing them to reduce power consumption on the device.
     *
     * @deprecated since version 8.0. It will be removed in future versions.
     * Use GDPushChannel class instead.
     * 
     * @property {function} onConnected Callback function to invoke after the connection is established.
     *
     * @property {function} onDisconnected Callback function to invoke after the connection is terminated.
     */
    var GDPushConnection = function() {
        var onConnected = null,
            onDisconnected = null,
            gdPushChannel = new GDPushChannel();

        Object.defineProperties(this, {
            'onConnected': {
                get: function() {
                    return onConnected;
                },
                set: function(callback) {
                    onConnected = callback;
                }
            },
            'onDisconnected': {
                get: function() {
                    return onDisconnected;
                },
                set: function(callback) {
                    onDisconnected = callback;
                }
            },
            'gdPushChannel': {
                get: function() {
                    console.warn('"gdPushChannel" is deprecated in GDPushConnection class. ' +
                        'It will be removed in future versions.\n' +
                        'Please, use GDPushChannel class constructor instead.');

                    return gdPushChannel;
                },
                configurable: false
            },
            'toString': {
                value: function() {
                    return '[object GDPushConnection]';
                }
            }
        })
    };

    Object.defineProperty(GDPushConnection, 'toString', {
        value: function() {
            return 'function GDPushConnection() { [native code] }';
        }
    });

    Object.preventExtensions(GDPushConnection);

    // ***** BEGIN: MODULE METHOD DEFINITIONS - GDPushConnection *****
    var gdPushConnectionDeprecationMessage = 'The "initialize", "connect" and "disconnect" functions are ' +
        'deprecated now. They will be removed in future versions.\n' +
        'These functions were added to enable management of power consumption by the application code. ' +
        'This is no longer required because of advances in the built-in power ' +
        'management of mobile operating systems and devices.';
    /**
     * @function GDPushConnection#initialize
     * 
     * @description Initialize the push connection in prior to establishing the connection.  This function
     * should be called before calling <a href="#connect">GDPushConnection.connect</a>.
     *
     * @deprecated It will be removed in future versions.
     * See connect and disconnect functions for more info.
     * 
     * @param {function} onConnected Callback function to invoke after the connection is established.
     *
     * @param {function} onDisconnected Callback function to invoke after the connection is terminated.
     *
     * @example
     * window.plugins.GDPushConnection.initialize(function(result) {
     *     // onConnected code here
     * }, function(result) {
     *     // onDisconnected code here
     * });
     */
    GDPushConnection.prototype.initialize = function(onConnected, onDisconnected) {
        console.warn(gdPushConnectionDeprecationMessage);

        if (typeof onConnected !== 'function') {
            throw new Error("ERROR in GDPushConnection.initialize: onConnected parameter " +
                "is not a function.");
        }

        if (typeof onDisconnected !== 'function') {
            throw new Error("ERROR in GDPushConnection.initialize: onDisconnected parameter " +
                "is not a function.");
        }

        this.onConnected = onConnected;
        this.onDisconnected = onDisconnected;
    };

    /**
     * @function GDPushConnection#connect
     *
     * @description Call this function to establish, or re-establish, the Push Channel connection with the
     * BlackBerry Dynamics proxy infrastructure Network Operating Center (NOC).
     *
     * @deprecated It will be removed in future versions.
     * The connect and disconnect functions are no longer required. They were added to
     * enable management of power consumption by the application code. This is no longer required 
     * because of advances in the built-in power management of mobile operating systems and devices.
     *
     * @example
     * window.plugins.GDPushConnection.initialize(function(result) {
     *     // onConnected code here
     * }, function(result) {
     *     // onDisconnected code here
     *     window.plugins.GDPushConnection.connect();
     * });
     */
    GDPushConnection.prototype.connect = function() {
        console.warn(gdPushConnectionDeprecationMessage);

        if (this.onConnected === null || typeof this.onConnected === 'undefined') {
            throw new Error("ERROR: GDPushConnection has no onConnected handler defined.");
        }

        cordovaExec(this.onConnected, this.onDisconnected, "GDPush", "connect", null);
    };

    /**
     * @function GDPushConnection#disconnect
     *
     * @description Call this function to terminate the Push Channel connection with the BlackBerry Dynamics
     * proxy infrastructure Network Operating Center (NOC).
     *
     * @deprecated It will be removed in future versions.
     * The connect and disconnect functions are no longer required. They were added to
     * enable management of power consumption by the application code. This is no longer required 
     * because of advances in the built-in power management of mobile operating systems and devices.
     * 
     * @example
     * window.plugins.GDPushConnection.initialize(function(result) {
     *     // onConnected code here
     *     window.plugins.GDPushConnection.disconnect();
     * }, function(result) {
     *     // onDisconnected code here
     * });
     */
    GDPushConnection.prototype.disconnect = function() {
        console.warn(gdPushConnectionDeprecationMessage);

        cordovaExec(this.onConnected, this.onDisconnected, "GDPush", "disconnect", null);
    };

    /**
     * @function GDPushConnection#createPushChannel
     *
     * @description Call this function to create a new push channel to receive notifications
     * from an application server.  Push Channels can only be established when the Push Connection
     * is open and operating.
     * 
     * @deprecated It will be removed in future versions.
     * Use GDPushChannel constructor instead.
     *
     * @param {function} responseCallback Callback function to invoke whenever a response is received by
     * this push channel.
     *
     * @returns {GDPushChannel}
     *
     * @example
     * See the example <a href="./GDPushChannel.html">here</a>
     */
    GDPushConnection.prototype.createPushChannel = function(responseCallback) {
        console.warn('"createPushChannel" function is deprecated in GDPushConnection class. ' +
            'It will be removed in future versions.\n' +
            'Please, use GDPushChannel class constructor instead.');

        return new GDPushChannel(responseCallback);
    };

    /**
     * @function GDPushConnection#parseChannelResponse
     *
     * @description Call this function to transform the push channel response text into a
     * GDPushChannelResponse object.
     *
     * @deprecated It will be removed in future versions.
     * Use parseChannelResponse function in GDPushChannel class instead.
     * 
     * @param {string} responseText A string representing the push channel response text.
     *
     * @return {GDPushChannelResponse} The push channel response object.
     *
     * @example
     * See the example <a href="./GDPushChannel.html">here</a>
     */
    GDPushConnection.prototype.parseChannelResponse = function(responseText) {
        console.warn('"parseChannelResponse" function is deprecated in GDPushConnection class. ' +
            'It will be removed in future versions.\n' +
            'Please, use "parseChannelResponse" function in GDPushChannel class instead.');

        return this.gdPushChannel.parseChannelResponse(responseText);
    };

    /**
     * @function GDPushConnection#isConnected
     *
     * @description This function returns the current status of the Push Channel connection.
     *
     * @deprecated It will be removed in future versions.
     * Use isAvailable function in GDPushChannel class instead.
     * 
     * @param {function} responseCallback Callback function to invoke when the function returns.
     * A single result string will be passed as the input to the callback function: "true" or
     * "false".
     *
     * @return {string} "true" or "false".
     *
     * @example
     * See the example <a href="./GDPushChannel.html">here</a>
     */
    GDPushConnection.prototype.isConnected = function(responseCallback) {
        console.warn('"isConnected" function is deprecated in GDPushConnection class. ' +
            'It will be removed in future versions.\n' +
            'Please, use "isAvailable" function in GDPushChannel class instead.');

        this.gdPushChannel.isAvailable(responseCallback);
    };

    // ***** END: MODULE METHOD DEFINITIONS - GDPushConnection *****
    function hideJSFunctionsImplementationInConsoleForObject(prototypeObject) {
        for (protoFunction in prototypeObject) {
            if (prototypeObject.hasOwnProperty(protoFunction)) {

                // Checking, if function property 'name' is configurable
                // (for old browser, which has pre-ES2015 implementation(Android 5.0) function name property isn't configurable)
                var objProtoProperty = prototypeObject[protoFunction],
                    isFuncNamePropConfigurable = Object.getOwnPropertyDescriptor(objProtoProperty, 'name').configurable;

                if (isFuncNamePropConfigurable) {
                    Object.defineProperty(prototypeObject[protoFunction],
                        'name', {
                            value: protoFunction,
                            configurable: false
                        }
                    );
                }

                Object.defineProperty(prototypeObject[protoFunction],
                    'toString', {
                        value: function() {
                            var funcName = this.name || protoFunction;
                            return 'function ' + funcName + '() { [native code] }';
                        },
                        writable: false,
                        configurable: false
                    });
            }
        }

        Object.preventExtensions(prototypeObject);
    }

    // hide functions implementation in web inspector
    hideJSFunctionsImplementationInConsoleForObject(GDPushConnection.prototype);

    var gdPushConnection = new GDPushConnection();
    Object.preventExtensions(gdPushConnection);
    // Install the plugin.
    module.exports = gdPushConnection;
}()); // End the Module Definition.
//************************************************************************************************