Skip to content

Commit 02d9603

Browse files
committed
Add channel to notification
Also pull out notification codes from Utils to NotificationUtil. Changed all support libraries version to 26.0.0-SNAPSHOT. Somehow, 26.+ won't resolve to 26.0.0-SNAPSHOT and 26.0.0-SNAPSHOT is the one having all the latest code, including setChannelId. Change-Id: I4dabfae5970fd3593f248b33cef23ee6e35d4b2b Fix: 38373595
1 parent c67f88a commit 02d9603

File tree

6 files changed

+84
-68
lines changed

6 files changed

+84
-68
lines changed

app/build.gradle

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ android {
3131
}
3232
}
3333

34-
productFlavors {
35-
target25 {
36-
targetSdkVersion 25
37-
}
38-
39-
targetO {
40-
targetSdkVersion 'O'
41-
}
42-
}
43-
4434
lintOptions {
4535
check 'NewApi'
4636
abortOnError true
@@ -76,9 +66,9 @@ android {
7666
}
7767

7868
dependencies {
79-
compile 'com.android.support:preference-v14:26.+'
80-
compile 'com.android.support:recyclerview-v7:26.+'
81-
compile 'com.android.support:support-v13:26.+'
69+
compile 'com.android.support:preference-v14:26.0.0-SNAPSHOT'
70+
compile 'com.android.support:recyclerview-v7:26.0.0-SNAPSHOT'
71+
compile 'com.android.support:support-v13:26.0.0-SNAPSHOT'
8272
compile 'com.google.android.gms:play-services-safetynet:+'
8373
compile(name:'setup-wizard-lib-platform-release', ext:'aar')
8474
}

app/src/main/java/com/afwsamples/testdpc/DeviceAdminReceiver.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.afwsamples.testdpc;
1818

19-
import static com.afwsamples.testdpc.policy.PolicyManagementFragment.OVERRIDE_KEY_SELECTION_KEY;
20-
2119
import android.annotation.TargetApi;
2220
import android.app.Notification;
2321
import android.app.NotificationManager;
@@ -35,10 +33,12 @@
3533
import android.os.UserHandle;
3634
import android.os.UserManager;
3735
import android.preference.PreferenceManager;
36+
import android.support.v7.app.NotificationCompat;
3837
import android.text.TextUtils;
3938
import android.util.Log;
4039
import android.widget.Toast;
4140

41+
import com.afwsamples.testdpc.common.NotificationUtil;
4242
import com.afwsamples.testdpc.common.Util;
4343
import com.afwsamples.testdpc.provision.PostProvisioningTask;
4444

@@ -59,6 +59,8 @@
5959
import java.util.Date;
6060
import java.util.List;
6161

62+
import static com.afwsamples.testdpc.policy.PolicyManagementFragment.OVERRIDE_KEY_SELECTION_KEY;
63+
6264
/**
6365
* Handles events related to the managed profile.
6466
*/
@@ -189,9 +191,9 @@ public void onProfileProvisioningComplete(Context context, Intent intent) {
189191
@Override
190192
public void onBugreportSharingDeclined(Context context, Intent intent) {
191193
Log.i(TAG, "Bugreport sharing declined");
192-
Util.showNotification(context, R.string.bugreport_title,
194+
NotificationUtil.showNotification(context, R.string.bugreport_title,
193195
context.getString(R.string.bugreport_sharing_declined),
194-
Util.BUGREPORT_NOTIFICATION_ID);
196+
NotificationUtil.BUGREPORT_NOTIFICATION_ID);
195197
}
196198

197199
@TargetApi(Build.VERSION_CODES.N)
@@ -234,8 +236,8 @@ protected String doInBackground(Void... params) {
234236

235237
@Override
236238
protected void onPostExecute(String message) {
237-
Util.showNotification(context, R.string.bugreport_title,
238-
message, Util.BUGREPORT_NOTIFICATION_ID);
239+
NotificationUtil.showNotification(context, R.string.bugreport_title,
240+
message, NotificationUtil.BUGREPORT_NOTIFICATION_ID);
239241
result.finish();
240242
}
241243

@@ -258,9 +260,9 @@ public void onBugreportFailed(Context context, Intent intent, int failureCode) {
258260
R.string.bugreport_failure_failed_completing);
259261
}
260262
Log.i(TAG, "Bugreport failed: " + failureReason);
261-
Util.showNotification(context, R.string.bugreport_title,
263+
NotificationUtil.showNotification(context, R.string.bugreport_title,
262264
context.getString(R.string.bugreport_failure_message, failureReason),
263-
Util.BUGREPORT_NOTIFICATION_ID);
265+
NotificationUtil.BUGREPORT_NOTIFICATION_ID);
264266
}
265267

266268

@@ -271,9 +273,9 @@ public void onUserAdded(Context context, Intent intent, UserHandle newUser) {
271273
String message = context.getString(R.string.on_user_added_message,
272274
userManager.getSerialNumberForUser(newUser));
273275
Log.i(TAG, message);
274-
Util.showNotification(context, R.string.on_user_added_title,
276+
NotificationUtil.showNotification(context, R.string.on_user_added_title,
275277
message,
276-
Util.USER_ADDED_NOTIFICATION_ID);
278+
NotificationUtil.USER_ADDED_NOTIFICATION_ID);
277279
}
278280

279281
@TargetApi(Build.VERSION_CODES.O)
@@ -283,8 +285,8 @@ public void onUserRemoved(Context context, Intent intent, UserHandle removedUser
283285
String message = context.getString(R.string.on_user_removed_message,
284286
userManager.getSerialNumberForUser(removedUser));
285287
Log.i(TAG, message);
286-
Util.showNotification(context, R.string.on_user_removed_title, message,
287-
Util.USER_REMOVED_NOTIFICATION_ID);
288+
NotificationUtil.showNotification(context, R.string.on_user_removed_title, message,
289+
NotificationUtil.USER_REMOVED_NOTIFICATION_ID);
288290
}
289291

290292
@TargetApi(Build.VERSION_CODES.M)
@@ -349,11 +351,11 @@ public void onPasswordExpiring(Context context, Intent intent, UserHandle user)
349351
devicePolicyManager.getPasswordExpiration(getComponentName(context));
350352
final boolean expiredBySelf = (timeNow >= timeAdminExpires && timeAdminExpires != 0);
351353

352-
Util.showNotification(context, R.string.password_expired_title,
354+
NotificationUtil.showNotification(context, R.string.password_expired_title,
353355
context.getString(expiredBySelf
354356
? R.string.password_expired_by_self
355357
: R.string.password_expired_by_others),
356-
Util.PASSWORD_EXPIRATION_NOTIFICATION_ID);
358+
NotificationUtil.PASSWORD_EXPIRATION_NOTIFICATION_ID);
357359
}
358360

