11package ly .count .sdk .java ;
22
33import java .io .File ;
4+ import java .util .Map ;
45
56import ly .count .sdk .java .internal .*;
67
1415 * </ul>
1516 */
1617
17- public class Countly extends CountlyLifecycle {
18+ public class Countly implements Usage {
1819
1920 /**
2021 * A class responsible for storage of device information sent to Countly server.
@@ -25,11 +26,13 @@ public class Countly extends CountlyLifecycle {
2526
2627 protected static Countly cly ;
2728 protected SDK sdk ;
29+ protected CtxCore ctx ;
30+ protected Log L ;
2831
2932 protected Countly (SDK sdk , CtxImpl ctx , Log logger ) {
30- super (logger );
3133 cly = this ;
32- super .sdkInterface = this .sdk = sdk ;
34+ L = logger ;
35+ this .sdk = sdk ;
3336 this .ctx = ctx ;
3437 }
3538
@@ -41,6 +44,82 @@ protected Countly(SDK sdk, CtxImpl ctx, Log logger) {
4144// return new CtxImpl(cly.sdk, cly.sdk.config(), directory, view);
4245// }
4346
47+ //protected CtxImpl ctx;
48+
49+ /**
50+ * Initialize Countly.
51+ * To be called only once on application start.
52+ *
53+ * @param directory storage location for Countly
54+ * @param config configuration object
55+ */
56+ public static void init (final File directory , final Config config ) {
57+ if (config == null ) {
58+ System .out .println ("[ERROR][Countly] Config cannot be null" );
59+ }
60+ else if (directory == null ) {
61+ System .out .println ("[ERROR][Countly] File cannot be null" );
62+ } else if (!directory .isDirectory ()) {
63+ System .out .println ("[ERROR][Countly] File must be a directory" );
64+ } else if (!directory .exists ()) {
65+ System .out .println ("[ERROR][Countly] File must exist" );
66+ } else {
67+ if (cly != null ) {
68+ System .out .println ("[ERROR][Countly] Countly shouldn't be initialized twice. Please either use Countly.isInitialized() to check status or call Countly.stop() before second Countly.init()." );
69+ stop (false );
70+ }
71+
72+ if (config .enableBackendMode ) {
73+ config .sdkName = "java-native-backend" ;
74+ }
75+
76+ if (config .requestQueueMaxSize < 1 ) {
77+ System .out .println ("[ERROR][Countly] init: Request queue max size can not be less than 1." );
78+ config .requestQueueMaxSize = 1 ;
79+ }
80+
81+ InternalConfig internalConfig = new InternalConfig (config );
82+ Log L = new Log (internalConfig );
83+ SDK sdk = new SDK ();
84+ sdk .init (new CtxImpl (sdk , internalConfig , L , directory ), L );
85+
86+ // config has been changed, thus recreating ctx
87+ cly = new Countly (sdk , new CtxImpl (sdk , sdk .config (), L , directory ), L );
88+ }
89+ }
90+
91+ /**
92+ * Stop Countly SDK. Stops all tasks and releases resources.
93+ * Waits for some tasks to complete, might block for some time.
94+ * Also clears all the data if called with {@code clearData = true}.
95+ *
96+ * @param clearData whether to clear all Countly data or not
97+ */
98+ public static void stop (boolean clearData ) {
99+ if (cly != null ) {
100+ System .out .println ("[ERROR][Countly] Stopping SDK" );
101+ cly .sdk .stop (cly .ctx , clearData );
102+ cly = null ;
103+ } else {
104+ System .out .println ("[ERROR][Countly] Countly isn't initialized to stop it" );
105+ }
106+ }
107+
108+ /**
109+ * Returns whether Countly SDK has been already initialized or not.
110+ *
111+ * @return true if already initialized
112+ */
113+ public static boolean isInitialized () { return cly != null ; }
114+
115+ /**
116+ * Returns whether Countly SDK has been given consent to record data for a particular {@link Config.Feature} or not.
117+ *
118+ * @return true if consent has been given
119+ */
120+ public static boolean isTracking (Config .Feature feature ) { return isInitialized () && ((Countly )cly ).sdk .isTracking (feature .getIndex ()); }
121+
122+
44123 /**
45124 * Returns active {@link Session} if any or creates new {@link Session} instance.
46125 *
@@ -55,7 +134,7 @@ public static Session session(){
55134 if (!isInitialized ()) {
56135 System .out .println ("[ERROR][Countly] SDK is not initialized yet." );
57136 }
58- return Cly . session (cly .ctx );
137+ return session (cly .ctx );
59138 }
60139
61140 public static ModuleBackendMode .BackendMode backendMode (){
@@ -95,7 +174,7 @@ public static Session getSession(){
95174 if (!isInitialized ()) {
96175 System .out .println ("[ERROR][Countly] SDK is not initialized yet." );
97176 }
98- return Cly . session (cly .ctx );
177+ return session (cly .ctx );
99178 }
100179
101180 /**
@@ -178,7 +257,7 @@ public static void onConsent(Config.Feature... features) {
178257 public static void onConsentRemoval (Config .Feature ... features ) {
179258 System .out .println ("[DEBUG][Countly] onConsentRemoval: features = " + features );
180259 if (!isInitialized ()) {
181- System .out .println ("[ERROR][Countly]Countly SDK is not initialized yet." );
260+ System .out .println ("[ERROR][Countly] onConsentRemoval: SDK is not initialized yet." );
182261 } else {
183262 int ftrs = 0 ;
184263 for (Config .Feature f : features ) {
@@ -188,5 +267,68 @@ public static void onConsentRemoval(Config.Feature... features) {
188267 }
189268 }
190269
270+ protected static Session session (CtxCore ctx ) {
271+ return cly .sdk .session (ctx , null );
272+ }
273+
274+ @ Override
275+ public Event event (String key ) {
276+ L .d ("[Cly] event: key = " + key );
277+ return ((Session ) sdk .session (ctx , null )).event (key );
278+ }
279+
280+ @ Override
281+ public Event timedEvent (String key ) {
282+ L .d ("[Cly] timedEvent: key = " + key );
283+ return ((Session ) sdk .session (ctx , null )).timedEvent (key );
284+ }
285+
286+ /**
287+ * Get current User Profile object.
288+ *
289+ * @see User#edit() to get {@link UserEditor} object
290+ * @see UserEditor#commit() to submit changes to the server
291+ * @return current User Profile instance
292+ */
293+ @ Override
294+ public User user () {
295+ L .d ("[Cly] user" );
296+ return ((Session ) sdk .session (ctx , null )).user ();
297+ }
191298
299+ @ Override
300+ public Usage addParam (String key , Object value ) {
301+ L .d ("[Cly] addParam: key = " + key + " value = " + value );
302+ return ((Session ) sdk .session (ctx , null )).addParam (key , value );
303+ }
304+
305+ @ Override
306+ public Usage addCrashReport (Throwable t , boolean fatal ) {
307+ L .d ("[Cly] addCrashReport: t = " + t + " fatal = " + fatal );
308+ return ((Session ) sdk .session (ctx , null )).addCrashReport (t , fatal );
309+ }
310+
311+ @ Override
312+ public Usage addCrashReport (Throwable t , boolean fatal , String name , Map <String , String > segments , String ... logs ) {
313+ L .d ("[Cly] addCrashReport: t = " + t + " fatal = " + fatal + " name = " + name + " segments = " + segments + " logs = " + logs );
314+ return ((Session ) sdk .session (ctx , null )).addCrashReport (t , fatal , name , segments , logs );
315+ }
316+
317+ @ Override
318+ public Usage addLocation (double latitude , double longitude ) {
319+ L .d ("[Cly] addLocation: latitude = " + latitude + " longitude = " + longitude );
320+ return ((Session ) sdk .session (ctx , null )).addLocation (latitude , longitude );
321+ }
322+
323+ @ Override
324+ public View view (String name , boolean start ) {
325+ L .d ("[Cly] view: name = " + name + " start = " + start );
326+ return ((Session ) sdk .session (ctx , null )).view (name , start );
327+ }
328+
329+ @ Override
330+ public View view (String name ) {
331+ L .d ("[Cly] view: name = " + name );
332+ return ((Session ) sdk .session (ctx , null )).view (name );
333+ }
192334}
0 commit comments