session-android/src/org/thoughtcrime/securesms/gcm/GcmIntentService.java
Moxie Marlinspike 83e260436b 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-05-06 08:40:55 -07:00

80 lines
2.9 KiB
Java

package org.thoughtcrime.securesms.gcm;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.thoughtcrimegson.Gson;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.service.RegistrationService;
import org.thoughtcrime.securesms.service.SendReceiveService;
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
import org.thoughtcrime.securesms.util.Util;
import java.io.IOException;
import java.util.ArrayList;
public class GcmIntentService extends GCMBaseIntentService {
public static final String GCM_SENDER_ID = "312334754206";
@Override
protected void onRegistered(Context context, String registrationId) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (!preferences.getBoolean(ApplicationPreferencesActivity.REGISTERED_GCM_PREF, false)) {
Intent intent = new Intent(RegistrationService.GCM_REGISTRATION_EVENT);
intent.putExtra(RegistrationService.GCM_REGISTRATION_ID, registrationId);
sendBroadcast(intent);
} else {
try {
getGcmSocket(context).registerGcmId(registrationId);
} catch (IOException e) {
Log.w("GcmIntentService", e);
}
}
}
@Override
protected void onUnregistered(Context context, String registrationId) {
try {
getGcmSocket(context).unregisterGcmId(registrationId);
} catch (IOException ioe) {
Log.w("GcmIntentService", ioe);
}
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.w("GcmIntentService", "Got GCM message!");
String data = intent.getStringExtra("message");
Log.w("GcmIntentService", "GCM message: " + data);
if (Util.isEmpty(data))
return;
IncomingGcmMessage message = new Gson().fromJson(data, IncomingGcmMessage.class);
ArrayList<IncomingTextMessage> messages = new ArrayList<IncomingTextMessage>();
messages.add(new IncomingTextMessage(message));
Intent receivedIntent = new Intent(context, SendReceiveService.class);
receivedIntent.setAction(SendReceiveService.RECEIVE_SMS_ACTION);
receivedIntent.putParcelableArrayListExtra("text_messages", messages);
context.startService(receivedIntent);
}
@Override
protected void onError(Context context, String s) {
Log.w("GcmIntentService", "GCM Error: " + s);
}
private GcmSocket getGcmSocket(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String localNumber = preferences.getString(ApplicationPreferencesActivity.LOCAL_NUMBER_PREF, null);
String password = preferences.getString(ApplicationPreferencesActivity.GCM_PASSWORD_PREF, null);
return new GcmSocket(context, localNumber, password);
}
}