Merge "Add spam blocking promo notification after user reports spam in after call notification."
diff --git a/java/com/android/dialer/logging/dialer_impression.proto b/java/com/android/dialer/logging/dialer_impression.proto
index cc65bf9..b1d038d 100644
--- a/java/com/android/dialer/logging/dialer_impression.proto
+++ b/java/com/android/dialer/logging/dialer_impression.proto
@@ -12,7 +12,7 @@
   // Event enums to be used for Impression Logging in Dialer.
   // It's perfectly acceptable for this enum to be large
   // Values should be from 1000 to 100000.
-  // Next Tag: 1379
+  // Next Tag: 1382
   enum Type {
     UNKNOWN_AOSP_EVENT_TYPE = 1000;
 
@@ -746,5 +746,13 @@
     LIGHTBRINGER_VIDEO_REQUESTED_FOR_FAVORITE_CONTACT = 1376;
     LIGHTBRINGER_VIDEO_REQUESTED_FOR_SUGGESTED_CONTACT = 1377;
     LIGHTBRINGER_VIDEO_REQUESTED_FOR_FAVORITE_CONTACT_DISAMBIG = 1378;
+
+    // Spam blocking after call notification promo shown for user.
+    SPAM_BLOCKING_AFTER_CALL_NOTIFICATION_PROMO_SHOWN = 1379;
+    // User enabled spam blocking through after call notification promo.
+    SPAM_BLOCKING_ENABLED_THROUGH_AFTER_CALL_NOTIFICATION_PROMO = 1380;
+    // Failure happened while enabling spam blocking through after call
+    // notification promo.
+    SPAM_BLOCKING_MODIFY_FAILURE_THROUGH_AFTER_CALL_NOTIFICATION_PROMO = 1381;
   }
 }
diff --git a/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java b/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java
index b5bdd74..891ac44 100644
--- a/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java
+++ b/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java
@@ -16,16 +16,23 @@
 
 package com.android.dialer.spam.promo;
 
+import android.annotation.SuppressLint;
 import android.app.FragmentManager;
+import android.app.Notification;
+import android.app.Notification.Builder;
+import android.app.PendingIntent;
 import android.content.Context;
 import android.content.DialogInterface.OnDismissListener;
 import android.preference.PreferenceManager;
 import android.support.design.widget.Snackbar;
+import android.support.v4.os.BuildCompat;
 import android.view.View;
 import android.widget.Toast;
 import com.android.dialer.configprovider.ConfigProviderBindings;
 import com.android.dialer.logging.DialerImpression;
 import com.android.dialer.logging.Logger;
+import com.android.dialer.notification.DialerNotificationManager;
+import com.android.dialer.notification.NotificationChannelId;
 import com.android.dialer.spam.SpamSettings;
 import com.android.dialer.spam.SpamSettings.ModifySettingListener;
 
@@ -139,4 +146,58 @@
             : context.getString(R.string.spam_blocking_settings_enable_error_text);
     Toast.makeText(context, toastText, Toast.LENGTH_LONG).show();
   }
