Skip to content

Commit

Permalink
Android native logs (#8384)
Browse files Browse the repository at this point in the history
* Android native logs

* Use latest version of the library
  • Loading branch information
larkox authored Dec 17, 2024
1 parent 3cda8df commit 640ff93
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
Expand All @@ -32,6 +31,7 @@
import com.mattermost.rnbeta.*;
import com.mattermost.rnutils.helpers.NotificationHelper;
import com.nozbe.watermelondb.WMDatabase;
import com.mattermost.turbolog.TurboLog;

import java.io.IOException;
import java.security.KeyFactory;
Expand Down Expand Up @@ -241,36 +241,36 @@ public static void createNotificationChannels(Context context) {
public static boolean verifySignature(final Context context, String signature, String serverUrl, String ackId) {
if (signature == null) {
// Backward compatibility with old push proxies
Log.i("Mattermost Notifications Signature verification", "No signature in the notification");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "No signature in the notification");
return true;
}

if (serverUrl == null) {
Log.i("Mattermost Notifications Signature verification", "No server_url for server_id");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "No server_url for server_id");
return false;
}

DatabaseHelper dbHelper = DatabaseHelper.Companion.getInstance();
if (dbHelper == null) {
Log.i("Mattermost Notifications Signature verification", "Cannot access the database");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "Cannot access the database");
return false;
}

WMDatabase db = getDatabaseForServer(dbHelper, context, serverUrl);
if (db == null) {
Log.i("Mattermost Notifications Signature verification", "Cannot access the server database");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "Cannot access the server database");
return false;
}

if (signature.equals("NO_SIGNATURE")) {
String version = queryConfigServerVersion(db);
if (version == null) {
Log.i("Mattermost Notifications Signature verification", "No server version");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "No server version");
return false;
}

if (!version.matches("[0-9]+(\\.[0-9]+)*")) {
Log.i("Mattermost Notifications Signature verification", "Invalid server version");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "Invalid server version");
return false;
}

Expand Down Expand Up @@ -324,7 +324,7 @@ public static boolean verifySignature(final Context context, String signature, S
}

if (rejected) {
Log.i("Mattermost Notifications Signature verification", "Server version should send signature");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "Server version should send signature");
return false;
}

Expand All @@ -334,7 +334,7 @@ public static boolean verifySignature(final Context context, String signature, S

String signingKey = queryConfigSigningKey(db);
if (signingKey == null) {
Log.i("Mattermost Notifications Signature verification", "No signing key");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "No signing key");
return false;
}

Expand All @@ -345,17 +345,17 @@ public static boolean verifySignature(final Context context, String signature, S

String storedDeviceToken = getDeviceToken(dbHelper);
if (storedDeviceToken == null) {
Log.i("Mattermost Notifications Signature verification", "No device token stored");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "No device token stored");
return false;
}
String[] tokenParts = storedDeviceToken.split(":", 2);
if (tokenParts.length != 2) {
Log.i("Mattermost Notifications Signature verification", "Wrong stored device token format");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "Wrong stored device token format");
return false;
}
String deviceToken = tokenParts[1].substring(0, tokenParts[1].length() -1 );
if (deviceToken.isEmpty()) {
Log.i("Mattermost Notifications Signature verification", "Empty stored device token");
TurboLog.Companion.i("Mattermost Notifications Signature verification", "Empty stored device token");
return false;
}

