mobile/android/base/DataReportingNotification.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko;
michael@0 7
michael@0 8 import org.mozilla.gecko.preferences.GeckoPreferences;
michael@0 9
michael@0 10 import android.app.Notification;
michael@0 11 import android.app.NotificationManager;
michael@0 12 import android.app.PendingIntent;
michael@0 13 import android.content.Context;
michael@0 14 import android.content.Intent;
michael@0 15 import android.content.SharedPreferences;
michael@0 16 import android.content.res.Resources;
michael@0 17 import android.graphics.Typeface;
michael@0 18 import android.os.Build;
michael@0 19 import android.support.v4.app.NotificationCompat;
michael@0 20 import android.text.Spannable;
michael@0 21 import android.text.SpannableString;
michael@0 22 import android.text.TextUtils;
michael@0 23 import android.text.style.StyleSpan;
michael@0 24
michael@0 25 public class DataReportingNotification {
michael@0 26
michael@0 27 private static final String LOGTAG = "DataReportNotification";
michael@0 28
michael@0 29 public static final String ALERT_NAME_DATAREPORTING_NOTIFICATION = "datareporting-notification";
michael@0 30
michael@0 31 private static final String PREFS_POLICY_NOTIFIED_TIME = "datareporting.policy.dataSubmissionPolicyNotifiedTime";
michael@0 32 private static final String PREFS_POLICY_VERSION = "datareporting.policy.dataSubmissionPolicyVersion";
michael@0 33 private static final int DATA_REPORTING_VERSION = 2;
michael@0 34
michael@0 35 public static void checkAndNotifyPolicy(Context context) {
michael@0 36 SharedPreferences dataPrefs = GeckoSharedPrefs.forApp(context);
michael@0 37 final int currentVersion = dataPrefs.getInt(PREFS_POLICY_VERSION, -1);
michael@0 38
michael@0 39 if (currentVersion < 1) {
michael@0 40 // This is a first run, so notify user about data policy.
michael@0 41 notifyDataPolicy(context, dataPrefs);
michael@0 42
michael@0 43 // If healthreport is enabled, set default preference value.
michael@0 44 if (AppConstants.MOZ_SERVICES_HEALTHREPORT) {
michael@0 45 SharedPreferences.Editor editor = dataPrefs.edit();
michael@0 46 editor.putBoolean(GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true);
michael@0 47 editor.commit();
michael@0 48 }
michael@0 49 return;
michael@0 50 }
michael@0 51
michael@0 52 if (currentVersion == 1) {
michael@0 53 // Redisplay notification only for Beta because version 2 updates Beta policy and update version.
michael@0 54 if (TextUtils.equals("beta", AppConstants.MOZ_UPDATE_CHANNEL)) {
michael@0 55 notifyDataPolicy(context, dataPrefs);
michael@0 56 } else {
michael@0 57 // Silently update the version.
michael@0 58 SharedPreferences.Editor editor = dataPrefs.edit();
michael@0 59 editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION);
michael@0 60 editor.commit();
michael@0 61 }
michael@0 62 return;
michael@0 63 }
michael@0 64
michael@0 65 if (currentVersion >= DATA_REPORTING_VERSION) {
michael@0 66 // Do nothing, we're at a current (or future) version.
michael@0 67 return;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 /**
michael@0 72 * Launch a notification of the data policy, and record notification time and version.
michael@0 73 */
michael@0 74 private static void notifyDataPolicy(Context context, SharedPreferences sharedPrefs) {
michael@0 75 boolean result = false;
michael@0 76 try {
michael@0 77 // Launch main App to launch Data choices when notification is clicked.
michael@0 78 Intent prefIntent = new Intent(GeckoApp.ACTION_LAUNCH_SETTINGS);
michael@0 79 prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.BROWSER_INTENT_CLASS_NAME);
michael@0 80
michael@0 81 GeckoPreferences.setResourceToOpen(prefIntent, "preferences_vendor");
michael@0 82 prefIntent.putExtra(ALERT_NAME_DATAREPORTING_NOTIFICATION, true);
michael@0 83
michael@0 84 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT);
michael@0 85 final Resources resources = context.getResources();
michael@0 86
michael@0 87 // Create and send notification.
michael@0 88 String notificationTitle = resources.getString(R.string.datareporting_notification_title);
michael@0 89 String notificationSummary;
michael@0 90 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
michael@0 91 notificationSummary = resources.getString(R.string.datareporting_notification_action);
michael@0 92 } else {
michael@0 93 // Display partial version of Big Style notification for supporting devices.
michael@0 94 notificationSummary = resources.getString(R.string.datareporting_notification_summary);
michael@0 95 }
michael@0 96 String notificationAction = resources.getString(R.string.datareporting_notification_action);
michael@0 97 String notificationBigSummary = resources.getString(R.string.datareporting_notification_summary);
michael@0 98
michael@0 99 // Make styled ticker text for display in notification bar.
michael@0 100 String tickerString = resources.getString(R.string.datareporting_notification_ticker_text);
michael@0 101 SpannableString tickerText = new SpannableString(tickerString);
michael@0 102 // Bold the notification title of the ticker text, which is the same string as notificationTitle.
michael@0 103 tickerText.setSpan(new StyleSpan(Typeface.BOLD), 0, notificationTitle.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
michael@0 104
michael@0 105 Notification notification = new NotificationCompat.Builder(context)
michael@0 106 .setContentTitle(notificationTitle)
michael@0 107 .setContentText(notificationSummary)
michael@0 108 .setSmallIcon(R.drawable.ic_status_logo)
michael@0 109 .setAutoCancel(true)
michael@0 110 .setContentIntent(contentIntent)
michael@0 111 .setStyle(new NotificationCompat.BigTextStyle()
michael@0 112 .bigText(notificationBigSummary))
michael@0 113 .addAction(R.drawable.firefox_settings_alert, notificationAction, contentIntent)
michael@0 114 .setTicker(tickerText)
michael@0 115 .build();
michael@0 116
michael@0 117 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
michael@0 118 int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode();
michael@0 119 notificationManager.notify(notificationID, notification);
michael@0 120
michael@0 121 // Record version and notification time.
michael@0 122 SharedPreferences.Editor editor = sharedPrefs.edit();
michael@0 123 long now = System.currentTimeMillis();
michael@0 124 editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now);
michael@0 125 editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION);
michael@0 126 editor.commit();
michael@0 127 result = true;
michael@0 128 } finally {
michael@0 129 // We want to track any errors, so record notification outcome.
michael@0 130 final String notificationEvent = TelemetryContract.Event.POLICY_NOTIFICATION_SUCCESS + result;
michael@0 131 Telemetry.sendUIEvent(notificationEvent);
michael@0 132 }
michael@0 133 }
michael@0 134 }

mercurial