Commit c1c8a8d2 authored by Archana Naik's avatar Archana Naik Committed by EddyVerbruggen
Browse files

Added amazon-fireos platform.

parent e5ecdc65
Loading
Loading
Loading
Loading
+44 −0
Changes for plugin.xml: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -64,6 +64,50 @@

	</platform>

    <!-- amazon-fireos -->
	<platform name="amazon-fireos">

		<config-file target="res/xml/config.xml" parent="/*">
			<feature name="PushPlugin" >
				<param name="android-package" value="com.amazon.cordova.plugin.PushPlugin"/>
			</feature>
            <preference name="showmessageinnotification" value="true" />
            <preference name="defaultnotificationmessage" value="New message has arrived!" />
		</config-file>
        
        <config-file target="AndroidManifest.xml" parent="/manifest">
            <!-- This permission ensures that no other application can intercept your ADM messages. "[YOUR PACKAGE NAME]" is your package name as defined in your <manifest> tag. -->
            <permission android:name="$PACKAGE_NAME.permission.RECEIVE_ADM_MESSAGE" android:protectionLevel="signature" />
            <uses-permission android:name="$PACKAGE_NAME.permission.RECEIVE_ADM_MESSAGE" />
            <!-- This permission allows your app access to receive push notifications from ADM. -->
            <uses-permission android:name="com.amazon.device.messaging.permission.RECEIVE" />
            <!-- ADM uses WAKE_LOCK to keep the processor from sleeping when a message is received. -->
            <uses-permission android:name="android.permission.WAKE_LOCK" />
		</config-file>

		<config-file target="AndroidManifest.xml" parent="/manifest/application">
            <!-- You must explicitly enable ADM and declare whether your app cannot work without ADM (android:required="true") or can work without ADM (android:required="false").
        I   f you specify android:required="false", your app must degrade gracefully if ADM
            is unavailable. -->
            <amazon:enable-feature android:name="com.amazon.device.messaging" android:required="true"/>
			<service android:exported="false" android:name="com.amazon.cordova.plugin.ADMMessageHandler" />
            <activity android:name="com.amazon.cordova.plugin.ADMHandlerActivity" />
            <receiver android:name="com.amazon.cordova.plugin.ADMMessageHandler$Receiver" android:permission="com.amazon.device.messaging.permission.SEND">
                <intent-filter>
                    <action android:name="com.amazon.device.messaging.intent.REGISTRATION" />
                    <action android:name="com.amazon.device.messaging.intent.RECEIVE" />
                    <category android:name="$PACKAGE_NAME" />
                </intent-filter>
            </receiver>		
        </config-file>
        
        <source-file src="src/amazon/PushPlugin.java" target-dir="src/com/amazon/cordova/plugin" />
        <source-file src="src/amazon/ADMMessageHandler.java" target-dir="src/com/amazon/cordova/plugin" />
        <source-file src="src/amazon/ADMHandlerActivity.java" target-dir="src/com/amazon/cordova/plugin" />
       		
	</platform>


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

+69 −0
Changes for src/amazon/ADMHandlerActivity.java: 69 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* 
 * Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. 
 * 
 * Licensed 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. 
 */

package com.amazon.cordova.plugin;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

public class ADMHandlerActivity extends Activity {

    /*
     * this activity will be started if the user touches a notification that we own. We send it's data off to the push
     * plugin for processing. If needed, we boot up the main activity to kickstart the application.
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean isPushPluginActive = PushPlugin.isActive();
        processPushBundle(isPushPluginActive);
        finish();
        if (!isPushPluginActive) {
            forceMainActivityReload();
        }
    }

    /**
     * Takes the pushBundle extras from the intent, and sends it through to the PushPlugin for processing.
     */
    private void processPushBundle(boolean isCordovaActive) {
        Bundle extras = getIntent().getExtras();

        if (extras != null) {
            Bundle originalExtras = extras
                .getBundle(ADMMessageHandler.PUSH_BUNDLE);
            ADMMessageHandler.cancelNotification(this);
            PushPlugin.sendExtras(originalExtras);
            // clean up the noticiationIntent extra
            ADMMessageHandler.cleanupNotificationIntent();
        }
    }

    /**
     * Forces the main activity to re-launch if it's unloaded.
     */
    private void forceMainActivityReload(/* Bundle extras */) {
        PackageManager pm = getPackageManager();
        Intent launchIntent = pm
            .getLaunchIntentForPackage(getApplicationContext()
                .getPackageName());
        startActivity(launchIntent);
    }

}
+217 −0
Changes for src/amazon/ADMMessageHandler.java: 217 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* 
 * Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. 
 * 
 * Licensed 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. 
 */

package com.amazon.cordova.plugin;

import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.app.Notification.Builder;

import com.amazon.device.messaging.ADMMessageHandlerBase;
import com.amazon.device.messaging.ADMMessageReceiver;

/**
 * The ADMMessageHandler class receives messages sent by ADM via the receiver.
 */

public class ADMMessageHandler extends ADMMessageHandlerBase {

    private static final String TAG = "ADMMessageHandler";
    private static final String ERROR_EVENT = "error";
    public static final String PUSH_BUNDLE = "pushBundle";

