Skip to content

Commit

Permalink
Fixed a bug that would throw a null pointer exception when calling "C…
Browse files Browse the repository at this point in the history
…ountlyPush.displayNotification " and CountlyPush was not initialized
  • Loading branch information
ArtursKadikis committed Dec 2, 2022
1 parent 1a033b9 commit d80cd98
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 22.06.2
* Fixed a bug that would throw a null pointer exception when calling "CountlyPush.displayNotification " and CountlyPush was not initialized

## 22.06.1
* Fixed a bug that would throw a null pointer exception when calling "CountlyPush.onTokenRefresh" and CountlyPush was not initialized

Expand Down
2 changes: 2 additions & 0 deletions app-kotlin/src/main/java/ly/count/android/demo/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ly.count.android.demo.kotlin

import android.app.Application
import android.util.Log
import ly.count.android.sdk.Countly
import ly.count.android.sdk.CountlyConfig
import ly.count.android.sdk.RemoteConfigCallback

//import ly.count.android.sdk.DeviceIdType

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android.useAndroidX=true
android.enableJetifier=true

# RELEASE FIELD SECTION
VERSION_NAME=22.06.1
VERSION_NAME=22.06.2
GROUP=ly.count.android

POM_URL=https://github.com/Countly/countly-sdk-android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public void testPrepareCommonRequest() {
break;
case "sdk_version":
if (a == 0) {
Assert.assertTrue(pair[1].equals("22.06.1"));
Assert.assertTrue(pair[1].equals("22.06.2"));
} else if (a == 1) {
Assert.assertTrue(pair[1].equals("123sdf.v-213"));
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/ly/count/android/sdk/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class Countly {

private final String DEFAULT_COUNTLY_SDK_VERSION_STRING = "22.06.1";
private final String DEFAULT_COUNTLY_SDK_VERSION_STRING = "22.06.2";

/**
* Used as request meta data on every request
Expand Down
59 changes: 45 additions & 14 deletions sdk/src/main/java/ly/count/android/sdk/messaging/CountlyPush.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -244,7 +247,7 @@ public void onReceive(Context context, Intent broadcast) {
*
* @return token string or null if no token is currently available.
*/
private static String getToken(Context context, Countly.CountlyMessagingProvider prov, ModuleLog L) {
private static String getToken(@Nullable Context context, @Nullable Countly.CountlyMessagingProvider prov, @Nullable ModuleLog L) {
//todo this seems to be broken
if (prov == Countly.CountlyMessagingProvider.FCM) {
try {
Expand Down Expand Up @@ -296,7 +299,7 @@ private static String getToken(Context context, Countly.CountlyMessagingProvider
* @param data {@code RemoteMessage#getData()} result
* @return {@code Boolean.TRUE} if displayed successfully, {@code Boolean.FALSE} if cannot display now, {@code null} if no Countly message is found in {@code data}
*/
public static Boolean displayMessage(Context context, final Map<String, String> data, @DrawableRes final int notificationSmallIcon, final Intent notificationIntent) {
public static Boolean displayMessage(@Nullable Context context, @Nullable final Map<String, String> data, @DrawableRes final int notificationSmallIcon, @Nullable final Intent notificationIntent) {
return displayMessage(context, decodeMessage(data), notificationSmallIcon, notificationIntent);
}

Expand All @@ -310,9 +313,14 @@ public static Boolean displayMessage(Context context, final Map<String, String>
* @param msg {@link Message} instance
* @return {@code Boolean.TRUE} if displayed successfully, {@code Boolean.FALSE} if cannot display now, {@code null} if no Countly message is found in {@code data}
*/
public static Boolean displayMessage(final Context context, final Message msg, @DrawableRes final int notificationSmallIcon, final Intent notificationIntent) {
public static Boolean displayMessage(@Nullable final Context context, @Nullable final Message msg, @DrawableRes final int notificationSmallIcon, @Nullable final Intent notificationIntent) {
Countly.sharedInstance().L.d("[CountlyPush, displayMessage] Displaying push message");

if(context == null) {
Countly.sharedInstance().L.e("[CountlyPush, displayMessage] Provided context was null, execution will stop");
return false;
}

if (!initFinished) {
Countly.sharedInstance().L.w("[CountlyPush, displayDialog] Push init has not been completed. Some things might not function.");
}
Expand Down Expand Up @@ -342,7 +350,12 @@ public static Boolean displayMessage(final Context context, final Message msg, @
* @param notificationIntent activity-starting intent to send when user taps on {@link Notification} or one of its {@link android.app.Notification.Action}s. Pass {@code null} to go with main activity.
* @return {@code Boolean.TRUE} if displayed successfully, {@code Boolean.FALSE} if cannot display now, {@code null} if message is not displayable as {@link Notification}
*/
public static Boolean displayNotification(final Context context, final Message msg, @DrawableRes final int notificationSmallIcon, final Intent notificationIntent) {
public static Boolean displayNotification(@Nullable final Context context, @Nullable final Message msg, @DrawableRes final int notificationSmallIcon, @Nullable final Intent notificationIntent) {
if(context == null) {
Countly.sharedInstance().L.e("[CountlyPush, displayNotification] Provided context was null, execution will stop");
return false;
}

if (!getPushConsent(context)) {
return null;
}
Expand All @@ -357,8 +370,16 @@ public static Boolean displayNotification(final Context context, final Message m

Countly.sharedInstance().L.d("[CountlyPush, displayNotification] Displaying push notification, additional intent provided:[" + (notificationIntent != null) + "]");

Set<String> allowedIntentClassNames;
Set<String> allowedIntentPackageNames;

if (!initFinished) {
Countly.sharedInstance().L.w("[CountlyPush, displayDialog] Push init has not been completed. Some things might not function.");
allowedIntentClassNames = new HashSet<>();
allowedIntentPackageNames = new HashSet<>();
} else {
allowedIntentClassNames = CountlyPush.countlyConfigPush.allowedIntentClassNames;
allowedIntentPackageNames = CountlyPush.countlyConfigPush.allowedIntentPackageNames;
}

final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Expand All @@ -368,7 +389,7 @@ public static Boolean displayNotification(final Context context, final Message m
return Boolean.FALSE;
}

Intent pushActivityIntent = createPushActivityIntent(context, msg, notificationIntent, 0, CountlyPush.countlyConfigPush.allowedIntentClassNames, CountlyPush.countlyConfigPush.allowedIntentPackageNames);
Intent pushActivityIntent = createPushActivityIntent(context, msg, notificationIntent, 0, allowedIntentClassNames, allowedIntentPackageNames);

final Notification.Builder builder = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? new Notification.Builder(context.getApplicationContext(), CHANNEL_ID) : new Notification.Builder(context.getApplicationContext()))
.setAutoCancel(true)
Expand All @@ -395,7 +416,7 @@ public static Boolean displayNotification(final Context context, final Message m
for (int i = 0; i < msg.buttons().size(); i++) {
Button button = msg.buttons().get(i);

pushActivityIntent = createPushActivityIntent(context, msg, notificationIntent, i + 1, CountlyPush.countlyConfigPush.allowedIntentClassNames, CountlyPush.countlyConfigPush.allowedIntentPackageNames);
pushActivityIntent = createPushActivityIntent(context, msg, notificationIntent, i + 1, allowedIntentClassNames, allowedIntentPackageNames);

builder.addAction(button.icon(), button.title(), PendingIntent.getActivity(context, msg.hashCode() + i + 1, pushActivityIntent, Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
}
Expand Down Expand Up @@ -428,7 +449,7 @@ public void call(Bitmap bitmap) {
return Boolean.TRUE;
}

private static Intent createPushActivityIntent(final Context context, final Message msg, final Intent notificationIntent, int index, Set<String> allowedIntentClassNames, Set<String> allowedIntentPackageNames) {
private static Intent createPushActivityIntent(@NonNull final Context context, @NonNull final Message msg, @Nullable final Intent notificationIntent, int index, @NonNull Set<String> allowedIntentClassNames, @NonNull Set<String> allowedIntentPackageNames) {
Intent pushActivityIntent = new Intent(context.getApplicationContext(), CountlyPushActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pushActivityIntent.setPackage(context.getApplicationContext().getPackageName());
Expand Down Expand Up @@ -459,7 +480,12 @@ private static Intent actionIntent(Context context, Intent notificationIntent, M
* @param msg message to get information from
* @return {@code Boolean.TRUE} if displayed successfully, {@code Boolean.FALSE} if cannot display now, {@code null} if message is not displayable as {@link Notification}
*/
public static Boolean displayDialog(final Activity activity, final Message msg) {
public static Boolean displayDialog(@Nullable final Activity activity, @Nullable final Message msg) {
if(activity == null) {
Countly.sharedInstance().L.e("[CountlyPush, displayDialog] Provided Activity was null, execution will stop");
return false;
}

if (!getPushConsent(activity)) {
return null;
}
Expand All @@ -475,7 +501,7 @@ public static Boolean displayDialog(final Activity activity, final Message msg)
public void call(Bitmap bitmap) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);

if (msg.media() != null) {
if (msg != null && msg.media() != null) {
addButtons(activity, builder, msg);

final LinearLayout layout = new LinearLayout(activity);
Expand Down Expand Up @@ -513,7 +539,7 @@ public void call(Bitmap bitmap) {
}

builder.setView(layout);
} else if (msg.link() != null) {
} else if (msg != null && msg.link() != null) {
if (msg.title() != null) {
builder.setTitle(msg.title());
}
Expand Down Expand Up @@ -653,7 +679,7 @@ public static void onTokenRefresh(String token, Countly.CountlyMessagingProvider
* @throws IllegalStateException
* @deprecated use 'CountlyConfigPush' object to init Countly Push: 'init(CountlyConfigPush countlyConfigPush)'.
*/
public static void init(final Application application, Countly.CountlyMessagingMode mode) throws IllegalStateException {
public static void init(@Nullable final Application application, @Nullable Countly.CountlyMessagingMode mode) throws IllegalStateException {
init(application, mode, null);
}

Expand All @@ -666,7 +692,7 @@ public static void init(final Application application, Countly.CountlyMessagingM
* @throws IllegalStateException
* @deprecated use 'CountlyConfigPush' object to init Countly Push: 'init(CountlyConfigPush countlyConfigPush)'.
*/
public static void init(final Application application, Countly.CountlyMessagingMode mode, Countly.CountlyMessagingProvider preferredProvider) throws IllegalStateException {
public static void init(@Nullable final Application application, @Nullable Countly.CountlyMessagingMode mode, @Nullable Countly.CountlyMessagingProvider preferredProvider) throws IllegalStateException {
CountlyConfigPush countlyConfigPush = new CountlyConfigPush(application, mode)
.setProvider(preferredProvider);
init(countlyConfigPush);
Expand All @@ -678,7 +704,12 @@ public static void init(final Application application, Countly.CountlyMessagingM
* @param countlyConfigPush push configuration
* @throws IllegalStateException
*/
public static void init(CountlyConfigPush countlyConfigPush) throws IllegalStateException {
public static void init(@Nullable CountlyConfigPush countlyConfigPush) throws IllegalStateException {
if(countlyConfigPush == null) {
Countly.sharedInstance().L.e("[CountlyPush, init] Can't initialize Countly Push, provided 'CountlyConfigPush' was null");
return;
}

Countly.sharedInstance().L.i("[CountlyPush, init] Initializing Countly Push, App:[" + (countlyConfigPush.application != null) + "], mode:[" + countlyConfigPush.mode + "] provider:[" + countlyConfigPush.provider + "]");

if (countlyConfigPush.application == null) {
Expand Down Expand Up @@ -851,7 +882,7 @@ private interface BitmapCallback {
void call(Bitmap bitmap);
}

private static void loadImage(final Context context, final Message msg, final BitmapCallback callback, final int attempt) {
private static void loadImage(@NonNull final Context context, @NonNull final Message msg, @NonNull final BitmapCallback callback, final int attempt) {
Utils.runInBackground(new Runnable() {
@Override public void run() {
final Bitmap[] bitmap = new Bitmap[] { null };
Expand Down

0 comments on commit d80cd98

Please sign in to comment.