Bug 833625 - Part 2: Android notifications for data reporting. r=rnewman

This commit is contained in:
Chenxia Liu 2013-05-10 21:29:09 -07:00
parent 3671f41f73
commit 3ccf6c761d
10 changed files with 123 additions and 2 deletions

View File

@ -840,6 +840,12 @@ abstract public class BrowserApp extends GeckoApp
menu.findItem(R.id.settings).setEnabled(true);
}
});
// Display notification for Mozilla data reporting, if data should be collected.
if (AppConstants.MOZ_DATA_REPORTING) {
DataReportingNotification.checkAndNotifyPolicy(BrowserApp.mAppContext);
}
} else if (event.equals("Telemetry:Gather")) {
Telemetry.HistogramAdd("PLACES_PAGES_COUNT", BrowserDB.getCount(getContentResolver(), "history"));
Telemetry.HistogramAdd("PLACES_BOOKMARKS_COUNT", BrowserDB.getCount(getContentResolver(), "bookmarks"));

View File

@ -0,0 +1,100 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; 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/. */
package org.mozilla.gecko;
import org.mozilla.gecko.GeckoPreferences;
import org.mozilla.gecko.GeckoPreferenceFragment;
import org.mozilla.gecko.util.ThreadUtils;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
public class DataReportingNotification {
private static final String LOGTAG = "DataReportNotification";
public static final String ALERT_NAME_DATAREPORTING_NOTIFICATION = "datareporting-notification";
private static final String DEFAULT_PREFS_BRANCH = AppConstants.ANDROID_PACKAGE_NAME + "_preferences";
private static final String PREFS_POLICY_NOTIFIED_TIME = "datareporting.policy.dataSubmissionPolicyNotifiedTime";
private static final String PREFS_POLICY_VERSION = "datareporting.policy.dataSubmissionPolicyVersion";
private static final int DATA_REPORTING_VERSION = 1;
public static void checkAndNotifyPolicy(Context context) {
SharedPreferences dataPrefs = context.getSharedPreferences(DEFAULT_PREFS_BRANCH, 0);
// Notify if user has not been notified or if policy version has changed.
if ((!dataPrefs.contains(PREFS_POLICY_NOTIFIED_TIME)) ||
(DATA_REPORTING_VERSION != dataPrefs.getInt(PREFS_POLICY_VERSION, -1))) {
// Launch Data Choices fragment when notification is clicked.
Intent prefIntent = new Intent(context, GeckoPreferences.class);
// Build launch intent based on Android version.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
prefIntent.putExtra("resource", "preferences_datareporting");
} else {
prefIntent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, GeckoPreferenceFragment.class.getName());
Bundle fragmentArgs = new Bundle();
fragmentArgs.putString("resource", "preferences_datareporting");
prefIntent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArgs);
}
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create and send notification.
String notificationTitle = context.getResources().getString(R.string.datareporting_notification_title);
String notificationSummary;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
notificationSummary = context.getResources().getString(R.string.datareporting_notification_action);
} else {
// Display partial version of Big Style notification for supporting devices.
notificationSummary = context.getResources().getString(R.string.datareporting_notification_summary_short);
}
String notificationAction = context.getResources().getString(R.string.datareporting_notification_action);
String notificationBigSummary = context.getResources().getString(R.string.datareporting_notification_summary);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(notificationTitle)
.setContentText(notificationSummary)
.setSmallIcon(R.drawable.ic_status_logo)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationBigSummary))
.addAction(R.drawable.ic_menu_settings, notificationAction, contentIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode();
notificationManager.notify(notificationID, notification);
// Record version and notification time.
SharedPreferences.Editor editor = dataPrefs.edit();
long now = System.currentTimeMillis();
editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now);
editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION);
// If healthreport is enabled, set default preference value.
if (AppConstants.MOZ_SERVICES_HEALTHREPORT) {
editor.putBoolean(GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true);
}
editor.commit();
}
}
}

View File

@ -5,6 +5,7 @@
package org.mozilla.gecko;
import org.mozilla.gecko.DataReportingNotification;
import org.mozilla.gecko.background.announcements.AnnouncementsBroadcastService;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.gfx.BitmapUtils;

View File

