Skip to content

Commit

Permalink
Merge pull request #1370 from OneSignal/feat/notification-accent-colo…
Browse files Browse the repository at this point in the history
…r-dark-mode

Feat/notification accent color dark mode
  • Loading branch information
nan-li authored Jul 1, 2021
2 parents d318188 + 3371d8d commit 0319b90
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -926,17 +926,28 @@ private static boolean isSoundEnabled(JSONObject fcmJson) {
}

// Android 5.0 accent color to use, only works when AndroidManifest.xml is targetSdkVersion >= 21
private static BigInteger getAccentColor(JSONObject fcmJson) {
static BigInteger getAccentColor(JSONObject fcmJson) {
try {
if (fcmJson.has("bgac"))
return new BigInteger(fcmJson.optString("bgac", null), 16);
} catch (Throwable t) {} // Can throw a parse error parse error.
} catch (Throwable t) {} // Can throw a parse error.

// Try to get "onesignal_notification_accent_color" from resources
// This will get the correct color for day and dark modes
try {
String defaultColor = OSUtils.getManifestMeta(currentContext, "com.onesignal.NotificationAccentColor.DEFAULT");
if (defaultColor != null)
String defaultColor = getResourceString(OneSignal.appContext, "onesignal_notification_accent_color", null);
if (defaultColor != null) {
return new BigInteger(defaultColor, 16);
} catch (Throwable t) {} // Can throw a parse error parse error.
}
} catch (Throwable t) {} // Can throw a parse error.

// Get accent color from Manifest
try {
String defaultColor = OSUtils.getManifestMeta(OneSignal.appContext, "com.onesignal.NotificationAccentColor.DEFAULT");
if (defaultColor != null) {
return new BigInteger(defaultColor, 16);
}
} catch (Throwable t) {} // Can throw a parse error.

return null;
}
Expand Down
3 changes: 3 additions & 0 deletions OneSignalSDK/unittest/src/main/res/values-night/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="onesignal_notification_accent_color">FFFF0000</string>
</resources>
3 changes: 3 additions & 0 deletions OneSignalSDK/unittest/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="onesignal_notification_accent_color">FF00FF00</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.robolectric.util.Scheduler;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,6 +128,10 @@ public static void OneSignal_handleNotificationOpen(Activity context, final JSON
OneSignal.handleNotificationOpen(context, data, fromAlert, notificationId);
}

public static BigInteger OneSignal_getAccentColor(JSONObject fcmJson) {
return GenerateNotification.getAccentColor(fcmJson);
}

public static BundleCompat createInternalPayloadBundle(Bundle bundle) {
BundleCompat retBundle = BundleCompatFactory.getInstance();
retBundle.putString("json_payload", OneSignalPackagePrivateHelper.bundleAsJSONObject(bundle).toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.onesignal;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(value = android.content.res.Resources.class)
public class ShadowResources {

// Returns 0 to mimic no resources found
@Implementation
public int getIdentifier(String name, String defType, String defPackage) {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.onesignal.ShadowOneSignal;
import com.onesignal.ShadowOneSignalRestClient;
import com.onesignal.ShadowReceiveReceiptController;
import com.onesignal.ShadowResources;
import com.onesignal.ShadowRoboNotificationManager;
import com.onesignal.ShadowRoboNotificationManager.PostedNotification;
import com.onesignal.ShadowTimeoutHandler;
Expand Down Expand Up @@ -119,6 +120,7 @@
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationBundleProcessor_ProcessFromFCMIntentService_NoWrap;
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationOpenedProcessor_processFromContext;
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationSummaryManager_updateSummaryNotificationAfterChildRemoved;
import static com.onesignal.OneSignalPackagePrivateHelper.OneSignal_getAccentColor;
import static com.onesignal.OneSignalPackagePrivateHelper.OneSignal_setTime;
import static com.onesignal.OneSignalPackagePrivateHelper.OneSignal_setupNotificationServiceExtension;
import static com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle;
Expand Down Expand Up @@ -246,7 +248,7 @@ private static Intent createOpenIntent(int notifId, Bundle bundle) {
private Intent createOpenIntent(Bundle bundle) {
return createOpenIntent(ShadowRoboNotificationManager.lastNotifId, bundle);
}

@Test
@Config (sdk = 22, shadows = { ShadowGenerateNotification.class })
public void shouldSetTitleCorrectly() throws Exception {
Expand Down Expand Up @@ -1413,7 +1415,6 @@ public void testRemoteNotificationReceivedHandler_notificationProcessingProperti
// 2. Add app context and setup the established notification extension service
OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
OneSignal_setupNotificationServiceExtension();

final boolean[] callbackEnded = {false};
OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {
public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
Expand Down Expand Up @@ -2305,6 +2306,59 @@ public void testNotificationWillShowInForegroundHandler_workTimeLongerThanTimeou
assertNotificationDbRecords(1);
}

/**
* Small icon accent color uses string.xml value in values-night when device in dark mode
*/
@Test
@Config(qualifiers = "night")
public void shouldUseDarkIconAccentColorInDarkMode_hasMetaData() throws Exception {
OneSignal.initWithContext(blankActivity);
// Add the 'com.onesignal.NotificationAccentColor.DEFAULT' as 'FF0000AA' meta-data tag
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationAccentColor.DEFAULT", "FF0000AA");

BigInteger defaultColor = OneSignal_getAccentColor(new JSONObject());
assertEquals("FFFF0000", defaultColor.toString(16).toUpperCase());
}

/**
* Small icon accent color uses string.xml value in values when device in day (non-dark) mode
*/
@Test
public void shouldUseDayIconAccentColorInDayMode() throws Exception {
OneSignal.initWithContext(blankActivity);
BigInteger defaultColor = OneSignal_getAccentColor(new JSONObject());
assertEquals("FF00FF00", defaultColor.toString(16).toUpperCase());
}

/**
* Small icon accent color uses value from Manifest if there are no resource strings provided
*/
@Test
@Config(shadows = { ShadowResources.class })
public void shouldUseManifestIconAccentColor() throws Exception {
OneSignal.initWithContext(blankActivity);

// Add the 'com.onesignal.NotificationAccentColor.DEFAULT' as 'FF0000AA' meta-data tag
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationAccentColor.DEFAULT", "FF0000AA");

BigInteger defaultColor = OneSignal_getAccentColor(new JSONObject());
assertEquals("FF0000AA", defaultColor.toString(16).toUpperCase());
}

/**
* Small icon accent color uses value of 'bgac' if available
*/
@Test
public void shouldUseBgacAccentColor_hasMetaData() throws Exception {
OneSignal.initWithContext(blankActivity);
// Add the 'com.onesignal.NotificationAccentColor.DEFAULT' as 'FF0000AA' meta-data tag
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationAccentColor.DEFAULT", "FF0000AA");
JSONObject fcmJson = new JSONObject();
fcmJson.put("bgac", "FF0F0F0F");
BigInteger defaultColor = OneSignal_getAccentColor(fcmJson);
assertEquals("FF0F0F0F", defaultColor.toString(16).toUpperCase());
}

/* Helpers */

private static void assertNoNotifications() {
Expand Down

0 comments on commit 0319b90

Please sign in to comment.