2013-03-26 04:26:03 +00:00
|
|
|
package org.thoughtcrime.securesms.gcm;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2013-04-01 02:16:06 +00:00
|
|
|
import android.util.Log;
|
2013-03-26 04:26:03 +00:00
|
|
|
|
|
|
|
import com.google.android.gcm.GCMBaseIntentService;
|
2013-04-01 02:16:06 +00:00
|
|
|
import com.google.thoughtcrimegson.Gson;
|
2013-03-26 04:26:03 +00:00
|
|
|
import org.thoughtcrime.securesms.service.RegistrationService;
|
2013-04-01 02:16:06 +00:00
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
2013-07-10 01:26:18 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-07-19 00:42:45 +00:00
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
2013-07-10 23:22:58 +00:00
|
|
|
import org.whispersystems.textsecure.push.PushServiceSocket;
|
2013-07-10 02:48:33 +00:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
2013-04-01 02:16:06 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2013-03-26 04:26:03 +00:00
|
|
|
|
|
|
|
public class GcmIntentService extends GCMBaseIntentService {
|
|
|
|
|
|
|
|
public static final String GCM_SENDER_ID = "312334754206";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRegistered(Context context, String registrationId) {
|
2013-07-11 21:58:40 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
2013-03-26 04:26:03 +00:00
|
|
|
Intent intent = new Intent(RegistrationService.GCM_REGISTRATION_EVENT);
|
|
|
|
intent.putExtra(RegistrationService.GCM_REGISTRATION_ID, registrationId);
|
|
|
|
sendBroadcast(intent);
|
|
|
|
} else {
|
2013-04-01 02:16:06 +00:00
|
|
|
try {
|
|
|
|
getGcmSocket(context).registerGcmId(registrationId);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("GcmIntentService", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onUnregistered(Context context, String registrationId) {
|
|
|
|
try {
|
2013-07-11 21:58:40 +00:00
|
|
|
getGcmSocket(context).unregisterGcmId();
|
2013-04-01 02:16:06 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("GcmIntentService", ioe);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 02:16:06 +00:00
|
|
|
|
2013-03-26 04:26:03 +00:00
|
|
|
@Override
|
|
|
|
protected void onMessage(Context context, Intent intent) {
|
2013-04-01 02:16:06 +00:00
|
|
|
String data = intent.getStringExtra("message");
|
|
|
|
Log.w("GcmIntentService", "GCM message: " + data);
|
|
|
|
|
|
|
|
if (Util.isEmpty(data))
|
|
|
|
return;
|
|
|
|
|
2013-07-19 00:42:45 +00:00
|
|
|
IncomingPushMessage message = new Gson().fromJson(data, IncomingPushMessage.class);
|
|
|
|
|
|
|
|
if (!message.hasAttachments()) handleIncomingTextMessage(context, message);
|
|
|
|
else handleIncomingMediaMessage(context, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onError(Context context, String s) {
|
|
|
|
Log.w("GcmIntentService", "GCM Error: " + s);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIncomingTextMessage(Context context, IncomingPushMessage message) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
ArrayList<IncomingTextMessage> messages = new ArrayList<IncomingTextMessage>();
|
|
|
|
messages.add(new IncomingTextMessage(message));
|
2013-04-01 02:16:06 +00:00
|
|
|
|
|
|
|
Intent receivedIntent = new Intent(context, SendReceiveService.class);
|
|
|
|
receivedIntent.setAction(SendReceiveService.RECEIVE_SMS_ACTION);
|
|
|
|
receivedIntent.putParcelableArrayListExtra("text_messages", messages);
|
2013-08-22 00:25:19 +00:00
|
|
|
receivedIntent.putExtra("push_type", message.getType());
|
2013-04-01 02:16:06 +00:00
|
|
|
context.startService(receivedIntent);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 00:42:45 +00:00
|
|
|
private void handleIncomingMediaMessage(Context context, IncomingPushMessage message) {
|
|
|
|
Intent receivedIntent = new Intent(context, SendReceiveService.class);
|
|
|
|
receivedIntent.setAction(SendReceiveService.RECEIVE_PUSH_MMS_ACTION);
|
|
|
|
receivedIntent.putExtra("media_message", message);
|
|
|
|
context.startService(receivedIntent);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
|
2013-07-08 23:29:28 +00:00
|
|
|
private PushServiceSocket getGcmSocket(Context context) {
|
2013-07-10 01:26:18 +00:00
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
String password = TextSecurePreferences.getPushServerPassword(context);
|
2013-07-08 23:29:28 +00:00
|
|
|
return new PushServiceSocket(context, localNumber, password);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
}
|