@ -51,18 +51,18 @@ public class GeckoPreferences
{
private static final String LOGTAG = "GeckoPreferences";
private static final String NON_PREF_PREFIX = "android.not_a_preference.";
public static final String INTENT_EXTRA_RESOURCES = "resource";
public static String PREFS_HEALTHREPORT_UPLOAD_ENABLED = NON_PREF_PREFIX + "healthreport.uploadEnabled";
private static boolean sIsCharEncodingEnabled = false;
private boolean mInitialized = false;
private static final String NON_PREF_PREFIX = "android.not_a_preference.";
// These match keys in resources/xml/preferences.xml.in.
private static String PREFS_ANNOUNCEMENTS_ENABLED = NON_PREF_PREFIX + "privacy.announcements.enabled";
private static String PREFS_DATA_REPORTING_PREFERENCES = NON_PREF_PREFIX + "datareporting.preferences";
private static String PREFS_TELEMETRY_ENABLED = "datareporting.telemetry.enabled";
private static String PREFS_CRASHREPORTER_ENABLED = "datareporting.crashreporter.submitEnabled";
private static String PREFS_HEALTHREPORT_UPLOAD_ENABLED = NON_PREF_PREFIX + "healthreport.uploadEnabled";
private static String PREFS_MENU_CHAR_ENCODING = "browser.menu.showCharacterEncoding";
private static String PREFS_MP_ENABLED = "privacy.masterpassword.enabled";
private static String PREFS_UPDATER_AUTODOWNLOAD = "app.update.autodownload";

View File

@ -77,6 +77,7 @@ FENNEC_JAVA_FILES = \
db/BrowserDB.java \
db/LocalBrowserDB.java \
db/DBUtils.java \
DataReportingNotification.java \
Distribution.java \
Divider.java \
DoorHanger.java \
@ -853,6 +854,7 @@ RES_DRAWABLE_MDPI_V11 = \
res/drawable-mdpi-v11/alert_addon.png \
res/drawable-mdpi-v11/alert_app.png \
res/drawable-mdpi-v11/alert_download.png \
res/drawable-mdpi-v11/firefox_settings_alert.png \
res/drawable-mdpi-v11/ic_menu_addons.png \
res/drawable-mdpi-v11/ic_menu_apps.png \
res/drawable-mdpi-v11/ic_menu_back.png \
@ -878,6 +880,7 @@ RES_DRAWABLE_HDPI_V11 = \
res/drawable-hdpi-v11/alert_addon.png \
res/drawable-hdpi-v11/alert_app.png \
res/drawable-hdpi-v11/alert_download.png \
res/drawable-hdpi-v11/firefox_settings_alert.png \
res/drawable-hdpi-v11/ic_menu_addons.png \
res/drawable-hdpi-v11/ic_menu_apps.png \
res/drawable-hdpi-v11/ic_menu_back.png \
@ -903,6 +906,7 @@ RES_DRAWABLE_XHDPI_V11 = \
res/drawable-xhdpi-v11/alert_addon.png \
res/drawable-xhdpi-v11/alert_app.png \
res/drawable-xhdpi-v11/alert_download.png \
res/drawable-xhdpi-v11/firefox_settings_alert.png \
res/drawable-xhdpi-v11/ic_menu_addons.png \
res/drawable-xhdpi-v11/ic_menu_apps.png \
res/drawable-xhdpi-v11/ic_menu_back.png \

View File

@ -132,6 +132,11 @@ size. -->
<!ENTITY pref_private_data_siteSettings2 "Site settings">
<!ENTITY pref_private_data_downloadFiles "Downloaded files">
<!ENTITY datareporting_notification_title "&brandShortName; stats &amp; data">
<!ENTITY datareporting_notification_action "Choose what to share">
<!ENTITY datareporting_notification_summary "To improve your experience, &brandShortName; automatically sends some information to &vendorShortName;.">
<!ENTITY datareporting_notification_summary_short "To improve your experience, &brandShortName;…">
<!-- Localization note (datareporting_fhr_title, datareporting_fhr_summary,
reporting_telemetry_title, datareporting_telemetry_summary,
datareporting_crashreporter_summary) : These match the strings in

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -133,6 +133,11 @@
<string name="pref_update_autodownload_disabled">&pref_update_autodownload_disabled;</string>
<string name="pref_update_autodownload_enabled">&pref_update_autodownload_enabled;</string>
<string name="datareporting_notification_title">&datareporting_notification_title;</string>
<string name="datareporting_notification_action">&datareporting_notification_action;</string>
<string name="datareporting_notification_summary">&datareporting_notification_summary;</string>
<string name="datareporting_notification_summary_short">&datareporting_notification_summary_short;</string>
<string name="datareporting_telemetry_title">&datareporting_telemetry_title;</string>
<string name="datareporting_telemetry_summary">&datareporting_telemetry_summary;</string>
<string name="datareporting_fhr_title">&datareporting_fhr_title;</string>