Bug 1129433 - Show "# tabs opened in background" notification in system tray (r=mcomella)
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user