Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new callback interface to receive the Capture creation event #96

Merged
merged 9 commits into from
Aug 26, 2024
5 changes: 5 additions & 0 deletions sample/src/main/java/com/deploygate/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public void onCreate() {
Log.i(TAG, message);
DeployGate.logInfo(message);
})
.setCaptureCreateCallback((captureUrl, createdAtMillis) -> {
String message = String.format(Locale.US, "onCaptureCreated(%s)", captureUrl);
Log.i(TAG, message);
DeployGate.logInfo(message);
})
.setEnabledOnNonDebuggableBuild(true)
.build();

Expand Down
13 changes: 12 additions & 1 deletion sample/src/main/java/com/deploygate/sample/SampleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

import com.deploygate.sdk.CustomAttributes;
import com.deploygate.sdk.DeployGate;
import com.deploygate.sdk.DeployGateCaptureCreateCallback;
import com.deploygate.sdk.DeployGateInitializeCallback;
import com.deploygate.sdk.DeployGateStatusChangeCallback;
import com.deploygate.sdk.DeployGateUpdateAvailableCallback;

public class SampleActivity extends Activity implements DeployGateInitializeCallback, DeployGateStatusChangeCallback, DeployGateUpdateAvailableCallback {
public class SampleActivity extends Activity implements DeployGateInitializeCallback, DeployGateStatusChangeCallback, DeployGateUpdateAvailableCallback, DeployGateCaptureCreateCallback {

private static final String TAG = "SampleActivity";

Expand Down Expand Up @@ -74,6 +75,7 @@ protected void onResume() {
DeployGate.registerInitializeCallback(this);
DeployGate.registerStatusChangeCallback(this);
DeployGate.registerUpdateAvailableCallback(this);
DeployGate.registerCaptureCreateCallback(this);

// finally, call refresh() method if you want to check the status immediately
DeployGate.refresh();
Expand All @@ -87,6 +89,7 @@ protected void onPause() {
DeployGate.unregisterInitializeCallback(this);
DeployGate.unregisterStatusChangeCallback(this);
DeployGate.unregisterUpdateAvailableCallback(this);
DeployGate.unregisterCaptureCreateCallback(this);
}


Expand Down Expand Up @@ -245,4 +248,12 @@ public void onUpdateAvailable(
mTitleText.setText(getString(R.string.update_available, serial, versionName, versionCode, message));
mUpdateButton.setVisibility(View.VISIBLE);
}

@Override
public void onCaptureCreated(String captureUrl, long createdAtMillis) {
// will be called when the capture is created
String message = "onCaptureCreated: url=" + captureUrl + ", created_at=" + createdAtMillis;
Log.d(TAG, message);
Toast.makeText(this, "onCaptureCreated called", Toast.LENGTH_LONG).show();
}
}
82 changes: 81 additions & 1 deletion sdk/src/main/java/com/deploygate/sdk/DeployGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class DeployGate {
private final HashSet<DeployGateInitializeCallback> mInitializeCallbacks;
private final HashSet<DeployGateStatusChangeCallback> mStatusChangeCallbacks;
private final HashSet<DeployGateUpdateAvailableCallback> mUpdateAvailableCallbacks;
private final HashSet<DeployGateCaptureCreateCallback> mCaptureCreateCallbacks;
private final HashMap<String, Bundle> mPendingEvents;
private final String mExpectedAuthor;
private String mAuthor;
Expand Down Expand Up @@ -164,6 +165,28 @@ public void onEvent(
} catch (Throwable t) {
Logger.w(t, "failed to report device states");
}
} else if(DeployGateEvent.ACTION_CAPTURE_CREATED.equals(action)) {
final String captureUrl = extras.getString(DeployGateEvent.EXTRA_CAPTURE_URL);
final long captureCreatedAt = extras.getLong(DeployGateEvent.EXTRA_CAPTURE_CREATED_AT, -1);

if (TextUtils.isEmpty(captureUrl)) {
Logger.w("Capture ID is missing in the extra");
return;
}

if (captureCreatedAt < 0) {
Logger.w("Capture created at is missing in the extra");
return;
}

mHandler.post(new Runnable() {
@Override
public void run() {
for (DeployGateCaptureCreateCallback callback : mCaptureCreateCallbacks) {
callback.onCaptureCreated(captureUrl, captureCreatedAt);
}
}
});
} else {
Logger.w("%s is not supported by this sdk version", action);
}
Expand Down Expand Up @@ -302,7 +325,6 @@ public void run() {
for (DeployGateStatusChangeCallback callback : mStatusChangeCallbacks) {
callback.onStatusChanged(false, false, null, false);
}

}
});
}
Expand All @@ -328,6 +350,7 @@ private DeployGate(
mInitializeCallbacks = new HashSet<>();
mStatusChangeCallbacks = new HashSet<>();
mUpdateAvailableCallbacks = new HashSet<>();
mCaptureCreateCallbacks = new HashSet<>();
mPendingEvents = new HashMap<>();
mExpectedAuthor = sdkConfiguration.appOwnerName;

Expand All @@ -345,6 +368,10 @@ private DeployGate(
mUpdateAvailableCallbacks.add(sdkConfiguration.updateAvailableCallback);
}

if (sdkConfiguration.captureCreateCallback != null) {
mCaptureCreateCallbacks.add(sdkConfiguration.captureCreateCallback);
}

mInitializedLatch = new CountDownLatch(1);
((Application) applicationContext).registerActivityLifecycleCallbacks(new VisibilityLifecycleCallbacks(mOnVisibilityChangeListener));
initService(true);
Expand Down Expand Up @@ -941,6 +968,52 @@ private void unregisterUpdateAvailableCallbackInternal(DeployGateUpdateAvailable
mUpdateAvailableCallbacks.remove(callback);
}

/**
* Register a callback listener about the new capture is created.
* Don't forget to unregister the callback listener when it is no longer needed (e.g., on destroying an activity.)
* If the listener has already in the callback list, just ignored.
*
* @param callback callback listener
* @see #unregisterCaptureCreateCallback(DeployGateCaptureCreateCallback)
* @since 4.9.0
*/
public static void registerCaptureCreateCallback(DeployGateCaptureCreateCallback callback) {
if (sInstance == null) {
return;
}
if (callback == null) {
return;
}

sInstance.registerCaptureCaptureCallbackInternal(callback);
}

private void registerCaptureCaptureCallbackInternal(DeployGateCaptureCreateCallback callback) {
mCaptureCreateCallbacks.add(callback);
}

/**
* Unregister a callback listener.
* If the listener was not registered, just ignored.
*
* @param callback callback listener to be removed
* @since 4.9.0
*/
public static void unregisterCaptureCreateCallback(DeployGateCaptureCreateCallback callback) {
if (sInstance == null) {
return;
}
if (callback == null) {
return;
}

sInstance.unregisterCaptureCaptureCallbackInternal(callback);
}

private void unregisterCaptureCaptureCallbackInternal(DeployGateCaptureCreateCallback callback) {
mCaptureCreateCallbacks.remove(callback);
}

/**
* Register a DeployGate event callback listener. Don't forget to call
* {@link #unregisterCallback(DeployGateCallback)} when the callback is no
Expand Down Expand Up @@ -1671,4 +1744,11 @@ static HashSet<DeployGateStatusChangeCallback> getStatusChangeCallbacks() {
static HashSet<DeployGateUpdateAvailableCallback> getUpdateAvailableCallbacks() {
return sInstance != null ? sInstance.mUpdateAvailableCallbacks : null;
}

/**
* Not a public API. This is only for testing.
*/
static HashSet<DeployGateCaptureCreateCallback> getCaptureCreateCallbacks() {
return sInstance != null ? sInstance.mCaptureCreateCallbacks : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.deploygate.sdk;

/**
* A callback interface to receive the capture creation event.
*
* @since 4.9.0
* @see DeployGate#registerCaptureCreateCallback(DeployGateCaptureCreateCallback)
*/
public interface DeployGateCaptureCreateCallback {
/**
* Callback to tell the new capture is successfully created.
*
* @param captureUrl URL of the created capture
* @param createdAtMillis Created time of the capture in milliseconds
* @since 4.9.0
*/
public void onCaptureCreated(
String captureUrl,
long createdAtMillis
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class DeployGateSdkConfiguration {
final DeployGateInitializeCallback initializeCallback;
final DeployGateStatusChangeCallback statusChangeCallback;
final DeployGateUpdateAvailableCallback updateAvailableCallback;
final DeployGateCaptureCreateCallback captureCreateCallback;

final boolean isCaptureEnabled;

Expand All @@ -30,7 +31,8 @@ private DeployGateSdkConfiguration(
builder.isCaptureEnabled,
builder.initializeCallback,
builder.statusChangeCallback,
builder.updateAvailableCallback
builder.updateAvailableCallback,
builder.captureCreateCallback
);
}

Expand All @@ -43,7 +45,8 @@ private DeployGateSdkConfiguration(
boolean isCaptureEnabled,
DeployGateInitializeCallback initializeCallback,
DeployGateStatusChangeCallback statusChangeCallback,
DeployGateUpdateAvailableCallback updateAvailableCallback
DeployGateUpdateAvailableCallback updateAvailableCallback,
DeployGateCaptureCreateCallback captureCallback
) {
this.customLogConfiguration = customLogConfiguration;
this.isDisabled = isDisabled;
Expand All @@ -54,6 +57,7 @@ private DeployGateSdkConfiguration(
this.initializeCallback = initializeCallback;
this.statusChangeCallback = statusChangeCallback;
this.updateAvailableCallback = updateAvailableCallback;
this.captureCreateCallback = captureCallback;
}

public static final class Builder {
Expand All @@ -71,6 +75,7 @@ public static final class Builder {
private DeployGateInitializeCallback initializeCallback = null;
private DeployGateStatusChangeCallback statusChangeCallback = null;
private DeployGateUpdateAvailableCallback updateAvailableCallback = null;
private DeployGateCaptureCreateCallback captureCreateCallback = null;

public Builder() {
}
Expand Down Expand Up @@ -184,6 +189,16 @@ public Builder setUpdateAvailableCallback(DeployGateUpdateAvailableCallback upda
return this;
}

/**
*
* @param captureCallback Set an instance of callback for the capture creation event.
* @return self
*/
public Builder setCaptureCreateCallback(DeployGateCaptureCreateCallback captureCallback) {
this.captureCreateCallback = captureCallback;
return this;
}

/**
* @return a new sdk configuration.
*/
Expand Down
15 changes: 15 additions & 0 deletions sdk/src/main/java/com/deploygate/service/DeployGateEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public interface DeployGateEvent {
*/
public static final String ACTION_COLLECT_DEVICE_STATES = "a.collect-device-states";

/**
* @since 4.9.0
*/
public static final String ACTION_CAPTURE_CREATED = "a.capture-created";

public static final String EXTRA_AUTHOR = "author";
public static final String EXTRA_EXPECTED_AUTHOR = "expectedAuthor";

Expand Down Expand Up @@ -116,6 +121,16 @@ public interface DeployGateEvent {
*/
public static final String EXTRA_CAPTURE_ID = "e.capture-id";

/**
* the url of the capture.
*/
public static final String EXTRA_CAPTURE_URL = "e.capture-url";

/**
* the created time of the capture.
*/
public static final String EXTRA_CAPTURE_CREATED_AT = "e.capture-created-at";

/**
* A event type for the app goes to foreground/background.
*/
Expand Down
25 changes: 25 additions & 0 deletions sdk/src/test/java/com/deploygate/sdk/DeployGateInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class DeployGateInterfaceTest {
DeployGateInitializeCallback initializeCallback;
DeployGateStatusChangeCallback statusChangeCallback;
DeployGateUpdateAvailableCallback updateAvailableCallback;
DeployGateCaptureCreateCallback captureCreateCallback;

@Before
public void setUp() {
Expand Down Expand Up @@ -77,6 +78,12 @@ public void onUpdateAvailable(int revision, String versionName, int versionCode)
// no-op
}
};
captureCreateCallback = new DeployGateCaptureCreateCallback() {
@Override
public void onCaptureCreated(String captureUrl, long createdAtMillis) {
// no-op
}
};

DeployGate.clear();
}
Expand Down Expand Up @@ -240,6 +247,15 @@ public void onUpdateAvailable(int revision, String versionName, int versionCode)
// no-op
}
});

DeployGate.registerCaptureCreateCallback(null);
DeployGate.registerCaptureCreateCallback(captureCreateCallback);
DeployGate.registerCaptureCreateCallback(new DeployGateCaptureCreateCallback() {
@Override
public void onCaptureCreated(String captureUrl, long createdAtMillis) {
// no-op
}
});
}

@Test
Expand Down Expand Up @@ -298,6 +314,15 @@ public void onUpdateAvailable(int revision, String versionName, int versionCode)
}
});
DeployGate.unregisterUpdateAvailableCallback(updateAvailableCallback);

DeployGate.unregisterCaptureCreateCallback(null);
DeployGate.unregisterCaptureCreateCallback(new DeployGateCaptureCreateCallback() {
@Override
public void onCaptureCreated(String captureUrl, long createdAtMillis) {
// no-op
}
});
DeployGate.unregisterCaptureCreateCallback(captureCreateCallback);
}

@Test
Expand Down
Loading
Loading