359361
@Deprecated
@@ -397,15 +399,15 @@ public void onPasswordFailed(Context context, Intent intent, UserHandle user) {
397399
: context.getResources().getQuantityString(
398400
R.plurals.password_failed_attempts_content, maxAttempts, maxAttempts);
399401

400-
Notification.Builder warn = new Notification.Builder(context)
401-
.setSmallIcon(R.drawable.ic_launcher)
402+
NotificationCompat.Builder warn = NotificationUtil.getNotificationBuilder(context);
403+
warn.setSmallIcon(R.drawable.ic_launcher)
402404
.setTicker(title)
403405
.setContentTitle(title)
404406
.setContentText(content)
405407
.setContentIntent(PendingIntent.getActivity(context, /* requestCode */ -1,
406408
new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD), /* flags */ 0));
407409

408-
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
410+
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
409411
inboxStyle.setBigContentTitle(title);
410412

411413
final DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance();
@@ -416,7 +418,7 @@ public void onPasswordFailed(Context context, Intent intent, UserHandle user) {
416418

417419
NotificationManager nm = (NotificationManager)
418420
context.getSystemService(Context.NOTIFICATION_SERVICE);
419-
nm.notify(PASSWORD_FAILED_NOTIFICATION_ID, warn.getNotification());
421+
nm.notify(PASSWORD_FAILED_NOTIFICATION_ID, warn.build());
420422
}
421423

