Commit b3cd934f authored by Tiago Cunha's avatar Tiago Cunha

PR feedback related stuff

parent 86b87261
...@@ -59,14 +59,14 @@ ...@@ -59,14 +59,14 @@
</intent-filter> </intent-filter>
</service> </service>
<service <service
android:name=".push.gcm.PushInstanceIDListenerService" android:name=".push.gcm.GcmInstanceIDListenerService"
android:exported="false"> android:exported="false">
<intent-filter> <intent-filter>
<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 <service
android:name=".push.gcm.RegistrationIntentService" android:name=".push.gcm.GcmRegistrationIntentService"
android:exported="false" /> android:exported="false" />
</application> </application>
......
...@@ -18,7 +18,7 @@ import chat.rocket.android.model.ServerConfig; ...@@ -18,7 +18,7 @@ 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.ddp.PublicSettingsConstants;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.push.gcm.RegistrationIntentService; import chat.rocket.android.push.gcm.GcmRegistrationIntentService;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
...@@ -168,7 +168,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -168,7 +168,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig)) .executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig))
.continueWith(task -> { .continueWith(task -> {
if (serverConfig.shouldSyncPushToken()) { if (serverConfig.shouldSyncPushToken()) {
Intent intent = new Intent(this, RegistrationIntentService.class); Intent intent = new Intent(this, GcmRegistrationIntentService.class);
startService(intent); startService(intent);
} }
......
package chat.rocket.android.api; package chat.rocket.android.api;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Patterns; import android.util.Patterns;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -19,7 +17,6 @@ import chat.rocket.android.model.ddp.PublicSetting; ...@@ -19,7 +17,6 @@ import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.RoomSubscription; import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.internal.MethodCall; import chat.rocket.android.model.internal.MethodCall;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.model.params.PushUpdate;
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;
import chat.rocket.android_ddp.DDPClientCallback; import chat.rocket.android_ddp.DDPClientCallback;
...@@ -307,19 +304,6 @@ public class MethodCallHelper { ...@@ -307,19 +304,6 @@ public class MethodCallHelper {
.onSuccessTask(task -> Task.forResult(null)); .onSuccessTask(task -> Task.forResult(null));
} }
public Task<Void> pushUpdate(@NonNull String pushId, @NonNull String token,
@Nullable String userId) {
return call("raix:push-update", TIMEOUT_MS, () -> {
JSONObject param = new PushUpdate(pushId, token, userId).toJson();
return new JSONArray().put(param);
}).onSuccessTask(task -> Task.forResult(null));
}
public Task<Void> pushSetUser(String pushId) {
return call("raix:push-setuser", TIMEOUT_MS, () -> new JSONArray().put(pushId))
.onSuccessTask(task -> Task.forResult(null));
}
/** /**
* send message. * send message.
*/ */
......
package chat.rocket.android.api;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import bolts.Task;
import chat.rocket.android.realm_helper.RealmHelper;
public class PushHelper extends MethodCallHelper {
public PushHelper(String serverConfigId) {
super(serverConfigId);
}
public PushHelper(Context context, String serverConfigId) {
super(context, serverConfigId);
}
public PushHelper(RealmHelper realmHelper,
DDPClientWrapper ddpClient) {
super(realmHelper, ddpClient);
}
public Task<Void> pushUpdate(@NonNull String pushId, @NonNull String token,
@Nullable String userId) {
return call("raix:push-update", TIMEOUT_MS, () -> {
JSONObject param = new PushUpdate(pushId, token, userId).toJson();
return new JSONArray().put(param);
}).onSuccessTask(task -> Task.forResult(null));
}
public Task<Void> pushSetUser(String pushId) {
return call("raix:push-setuser", TIMEOUT_MS, () -> new JSONArray().put(pushId))
.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.model.params;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
public class PushUpdate {
private String pushId;
private String gcmToken;
private String userId;
public PushUpdate(@NonNull String pushId, @NonNull String gcmToken, @Nullable String userId) {
this.pushId = pushId;
this.gcmToken = gcmToken;
this.userId = userId;
}
public 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;
}
}
...@@ -7,8 +7,6 @@ public interface PushConstants { ...@@ -7,8 +7,6 @@ public interface PushConstants {
String TITLE = "title"; String TITLE = "title";
String NOT_ID = "notId"; String NOT_ID = "notId";
String PUSH_BUNDLE = "pushBundle"; String PUSH_BUNDLE = "pushBundle";
String SERVER_CONFIG_ID = "serverConfigId";
String ROOM_ID = "roomId";
String ICON = "icon"; String ICON = "icon";
String ICON_COLOR = "iconColor"; String ICON_COLOR = "iconColor";
String SOUND = "sound"; String SOUND = "sound";
...@@ -70,4 +68,8 @@ public interface PushConstants { ...@@ -70,4 +68,8 @@ public interface PushConstants {
String TWILIO_SOUND = "twi_sound"; String TWILIO_SOUND = "twi_sound";
String START_IN_BACKGROUND = "cdvStartInBackground"; String START_IN_BACKGROUND = "cdvStartInBackground";
String FORCE_START = "force-start"; String FORCE_START = "force-start";
// RC specific constants
String SERVER_CONFIG_ID = "serverConfigId";
String ROOM_ID = "roomId";
} }
\ No newline at end of file
...@@ -11,19 +11,19 @@ import chat.rocket.android.model.ddp.PublicSettingsConstants; ...@@ -11,19 +11,19 @@ import chat.rocket.android.model.ddp.PublicSettingsConstants;
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;
public class PushInstanceIDListenerService extends InstanceIDListenerService { public class GcmInstanceIDListenerService extends InstanceIDListenerService {
@Override @Override
public void onTokenRefresh() { public void onTokenRefresh() {
List<ServerConfig> serverConfigs = getServerConfigs(); List<ServerConfig> serverConfigs = getServerConfigs();
flagForRefresh(serverConfigs); updateSyncPushToken(serverConfigs);
if (!shouldRefreshToken(serverConfigs)) { if (!shouldRefreshToken(serverConfigs)) {
return; return;
} }
Intent intent = new Intent(this, RegistrationIntentService.class); Intent intent = new Intent(this, GcmRegistrationIntentService.class);
startService(intent); startService(intent);
} }
...@@ -32,7 +32,7 @@ public class PushInstanceIDListenerService extends InstanceIDListenerService { ...@@ -32,7 +32,7 @@ public class PushInstanceIDListenerService extends InstanceIDListenerService {
realm -> realm.where(ServerConfig.class).findAll()); realm -> realm.where(ServerConfig.class).findAll());
} }
private void flagForRefresh(List<ServerConfig> serverConfigs) { private void updateSyncPushToken(List<ServerConfig> serverConfigs) {
final RealmHelper realmHelper = RealmStore.getDefault(); final RealmHelper realmHelper = RealmStore.getDefault();
for (final ServerConfig serverConfig : serverConfigs) { for (final ServerConfig serverConfig : serverConfigs) {
......
...@@ -9,7 +9,7 @@ import com.google.android.gms.iid.InstanceID; ...@@ -9,7 +9,7 @@ import com.google.android.gms.iid.InstanceID;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.PushHelper;
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.ddp.PublicSettingsConstants;
...@@ -17,10 +17,10 @@ import chat.rocket.android.model.ddp.User; ...@@ -17,10 +17,10 @@ import chat.rocket.android.model.ddp.User;
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;
public class RegistrationIntentService extends IntentService { public class GcmRegistrationIntentService extends IntentService {
public RegistrationIntentService() { public GcmRegistrationIntentService() {
super("RegistrationIntentService"); super("GcmRegistrationIntentService");
} }
@Override @Override
...@@ -59,7 +59,7 @@ public class RegistrationIntentService extends IntentService { ...@@ -59,7 +59,7 @@ public class RegistrationIntentService extends IntentService {
final User currentUser = realmHelper.executeTransactionForRead(realm -> final User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst()); User.queryCurrentUser(realm).findFirst());
new MethodCallHelper(serverConfig.getServerConfigId()).pushUpdate( new PushHelper(serverConfig.getServerConfigId()).pushUpdate(
RocketChatCache.getPushId(this), token, currentUser != null ? currentUser.getId() : null) RocketChatCache.getPushId(this), token, currentUser != null ? currentUser.getId() : null)
.onSuccess(task -> { .onSuccess(task -> {
markRefreshAsDone(serverConfig); markRefreshAsDone(serverConfig);
......
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
import chat.rocket.android.RocketChatCache; 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;
...@@ -21,6 +22,7 @@ import hugo.weaving.DebugLog; ...@@ -21,6 +22,7 @@ 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;
...@@ -28,6 +30,7 @@ public class CurrentUserObserver extends AbstractModelObserver<User> { ...@@ -28,6 +30,7 @@ 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;
} }
...@@ -60,7 +63,7 @@ public class CurrentUserObserver extends AbstractModelObserver<User> { ...@@ -60,7 +63,7 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
final String userId = user.getId(); final String userId = user.getId();
// update push info // update push info
methodCall.pushSetUser(RocketChatCache.getPushId(context)).continueWith(new LogcatIfError()); 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 -> {
......
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