gecko-dev/mobile/android/base/NotificationHandler.java.in
2012-05-21 12:12:37 +01:00

75 lines
2.9 KiB
Java

/* -*- 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
{
private static final String LOGTAG = "GeckoNotificationHandler";
@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(LOGTAG, "NotificationHandler.handleIntent\n" +
"- action = '" + action + "'\n" +
"- alertName = '" + alertName + "'\n" +
"- alertCookie = '" + alertCookie + "'");
int notificationID = alertName.hashCode();
Log.i(LOGTAG, "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(LOGTAG, "startActivity with intent: Action='" + appIntent.getAction() + "'" +
", args='" + appIntent.getStringExtra("args") + "'" );
context.startActivity(appIntent);
} catch (ActivityNotFoundException e) {
Log.e(LOGTAG, "NotificationHandler Exception: ", e);
}
}
}
}