embedding/android/NotificationHandler.java.in
author Gregory Szorc <gps@mozilla.com>
Wed, 28 Jan 2015 13:37:00 -0800
branchGECKO170_2012101707_RELBRANCH
changeset 110541 0fdb0a5be23e00b06c9088f1e9c737fd305a6f7d
parent 98529 f4157e8c410708d76703f19e4dfb61859bfe32d8
permissions -rw-r--r--
Close old release branch GECKO170_2012101707_RELBRANCH

/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#filter substitution
package @ANDROID_PACKAGE_NAME@;

import android.app.NotificationManager;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.net.Uri;

public class NotificationHandler
    extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null)
            handleIntent(context, intent);
    }

    protected void handleIntent(Context context, Intent notificationIntent) {
        String action = notificationIntent.getAction();
        String alertName = "";
        String alertCookie = "";
        Uri data = notificationIntent.getData();
        if (data != null) {
            alertName = data.getSchemeSpecificPart();
            alertCookie = data.getFragment();
            if (alertCookie == null)
                alertCookie = "";
        }

        Log.i("GeckoAppJava", "NotificationHandler.handleIntent\n" +
              "- action = '" + action + "'\n" +
              "- alertName = '" + alertName + "'\n" +
              "- alertCookie = '" + alertCookie + "'");

        int notificationID = alertName.hashCode();

        Log.i("GeckoAppJava", "Handle notification ID " + notificationID);

        if (App.mAppContext != null) {
            // This should call the observer, if any
            App.mAppContext.handleNotification(action, alertName, alertCookie);
        } else {
            // The app is not running, just cancel this notification
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(notificationID);
        }

        if (App.ACTION_ALERT_CLICK.equals(action)) {
            // Start or bring to front the main activity
            Intent appIntent = new Intent(Intent.ACTION_MAIN);
            appIntent.setClassName(context, "@ANDROID_PACKAGE_NAME@.App");
            appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            appIntent.putExtra("args", "-alert " + alertName + (alertCookie.length() > 0 ? "#" + alertCookie : ""));
            try {
                Log.i("GeckoAppJava", "startActivity with intent: Action='" + appIntent.getAction() + "'" +
                      ", args='" + appIntent.getStringExtra("args") + "'" );
                context.startActivity(appIntent);
            } catch (ActivityNotFoundException e) {
                Log.e("GeckoAppJava", "NotificationHandler Exception: ", e);
            }
        }
    }
}