gecko/mobile/android/base/NotificationHandler.java.in

75 lines
2.5 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.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.NotificationManager;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
import org.mozilla.gecko.GeckoAppShell;
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) {
// only fire the callback if the intent was clicked, not if it was cancelled
String action = notificationIntent.getAction();
if (App.ACTION_ALERT_CLEAR.equals(action))
return;
String appName = "";
Uri data = notificationIntent.getData();
if (data != null) {
appName = data.getQueryParameter("app");
if (appName == null)
appName = "@ANDROID_PACKAGE_NAME@.App";
}
sendIntent(context, App.ACTION_ALERT_CALLBACK,
appName,
data);
}
private void sendIntent(Context context, String action, String className, Uri data) {
Intent appIntent = new Intent(action);
if (TextUtils.isEmpty(className))
appIntent.setClassName(context, "@ANDROID_PACKAGE_NAME@.App");
else
appIntent.setClassName(context, className);
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (data != null)
appIntent.setData(data);
try {
Log.i(LOGTAG, "startActivity with intent: Action='" + appIntent.getAction() +
" appName='" + className + "'");
context.startActivity(appIntent);
} catch (ActivityNotFoundException e) {
Log.e(LOGTAG, "NotificationHandler Exception: ", e);
}
}
}