+
+  /**
+   * Shows a spam blocking promo notification.
+   *
+   * @param notificationTag a string identifier for this notification.
+   * @param notificationId an identifier for this notification.
+   * @param contentIntent pending intent to be sent when notification is clicked.
+   * @param actionIntent pending intent to be sent when enable-spam-blocking button is clicked.
+   */
+  public void showSpamBlockingPromoNotification(
+      String notificationTag,
+      int notificationId,
+      PendingIntent contentIntent,
+      PendingIntent actionIntent) {
+    updateLastShowSpamTimestamp();
+    Logger.get(context)
+        .logImpression(DialerImpression.Type.SPAM_BLOCKING_AFTER_CALL_NOTIFICATION_PROMO_SHOWN);
+    DialerNotificationManager.notify(
+        context,
+        notificationTag,
+        notificationId,
+        getSpamBlockingPromoNotification(contentIntent, actionIntent));
+  }
+
+  /**
+   * Builds a spam blocking promo notification with given intents.
+   *
+   * @param contentIntent pending intent to be sent when notification is clicked.
+   * @param actionIntent pending intent to be sent when enable-spam-blocking button is clicked.
+   */
+  @SuppressLint("NewApi")
+  private Notification getSpamBlockingPromoNotification(
+      PendingIntent contentIntent, PendingIntent actionIntent) {
+    Notification.Builder builder =
+        new Builder(context)
+            .setContentIntent(contentIntent)
+            .setCategory(Notification.CATEGORY_STATUS)
+            .setPriority(Notification.PRIORITY_DEFAULT)
+            .setColor(context.getColor(R.color.dialer_theme_color))
+            .setSmallIcon(R.drawable.quantum_ic_call_vd_theme_24)
+            .setContentText(context.getString(R.string.spam_blocking_promo_text))
+            .addAction(
+                new Notification.Action.Builder(
+                        R.drawable.quantum_ic_block_vd_theme_24,
+                        context.getString(R.string.spam_blocking_promo_action_filter_spam),
+                        actionIntent)
+                    .build())
+            .setContentTitle(context.getString(R.string.spam_blocking_promo_title));
+
+    if (BuildCompat.isAtLeastO()) {
+      builder.setChannelId(NotificationChannelId.DEFAULT);
+    }
+    return builder.build();
+  }
 }
diff --git a/java/com/android/incallui/spam/SpamNotificationService.java b/java/com/android/incallui/spam/SpamNotificationService.java
index b85ab11..b418ea2 100644
--- a/java/com/android/incallui/spam/SpamNotificationService.java
+++ b/java/com/android/incallui/spam/SpamNotificationService.java
@@ -16,6 +16,7 @@
 
 package com.android.incallui.spam;
 
+import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
@@ -31,6 +32,8 @@
 import com.android.dialer.logging.ReportingLocation;
 import com.android.dialer.notification.DialerNotificationManager;
 import com.android.dialer.spam.SpamComponent;
+import com.android.dialer.spam.SpamSettings;
+import com.android.dialer.spam.promo.SpamBlockingPromoHelper;
 import com.android.incallui.call.DialerCall;
 
 /**
@@ -51,18 +54,29 @@
   private static final String EXTRA_NOTIFICATION_ID = "service_notification_id";
   private static final String EXTRA_CONTACT_LOOKUP_RESULT_TYPE =
       "service_contact_lookup_result_type";
+
+  private String notificationTag;
+  private int notificationId;
+
   /** Creates an intent to start this service. */
   public static Intent createServiceIntent(
-      Context context, DialerCall call, String action, String notificationTag, int notificationId) {
+      Context context,
+      @Nullable DialerCall call,
+      String action,
+      String notificationTag,
+      int notificationId) {
     Intent intent = new Intent(context, SpamNotificationService.class);
     intent.setAction(action);
-    intent.putExtra(EXTRA_PHONE_NUMBER, call.getNumber());
-    intent.putExtra(EXTRA_CALL_ID, call.getUniqueCallId());
-    intent.putExtra(EXTRA_CALL_START_TIME_MILLIS, call.getTimeAddedMs());
     intent.putExtra(EXTRA_NOTIFICATION_TAG, notificationTag);
     intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
-    intent.putExtra(
-        EXTRA_CONTACT_LOOKUP_RESULT_TYPE, call.getLogState().contactLookupResult.getNumber());
+
+    if (call != null) {
+      intent.putExtra(EXTRA_PHONE_NUMBER, call.getNumber());
+      intent.putExtra(EXTRA_CALL_ID, call.getUniqueCallId());
+      intent.putExtra(EXTRA_CALL_START_TIME_MILLIS, call.getTimeAddedMs());
+      intent.putExtra(
+          EXTRA_CONTACT_LOOKUP_RESULT_TYPE, call.getLogState().contactLookupResult.getNumber());
+    }
     return intent;
   }
 