    // An identifier for ADM notification unique within your application
    // It allows you to update the same notification later on
    public static final int NOTIFICATION_ID = 519;
    static Intent notificationIntent = null;

    /**
     * Class constructor.
     */
    public ADMMessageHandler() {
        super(ADMMessageHandler.class.getName());
    }

    /**
     * Class constructor, including the className argument.
     * 
     * @param className
     *            The name of the class.
     */
    public ADMMessageHandler(final String className) {
        super(className);
    }

    /**
     * The Receiver class listens for messages from ADM and forwards them to the ADMMessageHandler class.
     */
    public static class Receiver extends ADMMessageReceiver {
        public Receiver() {
            super(ADMMessageHandler.class);

        }

        // Nothing else is required here; your broadcast receiver automatically
        // forwards intents to your service for processing.
    }

    /** {@inheritDoc} */
    @Override
    protected void onRegistered(final String newRegistrationId) {
        // You start the registration process by calling startRegister() in your Main Activity. 
        // When the registration ID is ready, ADM calls onRegistered()
        // on your app. Transmit the passed-in registration ID to your server, so
        // your server can send messages to this app instance. onRegistered() is also
        // called if your registration ID is rotated or changed for any reason;
        // your app should pass the new registration ID to your server if this occurs.

        // we fire the register event in the web app, register handler should
        // fire to send the registration ID to your server via a header key/value pair over HTTP.(AJAX)
        PushPlugin.sendRegistrationIdWithEvent(PushPlugin.REGISTER,
            newRegistrationId);
    }

    /** {@inheritDoc} */
    @Override
    protected void onUnregistered(final String registrationId) {
        // If your app is unregistered on this device, inform your server that
        // this app instance is no longer a valid target for messages.
        PushPlugin.sendRegistrationIdWithEvent(PushPlugin.UNREGISTER,
            registrationId);
    }

    /** {@inheritDoc} */
    @Override
    protected void onRegistrationError(final String errorId) {
        // You should consider a registration error fatal. In response, your app
        // may degrade gracefully, or you may wish to notify the user that this part
        // of your app's functionality is not available.
        try {
            JSONObject json;
            json = new JSONObject().put(PushPlugin.EVENT, ERROR_EVENT);
            json.put(PushPlugin.REG_ID, errorId);

            PushPlugin.sendJavascript(json);
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }
    }

    /** {@inheritDoc} */
    @Override
    protected void onMessage(final Intent intent) {
        // Extract the message content from the set of extras attached to
        // the com.amazon.device.messaging.intent.RECEIVE intent.

        // Extract the payload from the message
        Bundle extras = intent.getExtras();
        if (extras != null) {
            // if we are in the foreground, just surface the payload, else post
            // it to the statusbar
            if (PushPlugin.isInForeground()) {
                extras.putBoolean(PushPlugin.FOREGROUND, true);
                PushPlugin.sendExtras(extras);
            } else {
                extras.putBoolean(PushPlugin.FOREGROUND, false);
                createNotification(this, extras);
            }
        }
    }

    /**
     * Creates a notification when app is not running or is not in foreground. It puts the message info into the Intent
     * extra
     * 
     * @param context
     * @param extras
     */
    public void createNotification(Context context, Bundle extras) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String appName = getAppName(this);

        // reuse the intent so that we can combine multiple messages into extra
        if (notificationIntent == null) {
            notificationIntent = new Intent(this, ADMHandlerActivity.class);
        }
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notificationIntent.putExtra("pushBundle", extras);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        final Builder mBuilder = new Notification.Builder(context);
        mBuilder.setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent);

        if (PushPlugin.showMessageInNotificationCenter()) {
            mBuilder.setContentText(extras.getString("message"));
        } else {
            mBuilder.setContentText(PushPlugin.defaultNotificationMessage());
        }

        String title = appName;
        mBuilder.setContentTitle(title).setTicker(title);
        mBuilder.setAutoCancel(true);
        // Because the ID remains unchanged, the existing notification is updated.
        mNotificationManager.notify((String) appName, NOTIFICATION_ID,
            mBuilder.build());
    }

    public static void cancelNotification(Context context) {
        NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel((String) getAppName(context),
            NOTIFICATION_ID);
    }

    private static String getAppName(Context context) {
        CharSequence appName = context.getPackageManager().getApplicationLabel(
            context.getApplicationInfo());
        return (String) appName;
    }

    // clean up the message in the intent
    static void cleanupNotificationIntent() {
        if (notificationIntent != null) {
            Bundle pushBundle = notificationIntent.getExtras().getBundle(
                PUSH_BUNDLE);
            if (pushBundle != null) {
                pushBundle.clear();
            }

        }
    }

    static Bundle getOfflineMessage() {
        Bundle pushBundle = null;
        if (notificationIntent != null) {
            pushBundle = notificationIntent.getExtras().getBundle(PUSH_BUNDLE);
            if (pushBundle.isEmpty()) {
                pushBundle = null;
            }
        }
        return pushBundle;
    }

}
+420 −0

File added.

Preview size limit exceeded, changes collapsed.