Bug 1288821 - Refactor Java notification interface; r=snorp

Refactor the notification-related methods in GeckoAppShell. In
particular, combine showPersistentAlertNotification into
showAlertNotification, and move code out of AndroidBridge.
This commit is contained in:
Jim Chen 2016-08-04 09:17:54 -04:00
parent 9ded18e06c
commit 535fa2c33f
4 changed files with 73 additions and 120 deletions

View File

@ -1013,81 +1013,90 @@ public class GeckoAppShell
}
}
private static PendingIntent makePersistentNotificationIntent(
int aNotificationID, String aType, String aPersistentData) {
Uri.Builder b = new Uri.Builder();
Uri u = b.scheme("notification-event").path(Integer.toString(aNotificationID))
.appendQueryParameter("type", aType)
private static PendingIntent makePersistentNotificationIntent(int notificationID, String type,
String persistentData) {
final Uri.Builder b = new Uri.Builder();
final Uri u = b.scheme("notification-event")
.path(Integer.toString(notificationID))
.appendQueryParameter("type", type)
.build();
Intent intent = GeckoService.getIntentToCreateServices(
getApplicationContext(), aType, aPersistentData);
final Intent intent = GeckoService.getIntentToCreateServices(
getApplicationContext(), type, persistentData);
intent.setData(u);
return PendingIntent.getService(
getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@WrapForJNI(stubName = "ShowPersistentAlertNotificationWrapper")
public static void showPersistentAlertNotification(
String aPersistentData,
String aImageUrl, String aAlertTitle, String aAlertText,
String aAlertCookie, String aAlertName, String aHost) {
int notificationID = aAlertName.hashCode();
PendingIntent clickIntent = makePersistentNotificationIntent(notificationID, "persistent-notification-click", aPersistentData);
PendingIntent closeIntent = makePersistentNotificationIntent(notificationID, "persistent-notification-close", aPersistentData);
notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, clickIntent, closeIntent);
}
@WrapForJNI(stubName = "ShowAlertNotificationWrapper")
public static void showAlertNotification(String aImageUrl, String aAlertTitle, String aAlertText, String aAlertCookie, String aAlertName, String aHost) {
// The intent to launch when the user clicks the expanded notification
Intent notificationIntent = new Intent(GeckoApp.ACTION_ALERT_CALLBACK);
notificationIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int notificationID = aAlertName.hashCode();
// Put the strings into the intent as an URI "alert:?name=<alertName>&app=<appName>&cookie=<cookie>"
Uri.Builder b = new Uri.Builder();
Uri dataUri = b.scheme("alert").path(Integer.toString(notificationID))
.appendQueryParameter("name", aAlertName)
.appendQueryParameter("cookie", aAlertCookie)
.build();
notificationIntent.setData(dataUri);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
ALERT_COOKIES.put(aAlertName, aAlertCookie);
callObserver(aAlertName, "alertshow", aAlertCookie);
notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent, null);
return PendingIntent.getService(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
@WrapForJNI
public static void closeNotification(String aAlertName) {
String alertCookie = ALERT_COOKIES.get(aAlertName);
if (alertCookie != null) {
callObserver(aAlertName, "alertfinished", alertCookie);
ALERT_COOKIES.remove(aAlertName);
public static void showAlertNotification(String imageUrl, String alertTitle, String alertText,
String alertCookie, String alertName, String host,
String persistentData) {
final int notificationID = alertName.hashCode();
final PendingIntent clickIntent, closeIntent;
if (persistentData != null) {
clickIntent = makePersistentNotificationIntent(
notificationID, "persistent-notification-click", persistentData);
closeIntent = makePersistentNotificationIntent(
notificationID, "persistent-notification-close", persistentData);
} else {
ALERT_COOKIES.put(alertName, alertCookie);
callObserver(alertName, "alertshow", alertCookie);
// The intent to launch when the user clicks the expanded notification
final Intent notificationIntent = new Intent(GeckoApp.ACTION_ALERT_CALLBACK);
notificationIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME,
AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Put the strings into the intent as an URI
// "alert:?name=<alertName>&app=<appName>&cookie=<cookie>"
final Uri.Builder b = new Uri.Builder();
final Uri dataUri = b.scheme("alert")
.path(Integer.toString(notificationID))
.appendQueryParameter("name", alertName)
.appendQueryParameter("cookie", alertCookie)
.build();
notificationIntent.setData(dataUri);
clickIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
closeIntent = null;
}
removeObserver(aAlertName);
notificationClient.add(notificationID, imageUrl, host, alertTitle,
alertText, clickIntent, closeIntent);
}
int notificationID = aAlertName.hashCode();
@WrapForJNI
public static void closeNotification(String alertName) {
final String alertCookie = ALERT_COOKIES.get(alertName);
if (alertCookie != null) {
callObserver(alertName, "alertfinished", alertCookie);
ALERT_COOKIES.remove(alertName);
}
removeObserver(alertName);
final int notificationID = alertName.hashCode();
notificationClient.remove(notificationID);
}
public static void handleNotification(String aAction, String aAlertName, String aAlertCookie) {
int notificationID = aAlertName.hashCode();
public static void handleNotification(String action, String alertName, String alertCookie) {
final int notificationID = alertName.hashCode();
if (GeckoApp.ACTION_ALERT_CALLBACK.equals(aAction)) {
callObserver(aAlertName, "alertclickcallback", aAlertCookie);
if (GeckoApp.ACTION_ALERT_CALLBACK.equals(action)) {
callObserver(alertName, "alertclickcallback", alertCookie);
if (notificationClient.isOngoing(notificationID)) {
// When clicked, keep the notification if it displays progress
return;
}
}
closeNotification(aAlertName);
closeNotification(alertName);
}
@WrapForJNI(stubName = "GetDpiWrapper")

View File

@ -75,18 +75,15 @@ AndroidAlerts::ShowPersistentNotification(const nsAString& aPersistentData,
nsAutoString host;
nsAlertsUtils::GetSourceHostPort(principal, host);
if (!aPersistentData.IsEmpty()) {
java::GeckoAppShell::ShowPersistentAlertNotificationWrapper(
aPersistentData, imageUrl, title, text, cookie, name, host);
} else {
if (aAlertListener) {
// This will remove any observers already registered for this id
nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(name, aAlertListener));
}
java::GeckoAppShell::ShowAlertNotificationWrapper(
imageUrl, title, text, cookie, name, host);
if (aPersistentData.IsEmpty() && aAlertListener) {
// This will remove any observers already registered for this id
nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(name, aAlertListener));
}
java::GeckoAppShell::ShowAlertNotification(
imageUrl, title, text, cookie, name, host,
!aPersistentData.IsEmpty() ? jni::StringParam(aPersistentData)
: jni::StringParam(nullptr));
return NS_OK;
}

View File

@ -443,43 +443,6 @@ AndroidBridge::GetClipboardText(nsAString& aText)
return !!text;
}
void
AndroidBridge::ShowPersistentAlertNotification(const nsAString& aPersistentData,
const nsAString& aImageUrl,
const nsAString& aAlertTitle,
const nsAString& aAlertText,
const nsAString& aAlertCookie,
const nsAString& aAlertName,
nsIPrincipal* aPrincipal)
{
nsAutoString host;
nsAlertsUtils::GetSourceHostPort(aPrincipal, host);
GeckoAppShell::ShowPersistentAlertNotificationWrapper
(aPersistentData, aImageUrl, aAlertTitle, aAlertText, aAlertCookie, aAlertName, host);
}
void
AndroidBridge::ShowAlertNotification(const nsAString& aImageUrl,
const nsAString& aAlertTitle,
const nsAString& aAlertText,
const nsAString& aAlertCookie,
nsIObserver *aAlertListener,
const nsAString& aAlertName,
nsIPrincipal* aPrincipal)
{
if (aAlertListener) {
// This will remove any observers already registered for this id
nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(aAlertName, aAlertListener));
}
nsAutoString host;
nsAlertsUtils::GetSourceHostPort(aPrincipal, host);
GeckoAppShell::ShowAlertNotificationWrapper
(aImageUrl, aAlertTitle, aAlertText, aAlertCookie, aAlertName, host);
}
int
AndroidBridge::GetDPI()
{

View File

@ -175,22 +175,6 @@ public:
bool GetClipboardText(nsAString& aText);
void ShowPersistentAlertNotification(const nsAString& aPersistentData,
const nsAString& aImageUrl,
const nsAString& aAlertTitle,
const nsAString& aAlertText,
const nsAString& aAlertCookie,
const nsAString& aAlertName,
nsIPrincipal* aPrincipal);
void ShowAlertNotification(const nsAString& aImageUrl,
const nsAString& aAlertTitle,
const nsAString& aAlertText,
const nsAString& aAlertCookie,
nsIObserver *aAlertListener,
const nsAString& aAlertName,
nsIPrincipal* aPrincipal);
int GetDPI();
int GetScreenDepth();