From cad2f71387a3ffdf4c5a53c7050d492c4606e8de Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Akram Date: Wed, 9 Dec 2020 19:54:41 +0500 Subject: [PATCH 1/6] =?UTF-8?q?=E2=80=94=20APM=20changes=20implemented=20f?= =?UTF-8?q?or=20app=5Fstart,=20foreground=20and=20background.=20=E2=80=94?= =?UTF-8?q?=20old=20iOS=20platform=20check=20was=20not=20working=20fixed.?= =?UTF-8?q?=20-=20android=20plugin=20auto=20initialise=20implemented?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Countly.js | 22 ++++++++++++++++++---- hooks/createService.js | 4 ++-- plugin.xml | 1 + src/android/CountlyCordova.java | 32 +++++++++++++++++++++++++++++++- src/android/CountlyNative.java | 26 +++++++++++++++++++++++++- src/ios/CountlyCordova.h | 1 + src/ios/CountlyCordova.m | 12 ++++++++++++ src/ios/CountlyNative.m | 6 ++++++ 8 files changed, 96 insertions(+), 8 deletions(-) diff --git a/Countly.js b/Countly.js index 475b66f..60a673e 100644 --- a/Countly.js +++ b/Countly.js @@ -4,12 +4,11 @@ Countly.appKey = ""; Countly.ready = false; Countly.version = "20.04"; Countly.isDebug = false; -var userAgent = navigator.userAgent || navigator.vendor || window.opera; -if (/android/i.test(userAgent)) { +if (window.cordova.platformId == "android") { Countly.isAndroid = true; Countly.messagingMode = {"TEST": "2", "PRODUCTION": "0"}; } -if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) { +if (window.cordova.platformId == "ios") { Countly.isiOS = true; Countly.messagingMode = {"TEST": "1", "PRODUCTION": "0", "ADHOC": "2"}; } @@ -505,7 +504,22 @@ Countly.askForStarRating = function(callback){ Countly.askForFeedback = function(widgetId, buttonText){ cordova.exec(Countly.onSuccess,Countly.onError,"CountlyCordova","askForFeedback",[widgetId, buttonText || ""]); } -// FEEDBACK-WORK + +// Call this function when app is loaded, so that the app launch duration can be recorded. +// Should be call after init. +Countly.appLoadingFinished = async function(){ + Countly.isInitialized().then((result) => { + if(result != "true") { + if(Countly.isDebug){ + console.warn('[CountlyCordova] appLoadingFinished, init must be called before appLoadingFinished'); + } + return; + } + },(err) => { + console.error(err); + }); + cordova.exec(Countly.onSuccess,Countly.onError,"CountlyCordova","appLoadingFinished",[]); +} // Push Notification Countly.sendPushToken = function(options){ diff --git a/hooks/createService.js b/hooks/createService.js index 7f7e7bd..05e7cd4 100644 --- a/hooks/createService.js +++ b/hooks/createService.js @@ -41,8 +41,8 @@ module.exports = function(context) { }); var countlyFiles = [ - __dirname +'/../../../platforms/ios/Pods/Countly/' +'CountlyNotificationService.h', - __dirname +'/../../../platforms/ios/Pods/Countly/' +'CountlyNotificationService.m' + __dirname +'/../../../platforms/ios/Pods/CountlyPod/' +'CountlyNotificationService.h', + __dirname +'/../../../platforms/ios/Pods/CountlyPod/' +'CountlyNotificationService.m' ]; extFiles.push(countlyFiles[0]); extFiles.push(countlyFiles[1]); diff --git a/plugin.xml b/plugin.xml index 26e618b..503bbe3 100644 --- a/plugin.xml +++ b/plugin.xml @@ -74,6 +74,7 @@ + diff --git a/src/android/CountlyCordova.java b/src/android/CountlyCordova.java index 711bbb9..fe2092e 100644 --- a/src/android/CountlyCordova.java +++ b/src/android/CountlyCordova.java @@ -24,12 +24,23 @@ public enum CountlyMessagingMode { TEST, PRODUCTION, } - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); Context context = this.cordova.getActivity().getApplicationContext(); if(countlyNative == null){ countlyNative = new CountlyNative( this.cordova.getActivity(), this.cordova.getActivity().getApplicationContext()); } + } + + @Override + public void pluginInitialize() { + } + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + Context context = this.cordova.getActivity().getApplicationContext(); + if ("init".equals(action)) { callbackContext.success(countlyNative.init(args)); } @@ -226,6 +237,9 @@ public void callback(String result) { } }); } + else if("appLoadingFinished".equals(action)){ + callbackContext.success(countlyNative.appLoadingFinished(args)); + } else if("sendPushToken".equals(action)){ callbackContext.success(countlyNative.sendPushToken(args)); } @@ -256,4 +270,20 @@ else if("enableApm".equals(action)){ return true; } + @Override + public void onResume(boolean multitasking) { + super.onResume(multitasking); + countlyNative.onHostResume(); + } + + @Override + public void onPause(boolean multitasking) { + super.onPause(multitasking); + countlyNative.onHostPause(); + } + + @Override + public void onStart() { + super.onStart(); + } } diff --git a/src/android/CountlyNative.java b/src/android/CountlyNative.java index 143f82e..88a168c 100644 --- a/src/android/CountlyNative.java +++ b/src/android/CountlyNative.java @@ -45,6 +45,7 @@ public class CountlyNative { private CountlyConfig config = new CountlyConfig(); private static Callback notificationListener = null; private static String lastStoredNotification = null; + private final Set validConsentFeatureNames = new HashSet(Arrays.asList( Countly.CountlyFeatureNames.sessions, Countly.CountlyFeatureNames.events, @@ -59,6 +60,9 @@ public class CountlyNative { public CountlyNative(Activity _activity, Context _context){ this.activity = _activity; this.context = _context; + Countly.sharedInstance(); + this.config.enableManualAppLoadedTrigger(); + this.config.enableManualForegroundBackgroundTriggerAPM(); } public static void onNotification(Map notification){ JSONObject json = new JSONObject(notification); @@ -104,6 +108,8 @@ public String init(JSONArray args){ this.config.setApplication(activity.getApplication()); } Countly.sharedInstance().init(this.config); + Countly.sharedInstance().apm().triggerForeground(); + return "initialized: success"; }catch (JSONException jsonException){ return jsonException.toString(); @@ -883,6 +889,12 @@ public String askForStarRating(JSONArray args){ return "askForStarRating success."; } + public String appLoadingFinished(JSONArray args){ + this.log("appLoadingFinished", args); + Countly.sharedInstance().apm().setAppIsLoaded(); + return "appLoadingFinished success!"; + } + public String sendPushToken(JSONArray args){ try { this.log("sendPushToken", args); @@ -973,7 +985,7 @@ public String recordNetworkTrace(JSONArray args){ public String enableApm(JSONArray args){ this.log("enableApm", args); - this.config.setRecordAppStartTime(false); + this.config.setRecordAppStartTime(true); return "enableApm success."; } @@ -1005,5 +1017,17 @@ static void log(String message, Throwable tr, LogLevel logLevel) { } } + public void onHostResume() { + if(Countly.sharedInstance().isInitialized()) { + Countly.sharedInstance().apm().triggerForeground(); + } + } + + public void onHostPause() { + if(Countly.sharedInstance().isInitialized()) { + Countly.sharedInstance().apm().triggerBackground(); + } + } + } diff --git a/src/ios/CountlyCordova.h b/src/ios/CountlyCordova.h index ce02511..01ea34b 100644 --- a/src/ios/CountlyCordova.h +++ b/src/ios/CountlyCordova.h @@ -76,5 +76,6 @@ - (void)endTrace:(CDVInvokedUrlCommand*)command; - (void)recordNetworkTrace:(CDVInvokedUrlCommand*)command; - (void)enableApm:(CDVInvokedUrlCommand*)command; +- (void)appLoadingFinished:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/CountlyCordova.m b/src/ios/CountlyCordova.m index 0af24fe..da1a0c2 100644 --- a/src/ios/CountlyCordova.m +++ b/src/ios/CountlyCordova.m @@ -791,5 +791,17 @@ - (void)enableApm:(CDVInvokedUrlCommand*)command }]; } +- (void)appLoadingFinished:(CDVInvokedUrlCommand*)command +{ + if(countlyNative == nil){ + countlyNative = CountlyNative.new; + } + [countlyNative onCall: @"appLoadingFinished" commandString: command.arguments callback: ^(NSString * theResult) + { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: theResult]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; +} + @end diff --git a/src/ios/CountlyNative.m b/src/ios/CountlyNative.m index d31bad5..af6c04c 100644 --- a/src/ios/CountlyNative.m +++ b/src/ios/CountlyNative.m @@ -989,6 +989,12 @@ - (void) onCall:(NSString *)method commandString:(NSArray *)command callback:(Re config.enablePerformanceMonitoring = YES; result(@"enableApm!"); + }else if ([@"appLoadingFinished" isEqualToString:method]) { + dispatch_async(dispatch_get_main_queue(), ^ { + [Countly.sharedInstance appLoadingFinished]; + }); + result(@"appLoadingFinished!"); + } else { COUNTLY_CORDOVA_LOG(@"Countly Bridge Method Not Implemented %@", method); result(@"Countly Bridge Method Not Implemented"); From b8e5c0d311f106ed3d51ea62e0d2a5d8e0f47dc9 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Akram Date: Wed, 9 Dec 2020 20:28:45 +0500 Subject: [PATCH 2/6] Example app updated for APM changes --- index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index bfaa201..b93aa96 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,6 @@

Countly Cordova Demo App

- @@ -150,6 +149,7 @@

Countly Cordova Demo App

Countly.onNotification(function(theNotification){ console.log("[CountlyCordova] onNotification : " + JSON.stringify(theNotification)); }); + app.init(); }); app = {}; function makeid() { @@ -185,6 +185,7 @@

Countly Cordova Demo App

}); // Set Automatic value download happens when the SDK is initiated or when the device ID is changed. Countly.init("https://try.count.ly", "YOUR_API_KEY").then((result) => { + Countly.appLoadingFinished(); /** * Push notifications settings * Should be call after init From 111f5eafa507879135b64e657d5702a86fd3f386 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Akram Date: Fri, 11 Dec 2020 17:30:57 +0500 Subject: [PATCH 3/6] onLoad callback added in example app. --- index.html | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index b93aa96..7a99bb4 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@ Countly Cordova Demo App - +

Countly Cordova Demo App

@@ -144,13 +144,18 @@

Countly Cordova Demo App