@@ -83,14 +97,18 @@
       return START_NOT_STICKY;
     }
     String number = intent.getStringExtra(EXTRA_PHONE_NUMBER);
-    String notificationTag = intent.getStringExtra(EXTRA_NOTIFICATION_TAG);
-    int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 1);
+    notificationTag = intent.getStringExtra(EXTRA_NOTIFICATION_TAG);
+    notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 1);
     String countryIso = GeoUtil.getCurrentCountryIso(this);
     ContactLookupResult.Type contactLookupResultType =
         ContactLookupResult.Type.forNumber(intent.getIntExtra(EXTRA_CONTACT_LOOKUP_RESULT_TYPE, 0));
 
     DialerNotificationManager.cancel(this, notificationTag, notificationId);
 
+    SpamSettings spamSettings = SpamComponent.get(this).spamSettings();
+    SpamBlockingPromoHelper spamBlockingPromoHelper =
+        new SpamBlockingPromoHelper(this, SpamComponent.get(this).spamSettings());
+
     switch (intent.getAction()) {
       case SpamNotificationActivity.ACTION_MARK_NUMBER_AS_SPAM:
         logCallImpression(
@@ -104,6 +122,13 @@
                 ReportingLocation.Type.FEEDBACK_PROMPT,
                 contactLookupResultType);
         new FilteredNumberAsyncQueryHandler(this).blockNumber(null, number, countryIso);
+        if (spamBlockingPromoHelper.shouldShowSpamBlockingPromo()) {
+          spamBlockingPromoHelper.showSpamBlockingPromoNotification(
+              notificationTag,
+              notificationId,
+              createPromoActivityPendingIntent(),
+              createEnableSpamBlockingPendingIntent());
+        }
         break;
       case SpamNotificationActivity.ACTION_MARK_NUMBER_AS_NOT_SPAM:
         logCallImpression(
@@ -117,6 +142,22 @@
                 ReportingLocation.Type.FEEDBACK_PROMPT,
                 contactLookupResultType);
         break;
+      case SpamNotificationActivity.ACTION_ENABLE_SPAM_BLOCKING:
+        Logger.get(this)
+            .logImpression(
+                DialerImpression.Type.SPAM_BLOCKING_ENABLED_THROUGH_AFTER_CALL_NOTIFICATION_PROMO);
+        spamSettings.modifySpamBlockingSetting(
+            true,
+            success -> {
+              if (!success) {
+                Logger.get(this)
+                    .logImpression(
+                        DialerImpression.Type
+                            .SPAM_BLOCKING_MODIFY_FAILURE_THROUGH_AFTER_CALL_NOTIFICATION_PROMO);
+              }
+              spamBlockingPromoHelper.showModifySettingOnCompleteToast(success);
+            });
+        break;
       default: // fall out
     }
     // TODO: call stopSelf() after async tasks complete (a bug)
@@ -137,4 +178,28 @@
             intent.getStringExtra(EXTRA_CALL_ID),
             intent.getLongExtra(EXTRA_CALL_START_TIME_MILLIS, 0));
   }
+
+  private PendingIntent createPromoActivityPendingIntent() {
+    Intent intent =
+        SpamNotificationActivity.createActivityIntent(
+            this,
+            null,
+            SpamNotificationActivity.ACTION_SHOW_SPAM_BLOCKING_PROMO_DIALOG,
+            notificationTag,
+            notificationId);
+    return PendingIntent.getActivity(
+        this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
+  }
+
+  private PendingIntent createEnableSpamBlockingPendingIntent() {
+    Intent intent =
+        SpamNotificationService.createServiceIntent(
+            this,
+            null,
+            SpamNotificationActivity.ACTION_ENABLE_SPAM_BLOCKING,
+            notificationTag,
+            notificationId);
+    return PendingIntent.getService(
+        this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
+  }
 }