Commit b420af80 authored by Yusuke Iwaki's avatar Yusuke Iwaki Committed by GitHub

Merge pull request #130 from RocketChat/refactor_push_codes

Refactor push-notification related codes
parents c020b195 a5ca821c
...@@ -66,9 +66,6 @@ ...@@ -66,9 +66,6 @@
<action android:name="com.google.android.gms.iid.InstanceID" /> <action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter> </intent-filter>
</service> </service>
<service
android:name=".push.gcm.GcmRegistrationIntentService"
android:exported="false" />
</application> </application>
</manifest> </manifest>
...@@ -12,7 +12,7 @@ public class RocketChatCache { ...@@ -12,7 +12,7 @@ public class RocketChatCache {
public static final String KEY_SELECTED_SERVER_CONFIG_ID = "selectedServerConfigId"; public static final String KEY_SELECTED_SERVER_CONFIG_ID = "selectedServerConfigId";
public static final String KEY_SELECTED_ROOM_ID = "selectedRoomId"; public static final String KEY_SELECTED_ROOM_ID = "selectedRoomId";
private static final String PUSH_ID = "pushId"; private static final String KEY_PUSH_ID = "pushId";
/** /**
* get SharedPreference instance for RocketChat application cache. * get SharedPreference instance for RocketChat application cache.
...@@ -21,36 +21,16 @@ public class RocketChatCache { ...@@ -21,36 +21,16 @@ public class RocketChatCache {
return context.getSharedPreferences("cache", Context.MODE_PRIVATE); return context.getSharedPreferences("cache", Context.MODE_PRIVATE);
} }
public static String getSelectedServerConfigId(Context context) { public static String getOrCreatePushId(Context context) {
return get(context).getString(KEY_SELECTED_SERVER_CONFIG_ID, "");
}
public static void setSelectedServerConfigId(Context context, String serverConfigId) {
setString(get(context), KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId);
}
public static String getSelectedRoomId(Context context) {
return get(context).getString(KEY_SELECTED_ROOM_ID, "");
}
public static void setSelectedRoomId(Context context, String roomId) {
setString(get(context), KEY_SELECTED_ROOM_ID, roomId);
}
public static String getPushId(Context context) {
SharedPreferences preferences = get(context); SharedPreferences preferences = get(context);
String pushId = null; if (!preferences.contains(KEY_PUSH_ID)) {
if (!preferences.contains(PUSH_ID)) {
// generates one and save // generates one and save
pushId = UUID.randomUUID().toString().replace("-", ""); String newId = UUID.randomUUID().toString().replace("-", "");
setString(preferences, PUSH_ID, pushId); preferences.edit()
.putString(KEY_PUSH_ID, newId)
.apply();
return newId;
} }
return preferences.getString(PUSH_ID, pushId); return preferences.getString(KEY_PUSH_ID, null);
}
private static void setString(SharedPreferences preferences, String key, String value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
} }
} }
...@@ -38,13 +38,6 @@ public class MethodCallHelper { ...@@ -38,13 +38,6 @@ public class MethodCallHelper {
protected final RealmHelper realmHelper; protected final RealmHelper realmHelper;
protected final DDPClientWrapper ddpClient; protected final DDPClientWrapper ddpClient;
/**
* initialize with ServerConfigId.
*/
public MethodCallHelper(String serverConfigId) {
this(null, serverConfigId);
}
/** /**
* initialize with Context and ServerConfigId. * initialize with Context and ServerConfigId.
*/ */
......
...@@ -3,66 +3,36 @@ package chat.rocket.android.api; ...@@ -3,66 +3,36 @@ package chat.rocket.android.api;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import bolts.Task; import bolts.Task;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
public class PushHelper extends MethodCallHelper { public class RaixPushHelper extends MethodCallHelper {
public PushHelper(String serverConfigId) { public RaixPushHelper(Context context, String serverConfigId) {
super(serverConfigId);
}
public PushHelper(Context context, String serverConfigId) {
super(context, serverConfigId); super(context, serverConfigId);
} }
public PushHelper(RealmHelper realmHelper, public RaixPushHelper(RealmHelper realmHelper,
DDPClientWrapper ddpClient) { DDPClientWrapper ddpClient) {
super(realmHelper, ddpClient); super(realmHelper, ddpClient);
} }
public Task<Void> pushUpdate(@NonNull String pushId, @NonNull String token, public Task<Void> pushUpdate(@NonNull String pushId, @NonNull String gcmToken,
@Nullable String userId) { @Nullable String userId) {
return call("raix:push-update", TIMEOUT_MS, () -> { return call("raix:push-update", TIMEOUT_MS, () ->
JSONObject param = new PushUpdate(pushId, token, userId).toJson(); new JSONArray().put(new JSONObject()
return new JSONArray().put(param); .put("id", pushId)
}).onSuccessTask(task -> Task.forResult(null)); .put("appName", "main")
.put("userId", userId != null ? userId : JSONObject.NULL)
.put("metadata", new JSONObject())
.put("token", new JSONObject().put("gcm", gcmToken))))
.onSuccessTask(task -> Task.forResult(null));
} }
public Task<Void> pushSetUser(String pushId) { public Task<Void> pushSetUser(String pushId) {
return call("raix:push-setuser", TIMEOUT_MS, () -> new JSONArray().put(pushId)) return call("raix:push-setuser", TIMEOUT_MS, () -> new JSONArray().put(pushId))
.onSuccessTask(task -> Task.forResult(null)); .onSuccessTask(task -> Task.forResult(null));
} }
private static class PushUpdate {
private String pushId;
private String gcmToken;
private String userId;
PushUpdate(@NonNull String pushId, @NonNull String gcmToken, @Nullable String userId) {
this.pushId = pushId;
this.gcmToken = gcmToken;
this.userId = userId;
}
JSONObject toJson() throws JSONException {
JSONObject param = new JSONObject();
param.put("id", pushId);
param.put("appName", "main");
param.put("userId", userId != null ? userId : JSONObject.NULL);
param.put("metadata", new JSONObject());
JSONObject tokenParam = new JSONObject();
tokenParam.put("gcm", gcmToken);
param.put("token", tokenParam);
return param;
}
}
} }
package chat.rocket.android.helper;
import io.realm.Realm;
import io.realm.RealmResults;
import java.util.List;
import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants;
/**
* utility class for getting value comprehensibly from public settings list.
*/
public class GcmPushSettingHelper {
public static RealmResults<PublicSetting> queryForGcmPushEnabled(Realm realm) {
return realm.where(PublicSetting.class)
.equalTo(PublicSetting.ID, PublicSettingsConstants.Push.ENABLE)
.or()
.equalTo(PublicSetting.ID, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER)
.findAll();
}
public static boolean isGcmPushEnabled(List<PublicSetting> results) {
return isPushEnabled(results) && hasValidGcmConfig(results);
}
private static boolean isPushEnabled(List<PublicSetting> results) {
for (PublicSetting setting : results) {
if (PublicSettingsConstants.Push.ENABLE.equals(setting.getId())) {
return "true".equals(setting.getValue());
}
}
return false;
}
private static boolean hasValidGcmConfig(List<PublicSetting> results) {
for (PublicSetting setting : results) {
if (PublicSettingsConstants.Push.GCM_PROJECT_NUMBER.equals(setting.getId())) {
return !TextUtils.isEmpty(setting.getValue());
}
}
return false;
}
}
...@@ -20,7 +20,6 @@ public class ServerConfig extends RealmObject { ...@@ -20,7 +20,6 @@ public class ServerConfig extends RealmObject {
public static final String STATE = "state"; public static final String STATE = "state";
public static final String SESSION = "session"; public static final String SESSION = "session";
public static final String ERROR = "error"; public static final String ERROR = "error";
public static final String SYNC_PUSH_TOKEN = "syncPushToken";
public static final int STATE_READY = 0; public static final int STATE_READY = 0;
public static final int STATE_CONNECTING = 1; public static final int STATE_CONNECTING = 1;
...@@ -32,7 +31,6 @@ public class ServerConfig extends RealmObject { ...@@ -32,7 +31,6 @@ public class ServerConfig extends RealmObject {
private int state; private int state;
private String session; private String session;
private String error; private String error;
private boolean syncPushToken;
/** /**
* Log the server connection is lost due to some exception. * Log the server connection is lost due to some exception.
...@@ -102,12 +100,4 @@ public class ServerConfig extends RealmObject { ...@@ -102,12 +100,4 @@ public class ServerConfig extends RealmObject {
public void setError(String error) { public void setError(String error) {
this.error = error; this.error = error;
} }
public boolean shouldSyncPushToken() {
return syncPushToken;
}
public void setSyncPushToken(boolean syncPushToken) {
this.syncPushToken = syncPushToken;
}
} }
package chat.rocket.android.model.internal;
import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.annotations.PrimaryKey;
import org.json.JSONException;
import org.json.JSONObject;
import chat.rocket.android.model.SyncState;
/**
* just stores gcm registration status.
*/
public class GcmPushRegistration extends RealmObject {
@SuppressWarnings({"PMD.ShortVariable"})
private static final String ID = "dummyId";
public static final String SYNC_STATE = "syncState";
public static final String GCM_PUSH_ENABLED = "gcmPushEnabled";
private static final int DEFAULT_ID = 0;
@PrimaryKey private int dummyId;
private int syncState;
private boolean gcmPushEnabled;
public boolean isGcmPushEnabled() {
return gcmPushEnabled;
}
public void setGcmPushEnabled(boolean gcmPushEnabled) {
this.gcmPushEnabled = gcmPushEnabled;
}
public int getSyncState() {
return syncState;
}
public void setSyncState(int syncState) {
this.syncState = syncState;
}
public static GcmPushRegistration updateGcmPushEnabled(Realm realm, boolean gcmPushEnabled)
throws JSONException {
GcmPushRegistration gcmPushRegistration = GcmPushRegistration.queryDefault(realm).findFirst();
if (gcmPushRegistration != null
&& (gcmPushRegistration.getSyncState() == SyncState.NOT_SYNCED
|| gcmPushRegistration.getSyncState() == SyncState.SYNCING)
&& gcmPushEnabled == gcmPushRegistration.isGcmPushEnabled()) {
// omit duplicated request.
return gcmPushRegistration;
}
return realm.createOrUpdateObjectFromJson(GcmPushRegistration.class, new JSONObject()
.put(ID, DEFAULT_ID)
.put(SYNC_STATE, SyncState.NOT_SYNCED)
.put(GCM_PUSH_ENABLED, gcmPushEnabled));
}
public static RealmQuery<GcmPushRegistration> queryDefault(Realm realm) {
return realm.where(GcmPushRegistration.class).equalTo(ID, DEFAULT_ID);
}
}
package chat.rocket.android.push.gcm; package chat.rocket.android.push.gcm;
import com.google.android.gms.gcm.GcmListenerService;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
......
package chat.rocket.android.push.gcm; package chat.rocket.android.push.gcm;
import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService; import com.google.android.gms.iid.InstanceIDListenerService;
import java.util.List; import java.util.List;
import chat.rocket.android.helper.GcmPushSettingHelper;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.PublicSetting; import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants; import chat.rocket.android.model.internal.GcmPushRegistration;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
...@@ -15,48 +14,28 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService { ...@@ -15,48 +14,28 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService {
@Override @Override
public void onTokenRefresh() { public void onTokenRefresh() {
List<ServerConfig> serverConfigs = getServerConfigs(); List<ServerConfig> serverConfigs = RealmStore.getDefault()
.executeTransactionForReadResults(realm ->
updateSyncPushToken(serverConfigs); realm.where(ServerConfig.class)
.isNotNull(ServerConfig.ID)
if (!shouldRefreshToken(serverConfigs)) { .isNotNull(ServerConfig.HOSTNAME)
return; .findAll());
for (ServerConfig serverConfig : serverConfigs) {
RealmHelper realmHelper = RealmStore.get(serverConfig.getServerConfigId());
if (realmHelper != null) {
updateGcmToken(realmHelper);
} }
Intent intent = new Intent(this, GcmRegistrationIntentService.class);
startService(intent);
} }
private List<ServerConfig> getServerConfigs() {
return RealmStore.getDefault().executeTransactionForReadResults(
realm -> realm.where(ServerConfig.class).findAll());
} }
private void updateSyncPushToken(List<ServerConfig> serverConfigs) { private void updateGcmToken(RealmHelper realmHelper) {
final RealmHelper realmHelper = RealmStore.getDefault(); final List<PublicSetting> results = realmHelper.executeTransactionForReadResults(
GcmPushSettingHelper::queryForGcmPushEnabled);
for (final ServerConfig serverConfig : serverConfigs) { final boolean gcmPushEnabled = GcmPushSettingHelper.isGcmPushEnabled(results);
final RealmHelper serverRealmHelper = RealmStore
.getOrCreate(serverConfig.getServerConfigId());
boolean isPushEnable = PublicSetting
.getBoolean(serverRealmHelper, PublicSettingsConstants.Push.ENABLE, false);
String senderId = PublicSetting
.getString(serverRealmHelper, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER, "").trim();
serverConfig.setSyncPushToken(isPushEnable && !"".equals(senderId)); if (gcmPushEnabled) {
realmHelper.executeTransaction(realm ->
realmHelper.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig)); GcmPushRegistration.updateGcmPushEnabled(realm, gcmPushEnabled));
}
} }
private boolean shouldRefreshToken(List<ServerConfig> serverConfigs) {
for (ServerConfig serverConfig : serverConfigs) {
if (serverConfig.shouldSyncPushToken()) {
return true;
}
}
return false;
} }
} }
\ No newline at end of file
package chat.rocket.android.push.gcm;
import android.app.IntentService;
import android.content.Intent;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import java.io.IOException;
import java.util.List;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.PushHelper;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
public class GcmRegistrationIntentService extends IntentService {
public GcmRegistrationIntentService() {
super("GcmRegistrationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final List<ServerConfig> serverConfigs = getServerConfigs();
for (ServerConfig serverConfig : serverConfigs) {
sendTokenTo(serverConfig);
}
}
private List<ServerConfig> getServerConfigs() {
return RealmStore.getDefault().executeTransactionForReadResults(
realm -> realm.where(ServerConfig.class).findAll());
}
private void sendTokenTo(final ServerConfig serverConfig) {
if (!serverConfig.shouldSyncPushToken()) {
return;
}
final RealmHelper realmHelper = RealmStore.get(serverConfig.getServerConfigId());
if (realmHelper == null) {
return;
}
final String senderId = PublicSetting
.getString(realmHelper, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER, "").trim();
if ("".equals(senderId)) {
markRefreshAsDone(serverConfig);
return;
}
try {
final String token = getToken(senderId);
final User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst());
new PushHelper(serverConfig.getServerConfigId()).pushUpdate(
RocketChatCache.getPushId(this), token, currentUser != null ? currentUser.getId() : null)
.onSuccess(task -> {
markRefreshAsDone(serverConfig);
return task;
});
} catch (Exception e) {
}
}
private String getToken(String senderId) throws IOException {
return InstanceID.getInstance(this)
.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
}
private void markRefreshAsDone(ServerConfig serverConfig) {
serverConfig.setSyncPushToken(false);
RealmStore.getDefault().executeTransaction(realm -> realm.copyToRealm(serverConfig));
}
}
\ No newline at end of file
...@@ -26,6 +26,7 @@ import chat.rocket.android.service.ddp.base.UserDataSubscriber; ...@@ -26,6 +26,7 @@ import chat.rocket.android.service.ddp.base.UserDataSubscriber;
import chat.rocket.android.service.observer.CurrentUserObserver; import chat.rocket.android.service.observer.CurrentUserObserver;
import chat.rocket.android.service.observer.FileUploadingToS3Observer; import chat.rocket.android.service.observer.FileUploadingToS3Observer;
import chat.rocket.android.service.observer.FileUploadingWithUfsObserver; import chat.rocket.android.service.observer.FileUploadingWithUfsObserver;
import chat.rocket.android.service.observer.GcmPushRegistrationObserver;
import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver; import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
import chat.rocket.android.service.observer.LoadMessageProcedureObserver; import chat.rocket.android.service.observer.LoadMessageProcedureObserver;
import chat.rocket.android.service.observer.MethodCallObserver; import chat.rocket.android.service.observer.MethodCallObserver;
...@@ -55,7 +56,8 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -55,7 +56,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
ReactiveNotificationManager.class, ReactiveNotificationManager.class,
FileUploadingToS3Observer.class, FileUploadingToS3Observer.class,
FileUploadingWithUfsObserver.class, FileUploadingWithUfsObserver.class,
PushSettingsObserver.class PushSettingsObserver.class,
GcmPushRegistrationObserver.class
}; };
private final Context appContext; private final Context appContext;
private final String serverConfigId; private final String serverConfigId;
......
...@@ -6,10 +6,8 @@ import io.realm.RealmResults; ...@@ -6,10 +6,8 @@ import io.realm.RealmResults;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.DDPClientWrapper; import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.api.PushHelper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.ddp.User; import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
...@@ -22,7 +20,6 @@ import hugo.weaving.DebugLog; ...@@ -22,7 +20,6 @@ import hugo.weaving.DebugLog;
*/ */
public class CurrentUserObserver extends AbstractModelObserver<User> { public class CurrentUserObserver extends AbstractModelObserver<User> {
private final MethodCallHelper methodCall; private final MethodCallHelper methodCall;
private final PushHelper pushHelper;
private boolean currentUserExists; private boolean currentUserExists;
private ArrayList<Registrable> listeners; private ArrayList<Registrable> listeners;
...@@ -30,7 +27,6 @@ public class CurrentUserObserver extends AbstractModelObserver<User> { ...@@ -30,7 +27,6 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
RealmHelper realmHelper, DDPClientWrapper ddpClient) { RealmHelper realmHelper, DDPClientWrapper ddpClient) {
super(context, hostname, realmHelper, ddpClient); super(context, hostname, realmHelper, ddpClient);
methodCall = new MethodCallHelper(realmHelper, ddpClient); methodCall = new MethodCallHelper(realmHelper, ddpClient);
pushHelper = new PushHelper(realmHelper, ddpClient);
currentUserExists = false; currentUserExists = false;
} }
...@@ -62,9 +58,6 @@ public class CurrentUserObserver extends AbstractModelObserver<User> { ...@@ -62,9 +58,6 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
final String userId = user.getId(); final String userId = user.getId();
// update push info
pushHelper.pushSetUser(RocketChatCache.getPushId(context)).continueWith(new LogcatIfError());
// get and observe Room subscriptions. // get and observe Room subscriptions.
methodCall.getRoomSubscriptions().onSuccess(task -> { methodCall.getRoomSubscriptions().onSuccess(task -> {
if (listeners != null) { if (listeners != null) {
......
package chat.rocket.android.service.observer;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import android.content.Context;
import io.realm.Realm;
import io.realm.RealmResults;
import java.io.IOException;
import java.util.List;
import bolts.Task;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.api.RaixPushHelper;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.model.internal.GcmPushRegistration;
import chat.rocket.android.realm_helper.RealmHelper;
/**
* call raix:push-update if needed.
*/
public class GcmPushRegistrationObserver extends AbstractModelObserver<GcmPushRegistration> {
public GcmPushRegistrationObserver(Context context, String hostname,
RealmHelper realmHelper,
DDPClientWrapper ddpClient) {
super(context, hostname, realmHelper, ddpClient);
}
@Override
public RealmResults<GcmPushRegistration> queryItems(Realm realm) {
return GcmPushRegistration.queryDefault(realm)
.equalTo(GcmPushRegistration.SYNC_STATE, SyncState.NOT_SYNCED)
.equalTo(GcmPushRegistration.GCM_PUSH_ENABLED, true)
.findAll();
}
@Override
public void onUpdateResults(List<GcmPushRegistration> results) {
if (results.isEmpty()) {
return;
}
realmHelper.executeTransaction(realm -> {
GcmPushRegistration.queryDefault(realm).findFirst().setSyncState(SyncState.SYNCING);
return null;
}).onSuccessTask(_task -> registerGcmTokenForServer()
).onSuccessTask(_task ->
realmHelper.executeTransaction(realm -> {
GcmPushRegistration.queryDefault(realm).findFirst().setSyncState(SyncState.SYNCED);
return null;
})
).continueWith(task -> {
if (task.isFaulted()) {
realmHelper.executeTransaction(realm -> {
GcmPushRegistration.queryDefault(realm).findFirst().setSyncState(SyncState.FAILED);
return null;
}).continueWith(new LogcatIfError());
}
return null;
});
}
private Task<Void> registerGcmTokenForServer() throws IOException {
final String senderId = PublicSetting
.getString(realmHelper, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER, "").trim();
final String gcmToken = getGcmToken(senderId);
final User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst());
final String userId = currentUser != null ? currentUser.getId() : null;
final String pushId = RocketChatCache.getOrCreatePushId(context);
return new RaixPushHelper(realmHelper, ddpClient)
.pushUpdate(pushId, gcmToken, userId);
}
private String getGcmToken(String senderId) throws IOException {
return InstanceID.getInstance(context)
.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
}
}
package chat.rocket.android.service.observer; package chat.rocket.android.service.observer;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmResults; import io.realm.RealmResults;
import java.util.List; import java.util.List;
import chat.rocket.android.api.DDPClientWrapper; import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.GcmPushSettingHelper;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.PublicSetting; import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants; import chat.rocket.android.model.internal.GcmPushRegistration;
import chat.rocket.android.push.gcm.GcmRegistrationIntentService;
import chat.rocket.android.push.interactors.DefaultPushInteractor;
import chat.rocket.android.push.interactors.PushInteractor;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
...@@ -24,61 +18,18 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { ...@@ -24,61 +18,18 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
super(context, hostname, realmHelper, ddpClient); super(context, hostname, realmHelper, ddpClient);
} }
@Override
public void onUpdateResults(List<PublicSetting> results) {
RealmHelper realmHelper = RealmStore.getDefault();
PushInteractor interactor = new DefaultPushInteractor();
String serverConfigId = interactor.getServerConfigId(hostname);
final ServerConfig serverConfig = realmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId)
.findFirst());
serverConfig.setSyncPushToken(isPushEnabled(results));
realmHelper
.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig))
.continueWith(task -> {
if (serverConfig.shouldSyncPushToken()) {
Intent intent = new Intent(
context.getApplicationContext(), GcmRegistrationIntentService.class);
context.getApplicationContext().startService(intent);
}
return task;
})
.continueWith(new LogcatIfError());
}
@Override @Override
public RealmResults<PublicSetting> queryItems(Realm realm) { public RealmResults<PublicSetting> queryItems(Realm realm) {
return realm.where(PublicSetting.class) return GcmPushSettingHelper.queryForGcmPushEnabled(realm);
.equalTo(PublicSetting.ID, PublicSettingsConstants.Push.ENABLE)
.or()
.equalTo(PublicSetting.ID, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER)
.findAll();
}
private boolean isPushEnabled(List<PublicSetting> results) {
return isPushEnable(results) && isGcmValid(results);
} }
private boolean isPushEnable(List<PublicSetting> results) { @Override
for (PublicSetting setting : results) { public void onUpdateResults(List<PublicSetting> results) {
if (PublicSettingsConstants.Push.ENABLE.equals(setting.getId())) { boolean gcmPushEnabled = GcmPushSettingHelper.isGcmPushEnabled(results);
return "true".equals(setting.getValue());
}
}
return false;
}
private boolean isGcmValid(List<PublicSetting> results) { if (gcmPushEnabled) {
for (PublicSetting setting : results) { realmHelper.executeTransaction(realm ->
if (PublicSettingsConstants.Push.GCM_PROJECT_NUMBER.equals(setting.getId())) { GcmPushRegistration.updateGcmPushEnabled(realm, gcmPushEnabled));
return setting.getValue() != null && !"".equals(setting.getValue());
}
} }
return false;
} }
} }
...@@ -5,7 +5,9 @@ import io.realm.Realm; ...@@ -5,7 +5,9 @@ import io.realm.Realm;
import io.realm.RealmResults; import io.realm.RealmResults;
import java.util.List; import java.util.List;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.DDPClientWrapper; import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.api.RaixPushHelper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.internal.GetUsersOfRoomsProcedure; import chat.rocket.android.model.internal.GetUsersOfRoomsProcedure;
import chat.rocket.android.model.internal.LoadMessageProcedure; import chat.rocket.android.model.internal.LoadMessageProcedure;
...@@ -20,6 +22,7 @@ import hugo.weaving.DebugLog; ...@@ -20,6 +22,7 @@ import hugo.weaving.DebugLog;
*/ */
public class SessionObserver extends AbstractModelObserver<Session> { public class SessionObserver extends AbstractModelObserver<Session> {
private final StreamRoomMessageManager streamNotifyMessage; private final StreamRoomMessageManager streamNotifyMessage;
private final RaixPushHelper pushHelper;
private int count; private int count;
/** /**
...@@ -31,6 +34,7 @@ public class SessionObserver extends AbstractModelObserver<Session> { ...@@ -31,6 +34,7 @@ public class SessionObserver extends AbstractModelObserver<Session> {
count = 0; count = 0;
streamNotifyMessage = new StreamRoomMessageManager(context, hostname, realmHelper, ddpClient); streamNotifyMessage = new StreamRoomMessageManager(context, hostname, realmHelper, ddpClient);
pushHelper = new RaixPushHelper(realmHelper, ddpClient);
} }
@Override @Override
...@@ -65,6 +69,11 @@ public class SessionObserver extends AbstractModelObserver<Session> { ...@@ -65,6 +69,11 @@ public class SessionObserver extends AbstractModelObserver<Session> {
@DebugLog @DebugLog
private void onLogin() { private void onLogin() {
streamNotifyMessage.register(); streamNotifyMessage.register();
// update push info
pushHelper
.pushSetUser(RocketChatCache.getOrCreatePushId(context))
.continueWith(new LogcatIfError());
} }
@DebugLog @DebugLog
......
...@@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView; ...@@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmConfiguration; import io.realm.RealmConfiguration;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults; import io.realm.RealmResults;
import org.json.JSONException; import org.json.JSONException;
...@@ -73,6 +74,19 @@ public class RealmHelper { ...@@ -73,6 +74,19 @@ public class RealmHelper {
} }
} }
public interface Query<T extends RealmObject> {
RealmQuery<T> query(Realm realm);
}
public <T extends RealmObject> boolean isObjectExists(Query<T> query) {
try (Realm realm = instance()) {
return query.query(realm).count() > 0;
} catch (Exception exception) {
RCLog.w(exception);
return false;
}
}
private boolean shouldUseSync() { private boolean shouldUseSync() {
// ref: realm-java:realm/realm-library/src/main/java/io/realm/AndroidNotifier.java // ref: realm-java:realm/realm-library/src/main/java/io/realm/AndroidNotifier.java
// #isAutoRefreshAvailable() // #isAutoRefreshAvailable()
...@@ -140,7 +154,7 @@ public class RealmHelper { ...@@ -140,7 +154,7 @@ public class RealmHelper {
} }
public <T extends RealmObject> RealmObjectObserver<T> createObjectObserver( public <T extends RealmObject> RealmObjectObserver<T> createObjectObserver(
RealmObjectObserver.Query<T> query) { RealmHelper.Query<T> query) {
return new RealmObjectObserver<T>(this, query); return new RealmObjectObserver<T>(this, query);
} }
......
...@@ -3,17 +3,16 @@ package chat.rocket.android.realm_helper; ...@@ -3,17 +3,16 @@ package chat.rocket.android.realm_helper;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmChangeListener; import io.realm.RealmChangeListener;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults; import io.realm.RealmResults;
public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmResultsObserver<T> { public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmResultsObserver<T> {
private final Query<T> query; private final RealmHelper.Query<T> query;
private OnUpdateListener<T> onUpdateListener; private OnUpdateListener<T> onUpdateListener;
private Impl<T> impl; private Impl<T> impl;
private String previousResultString; private String previousResultString;
/*package*/ RealmObjectObserver(RealmHelper helper, Query<T> query) { /*package*/ RealmObjectObserver(RealmHelper helper, RealmHelper.Query<T> query) {
super(helper); super(helper);
this.query = query; this.query = query;
setImpl(new Impl<T>()); setImpl(new Impl<T>());
...@@ -54,10 +53,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes ...@@ -54,10 +53,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
super.sub(); super.sub();
} }
public interface Query<T extends RealmObject> {
RealmQuery<T> query(Realm realm);
}
public interface OnUpdateListener<T extends RealmObject> { public interface OnUpdateListener<T extends RealmObject> {
void onUpdateObject(T element); void onUpdateObject(T element);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment