Skip to content

Commit 74ebd7f

Browse files
committed
feat(channel): Move isNotificationChannelSupported method to BuildUtil class. (#92)
1 parent 7111463 commit 74ebd7f

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

AndroidSDK/src/com/leanplum/Leanplum.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.leanplum.internal.Util.DeviceIdInfo;
5353
import com.leanplum.internal.VarCache;
5454
import com.leanplum.messagetemplates.MessageTemplates;
55+
import com.leanplum.utils.BuildUtil;
5556
import com.leanplum.utils.SharedPreferencesUtil;
5657

5758
import org.json.JSONArray;
@@ -736,7 +737,7 @@ protected Void doInBackground(Void... params) {
736737
Log.d("No variants received from the server.");
737738
}
738739

739-
if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) {
740+
if (BuildUtil.isNotificationChannelSupported(context)) {
740741
// Get notification channels and groups.
741742
JSONArray notificationChannels = response.optJSONArray(
742743
Constants.Keys.NOTIFICATION_CHANNELS);
@@ -821,7 +822,7 @@ public void run() {
821822
try {
822823
NotificationCompat.Builder builder =
823824
LeanplumNotificationHelper.getDefaultNotificationBuilder(context,
824-
LeanplumNotificationChannel.isNotificationChannelSupported(context));
825+
BuildUtil.isNotificationChannelSupported(context));
825826
if (builder == null) {
826827
return;
827828
}

AndroidSDK/src/com/leanplum/LeanplumNotificationChannel.java

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@
2828
import android.app.NotificationManager;
2929
import android.content.Context;
3030
import android.content.SharedPreferences;
31-
import android.os.Build;
3231
import android.text.TextUtils;
3332

3433
import com.leanplum.internal.CollectionUtil;
3534
import com.leanplum.internal.Constants;
3635
import com.leanplum.internal.JsonConverter;
3736
import com.leanplum.internal.Log;
3837
import com.leanplum.internal.Util;
38+
import com.leanplum.utils.BuildUtil;
3939
import com.leanplum.utils.SharedPreferencesUtil;
4040

4141
import org.json.JSONArray;
4242

43-
import java.util.ArrayList;
4443
import java.util.HashMap;
4544
import java.util.Iterator;
4645
import java.util.List;
@@ -54,7 +53,6 @@
5453
*/
5554
@TargetApi(26)
5655
class LeanplumNotificationChannel {
57-
private static int targetSdk = -1;
5856

5957
/**
6058
* Configures notification channels.
@@ -359,7 +357,7 @@ private static void createNotificationChannel(Context context, String channelId,
359357
if (context == null || TextUtils.isEmpty(channelId)) {
360358
return;
361359
}
362-
if (isNotificationChannelSupported(context)) {
360+
if (BuildUtil.isNotificationChannelSupported(context)) {
363361
try {
364362
NotificationManager notificationManager =
365363
context.getSystemService(NotificationManager.class);
@@ -408,7 +406,7 @@ private static void deleteNotificationChannel(Context context, String channelId)
408406
if (context == null) {
409407
return;
410408
}
411-
if (isNotificationChannelSupported(context)) {
409+
if (BuildUtil.isNotificationChannelSupported(context)) {
412410
try {
413411
NotificationManager notificationManager =
414412
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -451,7 +449,7 @@ private static void createNotificationGroup(Context context, String groupId, Str
451449
if (context == null || TextUtils.isEmpty(groupId)) {
452450
return;
453451
}
454-
if (isNotificationChannelSupported(context)) {
452+
if (BuildUtil.isNotificationChannelSupported(context)) {
455453
try {
456454
NotificationManager notificationManager =
457455
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -478,7 +476,7 @@ private static void deleteNotificationGroup(Context context, String groupId) {
478476
if (context == null || TextUtils.isEmpty(groupId)) {
479477
return;
480478
}
481-
if (isNotificationChannelSupported(context)) {
479+
if (BuildUtil.isNotificationChannelSupported(context)) {
482480
try {
483481
NotificationManager notificationManager =
484482
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -501,7 +499,7 @@ private static void deleteNotificationGroup(Context context, String groupId) {
501499
* @return Returns all notification channels belonging to the calling package.
502500
*/
503501
static List<NotificationChannel> getNotificationChannels(Context context) {
504-
if (isNotificationChannelSupported(context)) {
502+
if (BuildUtil.isNotificationChannelSupported(context)) {
505503
NotificationManager notificationManager =
506504
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
507505
if (notificationManager == null) {
@@ -520,7 +518,7 @@ static List<NotificationChannel> getNotificationChannels(Context context) {
520518
* @return Id of default notification channel.
521519
*/
522520
static String getDefaultNotificationChannelId(Context context) {
523-
if (isNotificationChannelSupported(context)) {
521+
if (BuildUtil.isNotificationChannelSupported(context)) {
524522
return retrieveDefaultNotificationChannel(context);
525523
}
526524
return null;
@@ -533,7 +531,7 @@ static String getDefaultNotificationChannelId(Context context) {
533531
* @return Returns all notification groups.
534532
*/
535533
static List<NotificationChannelGroup> getNotificationGroups(Context context) {
536-
if (isNotificationChannelSupported(context)) {
534+
if (BuildUtil.isNotificationChannelSupported(context)) {
537535
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
538536
Context.NOTIFICATION_SERVICE);
539537
if (notificationManager == null) {
@@ -545,29 +543,6 @@ static List<NotificationChannelGroup> getNotificationGroups(Context context) {
545543
return null;
546544
}
547545

548-
/**
549-
* Whether notification channels are supported.
550-
*
551-
* @param context The application context.
552-
* @return True if notification channels are supported, false otherwise.
553-
*/
554-
static boolean isNotificationChannelSupported(Context context) {
555-
return Build.VERSION.SDK_INT >= 26 && getTargetSdkVersion(context) >= 26;
556-
}
557-
558-
/**
559-
* Returns target SDK version parsed from manifest.
560-
*
561-
* @param context The application context.
562-
* @return Target SDK version.
563-
*/
564-
private static int getTargetSdkVersion(Context context) {
565-
if (targetSdk == -1 && context != null) {
566-
targetSdk = context.getApplicationInfo().targetSdkVersion;
567-
}
568-
return targetSdk;
569-
}
570-
571546
/**
572547
* Helper class holding Notification Channel data parsed from JSON.
573548
*/

AndroidSDK/src/com/leanplum/LeanplumNotificationHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.leanplum.internal.JsonConverter;
2929
import com.leanplum.internal.Log;
30+
import com.leanplum.utils.BuildUtil;
3031

3132
import java.util.Map;
3233

@@ -78,7 +79,7 @@ static NotificationCompat.Builder getDefaultNotificationBuilder(Context context,
7879
static NotificationCompat.Builder getNotificationBuilder(Context context, Bundle message) {
7980
NotificationCompat.Builder builder = null;
8081
// If we are targeting API 26, try to find supplied channel to post notification.
81-
if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) {
82+
if (BuildUtil.isNotificationChannelSupported(context)) {
8283
try {
8384
String channel = message.getString("lp_channel");
8485
if (!TextUtils.isEmpty(channel)) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2017, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
package com.leanplum.utils;
22+
23+
import android.content.Context;
24+
import android.os.Build;
25+
26+
/**
27+
* Utilities related to Build Version and target SDK.
28+
*
29+
* @author Anna Orlova
30+
*/
31+
public class BuildUtil {
32+
private static int targetSdk = -1;
33+
34+
/**
35+
* Whether notification channels are supported.
36+
*
37+
* @param context The application context.
38+
* @return True if notification channels are supported, false otherwise.
39+
*/
40+
public static boolean isNotificationChannelSupported(Context context) {
41+
return Build.VERSION.SDK_INT >= 26 && getTargetSdkVersion(context) >= 26;
42+
}
43+
44+
/**
45+
* Returns target SDK version parsed from manifest.
46+
*
47+
* @param context The application context.
48+
* @return Target SDK version.
49+
*/
50+
private static int getTargetSdkVersion(Context context) {
51+
if (targetSdk == -1 && context != null) {
52+
targetSdk = context.getApplicationInfo().targetSdkVersion;
53+
}
54+
return targetSdk;
55+
}
56+
}

0 commit comments

Comments
 (0)