Expand All @@ -366,19 +366,19 @@ public static boolean verifySignature(final Context context, String signature, S
.build()
.parseSignedClaims(signature);
} catch (MissingClaimException e) {
Log.i("Mattermost Notifications Signature verification", String.format("Missing claim: %s", e.getMessage()));
TurboLog.Companion.i("Mattermost Notifications Signature verification", String.format("Missing claim: %s", e.getMessage()));
e.printStackTrace();
return false;
} catch (IncorrectClaimException e) {
Log.i("Mattermost Notifications Signature verification", String.format("Incorrect claim: %s", e.getMessage()));
TurboLog.Companion.i("Mattermost Notifications Signature verification", String.format("Incorrect claim: %s", e.getMessage()));
e.printStackTrace();
return false;
} catch (JwtException e) {
Log.i("Mattermost Notifications Signature verification", String.format("Cannot verify JWT: %s", e.getMessage()));
TurboLog.Companion.i("Mattermost Notifications Signature verification", String.format("Cannot verify JWT: %s", e.getMessage()));
e.printStackTrace();
return false;
} catch (Exception e) {
Log.i("Mattermost Notifications Signature verification", String.format("Exception while parsing JWT: %s", e.getMessage()));
TurboLog.Companion.i("Mattermost Notifications Signature verification", String.format("Exception while parsing JWT: %s", e.getMessage()));
e.printStackTrace();
return false;
}
Expand Down Expand Up @@ -572,7 +572,7 @@ private static Bitmap userAvatar(final Context context, @NonNull final String se
Double lastUpdateAt = 0.0;
if (!TextUtils.isEmpty(urlOverride)) {
Request request = new Request.Builder().url(urlOverride).build();
Log.i("ReactNative", String.format("Fetch override profile image %s", urlOverride));
TurboLog.Companion.i("ReactNative", String.format("Fetch override profile image %s", urlOverride));
response = client.newCall(request).execute();
} else {
DatabaseHelper dbHelper = DatabaseHelper.Companion.getInstance();
Expand All @@ -594,7 +594,7 @@ private static Bitmap userAvatar(final Context context, @NonNull final String se

bitmapCache.removeBitmap(userId, serverUrl);
String url = String.format("api/v4/users/%s/image", userId);
Log.i("ReactNative", String.format("Fetch profile image %s", url));
TurboLog.Companion.i("ReactNative", String.format("Fetch profile image %s", url));
response = Network.getSync(serverUrl, url, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mattermost.helpers

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
Expand All @@ -15,6 +14,7 @@ import com.mattermost.helpers.push_notification.fetchNeededUsers
import com.mattermost.helpers.push_notification.fetchPosts
import com.mattermost.helpers.push_notification.fetchTeamIfNeeded
import com.mattermost.helpers.push_notification.fetchThread
import com.mattermost.turbolog.TurboLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand Down Expand Up @@ -50,7 +50,7 @@ class PushNotificationDataRunnable {
val isCRTEnabled = initialData.getString("is_crt_enabled") == "true"
val ackId = initialData.getString("ack_id")

Log.i("ReactNative", "Start fetching notification data in server=$serverUrl for channel=$channelId and ack=$ackId")
TurboLog.i("ReactNative", "Start fetching notification data in server=$serverUrl for channel=$channelId and ack=$ackId")

val receivingThreads = isCRTEnabled && !rootId.isNullOrEmpty()
val notificationData = Arguments.createMap()
Expand Down Expand Up @@ -104,13 +104,13 @@ class PushNotificationDataRunnable {
dbHelper.saveToDatabase(db, notificationData, teamId, channelId, receivingThreads)
}

Log.i("ReactNative", "Done processing push notification=$serverUrl for channel=$channelId and ack=$ackId")
TurboLog.i("ReactNative", "Done processing push notification=$serverUrl for channel=$channelId and ack=$ackId")
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
db?.close()
Log.i("ReactNative", "DONE fetching notification data")
TurboLog.i("ReactNative", "DONE fetching notification data")
}

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.mattermost.rnbeta
import android.app.PendingIntent
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.core.app.NotificationCompat
import com.mattermost.helpers.CustomPushNotificationHelper
import com.mattermost.helpers.DatabaseHelper
import com.mattermost.helpers.Network
import com.mattermost.helpers.PushNotificationDataHelper
import com.mattermost.helpers.database_extension.getServerUrlForIdentifier
import com.mattermost.rnutils.helpers.NotificationHelper
import com.mattermost.turbolog.TurboLog
import com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME
import com.wix.reactnativenotifications.core.AppLaunchHelper
import com.wix.reactnativenotifications.core.AppLifecycleFacade
Expand Down Expand Up @@ -84,7 +84,7 @@ class CustomPushNotification(
}

if (!CustomPushNotificationHelper.verifySignature(mContext, signature, serverUrl, ackId)) {
Log.i("Mattermost Notifications Signature verification", "Notification skipped because we could not verify it.")
TurboLog.i("Mattermost Notifications Signature verification", "Notification skipped because we could not verify it.")
return
}

Expand All @@ -104,7 +104,7 @@ class CustomPushNotification(
when (type) {
CustomPushNotificationHelper.PUSH_TYPE_MESSAGE, CustomPushNotificationHelper.PUSH_TYPE_SESSION -> {
val currentActivityName = mAppLifecycleFacade.runningReactContext?.currentActivity?.componentName?.className ?: ""
Log.i("ReactNative", currentActivityName)
TurboLog.i("ReactNative", currentActivityName)
if (!mAppLifecycleFacade.isAppVisible() || currentActivityName != "MainActivity") {
var createSummary = type == CustomPushNotificationHelper.PUSH_TYPE_MESSAGE
if (type == CustomPushNotificationHelper.PUSH_TYPE_MESSAGE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import com.facebook.react.PackageList
import com.facebook.react.ReactHost
import com.facebook.react.ReactInstanceManager
Expand All @@ -20,6 +19,8 @@ import com.facebook.react.modules.network.OkHttpClientProvider
import com.facebook.soloader.SoLoader
import com.mattermost.networkclient.RCTOkHttpClientFactory
import com.mattermost.rnshare.helpers.RealPathUtil
import com.mattermost.turbolog.TurboLog
import com.mattermost.turbolog.ConfigureOptions
import com.nozbe.watermelondb.jsi.JSIInstaller
import com.reactnativenavigation.NavigationApplication
import com.wix.reactnativenotifications.RNNotificationsPackage
Expand Down Expand Up @@ -63,7 +64,9 @@ class MainApplication : NavigationApplication(), INotificationsApplication {
// Delete any previous temp files created by the app
val tempFolder = File(applicationContext.cacheDir, RealPathUtil.CACHE_DIR_NAME)
RealPathUtil.deleteTempFiles(tempFolder)
Log.i("ReactNative", "Cleaning temp cache " + tempFolder.absolutePath)
TurboLog.configure(options = ConfigureOptions(logsDirectory = applicationContext.cacheDir.absolutePath + "/logs", logPrefix = applicationContext.packageName))

TurboLog.i("ReactNative", "Cleaning temp cache " + tempFolder.absolutePath)

// Tells React Native to use our RCTOkHttpClientFactory which builds an OKHttpClient
// with a cookie jar defined in APIClientModule and an interceptor to intercept all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import android.content.Intent;
import android.app.IntentService;
import android.os.Bundle;
import android.util.Log;

import com.mattermost.rnutils.helpers.NotificationHelper;
import com.mattermost.turbolog.TurboLog;
import com.wix.reactnativenotifications.core.NotificationIntentAdapter;

public class NotificationDismissService extends IntentService {
Expand All @@ -20,6 +20,6 @@ protected void onHandleIntent(Intent intent) {
final Bundle bundle = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);

NotificationHelper.INSTANCE.dismissNotification(context, bundle);
Log.i("ReactNative", "Dismiss notification");
TurboLog.Companion.i("ReactNative", "Dismiss notification");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
Expand All @@ -18,6 +17,7 @@
import com.facebook.react.bridge.WritableMap;

import com.mattermost.helpers.*;
import com.mattermost.turbolog.TurboLog;
import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
import com.wix.reactnativenotifications.core.notification.PushNotificationProps;

Expand Down Expand Up @@ -82,22 +82,22 @@ protected void replyToMessage(final String serverUrl, final int notificationId,
public void resolve(@Nullable Object value) {
if (value != null) {
onReplySuccess(notificationId, message);
Log.i("ReactNative", "Reply SUCCESS");
TurboLog.Companion.i("ReactNative", "Reply SUCCESS");
} else {
Log.i("ReactNative", "Reply FAILED resolved without value");
TurboLog.Companion.i("ReactNative", "Reply FAILED resolved without value");
onReplyFailed(notificationId);
}
}

@Override
public void reject(Throwable reason) {
Log.i("ReactNative", String.format("Reply FAILED exception %s", reason.getMessage()));
TurboLog.Companion.i("ReactNative", String.format("Reply FAILED exception %s", reason.getMessage()));
onReplyFailed(notificationId);
}

@Override
public void reject(String code, String message) {
Log.i("ReactNative",
TurboLog.Companion.i("ReactNative",
String.format("Reply FAILED status %s BODY %s", code, message)
);
onReplyFailed(notificationId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mattermost.rnbeta;

import android.os.Bundle;
import android.util.Log;

import java.lang.System;
import java.util.Objects;
Expand All @@ -12,14 +11,15 @@

import com.facebook.react.bridge.WritableMap;
import com.mattermost.helpers.*;
import com.mattermost.turbolog.TurboLog;

import okhttp3.Response;

public class ReceiptDelivery {
private static final String[] ackKeys = new String[]{"post_id", "root_id", "category", "message", "team_id", "channel_id", "channel_name", "type", "sender_id", "sender_name", "version"};

public static Bundle send(final String ackId, final String serverUrl, final String postId, final String type, final boolean isIdLoaded) {
Log.i("ReactNative", String.format("Send receipt delivery ACK=%s TYPE=%s to URL=%s with ID-LOADED=%s", ackId, type, serverUrl, isIdLoaded));
TurboLog.Companion.i("ReactNative", String.format("Send receipt delivery ACK=%s TYPE=%s to URL=%s with ID-LOADED=%s", ackId, type, serverUrl, isIdLoaded));
WritableMap options = Arguments.createMap();
WritableMap headers = Arguments.createMap();
WritableMap body = Arguments.createMap();
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@mattermost/react-native-emm": "1.5.0",
"@mattermost/react-native-network-client": "1.7.3",
"@mattermost/react-native-paste-input": "0.8.0",
"@mattermost/react-native-turbo-log": "0.4.0",
"@mattermost/react-native-turbo-log": "github:mattermost/react-native-turbo-log#d8ddf5e7974546aff3e83b2c563907eb609ac5f4",
"@mattermost/rnshare": "file:./libraries/@mattermost/rnshare",
"@mattermost/rnutils": "file:./libraries/@mattermost/rnutils",
"@msgpack/msgpack": "2.8.0",
Expand Down

0 comments on commit 640ff93

Please sign in to comment.