Commit 4d709b6e authored by Yusuke Iwaki's avatar Yusuke Iwaki

remove push interactor.

parent b6b6ef68
...@@ -20,7 +20,6 @@ import android.support.v4.util.SparseArrayCompat; ...@@ -20,7 +20,6 @@ import android.support.v4.util.SparseArrayCompat;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
import android.util.Log; import android.util.Log;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -32,7 +31,9 @@ import java.net.URL; ...@@ -32,7 +31,9 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import chat.rocket.android.activity.MainActivity; import chat.rocket.android.activity.MainActivity;
import chat.rocket.android.push.interactors.PushInteractor; import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.realm_helper.RealmStore;
public class PushNotificationHandler implements PushConstants { public class PushNotificationHandler implements PushConstants {
...@@ -56,8 +57,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -56,8 +57,7 @@ public class PushNotificationHandler implements PushConstants {
} }
} }
public void showNotificationIfPossible(Context context, PushInteractor pushInteractor, public void showNotificationIfPossible(Context context, Bundle extras) {
Bundle extras) {
// Send a notification if there is a message or title, otherwise just send data // Send a notification if there is a message or title, otherwise just send data
String message = extras.getString(MESSAGE); String message = extras.getString(MESSAGE);
...@@ -79,11 +79,11 @@ public class PushNotificationHandler implements PushConstants { ...@@ -79,11 +79,11 @@ public class PushNotificationHandler implements PushConstants {
extras.putString(TITLE, getAppName(context)); extras.putString(TITLE, getAppName(context));
} }
createNotification(context, pushInteractor, extras); createNotification(context, extras);
} }
} }
public void createNotification(Context context, PushInteractor pushInteractor, Bundle extras) { public void createNotification(Context context, Bundle extras) {
NotificationManager mNotificationManager = NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(context); String appName = getAppName(context);
...@@ -97,8 +97,11 @@ public class PushNotificationHandler implements PushConstants { ...@@ -97,8 +97,11 @@ public class PushNotificationHandler implements PushConstants {
return; return;
} }
String serverConfigId = pushInteractor.getServerConfigId(serverUrl); ServerConfig serverConfig = RealmStore.getDefault().executeTransactionForRead(realm ->
if (serverConfigId == null) { realm.where(ServerConfig.class)
.equalTo(ServerConfig.HOSTNAME, ServerPolicyHelper.enforceHostname(serverUrl))
.findFirst());
if (serverConfig == null) {
return; return;
} }
...@@ -106,7 +109,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -106,7 +109,7 @@ public class PushNotificationHandler implements PushConstants {
Intent notificationIntent = new Intent(context, MainActivity.class); Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra(PUSH_BUNDLE, extras); notificationIntent.putExtra(PUSH_BUNDLE, extras);
notificationIntent.putExtra(SERVER_CONFIG_ID, serverConfigId); notificationIntent.putExtra(SERVER_CONFIG_ID, serverConfig.getServerConfigId());
notificationIntent.putExtra(ROOM_ID, roomId); notificationIntent.putExtra(ROOM_ID, roomId);
notificationIntent.putExtra(NOT_ID, notId); notificationIntent.putExtra(NOT_ID, notId);
......
package chat.rocket.android.push.gcm; package chat.rocket.android.push.gcm;
import android.annotation.SuppressLint; import com.google.android.gms.gcm.GcmListenerService;
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;
...@@ -16,10 +14,7 @@ import java.util.ArrayList; ...@@ -16,10 +14,7 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import chat.rocket.android.push.PushConstants; import chat.rocket.android.push.PushConstants;
import chat.rocket.android.push.PushNotificationHandler; import chat.rocket.android.push.PushNotificationHandler;
import chat.rocket.android.push.interactors.DefaultPushInteractor;
import chat.rocket.android.push.interactors.PushInteractor;
@SuppressLint("NewApi")
public class GCMIntentService extends GcmListenerService implements PushConstants { public class GCMIntentService extends GcmListenerService implements PushConstants {
private static final String LOG_TAG = "GCMIntentService"; private static final String LOG_TAG = "GCMIntentService";
...@@ -34,13 +29,9 @@ public class GCMIntentService extends GcmListenerService implements PushConstant ...@@ -34,13 +29,9 @@ public class GCMIntentService extends GcmListenerService implements PushConstant
Context applicationContext = getApplicationContext(); Context applicationContext = getApplicationContext();
PushInteractor pushInteractor = new DefaultPushInteractor();
extras = normalizeExtras(applicationContext, extras); extras = normalizeExtras(applicationContext, extras);
PushNotificationHandler pushNotificationHandler = new PushNotificationHandler(); new PushNotificationHandler().showNotificationIfPossible(applicationContext, extras);
pushNotificationHandler.showNotificationIfPossible(applicationContext, pushInteractor, extras);
} }
/* /*
......
...@@ -25,22 +25,15 @@ public class GcmRegistrationIntentService extends IntentService { ...@@ -25,22 +25,15 @@ public class GcmRegistrationIntentService extends IntentService {
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
final List<ServerConfig> serverConfigs = getServerConfigs(); final List<ServerConfig> serverConfigs = RealmStore.getDefault()
.executeTransactionForReadResults(realm ->
realm.where(ServerConfig.class).equalTo(ServerConfig.SYNC_PUSH_TOKEN, true).findAll());
for (ServerConfig serverConfig : serverConfigs) { for (ServerConfig serverConfig : serverConfigs) {
sendTokenTo(serverConfig); registerGcmTokenForServer(serverConfig);
} }
} }
private List<ServerConfig> getServerConfigs() { private void registerGcmTokenForServer(final ServerConfig serverConfig) {
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()); final RealmHelper realmHelper = RealmStore.get(serverConfig.getServerConfigId());
if (realmHelper == null) { if (realmHelper == null) {
return; return;
...@@ -54,13 +47,13 @@ public class GcmRegistrationIntentService extends IntentService { ...@@ -54,13 +47,13 @@ public class GcmRegistrationIntentService extends IntentService {
} }
try { try {
final String token = getToken(senderId); final String gcmToken = getGcmToken(senderId);
final User currentUser = realmHelper.executeTransactionForRead(realm -> final User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst()); User.queryCurrentUser(realm).findFirst());
new PushHelper(getBaseContext(), serverConfig.getServerConfigId()).pushUpdate( new PushHelper(getBaseContext(), serverConfig.getServerConfigId()).pushUpdate(
RocketChatCache.getPushId(this), token, currentUser != null ? currentUser.getId() : null) RocketChatCache.getPushId(this), gcmToken, currentUser != null ? currentUser.getId() : null)
.onSuccess(task -> { .onSuccess(task -> {
markRefreshAsDone(serverConfig); markRefreshAsDone(serverConfig);
return task; return task;
...@@ -69,7 +62,7 @@ public class GcmRegistrationIntentService extends IntentService { ...@@ -69,7 +62,7 @@ public class GcmRegistrationIntentService extends IntentService {
} }
} }
private String getToken(String senderId) throws IOException { private String getGcmToken(String senderId) throws IOException {
return InstanceID.getInstance(this) return InstanceID.getInstance(this)
.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); .getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} }
......
package chat.rocket.android.push.interactors;
import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.realm_helper.RealmStore;
public class DefaultPushInteractor implements PushInteractor {
@Override
public String getServerConfigId(String hostname) {
final ServerConfig serverConfig = RealmStore.getDefault()
.executeTransactionForRead(
realm -> realm.where(ServerConfig.class)
.equalTo(ServerConfig.HOSTNAME, ServerPolicyHelper.enforceHostname(hostname))
.findFirst());
return serverConfig != null ? serverConfig.getServerConfigId() : "";
}
}
package chat.rocket.android.push.interactors;
public interface PushInteractor {
String getServerConfigId(String hostname);
}
...@@ -8,12 +8,11 @@ import io.realm.RealmResults; ...@@ -8,12 +8,11 @@ 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.LogcatIfError;
import chat.rocket.android.helper.ServerPolicyHelper;
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;
import chat.rocket.android.push.gcm.GcmRegistrationIntentService; 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; import chat.rocket.android.realm_helper.RealmStore;
...@@ -28,11 +27,9 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> { ...@@ -28,11 +27,9 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
public void onUpdateResults(List<PublicSetting> results) { public void onUpdateResults(List<PublicSetting> results) {
RealmHelper realmHelper = RealmStore.getDefault(); RealmHelper realmHelper = RealmStore.getDefault();
PushInteractor interactor = new DefaultPushInteractor(); final ServerConfig serverConfig = realmHelper.executeTransactionForRead(realm ->
String serverConfigId = interactor.getServerConfigId(hostname); realm.where(ServerConfig.class)
.equalTo(ServerConfig.HOSTNAME, ServerPolicyHelper.enforceHostname(hostname))
final ServerConfig serverConfig = realmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId)
.findFirst()); .findFirst());
serverConfig.setSyncPushToken(isPushEnabled(results)); serverConfig.setSyncPushToken(isPushEnabled(results));
......
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