Skip to content

Commit

Permalink
add android monitor connectivity status and connection metering
Browse files Browse the repository at this point in the history
ref-in android logs
add StringSlice
remove Sentry messaging
  • Loading branch information
irov committed Oct 29, 2023
1 parent af575d7 commit 7bc549a
Show file tree
Hide file tree
Showing 26 changed files with 407 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected void onCreate(Bundle savedInstanceState) {

this.setState("activity.init", "create");

MengineLog.logInfo(TAG, "onCreate");
MengineLog.logMessage(TAG, "onCreate");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onCreate: broken libraries");
Expand Down Expand Up @@ -394,7 +394,7 @@ public void onMengineCurrentSceneChange(String name) {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
MengineLog.logInfo(TAG, "onActivityResult request: %d result: %d"
MengineLog.logMessage(TAG, "onActivityResult request: %d result: %d"
, requestCode
, resultCode
);
Expand Down Expand Up @@ -422,7 +422,7 @@ protected void onStart(){

this.setState("activity.lifecycle", "start");

MengineLog.logInfo(TAG, "onStart");
MengineLog.logMessage(TAG, "onStart");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onStart: broken libraries");
Expand All @@ -449,7 +449,7 @@ protected void onStop() {

this.setState("activity.lifecycle", "stop");

MengineLog.logInfo(TAG, "onStop");
MengineLog.logMessage(TAG, "onStop");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onStop: broken libraries");
Expand Down Expand Up @@ -478,7 +478,7 @@ protected void onPause() {

this.setState("activity.lifecycle", "pause");

MengineLog.logInfo(TAG, "onPause");
MengineLog.logMessage(TAG, "onPause");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onPause: broken libraries");
Expand All @@ -505,7 +505,7 @@ protected void onResume() {

this.setState("activity.lifecycle", "resume");

MengineLog.logInfo(TAG, "onResume");
MengineLog.logMessage(TAG, "onResume");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onResume: broken libraries");
Expand All @@ -530,7 +530,7 @@ protected void onResume() {
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);

MengineLog.logInfo(TAG, "onNewIntent intent: %s", intent);
MengineLog.logMessage(TAG, "onNewIntent intent: %s", intent);

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onNewIntent: broken libraries");
Expand All @@ -549,7 +549,7 @@ protected void onNewIntent(Intent intent) {
protected void onDestroy() {
this.setState("activity.lifecycle", "destroy");

MengineLog.logInfo(TAG, "onDestroy");
MengineLog.logMessage(TAG, "onDestroy");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onDestroy: broken libraries");
Expand Down Expand Up @@ -591,7 +591,7 @@ protected void onRestart() {

this.setState("activity.lifecycle", "restart");

MengineLog.logInfo(TAG, "onRestart");
MengineLog.logMessage(TAG, "onRestart");

if (mBrokenLibraries == true) {
MengineLog.logWarning(TAG, "onRestart: broken libraries");
Expand All @@ -618,7 +618,7 @@ public void onConfigurationChanged(Configuration newConfig) {

this.setState("configuration.orientation", newConfig.orientation);

MengineLog.logInfo(TAG, "onConfigurationChanged config: %s"
MengineLog.logMessage(TAG, "onConfigurationChanged config: %s"
, newConfig.toString()
);

Expand All @@ -639,7 +639,7 @@ public void onConfigurationChanged(Configuration newConfig) {
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

MengineLog.logInfo(TAG, "onRequestPermissionsResult request: %d permissions: %s grantResults: %s"
MengineLog.logMessage(TAG, "onRequestPermissionsResult request: %d permissions: %s grantResults: %s"
, requestCode
, Arrays.toString(permissions)
, Arrays.toString(grantResults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import android.content.pm.PackageManager;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
Expand Down Expand Up @@ -68,6 +71,11 @@ public String getBuildUsername() {
private String m_sessionId;
private long m_sessionTimestamp = -1;

private boolean m_networkAvailable = false;
private boolean m_networkUnmetered = false;
private MengineNetworkTransport m_networkTransport = MengineNetworkTransport.NETWORKTRANSPORT_UNKNOWN;
private ConnectivityManager.NetworkCallback m_networkCallback;

private ArrayList<MenginePlugin> m_plugins = new ArrayList<>();
private Map<String, MenginePlugin> m_dictionaryPlugins = new HashMap<>();
private Map<String, Object> m_states = new HashMap<>();
Expand Down Expand Up @@ -510,10 +518,6 @@ public MenginePluginExtension createPluginExtension(MengineActivity activity, Me
}

public void onMengineCaughtException(Throwable throwable) {
MengineLog.logInfo(TAG, "onMengineCaughtException: %s"
, throwable.getLocalizedMessage()
);

String message = throwable.getLocalizedMessage();
this.setState("exception.message", message);

Expand Down Expand Up @@ -548,7 +552,7 @@ public void showToast(String format, Object ... args) {
public void showAlertDialog(String format, Object ... args) {
String message = MengineLog.buildTotalMsg(format, args);

MengineLog.logInfo(TAG, "show alert dialog: %s"
MengineLog.logMessage(TAG, "show alert dialog: %s"
, message
);

Expand Down Expand Up @@ -602,11 +606,121 @@ private boolean isMainProcess() {
return true;
}

private void initializeMonitorConnectivityStatusAndConnectionMetering() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return;
}

ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);

MengineLog.logMessage(TAG, "NetworkCallback onAvailable");

m_networkAvailable = true;
}

@Override
public void onLost(@NonNull Network network) {
super.onLost(network);

MengineLog.logMessage(TAG, "NetworkCallback onLost");

m_networkAvailable = false;
}

private MengineNetworkTransport getNetworkTransport(@NonNull NetworkCapabilities networkCapabilities) {
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_CELLULAR;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_WIFI;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_BLUETOOTH;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_ETHERNET;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_VPN;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_WIFI_AWARE;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_LOWPAN;
}

if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_USB) == true) {
return MengineNetworkTransport.NETWORKTRANSPORT_USB;
}

return MengineNetworkTransport.NETWORKTRANSPORT_UNKNOWN;
}

@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
final boolean unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);

MengineNetworkTransport transport = this.getNetworkTransport(networkCapabilities);

MengineLog.logMessage(TAG, "NetworkCallback onCapabilitiesChanged unmetered: %b transport: %s"
, unmetered
, transport
);

m_networkUnmetered = unmetered;
m_networkTransport = transport;
}
};

Context context = this.getApplicationContext();

ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(ConnectivityManager.class);
connectivityManager.registerDefaultNetworkCallback(networkCallback);

m_networkCallback = networkCallback;
}

private void finalizeMonitorConnectivityStatusAndConnectionMetering() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return;
}

if (m_networkCallback == null) {
return;
}

Context context = this.getApplicationContext();

ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(ConnectivityManager.class);
connectivityManager.registerDefaultNetworkCallback(m_networkCallback);

m_networkCallback = null;
}

@Override
public void onCreate() {
super.onCreate();

boolean isMainProcess = this.isMainProcess();
String versionName = this.getVersionName();
int versionCode = this.getVersionCode();

MengineLog.logMessage(TAG, "onCreate isMainProcess: %b VersionName: %s [%d]"
, isMainProcess
, versionName
, versionCode
);

List<MenginePluginApplicationListener> applicationListeners = this.getApplicationListeners();

Expand Down Expand Up @@ -634,6 +748,8 @@ public void onCreate() {

this.setState("application.init", "started");

this.initializeMonitorConnectivityStatusAndConnectionMetering();

try {
SDL.loadLibrary("SDLApplication");
} catch (UnsatisfiedLinkError e) {
Expand All @@ -653,8 +769,6 @@ public void onCreate() {
MengineLog.setMengineApplication(this);
MengineAnalytics.setMengineApplication(this);

MengineLog.logInfo(TAG, "onCreate");

if (this.getMetaDataBoolean("mengine.secure.allow_android_id", true) == true) {
m_androidId = this.getSecureAndroidId();
} else {
Expand Down Expand Up @@ -746,10 +860,47 @@ public void onCreate() {
MengineAnalytics.addContextGetterParameterLong("connection", new MengineAnalyticsGetter<Long>() {
@Override
public Long get() {
Context context = MengineApplication.this.getApplicationContext();
int status = MengineUtils.getConectivityStatus(context);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return -3L;
}

if (m_networkAvailable == false) {
return -2L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_CELLULAR) {
return 1L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_WIFI) {
return 2L;
}

return Long.valueOf(status);
if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_BLUETOOTH) {
return 3L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_ETHERNET) {
return 4L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_VPN) {
return 5L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_WIFI_AWARE) {
return 6L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_LOWPAN) {
return 7L;
}

if (m_networkTransport == MengineNetworkTransport.NETWORKTRANSPORT_USB) {
return 8L;
}

return -1L;
}
});

Expand Down Expand Up @@ -812,7 +963,7 @@ public Long get() {

@Override
public void onTerminate() {
MengineLog.logInfo(TAG, "onTerminate");
MengineLog.logMessage(TAG, "onTerminate");

List<MenginePluginApplicationListener> applicationListeners = this.getApplicationListeners();

Expand All @@ -824,6 +975,8 @@ public void onTerminate() {
plugin.onFinalize(this);
}

this.finalizeMonitorConnectivityStatusAndConnectionMetering();

MengineLog.setMengineApplication(null);
MengineAnalytics.setMengineApplication(null);

Expand Down Expand Up @@ -852,7 +1005,7 @@ public void attachBaseContext(Context base) {

MultiDex.install(this);

MengineLog.logInfo(TAG, "attachBaseContext");
MengineLog.logMessage(TAG, "attachBaseContext");

List<MenginePluginApplicationListener> applicationListeners = this.getApplicationListeners();

Expand All @@ -865,7 +1018,7 @@ public void attachBaseContext(Context base) {
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);

MengineLog.logInfo(TAG, "onConfigurationChanged config: %s"
MengineLog.logMessage(TAG, "onConfigurationChanged config: %s"
, newConfig.toString()
);

Expand All @@ -876,11 +1029,11 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
}
}

public void onMengineLogger(String category, int level, int filter, int color, String msg) {
public void onMengineLogger(int level, String category, String msg) {
List<MenginePluginLoggerListener> listeners = this.getLoggerListeners();

for(MenginePluginLoggerListener l : listeners) {
l.onMengineLogger(this, category, level, filter, color, msg);
l.onMengineLogger(this, level, category, msg);
}
}

Expand Down
Loading

0 comments on commit 7bc549a

Please sign in to comment.