Skip to content

Commit 8dc340d

Browse files
committed
Add InterruptionLevel to API
1 parent bfa8ca1 commit 8dc340d

File tree

8 files changed

+113
-47
lines changed

8 files changed

+113
-47
lines changed

firebase-appdistribution-api/src/main/java/com/google/firebase/appdistribution/FirebaseAppDistribution.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
package com.google.firebase.appdistribution;
1616

1717
import android.app.Activity;
18-
import android.app.NotificationChannel;
1918
import android.net.Uri;
2019
import androidx.annotation.NonNull;
2120
import androidx.annotation.Nullable;
22-
import androidx.core.app.NotificationCompat;
2321
import com.google.android.gms.tasks.Task;
2422
import com.google.firebase.FirebaseApp;
2523
import com.google.firebase.appdistribution.internal.FirebaseAppDistributionProxy;
@@ -202,17 +200,13 @@ public interface FirebaseAppDistribution {
202200
* <li>Starts a full screen activity for the tester to compose and submit the feedback
203201
* </ol>
204202
*
205-
* <p>On Android 8 and above, the notification will be created in its own notification channel.
206-
*
207203
* @param infoTextResourceId string resource ID of text to display to the tester before collecting
208204
* feedback data (e.g. Terms and Conditions)
209-
* @param importance the level of interruption for the feedback notification channel. Once the
210-
* channel's importance is set it cannot be changed except by the user. See {@link
211-
* NotificationChannel#setImportance}. On platforms below Android 8, the importance will be
212-
* translated into a comparable notification priority (see {@link
213-
* NotificationCompat.Builder#setPriority}).
205+
* @param interruptionLevel the level of interruption for the feedback notification. On platforms
206+
* below Android 8, this corresponds to a notification channel importance and once set cannot
207+
* be changed except by the user.
214208
*/
215-
void showFeedbackNotification(int infoTextResourceId, int importance);
209+
void showFeedbackNotification(int infoTextResourceId, InterruptionLevel interruptionLevel);
216210

217211
/**
218212
* Displays a notification that, when tapped, will take a screenshot of the current activity, then
@@ -233,17 +227,14 @@ public interface FirebaseAppDistribution {
233227
* <li>Starts a full screen activity for the tester to compose and submit the feedback
234228
* </ol>
235229
*
236-
* <p>On Android 8 and above, the notification will be created in its own notification channel.
237-
*
238230
* @param infoText text to display to the tester before collecting feedback data (e.g. Terms and
239231
* Conditions)
240-
* @param importance the level of interruption for the feedback notification channel. Once the
241-
* channel's importance is set it cannot be changed except by the user. See {@link
242-
* NotificationChannel#setImportance}. On platforms below Android 8, the importance will be
243-
* translated into a comparable notification priority (see {@link
244-
* NotificationCompat.Builder#setPriority}).
232+
* @param interruptionLevel the level of interruption for the feedback notification. On platforms
233+
* below Android 8, this corresponds to a notification channel importance and once set cannot
234+
* be changed except by the user.
245235
*/
246-
void showFeedbackNotification(@NonNull CharSequence infoText, int importance);
236+
void showFeedbackNotification(
237+
@NonNull CharSequence infoText, InterruptionLevel interruptionLevel);
247238

248239
/**
249240
* Hides the notification shown with {@link #showFeedbackNotification}.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.google.firebase.appdistribution;
2+
3+
import android.app.NotificationManager;
4+
import androidx.core.app.NotificationCompat;
5+
6+
/** An enum specifying the level of interruption of a notification when it is created. */
7+
public enum InterruptionLevel {
8+
9+
/**
10+
* Minimum interruption level.
11+
*
12+
* <p>Translates to {@link NotificationManager#IMPORTANCE_MIN} on Android O+ and {@link
13+
* NotificationCompat#PRIORITY_MIN} on older platforms.
14+
*/
15+
MIN,
16+
17+
/**
18+
* Low interruption level.
19+
*
20+
* <p>Translates to {@link NotificationManager#IMPORTANCE_LOW} on Android O+ and {@link
21+
* NotificationCompat#PRIORITY_LOW} on older platforms.
22+
*/
23+
LOW,
24+
25+
/**
26+
* Default interruption level.
27+
*
28+
* <p>Translates to {@link NotificationManager#IMPORTANCE_DEFAULT} on Android O+ and {@link
29+
* NotificationCompat#PRIORITY_DEFAULT} on older platforms.
30+
*/
31+
DEFAULT,
32+
33+
/**
34+
* High interruption level.
35+
*
36+
* <p>Translates to {@link NotificationManager#IMPORTANCE_HIGH} on Android O+ and {@link
37+
* NotificationCompat#PRIORITY_HIGH} on older platforms.
38+
*/
39+
HIGH,
40+
41+
/**
42+
* Maximum interruption level.
43+
*
44+
* <p>Translates to {@link NotificationManager#IMPORTANCE_HIGH} on Android O+ and {@link
45+
* NotificationCompat#PRIORITY_MAX} on older platforms.
46+
*/
47+
MAX
48+
}

firebase-appdistribution-api/src/main/java/com/google/firebase/appdistribution/internal/FirebaseAppDistributionProxy.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.firebase.appdistribution.AppDistributionRelease;
2222
import com.google.firebase.appdistribution.FirebaseAppDistribution;
2323
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
24+
import com.google.firebase.appdistribution.InterruptionLevel;
2425
import com.google.firebase.appdistribution.UpdateTask;
2526
import com.google.firebase.inject.Provider;
2627

@@ -95,13 +96,15 @@ public void startFeedback(@NonNull CharSequence infoText, @Nullable Uri screensh
9596
}
9697

9798
@Override
98-
public void showFeedbackNotification(int infoTextResourceId, int importance) {
99-
delegate.showFeedbackNotification(infoTextResourceId, importance);
99+
public void showFeedbackNotification(
100+
int infoTextResourceId, InterruptionLevel interruptionLevel) {
101+
delegate.showFeedbackNotification(infoTextResourceId, interruptionLevel);
100102
}
101103

102104
@Override
103-
public void showFeedbackNotification(@NonNull CharSequence infoText, int importance) {
104-
delegate.showFeedbackNotification(infoText, importance);
105+
public void showFeedbackNotification(
106+
@NonNull CharSequence infoText, InterruptionLevel interruptionLevel) {
107+
delegate.showFeedbackNotification(infoText, interruptionLevel);
105108
}
106109

107110
@Override

firebase-appdistribution-api/src/main/java/com/google/firebase/appdistribution/internal/FirebaseAppDistributionStub.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.firebase.appdistribution.FirebaseAppDistribution;
3131
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
3232
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
33+
import com.google.firebase.appdistribution.InterruptionLevel;
3334
import com.google.firebase.appdistribution.OnProgressListener;
3435
import com.google.firebase.appdistribution.UpdateTask;
3536
import java.util.concurrent.Executor;
@@ -85,10 +86,12 @@ public void startFeedback(int infoTextResourceId, @Nullable Uri screenshotUri) {
8586
public void startFeedback(@NonNull CharSequence infoText, @Nullable Uri screenshotUri) {}
8687

8788
@Override
88-
public void showFeedbackNotification(int infoTextResourceId, int importance) {}
89+
public void showFeedbackNotification(
90+
int infoTextResourceId, InterruptionLevel interruptionLevel) {}
8991

9092
@Override
91-
public void showFeedbackNotification(@NonNull CharSequence infoText, int importance) {}
93+
public void showFeedbackNotification(
94+
@NonNull CharSequence infoText, InterruptionLevel interruptionLevel) {}
9295

9396
@Override
9497
public void cancelFeedbackNotification() {}

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.google.firebase.appdistribution.FirebaseAppDistribution;
4040
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
4141
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
42+
import com.google.firebase.appdistribution.InterruptionLevel;
4243
import com.google.firebase.appdistribution.UpdateProgress;
4344
import com.google.firebase.appdistribution.UpdateStatus;
4445
import com.google.firebase.appdistribution.UpdateTask;
@@ -352,13 +353,15 @@ public void startFeedback(@NonNull CharSequence infoText, @Nullable Uri screensh
352353
}
353354

354355
@Override
355-
public void showFeedbackNotification(int infoTextResourceId, int importance) {
356-
showFeedbackNotification(getText(infoTextResourceId), importance);
356+
public void showFeedbackNotification(
357+
int infoTextResourceId, InterruptionLevel interruptionLevel) {
358+
showFeedbackNotification(getText(infoTextResourceId), interruptionLevel);
357359
}
358360

359361
@Override
360-
public void showFeedbackNotification(@NonNull CharSequence infoText, int importance) {
361-
notificationsManager.showFeedbackNotification(infoText, importance);
362+
public void showFeedbackNotification(
363+
@NonNull CharSequence infoText, InterruptionLevel interruptionLevel) {
364+
notificationsManager.showFeedbackNotification(infoText, interruptionLevel);
362365
}
363366

364367
@Override

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionNotificationsManager.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import android.app.NotificationChannel;
1818
import android.app.NotificationChannelGroup;
19-
import android.app.NotificationManager;
2019
import android.app.PendingIntent;
2120
import android.content.Context;
2221
import android.content.Intent;
@@ -29,6 +28,7 @@
2928
import androidx.annotation.VisibleForTesting;
3029
import androidx.core.app.NotificationCompat;
3130
import androidx.core.app.NotificationManagerCompat;
31+
import com.google.firebase.appdistribution.InterruptionLevel;
3232

3333
class FirebaseAppDistributionNotificationsManager {
3434
private static final String TAG = "FirebaseAppDistributionNotificationsManager";
@@ -78,7 +78,7 @@ void showAppUpdateNotification(long totalBytes, long downloadedBytes, int string
7878
Notification.APP_UPDATE,
7979
R.string.app_update_notification_channel_name,
8080
R.string.app_update_notification_channel_description,
81-
NotificationManager.IMPORTANCE_DEFAULT);
81+
InterruptionLevel.DEFAULT);
8282
}
8383

8484
if (!notificationManager.areNotificationsEnabled()) {
@@ -129,7 +129,8 @@ private static int getPendingIntentFlags(int baseFlags) {
129129
: baseFlags;
130130
}
131131

132-
public void showFeedbackNotification(@NonNull CharSequence infoText, int importance) {
132+
public void showFeedbackNotification(
133+
@NonNull CharSequence infoText, InterruptionLevel interruptionLevel) {
133134
// Create the NotificationChannel, but only on API 26+ because
134135
// the NotificationChannel class is new and not in the support library
135136
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -138,7 +139,7 @@ public void showFeedbackNotification(@NonNull CharSequence infoText, int importa
138139
Notification.FEEDBACK,
139140
R.string.feedback_notification_channel_name,
140141
R.string.feedback_notification_channel_description,
141-
importance);
142+
interruptionLevel);
142143
}
143144

144145
if (!notificationManager.areNotificationsEnabled()) {
@@ -161,7 +162,7 @@ public void showFeedbackNotification(@NonNull CharSequence infoText, int importa
161162
.setSmallIcon(appIconSource.getNonAdaptiveIconOrDefault(context))
162163
.setContentTitle(context.getString(R.string.feedback_notification_title))
163164
.setContentText(context.getString(R.string.feedback_notification_text, appLabel))
164-
.setPriority(convertImportanceToPriority(importance))
165+
.setPriority(getNotificationPriority(interruptionLevel))
165166
.setOngoing(true)
166167
.setOnlyAlertOnce(true)
167168
.setAutoCancel(false)
@@ -177,29 +178,44 @@ public void cancelFeedbackNotification() {
177178
.cancel(Notification.FEEDBACK.tag, Notification.FEEDBACK.id);
178179
}
179180

180-
private int convertImportanceToPriority(int importance) {
181-
switch (importance) {
182-
case NotificationManagerCompat.IMPORTANCE_MIN:
181+
private int getNotificationPriority(InterruptionLevel interruptionLevel) {
182+
switch (interruptionLevel) {
183+
case MIN:
183184
return NotificationCompat.PRIORITY_MIN;
184-
case NotificationManagerCompat.IMPORTANCE_LOW:
185+
case LOW:
185186
return NotificationCompat.PRIORITY_LOW;
186-
case NotificationManagerCompat.IMPORTANCE_HIGH:
187+
case HIGH:
187188
return NotificationCompat.PRIORITY_HIGH;
188-
case NotificationManagerCompat.IMPORTANCE_MAX:
189+
case MAX:
189190
return NotificationCompat.PRIORITY_MAX;
190-
case NotificationManagerCompat.IMPORTANCE_UNSPECIFIED:
191-
case NotificationManagerCompat.IMPORTANCE_NONE:
192-
case NotificationManagerCompat.IMPORTANCE_DEFAULT:
191+
case DEFAULT:
193192
default:
194193
return NotificationCompat.PRIORITY_DEFAULT;
195194
}
196195
}
197196

197+
private int getChannelImportance(InterruptionLevel interruptionLevel) {
198+
switch (interruptionLevel) {
199+
case MIN:
200+
return NotificationManagerCompat.IMPORTANCE_MIN;
201+
case LOW:
202+
return NotificationManagerCompat.IMPORTANCE_LOW;
203+
case HIGH:
204+
case MAX: // IMPORTANCE_MAX exists but is so far unused
205+
return NotificationManagerCompat.IMPORTANCE_HIGH;
206+
case DEFAULT:
207+
default:
208+
return NotificationManagerCompat.IMPORTANCE_DEFAULT;
209+
}
210+
}
211+
198212
@RequiresApi(Build.VERSION_CODES.O)
199-
private void createChannel(Notification notification, int name, int description, int importance) {
213+
private void createChannel(
214+
Notification notification, int name, int description, InterruptionLevel interruptionLevel) {
200215
notificationManager.createNotificationChannelGroup(
201216
new NotificationChannelGroup(
202217
CHANNEL_GROUP_ID, context.getString(R.string.notifications_group_name)));
218+
int importance = getChannelImportance(interruptionLevel);
203219
NotificationChannel channel =
204220
new NotificationChannel(notification.channelId, context.getString(name), importance);
205221
channel.setDescription(context.getString(description));

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionNotificationsManagerTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.content.Intent;
2929
import androidx.test.core.app.ApplicationProvider;
3030
import com.google.firebase.FirebaseApp;
31+
import com.google.firebase.appdistribution.InterruptionLevel;
3132
import org.junit.Before;
3233
import org.junit.Test;
3334
import org.junit.runner.RunWith;
@@ -110,7 +111,7 @@ public void showAppUpdateNotification_withZeroTotalBytes_shows0Percent() {
110111
@Test
111112
public void showFeedbackNotification_createsGroupAndChannel() {
112113
firebaseAppDistributionNotificationsManager.showFeedbackNotification(
113-
"Terms and conditions", IMPORTANCE_HIGH);
114+
"Terms and conditions", InterruptionLevel.HIGH);
114115
assertThat(shadowOf(notificationManager).getNotificationChannelGroup(CHANNEL_GROUP_ID))
115116
.isNotNull();
116117
assertThat(shadowOf(notificationManager).getNotificationChannels()).hasSize(1);
@@ -123,7 +124,7 @@ public void showFeedbackNotification_createsGroupAndChannel() {
123124
@Test
124125
public void showFeedbackNotification_setsIntentToScreenshotActivity() {
125126
firebaseAppDistributionNotificationsManager.showFeedbackNotification(
126-
"Terms and conditions", IMPORTANCE_HIGH);
127+
"Terms and conditions", InterruptionLevel.HIGH);
127128
assertThat(shadowOf(notificationManager).size()).isEqualTo(1);
128129
Notification notification =
129130
shadowOf(notificationManager).getNotification(FEEDBACK.tag, FEEDBACK.id);
@@ -141,7 +142,7 @@ public void showFeedbackNotification_setsIntentToScreenshotActivity() {
141142
@Test
142143
public void showFeedbackNotification_convertsImportanceToPriority() {
143144
firebaseAppDistributionNotificationsManager.showFeedbackNotification(
144-
"Terms and conditions", IMPORTANCE_HIGH);
145+
"Terms and conditions", InterruptionLevel.HIGH);
145146
assertThat(shadowOf(notificationManager).size()).isEqualTo(1);
146147
Notification notification =
147148
shadowOf(notificationManager).getNotification(FEEDBACK.tag, FEEDBACK.id);

firebase-appdistribution/test-app/src/main/java/com/googletest/firebase/appdistribution/testapp/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.core.widget.doOnTextChanged
2020
import com.google.android.gms.tasks.Task
2121
import com.google.android.material.textfield.TextInputLayout
2222
import com.google.firebase.appdistribution.AppDistributionRelease
23+
import com.google.firebase.appdistribution.InterruptionLevel
2324
import com.google.firebase.appdistribution.UpdateProgress
2425
import com.google.firebase.appdistribution.ktx.appDistribution
2526
import com.google.firebase.ktx.Firebase
@@ -94,7 +95,7 @@ class MainActivity : AppCompatActivity() {
9495
disableAllFeedbackTriggers()
9596
Log.i(TAG, "Enabling notification trigger (SDK)")
9697
firebaseAppDistribution.showFeedbackNotification(
97-
R.string.termsAndConditions, NotificationManagerCompat.IMPORTANCE_HIGH)
98+
R.string.termsAndConditions, InterruptionLevel.HIGH)
9899
}
99100
FeedbackTrigger.CUSTOM_NOTIFICATION.label -> {
100101
disableAllFeedbackTriggers()

0 commit comments

Comments
 (0)