Skip to content

Commit 1712110

Browse files
committed
Refactored unit test classes.
1 parent f77d2bb commit 1712110

File tree

9 files changed

+723
-633
lines changed

9 files changed

+723
-633
lines changed

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void unregister(Promise promise) {
153153
return;
154154
}
155155

156-
NotificationHub hub = notificationHubUtil.createNotificationHub(hubName, connectionString, reactContext);
156+
NotificationHub hub = ReactNativeUtil.createNotificationHub(hubName, connectionString, reactContext);
157157
try {
158158
hub.unregister();
159159
notificationHubUtil.setRegistrationID(reactContext, null);

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubUtil.java

Lines changed: 1 addition & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
import static com.azure.reactnative.notificationhub.ReactNativeConstants.*;
2929

3030
public class ReactNativeNotificationHubUtil {
31-
public static final String TAG = "NotificationHubUtil";
31+
public static final String TAG = "ReactNativeNotificationHubUtil";
3232

3333
private static ReactNativeNotificationHubUtil sharedNotificationHubUtilInstance = null;
3434

35-
private final ExecutorService mPool = Executors.newFixedThreadPool(1);
36-
3735
private boolean mIsForeground;
3836

3937
public static class IntentFactory {
@@ -163,194 +161,6 @@ public boolean getAppIsForeground() {
163161
return mIsForeground;
164162
}
165163

166-
public void runInWorkerThread(Runnable runnable) {
167-
mPool.execute(runnable);
168-
}
169-
170-
public NotificationHub createNotificationHub(String hubName, String connectionString, ReactContext reactContext) {
171-
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
172-
return hub;
173-
}
174-
175-
public JSONObject convertBundleToJSON(Bundle bundle) {
176-
JSONObject json = new JSONObject();
177-
Set<String> keys = bundle.keySet();
178-
for (String key : keys) {
179-
try {
180-
json.put(key, bundle.get(key));
181-
} catch (JSONException e) {
182-
}
183-
}
184-
185-
return json;
186-
}
187-
188-
public Intent createBroadcastIntent(String action, JSONObject json) {
189-
Intent intent = IntentFactory.createIntent(action);
190-
intent.putExtra("event", DEVICE_NOTIF_EVENT);
191-
intent.putExtra("data", json.toString());
192-
193-
return intent;
194-
}
195-
196-
public Class getMainActivityClass(Context context) {
197-
String packageName = context.getPackageName();
198-
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
199-
String className = launchIntent.getComponent().getClassName();
200-
try {
201-
return Class.forName(className);
202-
} catch (ClassNotFoundException e) {
203-
Log.e(TAG, ERROR_ACTIVITY_CLASS_NOT_FOUND, e);
204-
return null;
205-
}
206-
}
207-
208-
public int getNotificationCompatPriority(String priorityString) {
209-
int priority = NotificationCompat.PRIORITY_DEFAULT;
210-
if (priorityString != null) {
211-
switch (priorityString.toLowerCase()) {
212-
case REMOTE_NOTIFICATION_PRIORITY_MAX:
213-
priority = NotificationCompat.PRIORITY_MAX;
214-
break;
215-
case REMOTE_NOTIFICATION_PRIORITY_HIGH:
216-
priority = NotificationCompat.PRIORITY_HIGH;
217-
break;
218-
case REMOTE_NOTIFICATION_PRIORITY_LOW:
219-
priority = NotificationCompat.PRIORITY_LOW;
220-
break;
221-
case REMOTE_NOTIFICATION_PRIORITY_MIN:
222-
priority = NotificationCompat.PRIORITY_MIN;
223-
break;
224-
case REMOTE_NOTIFICATION_PRIORITY_NORMAL:
225-
priority = NotificationCompat.PRIORITY_DEFAULT;
226-
break;
227-
}
228-
}
229-
230-
return priority;
231-
}
232-
233-
public int getSmallIcon(Bundle bundle, Resources res, String packageName) {
234-
int smallIconResId;
235-
String smallIcon = bundle.getString(KEY_REMOTE_NOTIFICATION_SMALL_ICON);
236-
237-
if (smallIcon != null) {
238-
smallIconResId = res.getIdentifier(smallIcon, RESOURCE_DEF_TYPE_MIPMAP, packageName);
239-
} else {
240-
smallIconResId = res.getIdentifier(RESOURCE_NAME_NOTIFICATION, RESOURCE_DEF_TYPE_MIPMAP, packageName);
241-
}
242-
243-
if (smallIconResId == 0) {
244-
smallIconResId = res.getIdentifier(RESOURCE_NAME_LAUNCHER, RESOURCE_DEF_TYPE_MIPMAP, packageName);
245-
246-
if (smallIconResId == 0) {
247-
smallIconResId = android.R.drawable.ic_dialog_info;
248-
}
249-
}
250-
251-
return smallIconResId;
252-
}
253-
254-
public int getLargeIcon(Bundle bundle, String largeIcon, Resources res, String packageName) {
255-
int largeIconResId;
256-
257-
if (largeIcon != null) {
258-
largeIconResId = res.getIdentifier(largeIcon, RESOURCE_DEF_TYPE_MIPMAP, packageName);
259-
} else {
260-
largeIconResId = res.getIdentifier(RESOURCE_NAME_LAUNCHER, RESOURCE_DEF_TYPE_MIPMAP, packageName);
261-
}
262-
263-
return largeIconResId;
264-
}
265-
266-
public Uri getSoundUri(Context context, Bundle bundle) {
267-
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
268-
String soundName = bundle.getString(KEY_REMOTE_NOTIFICATION_SOUND_NAME);
269-
if (soundName != null) {
270-
if (!"default".equalsIgnoreCase(soundName)) {
271-
272-
// sound name can be full filename, or just the resource name.
273-
// So the strings 'my_sound.mp3' AND 'my_sound' are accepted
274-
// The reason is to make the iOS and android javascript interfaces compatible
275-
276-
int resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
277-
if (resId == 0) {
278-
soundName = soundName.substring(0, soundName.lastIndexOf('.'));
279-
resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
280-
}
281-
282-
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resId);
283-
}
284-
}
285-
286-
return soundUri;
287-
}
288-
289-
public Intent createNotificationIntent(Context context, Bundle bundle, Class intentClass) {
290-
Intent intent = IntentFactory.createIntent(context, intentClass);
291-
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
292-
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, true);
293-
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, false);
294-
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, false);
295-
intent.putExtra(KEY_INTENT_NOTIFICATION, bundle);
296-
297-
return intent;
298-
}
299-
300-
public void processNotificationActions(Context context, Bundle bundle,
301-
NotificationCompat.Builder notification,
302-
int notificationID) {
303-
JSONArray actionsArray = null;
304-
try {
305-
actionsArray = bundle.getString(KEY_REMOTE_NOTIFICATION_ACTIONS) != null ?
306-
new JSONArray(bundle.getString(KEY_REMOTE_NOTIFICATION_ACTIONS)) : null;
307-
} catch (JSONException e) {
308-
Log.e(TAG, ERROR_COVERT_ACTIONS, e);
309-
}
310-
311-
if (actionsArray != null) {
312-
// No icon for now. The icon value of 0 shows no icon.
313-
int icon = 0;
314-
315-
// Add button for each actions.
316-
for (int i = 0; i < actionsArray.length(); i++) {
317-
String action;
318-
try {
319-
action = actionsArray.getString(i);
320-
} catch (JSONException e) {
321-
Log.e(TAG, ERROR_GET_ACTIONS_ARRAY, e);
322-
continue;
323-
}
324-
325-
Intent actionIntent = IntentFactory.createIntent();
326-
actionIntent.setAction(context.getPackageName() + "." + action);
327-
// Add "action" for later identifying which button gets pressed.
328-
bundle.putString(KEY_REMOTE_NOTIFICATION_ACTION, action);
329-
actionIntent.putExtra(KEY_INTENT_NOTIFICATION, bundle);
330-
PendingIntent pendingActionIntent = PendingIntent.getBroadcast(context, notificationID, actionIntent,
331-
PendingIntent.FLAG_UPDATE_CURRENT);
332-
notification.addAction(icon, action, pendingActionIntent);
333-
}
334-
}
335-
}
336-
337-
public NotificationCompat.Builder initNotificationCompatBuilder(Context context,
338-
String notificationChannelID,
339-
String title,
340-
CharSequence ticker,
341-
int visibility,
342-
int priority,
343-
boolean autoCancel) {
344-
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, notificationChannelID)
345-
.setContentTitle(title)
346-
.setTicker(ticker)
347-
.setVisibility(visibility)
348-
.setPriority(priority)
349-
.setAutoCancel(autoCancel);
350-
351-
return notificationBuilder;
352-
}
353-
354164
private String getPref(Context context, String key) {
355165
SharedPreferences prefs =
356166
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationsHandler.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ private ReactNativeNotificationsHandler() {
3232
}
3333

3434
public static void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
35-
final ReactNativeNotificationHubUtil hubUtil = ReactNativeNotificationHubUtil.getInstance();
36-
hubUtil.runInWorkerThread(new Runnable() {
35+
ReactNativeUtil.runInWorkerThread(new Runnable() {
3736
public void run() {
3837
try {
3938
Thread.currentThread().sleep(delay);
40-
JSONObject json = hubUtil.convertBundleToJSON(bundle);
41-
Intent event = hubUtil.createBroadcastIntent(TAG, json);
39+
JSONObject json = ReactNativeUtil.convertBundleToJSON(bundle);
40+
Intent event = ReactNativeUtil.createBroadcastIntent(TAG, json);
4241
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
4342
localBroadcastManager.sendBroadcast(event);
4443
} catch (Exception e) {
@@ -49,8 +48,7 @@ public void run() {
4948

5049
public static void sendNotification(Context context, Bundle bundle, String notificationChannelID) {
5150
try {
52-
final ReactNativeNotificationHubUtil hubUtil = ReactNativeNotificationHubUtil.getInstance();
53-
Class intentClass = hubUtil.getMainActivityClass(context);
51+
Class intentClass = ReactNativeUtil.getMainActivityClass(context);
5452
if (intentClass == null) {
5553
Log.e(TAG, ERROR_NO_ACTIVITY_CLASS);
5654
return;
@@ -76,8 +74,9 @@ public static void sendNotification(Context context, Bundle bundle, String notif
7674
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
7775
}
7876

79-
int priority = hubUtil.getNotificationCompatPriority(bundle.getString(KEY_REMOTE_NOTIFICATION_PRIORITY));
80-
NotificationCompat.Builder notificationBuilder = hubUtil.initNotificationCompatBuilder(
77+
int priority = ReactNativeUtil.getNotificationCompatPriority(
78+
bundle.getString(KEY_REMOTE_NOTIFICATION_PRIORITY));
79+
NotificationCompat.Builder notificationBuilder = ReactNativeUtil.initNotificationCompatBuilder(
8180
context,
8281
notificationChannelID,
8382
title,
@@ -103,11 +102,11 @@ public static void sendNotification(Context context, Bundle bundle, String notif
103102
notificationBuilder.setNumber(Integer.parseInt(numberString));
104103
}
105104

106-
int smallIconResId = hubUtil.getSmallIcon(bundle, res, packageName);
105+
int smallIconResId = ReactNativeUtil.getSmallIcon(bundle, res, packageName);
107106
notificationBuilder.setSmallIcon(smallIconResId);
108107

109108
String largeIcon = bundle.getString(KEY_REMOTE_NOTIFICATION_LARGE_ICON);
110-
int largeIconResId = hubUtil.getLargeIcon(bundle, largeIcon, res, packageName);
109+
int largeIconResId = ReactNativeUtil.getLargeIcon(bundle, largeIcon, res, packageName);
111110
Bitmap largeIconBitmap = BitmapFactory.decodeResource(res, largeIconResId);
112111
if (largeIconResId != 0 && (largeIcon != null || Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
113112
notificationBuilder.setLargeIcon(largeIconBitmap);
@@ -121,10 +120,10 @@ public static void sendNotification(Context context, Bundle bundle, String notif
121120
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
122121

123122
// Create notification intent
124-
Intent intent = hubUtil.createNotificationIntent(context, bundle, intentClass);
123+
Intent intent = ReactNativeUtil.createNotificationIntent(context, bundle, intentClass);
125124

126125
if (!bundle.containsKey(KEY_REMOTE_NOTIFICATION_PLAY_SOUND) || bundle.getBoolean(KEY_REMOTE_NOTIFICATION_PLAY_SOUND)) {
127-
Uri soundUri = hubUtil.getSoundUri(context, bundle);
126+
Uri soundUri = ReactNativeUtil.getSoundUri(context, bundle);
128127
notificationBuilder.setSound(soundUri);
129128
}
130129

@@ -157,7 +156,7 @@ public static void sendNotification(Context context, Bundle bundle, String notif
157156
}
158157

159158
// Process notification's actions
160-
hubUtil.processNotificationActions(context, bundle, notificationBuilder, notificationID);
159+
ReactNativeUtil.processNotificationActions(context, bundle, notificationBuilder, notificationID);
161160

162161
Notification notification = notificationBuilder.build();
163162
NotificationManager notificationManager = (NotificationManager) context.getSystemService(

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void onSuccess(InstanceIdResult instanceIdResult) {
6161
// sent to your server. If it is not stored, send the token to your server.
6262
// Also check if the token has been compromised and needs refreshing.
6363
if (regID == null || storedToken != token) {
64-
NotificationHub hub = new NotificationHub(hubName, connectionString,
64+
NotificationHub hub = ReactNativeUtil.createNotificationHub(hubName, connectionString,
6565
ReactNativeRegistrationIntentService.this);
6666
Log.d(TAG, "NH Registration refreshing with token : " + token);
6767

0 commit comments

Comments
 (0)