Skip to content

Commit

Permalink
Support unregister device
Browse files Browse the repository at this point in the history
  • Loading branch information
rickmak committed Dec 20, 2016
2 parents ad798df + efe35e1 commit f5ac936
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.skygear.skygear;

import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Map;

import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public class UnregisterDeviceRequestUnitTest {
@Test
public void testUnregisterDeviceRequestCreateFlow() throws Exception {
UnregisterDeviceRequest request = new UnregisterDeviceRequest("device_1");

assertEquals("device:unregister", request.action);

Map<String, Object> data = request.data;
assertEquals("device_1", data.get("id"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.skygear.skygear;

import android.support.test.runner.AndroidJUnit4;

import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public class UnregisterDeviceResponseHandlerUnitTest {
@Test
public void testUnregisterDeviceResponseSuccessFlow() throws Exception {
JSONObject result = new JSONObject();
result.put("id", "device_1");

final boolean[] checkpoints = new boolean[] { false };
UnregisterDeviceResponseHandler handler = new UnregisterDeviceResponseHandler() {
@Override
public void onUnregisterSuccess(String deviceId) {
assertEquals("device_1", deviceId);
checkpoints[0] = true;
}

@Override
public void onUnregisterError(Error error) {
fail("Should not get fail callback");
}
};

handler.onSuccess(result);
assertTrue(checkpoints[0]);
}

@Test
public void testUnregisterDeviceResponseFailFlow() throws Exception {
final boolean[] checkpoints = new boolean[] { false };
UnregisterDeviceResponseHandler handler = new UnregisterDeviceResponseHandler() {
@Override
public void onUnregisterSuccess(String deviceId) {
fail("Should not get success callback");
}

@Override
public void onUnregisterError(Error error) {
assertEquals("Test error", error.getMessage());
checkpoints[0] = true;
}
};

handler.onFail(new Error("Test error"));
assertTrue(checkpoints[0]);
}
}
63 changes: 60 additions & 3 deletions skygear/src/main/java/io/skygear/skygear/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,27 @@ public void loginWithEmail(String email, String password, AuthResponseHandler ha
* @param handler the response handler
*/
public void logout(LogoutResponseHandler handler) {
Request req = new LogoutRequest();
req.responseHandler = new LogoutResponseHandlerWrapper(this, handler);
final Request logoutRequest = new LogoutRequest();
logoutRequest.responseHandler = new LogoutResponseHandlerWrapper(this, handler);

this.requestManager.sendRequest(req);
String deviceId = this.persistentStore.deviceId;
if (this.getCurrentUser() != null && deviceId != null) {
// Try to unregister the device token before login out
this.unregisterDeviceToken(new UnregisterDeviceResponseHandler() {
@Override
public void onUnregisterSuccess(String deviceId) {
Container.this.requestManager.sendRequest(logoutRequest);
}

@Override
public void onUnregisterError(Error error) {
Log.w(TAG, "Fail to unregister device", error);
Container.this.requestManager.sendRequest(logoutRequest);
}
});
} else {
this.requestManager.sendRequest(logoutRequest);
}
}

/**
Expand All @@ -251,6 +268,11 @@ public void whoami(AuthResponseHandler handler) {
this.requestManager.sendRequest(req);
}

/**
* Register device token.
*
* @param token the token
*/
public void registerDeviceToken(String token) {
this.persistentStore.deviceToken = token;
this.persistentStore.save();
Expand Down Expand Up @@ -283,6 +305,41 @@ public void onRegisterError(Error error) {
}
}

/**
* Unregister device token.
*/
public void unregisterDeviceToken() {
this.unregisterDeviceToken(new UnregisterDeviceResponseHandler() {
@Override
public void onUnregisterSuccess(String deviceId) {
Log.i(TAG, "Successfully register device with ID = " + deviceId);
}

@Override
public void onUnregisterError(Error error) {
Log.w(TAG, String.format(
"Fail to unregister device token: %s",
error.getMessage()
));
}
});
}

/**
* Unregister device token.
*
* @param handler the response handler
*/
public void unregisterDeviceToken(UnregisterDeviceResponseHandler handler) {
String deviceId = this.persistentStore.deviceId;
if (this.getCurrentUser() != null && deviceId != null) {
UnregisterDeviceRequest request = new UnregisterDeviceRequest(deviceId);
request.responseHandler = handler;

this.requestManager.sendRequest(request);
}
}

@Override
public void resolveAuthUser(User user) {
this.persistentStore.currentUser = user;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.skygear.skygear;

import java.util.HashMap;

public class UnregisterDeviceRequest extends Request {
public UnregisterDeviceRequest() {
super("device:unregister");

this.data = new HashMap<>();
}

public UnregisterDeviceRequest(String deviceId) {
this();

if (deviceId != null) {
this.data.put("id", deviceId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.skygear.skygear;

import org.json.JSONException;
import org.json.JSONObject;

/**
* The Skygear Unregister Device Response Handler.
*/
public abstract class UnregisterDeviceResponseHandler implements ResponseHandler {

/**
* Unregister success callback.
*
* @param deviceId the device id
*/
public abstract void onUnregisterSuccess(String deviceId);

/**
* Unregister error callback.
*
* @param error the error
*/
public abstract void onUnregisterError(Error error);

@Override
public void onSuccess(JSONObject result) {
try {
String deviceId = result.getString("id");
this.onUnregisterSuccess(deviceId);
} catch (JSONException e) {
this.onUnregisterError(new Error("Malformed server response"));
}
}

@Override
public void onFail(Error error) {
this.onUnregisterError(error);
}
}

0 comments on commit f5ac936

Please sign in to comment.