michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.preferences.GeckoPreferences; michael@0: michael@0: import android.app.Notification; michael@0: import android.app.NotificationManager; michael@0: import android.app.PendingIntent; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.content.SharedPreferences; michael@0: import android.content.res.Resources; michael@0: import android.graphics.Typeface; michael@0: import android.os.Build; michael@0: import android.support.v4.app.NotificationCompat; michael@0: import android.text.Spannable; michael@0: import android.text.SpannableString; michael@0: import android.text.TextUtils; michael@0: import android.text.style.StyleSpan; michael@0: michael@0: public class DataReportingNotification { michael@0: michael@0: private static final String LOGTAG = "DataReportNotification"; michael@0: michael@0: public static final String ALERT_NAME_DATAREPORTING_NOTIFICATION = "datareporting-notification"; michael@0: michael@0: private static final String PREFS_POLICY_NOTIFIED_TIME = "datareporting.policy.dataSubmissionPolicyNotifiedTime"; michael@0: private static final String PREFS_POLICY_VERSION = "datareporting.policy.dataSubmissionPolicyVersion"; michael@0: private static final int DATA_REPORTING_VERSION = 2; michael@0: michael@0: public static void checkAndNotifyPolicy(Context context) { michael@0: SharedPreferences dataPrefs = GeckoSharedPrefs.forApp(context); michael@0: final int currentVersion = dataPrefs.getInt(PREFS_POLICY_VERSION, -1); michael@0: michael@0: if (currentVersion < 1) { michael@0: // This is a first run, so notify user about data policy. michael@0: notifyDataPolicy(context, dataPrefs); michael@0: michael@0: // If healthreport is enabled, set default preference value. michael@0: if (AppConstants.MOZ_SERVICES_HEALTHREPORT) { michael@0: SharedPreferences.Editor editor = dataPrefs.edit(); michael@0: editor.putBoolean(GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true); michael@0: editor.commit(); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (currentVersion == 1) { michael@0: // Redisplay notification only for Beta because version 2 updates Beta policy and update version. michael@0: if (TextUtils.equals("beta", AppConstants.MOZ_UPDATE_CHANNEL)) { michael@0: notifyDataPolicy(context, dataPrefs); michael@0: } else { michael@0: // Silently update the version. michael@0: SharedPreferences.Editor editor = dataPrefs.edit(); michael@0: editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION); michael@0: editor.commit(); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (currentVersion >= DATA_REPORTING_VERSION) { michael@0: // Do nothing, we're at a current (or future) version. michael@0: return; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Launch a notification of the data policy, and record notification time and version. michael@0: */ michael@0: private static void notifyDataPolicy(Context context, SharedPreferences sharedPrefs) { michael@0: boolean result = false; michael@0: try { michael@0: // Launch main App to launch Data choices when notification is clicked. michael@0: Intent prefIntent = new Intent(GeckoApp.ACTION_LAUNCH_SETTINGS); michael@0: prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.BROWSER_INTENT_CLASS_NAME); michael@0: michael@0: GeckoPreferences.setResourceToOpen(prefIntent, "preferences_vendor"); michael@0: prefIntent.putExtra(ALERT_NAME_DATAREPORTING_NOTIFICATION, true); michael@0: michael@0: PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT); michael@0: final Resources resources = context.getResources(); michael@0: michael@0: // Create and send notification. michael@0: String notificationTitle = resources.getString(R.string.datareporting_notification_title); michael@0: String notificationSummary; michael@0: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { michael@0: notificationSummary = resources.getString(R.string.datareporting_notification_action); michael@0: } else { michael@0: // Display partial version of Big Style notification for supporting devices. michael@0: notificationSummary = resources.getString(R.string.datareporting_notification_summary); michael@0: } michael@0: String notificationAction = resources.getString(R.string.datareporting_notification_action); michael@0: String notificationBigSummary = resources.getString(R.string.datareporting_notification_summary); michael@0: michael@0: // Make styled ticker text for display in notification bar. michael@0: String tickerString = resources.getString(R.string.datareporting_notification_ticker_text); michael@0: SpannableString tickerText = new SpannableString(tickerString); michael@0: // Bold the notification title of the ticker text, which is the same string as notificationTitle. michael@0: tickerText.setSpan(new StyleSpan(Typeface.BOLD), 0, notificationTitle.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); michael@0: michael@0: Notification notification = new NotificationCompat.Builder(context) michael@0: .setContentTitle(notificationTitle) michael@0: .setContentText(notificationSummary) michael@0: .setSmallIcon(R.drawable.ic_status_logo) michael@0: .setAutoCancel(true) michael@0: .setContentIntent(contentIntent) michael@0: .setStyle(new NotificationCompat.BigTextStyle() michael@0: .bigText(notificationBigSummary)) michael@0: .addAction(R.drawable.firefox_settings_alert, notificationAction, contentIntent) michael@0: .setTicker(tickerText) michael@0: .build(); michael@0: michael@0: NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); michael@0: int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode(); michael@0: notificationManager.notify(notificationID, notification); michael@0: michael@0: // Record version and notification time. michael@0: SharedPreferences.Editor editor = sharedPrefs.edit(); michael@0: long now = System.currentTimeMillis(); michael@0: editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now); michael@0: editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION); michael@0: editor.commit(); michael@0: result = true; michael@0: } finally { michael@0: // We want to track any errors, so record notification outcome. michael@0: final String notificationEvent = TelemetryContract.Event.POLICY_NOTIFICATION_SUCCESS + result; michael@0: Telemetry.sendUIEvent(notificationEvent); michael@0: } michael@0: } michael@0: }