Bug 1129433 - Show "# tabs opened in background" notification in system tray (r=mcomella)

This commit is contained in:
Martyn Haigh
2015-03-20 13:42:09 +00:00
parent 95dba84a40
commit 4e482cae7b
5 changed files with 73 additions and 11 deletions

View File

@@ -186,6 +186,20 @@
<!ENTITY tab_queue_toast_message "Open later">
<!ENTITY tab_queue_toast_action "Open now">
<!-- Localization note (tab_queue_notification_text_plural) : The
formatD is replaced with the number of tabs queued. The
number of tabs queued is always more than one. We can't use
Android plural forms, sadly. See Bug #753859. -->
<!ENTITY tab_queue_notification_text_plural "&formatD; tabs queued">
<!-- Localization note (tab_queue_notification_title_plural) : This is the
title of a notification; we expect more than one tab queued. -->
<!ENTITY tab_queue_notification_title_plural "Tabs Queued">
<!-- Localization note (tab_queue_notification_title_singular) : This is the
title of a notification; we expect only one tab queued. -->
<!ENTITY tab_queue_notification_title_singular "Tab Queued">
<!-- Localization note (tab_queue_notification_text_singular) : This is the
text of a notification; we expect only one tab queued. -->
<!ENTITY tab_queue_notification_text_singular "1 tab queued">
<!ENTITY pref_char_encoding "Character encoding">
<!ENTITY pref_char_encoding_on "Show menu">

View File

@@ -5,6 +5,7 @@
<resources>
<item type="id" name="tabQueueNotification"/>
<item type="id" name="guestNotification"/>
<item type="id" name="original_height"/>
<item type="id" name="menu_items"/>

View File

@@ -241,6 +241,10 @@
<string name="tab_queue_toast_message">&tab_queue_toast_message;</string>
<string name="tab_queue_toast_action">&tab_queue_toast_action;</string>
<string name="tab_queue_notification_text_singular">&tab_queue_notification_text_singular;</string>
<string name="tab_queue_notification_text_plural">&tab_queue_notification_text_plural;</string>
<string name="tab_queue_notification_title_singular">&tab_queue_notification_title_singular;</string>
<string name="tab_queue_notification_title_plural">&tab_queue_notification_title_plural;</string>
<string name="pref_about_firefox">&pref_about_firefox;</string>
<string name="pref_vendor_faqs">&pref_vendor_faqs;</string>

View File

@@ -5,20 +5,24 @@
package org.mozilla.gecko.tabqueue;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.util.ThreadUtils;
import android.text.TextUtils;
import android.util.Log;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.app.NotificationCompat;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import org.mozilla.gecko.BrowserApp;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.R;
import org.mozilla.gecko.util.ThreadUtils;
public class TabQueueHelper {
private static final String LOGTAG = "Gecko" + TabQueueHelper.class.getSimpleName();
public static final String FILE_NAME = "tab_queue_url_list.json";
public static final String LOAD_URLS_ACTION = "TAB_QUEUE_LOAD_URLS_ACTION";
public static final int TAB_QUEUE_NOTIFICATION_ID = R.id.tabQueueNotification;
/**
* Reads file and converts any content to JSON, adds passed in URL to the data and writes back to the file,
@@ -27,8 +31,9 @@ public class TabQueueHelper {
* @param profile
* @param url URL to add
* @param filename filename to add URL to
* @return the number of tabs currently queued
*/
public static void queueURL(final GeckoProfile profile, final String url, final String filename) {
public static int queueURL(final GeckoProfile profile, final String url, final String filename) {
ThreadUtils.assertNotOnUiThread();
JSONArray jsonArray = profile.readJSONArrayFromFile(filename);
@@ -36,5 +41,40 @@ public class TabQueueHelper {
jsonArray.put(url);
profile.writeFile(filename, jsonArray.toString());
return jsonArray.length();
}
/**
* Displays a notification showing the total number of tabs queue. If there is already a notification displayed, it
* will be replaced.
*
* @param context
* @param tabsQueued
*/
static public void showNotification(Context context, int tabsQueued) {
Intent resultIntent = new Intent(context, BrowserApp.class);
resultIntent.setAction(TabQueueHelper.LOAD_URLS_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
String title, text;
final Resources resources = context.getResources();
if(tabsQueued == 1) {
title = resources.getString(R.string.tab_queue_notification_title_singular);
text = resources.getString(R.string.tab_queue_notification_text_singular);
} else {
title = resources.getString(R.string.tab_queue_notification_title_plural);
text = resources.getString(R.string.tab_queue_notification_text_plural, tabsQueued);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_status_logo)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(TabQueueHelper.TAB_QUEUE_NOTIFICATION_ID, builder.build());
}
}

View File

@@ -6,6 +6,7 @@
package org.mozilla.gecko.tabqueue;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.PixelFormat;
@@ -160,8 +161,10 @@ public class TabQueueService extends Service {
executorService.submit(new Runnable() {
@Override
public void run() {
final GeckoProfile profile = GeckoProfile.get(getApplicationContext());
TabQueueHelper.queueURL(profile, intentData, filename);
Context applicationContext = getApplicationContext();
final GeckoProfile profile = GeckoProfile.get(applicationContext);
int tabsQueued = TabQueueHelper.queueURL(profile, intentData, filename);
TabQueueHelper.showNotification(applicationContext, tabsQueued);
}
});
}