422424
@Deprecated
@@ -520,8 +522,8 @@ private static void updatePasswordQualityNotification(Context context) {
520522
context.getSystemService(Context.NOTIFICATION_SERVICE);
521523

522524
if (!devicePolicyManager.isActivePasswordSufficient()) {
523-
Notification.Builder warn = new Notification.Builder(context)
524-
.setOngoing(true)
525+
NotificationCompat.Builder warn = NotificationUtil.getNotificationBuilder(context);
526+
warn.setOngoing(true)
525527
.setSmallIcon(R.drawable.ic_launcher)
526528
.setTicker(context.getText(R.string.password_not_compliant_title))
527529
.setContentTitle(context.getText(R.string.password_not_compliant_title))

app/src/main/java/com/afwsamples/testdpc/PackageMonitorReceiver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import android.content.Intent;
66
import android.text.TextUtils;
77

8-
import com.afwsamples.testdpc.common.Util;
8+
import com.afwsamples.testdpc.common.NotificationUtil;
99

1010
public class PackageMonitorReceiver extends BroadcastReceiver {
1111
private static final String TAG = "PackageMonitorReceiver";
@@ -27,7 +27,7 @@ public void onReceive(Context context, Intent intent) {
2727
return;
2828
}
2929
String notificationBody = buildNotificationText(context, packageName, action);
30-
Util.showNotification(context,
30+
NotificationUtil.showNotification(context,
3131
R.string.package_changed_notification_title,
3232
notificationBody,
3333
PACKAGE_CHANGED_NOTIIFICATION_ID);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.afwsamples.testdpc.common;
2+
3+
import android.app.Notification;
4+
import android.app.NotificationChannel;
5+
import android.app.NotificationManager;
6+
import android.content.Context;
7+
import android.os.Build;
8+
import android.support.annotation.RequiresApi;
9+
import android.support.annotation.StringRes;
10+
import android.support.v4.os.BuildCompat;
11+
import android.support.v7.app.NotificationCompat;
12+
13+
import com.afwsamples.testdpc.R;
14+
15+
public class NotificationUtil {
16+
private static final String TAG = "NotificationUtil";
17+
private static final String DEFAULT_CHANNEL_ID = "default_testdpc_channel";
18+
public static final int BUGREPORT_NOTIFICATION_ID = 1;
19+
public static final int PASSWORD_EXPIRATION_NOTIFICATION_ID = 2;
20+
public static final int USER_ADDED_NOTIFICATION_ID = 3;
21+
public static final int USER_REMOVED_NOTIFICATION_ID = 4;
22+
23+
public static void showNotification(
24+
Context context, @StringRes int titleId, String msg, int notificationId) {
25+
NotificationManager notificationManager =
26+
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
27+
Notification notification = getNotificationBuilder(context)
28+
.setSmallIcon(R.drawable.ic_launcher)
29+
.setContentTitle(context.getString(titleId))
30+
.setContentText(msg)
31+
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
32+
.build();
33+
notificationManager.notify(notificationId, notification);
34+
}
35+
36+
public static NotificationCompat.Builder getNotificationBuilder(Context context) {
37+
if (BuildCompat.isAtLeastO()) {
38+
createDefaultNotificationChannel(context);
39+
}
40+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
41+
builder.setChannelId(DEFAULT_CHANNEL_ID);
42+
return builder;
43+
}
44+
45+
@RequiresApi(Build.VERSION_CODES.O)
46+
private static void createDefaultNotificationChannel(Context context) {
47+
NotificationManager notificationManager =
48+
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
49+
String appName = context.getString(R.string.app_name);
50+
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID,
51+
appName, NotificationManager.IMPORTANCE_DEFAULT);
52+
notificationManager.createNotificationChannel(channel);
53+
}
54+
55+
}

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,6 @@
5757
public class Util {
5858
private static final String TAG = "Util";
5959
private static final int DEFAULT_BUFFER_SIZE = 4096;
60-
public static final int BUGREPORT_NOTIFICATION_ID = 1;
61-
public static final int PASSWORD_EXPIRATION_NOTIFICATION_ID = 2;
62-
public static final int USER_ADDED_NOTIFICATION_ID = 3;
63-
public static final int USER_REMOVED_NOTIFICATION_ID = 4;
64-
private static final String DEFAULT_CHANNEL_ID = "default_testdpc_channel";
65-
66-
public static void showNotification(Context context, int titleId, String msg,
67-
int notificationId) {
68-
NotificationManager notificationManager =
69-
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
70-
Log.d(TAG, "showNotification: " + BuildCompat.isAtLeastO());
71-
if (BuildCompat.isAtLeastO()) {
72-
createDefaultNotificationChannel(context, notificationManager);
73-
}
74-
Notification notification = new NotificationCompat.Builder(context)
75-
.setSmallIcon(R.drawable.ic_launcher)
76-
.setContentTitle(context.getString(titleId))
77-
.setContentText(msg)
78-
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
79-
.setChannel(DEFAULT_CHANNEL_ID)
80-
.build();
81-
notificationManager.notify(notificationId, notification);
82-
}
83-
84-
@RequiresApi(VERSION_CODES.O)
85-
private static void createDefaultNotificationChannel(
86-
Context context, NotificationManager notificationManager) {
87-
String appName = context.getString(R.string.app_name);
88-
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID,
89-
appName, NotificationManager.IMPORTANCE_DEFAULT);
90-
notificationManager.createNotificationChannel(channel);
91-
}
9260

9361
/**
9462
* Format a friendly datetime for the current locale according to device policy documentation.

app/src/main/java/com/afwsamples/testdpc/comp/DeviceOwnerService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.util.Log;
2828

2929
import com.afwsamples.testdpc.R;
30+
import com.afwsamples.testdpc.common.NotificationUtil;
3031
import com.afwsamples.testdpc.common.Util;
3132

3233
public class DeviceOwnerService extends Service {
@@ -56,7 +57,7 @@ private DeviceOwnerServiceImpl(Context context) {
5657
@Override
5758
public void notifyUserIsUnlocked(UserHandle callingUserHandle) throws RemoteException {
5859
long userSerialNumber = mUserManager.getSerialNumberForUser(callingUserHandle);
59-
Util.showNotification(mContext, R.string.po_user_status,
60+
NotificationUtil.showNotification(mContext, R.string.po_user_status,
6061
mContext.getString(R.string.po_user_is_unlocked, userSerialNumber), 0);
6162
Log.d(TAG, "notifyUserIsUnlocked() called for user with serial " + userSerialNumber);
6263
}

0 commit comments

Comments
 (0)