Skip to content

Commit cb541e6

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: support application context when initialize SDK. (#25)
Co-authored-by: xiaoweii <[email protected]>
1 parent abfdbf1 commit cb541e6

File tree

6 files changed

+36
-55
lines changed

6 files changed

+36
-55
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Add the following dependency to your `app` module's `build.gradle` file.
2020

2121
```groovy
2222
dependencies {
23-
implementation 'software.aws.solution:clickstream:0.5.1'
23+
implementation 'software.aws.solution:clickstream:0.5.2'
2424
}
2525
```
2626

@@ -32,7 +32,7 @@ Find the res directory under your `project/app/src/main` , and manually create
3232

3333
![](images/raw_folder.png)
3434

35-
Downlod your `amplifyconfiguration.json` file from your clickstream control plane, and paste it to raw floder, the json file will be as following:
35+
Download your `amplifyconfiguration.json` file from your clickstream control plane, and paste it to raw folder, the json file will be as following:
3636

3737
```json
3838
{
@@ -69,7 +69,7 @@ public void onCreate() {
6969
super.onCreate();
7070

7171
try{
72-
ClickstreamAnalytics.init(this);
72+
ClickstreamAnalytics.init(getApplicationContext());
7373
Log.i("MyApp", "Initialized ClickstreamAnalytics");
7474
} catch (AmplifyException error){
7575
Log.e("MyApp", "Could not initialize ClickstreamAnalytics", error);

clickstream/src/main/java/software/aws/solution/clickstream/AWSClickstreamPlugin.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package software.aws.solution.clickstream;
1717

18-
import android.app.Application;
1918
import android.content.Context;
2019
import androidx.annotation.NonNull;
2120
import androidx.annotation.Nullable;
@@ -45,18 +44,18 @@
4544
public final class AWSClickstreamPlugin extends AnalyticsPlugin<Object> {
4645

4746
private static final Log LOG = LogFactory.getLog(AWSClickstreamPlugin.class);
48-
private final Application application;
47+
private final Context context;
4948
private AnalyticsClient analyticsClient;
5049
private AutoEventSubmitter autoEventSubmitter;
5150
private ActivityLifecycleManager activityLifecycleManager;
5251

5352
/**
5453
* Constructs a new {@link AWSClickstreamPlugin}.
5554
*
56-
* @param application Global application context
55+
* @param context ApplicationContext
5756
*/
58-
public AWSClickstreamPlugin(final Application application) {
59-
this.application = application;
57+
public AWSClickstreamPlugin(final Context context) {
58+
this.context = context;
6059
}
6160

6261
@Override
@@ -79,13 +78,13 @@ public void identifyUser(@NonNull String userId, @Nullable UserProfile profile)
7978
@Override
8079
public void disable() {
8180
autoEventSubmitter.stop();
82-
activityLifecycleManager.stopLifecycleTracking(application);
81+
activityLifecycleManager.stopLifecycleTracking(context);
8382
}
8483

8584
@Override
8685
public void enable() {
8786
autoEventSubmitter.start();
88-
activityLifecycleManager.startLifecycleTracking(application, ProcessLifecycleOwner.get().getLifecycle());
87+
activityLifecycleManager.startLifecycleTracking(context, ProcessLifecycleOwner.get().getLifecycle());
8988
}
9089

9190
@Override
@@ -197,7 +196,7 @@ public void configure(
197196
autoEventSubmitter.start();
198197

199198
activityLifecycleManager = new ActivityLifecycleManager(clickstreamManager);
200-
activityLifecycleManager.startLifecycleTracking(application, ProcessLifecycleOwner.get().getLifecycle());
199+
activityLifecycleManager.startLifecycleTracking(this.context, ProcessLifecycleOwner.get().getLifecycle());
201200
}
202201

203202
@Override

clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.app.Activity;
1919
import android.app.Application;
20+
import android.content.Context;
2021
import android.os.Bundle;
2122
import androidx.annotation.NonNull;
2223
import androidx.lifecycle.Lifecycle;
@@ -35,11 +36,8 @@
3536
**/
3637
final class ActivityLifecycleManager implements Application.ActivityLifecycleCallbacks, LifecycleEventObserver {
3738
private static final Log LOG = LogFactory.getLog(ActivityLifecycleManager.class);
38-
3939
private final SessionClient sessionClient;
4040
private final AutoRecordEventClient autoRecordEventClient;
41-
private boolean inForeground;
42-
private int foregroundActivityCount;
4341

4442
/**
4543
* Constructor. Registers to receive activity lifecycle events.
@@ -49,17 +47,21 @@ final class ActivityLifecycleManager implements Application.ActivityLifecycleCal
4947
ActivityLifecycleManager(final ClickstreamManager clickstreamManager) {
5048
this.sessionClient = clickstreamManager.getSessionClient();
5149
this.autoRecordEventClient = clickstreamManager.getAutoRecordEventClient();
52-
inForeground = false;
53-
foregroundActivityCount = 0;
5450
}
5551

56-
void startLifecycleTracking(final Application application, Lifecycle lifecycle) {
57-
application.registerActivityLifecycleCallbacks(this);
58-
lifecycle.addObserver(this);
52+
void startLifecycleTracking(final Context context, Lifecycle lifecycle) {
53+
if (context instanceof Application) {
54+
((Application) context).registerActivityLifecycleCallbacks(this);
55+
lifecycle.addObserver(this);
56+
} else {
57+
LOG.warn("The context is not ApplicationContext, so lifecycle events are not automatically recorded");
58+
}
5959
}
6060

61-
void stopLifecycleTracking(final Application application) {
62-
application.unregisterActivityLifecycleCallbacks(this);
61+
void stopLifecycleTracking(final Context context) {
62+
if (context instanceof Application) {
63+
((Application) context).unregisterActivityLifecycleCallbacks(this);
64+
}
6365
}
6466

6567
@Override
@@ -77,8 +79,6 @@ public void onActivityResumed(final Activity activity) {
7779
// An activity came to foreground. Application potentially entered foreground as well
7880
// if there were no other activities in the foreground.
7981
LOG.debug("Activity resumed: " + activity.getLocalClassName());
80-
checkIfApplicationEnteredForeground();
81-
foregroundActivityCount++;
8282
autoRecordEventClient.recordViewScreen(activity);
8383
}
8484

@@ -97,8 +97,6 @@ public void onActivityStopped(final Activity activity) {
9797
// no other activities in non-stopped states, in which case app is not visible to user and has
9898
// entered background state.
9999
LOG.debug("Activity stopped: " + activity.getLocalClassName());
100-
foregroundActivityCount--;
101-
checkIfApplicationEnteredBackground();
102100
}
103101

104102
@Override
@@ -112,29 +110,6 @@ public void onActivityDestroyed(final Activity activity) {
112110
LOG.debug("Activity destroyed " + activity.getLocalClassName());
113111
}
114112

115-
/**
116-
* Called from onActivityResumed to check if the application came to the foreground.
117-
*/
118-
private void checkIfApplicationEnteredForeground() {
119-
// if nothing is in the activity lifecycle map indicating that we are likely in the background, and the flag
120-
// indicates we are indeed in the background.
121-
if (foregroundActivityCount == 0 && !inForeground) {
122-
inForeground = true;
123-
LOG.debug("Application open.");
124-
}
125-
}
126-
127-
/**
128-
* Called from onActivityStopped to check if the application receded to the background.
129-
*/
130-
private void checkIfApplicationEnteredBackground() {
131-
// If the App is in the foreground and there are no longer any activities that have not been stopped.
132-
if (foregroundActivityCount == 0 && inForeground) {
133-
inForeground = false;
134-
LOG.debug("Application exit.");
135-
}
136-
}
137-
138113
@Override
139114
public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {
140115
if (event == Lifecycle.Event.ON_STOP) {

clickstream/src/main/java/software/aws/solution/clickstream/ClickstreamAnalytics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
package software.aws.solution.clickstream;
1717

18-
import android.app.Application;
18+
import android.content.Context;
1919
import androidx.annotation.NonNull;
2020

2121
import com.amplifyframework.AmplifyException;
@@ -40,7 +40,7 @@ private ClickstreamAnalytics() {
4040
* @param context ApplicationContext
4141
* @throws AmplifyException Exception of init.
4242
*/
43-
public static void init(@NonNull Application context) throws AmplifyException {
43+
public static void init(@NonNull Context context) throws AmplifyException {
4444
Amplify.addPlugin(new AWSClickstreamPlugin(context));
4545
Amplify.configure(context);
4646
}

clickstream/src/test/java/software/aws/solution/clickstream/ActivityLifecycleManagerUnitTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public final class ActivityLifecycleManagerUnitTest {
4747
private Application.ActivityLifecycleCallbacks callbacks;
4848
private Log log;
4949
private LifecycleRegistry lifecycle;
50+
private ActivityLifecycleManager lifecycleManager;
5051

5152
/**
5253
* Setup dependencies and object under test.
@@ -60,7 +61,7 @@ public void setup() throws Exception {
6061
this.autoRecordEventClient = mock(AutoRecordEventClient.class);
6162
when(clickstreamManager.getSessionClient()).thenReturn(sessionClient);
6263
when(clickstreamManager.getAutoRecordEventClient()).thenReturn(autoRecordEventClient);
63-
ActivityLifecycleManager lifecycleManager = new ActivityLifecycleManager(clickstreamManager);
64+
lifecycleManager = new ActivityLifecycleManager(clickstreamManager);
6465
this.callbacks = lifecycleManager;
6566
log = mock(Log.class);
6667
ReflectUtil.modifyFiled(this.callbacks, "LOG", log);
@@ -82,7 +83,6 @@ public void testWhenAppOpened() {
8283
callbacks.onActivityCreated(activity, bundle);
8384
callbacks.onActivityStarted(activity);
8485
callbacks.onActivityResumed(activity);
85-
verify(log).debug("Application open.");
8686
}
8787

8888
/**
@@ -104,9 +104,16 @@ public void testWhenAppEnterAndExit() {
104104
callbacks.onActivitySaveInstanceState(activity, bundle);
105105
callbacks.onActivityStopped(activity);
106106
callbacks.onActivityDestroyed(activity);
107+
}
107108

108-
verify(log).debug("Application open.");
109-
verify(log).debug("Application exit.");
109+
/**
110+
* test invalid context.
111+
*/
112+
@Test
113+
public void testContextIsInValid() {
114+
Activity activity = mock(Activity.class);
115+
lifecycleManager.startLifecycleTracking(activity, lifecycle);
116+
verify(log).warn("The context is not ApplicationContext, so lifecycle events are not automatically recorded");
110117
}
111118

112119
/**

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ org.gradle.jvmargs=-Xmx4g
1212
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1313
org.gradle.parallel=true
1414

15-
VERSION_NAME=0.5.1
15+
VERSION_NAME=0.5.2
1616

1717
POM_GROUP=software.aws.solution
1818
POM_ARTIFACT_ID=clickstream

0 commit comments

Comments
 (0)