-
Notifications
You must be signed in to change notification settings - Fork 1
Included HermesIntentService. Changes to HermesBroadcastReceiver. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sahildave
wants to merge
2
commits into
raveeshbhalla:master
Choose a base branch
from
sahildave:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 38 additions & 23 deletions
61
hermes/src/main/java/in/raveesh/hermes/receivers/HermesBroadcastReceiver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,53 @@ | ||
| package in.raveesh.hermes.receivers; | ||
|
|
||
| import android.content.BroadcastReceiver; | ||
| import android.app.Activity; | ||
| import android.content.ComponentName; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.support.v4.content.WakefulBroadcastReceiver; | ||
| import android.util.Log; | ||
|
|
||
| import in.raveesh.hermes.Hermes; | ||
|
|
||
| public class HermesBroadcastReceiver extends BroadcastReceiver { | ||
| /** | ||
| * This {@code HermesBroadcastReceiver} takes care of creating and managing a | ||
| * partial wake lock for your app. It passes off the work of processing the GCM | ||
| * message to {@code HermesIntentService}. | ||
| * | ||
| * The wakelock is release when {@code HermesIntentService} calls | ||
| * {@code completeWakefulIntent()} | ||
| * | ||
| */ | ||
| public class HermesBroadcastReceiver extends WakefulBroadcastReceiver { | ||
|
|
||
| private Class intentService; | ||
| public HermesBroadcastReceiver() { | ||
| } | ||
|
|
||
| protected boolean gotRegistrationId = false; | ||
| protected boolean error = false; | ||
| protected String registrationID; | ||
| /** | ||
| * This sets the class which should be called when the GCM message is | ||
| * received. | ||
| * This method should be called when creating the BroadcastReceiver, preferably in the | ||
| * constructor. When {@code onReceive} is called, the @param intentService class would | ||
| * be called to process the message | ||
| * @param intentService the intent service class which should be called to process the gcm | ||
| * message. Should extend {@code HermesIntentService} | ||
| */ | ||
| public void setIntentService(Class intentService) { | ||
| this.intentService = intentService; | ||
| Log.d("Hermes", "Setting intentService class to - " + intentService); | ||
| } | ||
|
|
||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| registrationID = intent.getStringExtra("registration_id"); | ||
| if (registrationID != null) { | ||
| gotRegistrationId = true; | ||
| if (Hermes.CALLBACK != null) { | ||
| Hermes.CALLBACK.registrationComplete(registrationID); | ||
| } | ||
| } else if (intent.getExtras() != null && intent.getExtras().get("error") != null) { | ||
| error = true; | ||
| if (Hermes.CALLBACK != null){ | ||
| Hermes.CALLBACK.registrationFailed(intent.getExtras().get("error").toString()); | ||
| } | ||
| ExponentialBackoffReceiver.attemptRegistration(context, Hermes.getDelay(), Hermes.getSenderId()); | ||
| } else { | ||
| Log.d(Hermes.TAG, "GCM Message Received"); | ||
|
|
||
| } | ||
|
|
||
| if(intentService == null ) throw new UnsupportedOperationException( | ||
| "Please use setIntentService in the " + | ||
| "constructor of your broadcastReceiver class."); | ||
|
|
||
| ComponentName comp = new ComponentName( | ||
| context.getPackageName(), | ||
| intentService.getName()); | ||
|
|
||
| startWakefulService(context, (intent.setComponent(comp))); | ||
| setResultCode(Activity.RESULT_OK); | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
hermes/src/main/java/in/raveesh/hermes/receivers/HermesIntentService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package in.raveesh.hermes.receivers; | ||
|
|
||
| import android.app.IntentService; | ||
| import android.content.Intent; | ||
| import android.util.Log; | ||
|
|
||
| import com.google.android.gms.gcm.GoogleCloudMessaging; | ||
|
|
||
| import in.raveesh.hermes.Hermes; | ||
|
|
||
| /** | ||
| * Handles the intent which contains the GCM message. | ||
| * It holds the wake lock started by {@code HermesBroadcastReceiver} while | ||
| * doing the computation and finally releases it by calling | ||
| * {@code completeWakefulIntent()} | ||
| * | ||
| * This class is abstract and methods | ||
| * {@code messageReceived}, {@code messageDeleted}, {@code messageSendError}, | ||
| * {@code messageSendEvent} should be overridden in the subclass. | ||
| * | ||
| */ | ||
| public abstract class HermesIntentService extends IntentService{ | ||
| protected boolean gotRegistrationId = false; | ||
| protected boolean error = false; | ||
| protected String registrationID; | ||
|
|
||
| public HermesIntentService(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onHandleIntent(Intent intent) { | ||
| Log.d("Hermes", "Received intent in HermesIntentService with action - "+intent.getAction()); | ||
|
|
||
| if(intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) { | ||
| registrationID = intent.getStringExtra("registration_id"); | ||
| if (registrationID != null) { | ||
| gotRegistrationId = true; | ||
| if (Hermes.CALLBACK != null) { | ||
| Hermes.CALLBACK.registrationComplete(registrationID); | ||
| } | ||
| } else if (intent.getExtras() != null && intent.getExtras().get("error") != null) { | ||
| error = true; | ||
| if (Hermes.CALLBACK != null){ | ||
| Hermes.CALLBACK.registrationFailed(intent.getExtras().get("error").toString()); | ||
| } | ||
|
|
||
| // Registration message was received but was error. | ||
| ExponentialBackoffReceiver.attemptRegistration(this, Hermes.getDelay(), Hermes.getSenderId()); | ||
| } | ||
| } else if(intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { | ||
| GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); | ||
| String messageType = gcm.getMessageType(intent); | ||
|
|
||
| // Logging and calling the appropriate abstract method. | ||
| if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { | ||
| Log.d("Hermes", "Message Received"); | ||
| messageReceived(); | ||
| } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { | ||
| Log.d("Hermes", "Message Deleted"); | ||
| messageDeleted(); | ||
| } else if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { | ||
| Log.d("Hermes", "Message Send Error"); | ||
| messageSendError(); | ||
| } else if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_EVENT.equals(messageType)) { | ||
| Log.d("Hermes", "Message Send Event"); | ||
| messageSendEvent(); | ||
| } | ||
| } | ||
|
|
||
| // Release the wake lock provided by the WakefulBroadcastReceiver. | ||
| HermesBroadcastReceiver.completeWakefulIntent(intent); | ||
| } | ||
|
|
||
| public abstract void messageReceived(); | ||
| public abstract void messageDeleted(); | ||
| public abstract void messageSendEvent(); | ||
| public abstract void messageSendError(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to dive into this, but could there be concerns if a package has more than one IntentService here? Wasn't how I was imagining myself to write Hermes, but I can see benefits to this approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so there would be a problem with having two intent services, that is why I am using setIntentService, which would be called by the subclass of HermesBroadcastReceiver.
Also, I am doing this because he have to specify the class name which we need to start (and mention it in manifest too.). If I just start HermesIntentService, the user would not receive any update in his subclass intentService (here GcmIntentService), because it wont be registered. So for registering the class properly setIntentService API can be used. This would ensure correct IntentService is called (albeit it has to be registered in manifest too)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I haven't read through code in detail, could you please add comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey I have added some JavaDocs and removed Utils class. Check it out.