Skip to content

Commit

Permalink
Merge pull request #23 from KtorZ/fix.activityStates
Browse files Browse the repository at this point in the history
Fix Activity States
  • Loading branch information
timanrebel committed Jul 23, 2015
2 parents 9ff5f59 + 43d8b53 commit 558cd36
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 87 deletions.
3 changes: 3 additions & 0 deletions android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Get the object id of the installation
* None

## Changelog
**[v0.8](https://github.com/timanrebel/Parse/releases/tag/0.8)**
- Resume the app on notification click if it was in background.

**[v0.7](https://github.com/timanrebel/Parse/releases/tag/0.7)**
- Add support for Appcelerator SDK 4.0.0
- Add support for retreiving objectId and installation id
Expand Down
Binary file not shown.
Binary file modified android/libs/armeabi-v7a/libeu.rebelcorp.parse.so
Binary file not shown.
Binary file modified android/libs/armeabi/libeu.rebelcorp.parse.so
Binary file not shown.
Binary file modified android/libs/x86/libeu.rebelcorp.parse.so
Binary file not shown.
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 0.7
version: 0.8
apiversion: 2
description: Titanium module wrapping the Parse Android SDK.
author: Timan Rebel
Expand Down
126 changes: 86 additions & 40 deletions android/src/eu/rebelcorp/parse/ParseModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.appcelerator.kroll.common.Log;

import android.content.Context;
import android.app.Activity;

import com.parse.Parse;
import com.parse.ParsePush;
Expand All @@ -30,58 +31,108 @@
public class ParseModule extends KrollModule
{

// Module instance
private static ParseModule module;
// Standard Debugging variables
private static final String TAG = "ParseModule";
// Module instance
private static ParseModule module;

// Standard Debugging variables
private static final String TAG = "ParseModule";

// tiapp.xml properties containing Parse's app id and client key
public static String PROPERTY_APP_ID = "Parse_AppId";
public static String PROPERTY_CLIENT_KEY = "Parse_ClientKey";

// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;
/* Control the state of the activity */
private boolean isModuleRunning;

// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;

public ParseModule()
{
super();

module = this;
}
public ParseModule()
{
super();
module = this;
setIsModuleRunning(true);
}

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
String appId = TiApplication.getInstance().getAppProperties().getString(ParseModule.PROPERTY_APP_ID, "");
String clientKey = TiApplication.getInstance().getAppProperties().getString(ParseModule.PROPERTY_CLIENT_KEY, "");

Log.d(TAG, "Initializing with: " + appId + ", " + clientKey + ";");

Parse.initialize(TiApplication.getInstance(), appId, clientKey);
}

// Methods
@Kroll.method
public void start()
{
/* Get control over the module's state */
public void onStart(Activity activity)
{
super.onStart(activity);
setIsModuleRunning(true);
}

public void onResume(Activity activity)
{
super.onResume(activity);
setIsModuleRunning(true);
}

public void onPause(Activity activity)
{
super.onPause(activity);
setIsModuleRunning(false);
}

public void onStop(Activity activity)
{
super.onStop(activity);
setIsModuleRunning(false);
}

public void onDestroy(Activity activity)
{
super.onDestroy(activity);
setIsModuleRunning(false);
}

private void setIsModuleRunning(boolean isModuleRunning)
{
this.isModuleRunning = isModuleRunning;
}

/* An accessor from the outside */
public boolean isModuleRunning()
{
return isModuleRunning;
}

/* Get an instance of that module*/
public static ParseModule getInstance() {
return module;
}

// Methods
@Kroll.method
public void start()
{
// Track Push opens
ParseAnalytics.trackAppOpened(TiApplication.getAppRootOrCurrentActivity().getIntent());

ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e != null) {
Log.e(TAG, "Installation initialization failed: " + e.getMessage());
}
}
});
}
}

@Kroll.method
public void enablePush() {
// Deprecated. Now happens automatically
// Deprecated. Now happens automatically
}

@Kroll.method
public void authenticate(@Kroll.argument String sessionToken) {
ParseUser.becomeInBackground(sessionToken, new LogInCallback() {
Expand All @@ -95,16 +146,16 @@ public void done(ParseUser user, ParseException e) {
});
}

@Kroll.method
public void subscribeChannel(@Kroll.argument String channel) {
ParsePush.subscribeInBackground(channel);
}
@Kroll.method
public void subscribeChannel(@Kroll.argument String channel) {
ParsePush.subscribeInBackground(channel);
}

@Kroll.method
public void unsubscribeChannel(@Kroll.argument String channel) {
ParsePush.unsubscribeInBackground(channel);
}

@Kroll.method
public void unsubscribeChannel(@Kroll.argument String channel) {
ParsePush.unsubscribeInBackground(channel);
}

@Kroll.method
public void putValue(@Kroll.argument String key, @Kroll.argument Object value) {
ParseInstallation.getCurrentInstallation().put(key, value);
Expand All @@ -120,9 +171,4 @@ public String getCurrentInstallationId() {
public String getObjectId() {
return ParseInstallation.getCurrentInstallation().getObjectId();
}

public static ParseModule getInstance() {
return module;
}

}
95 changes: 49 additions & 46 deletions android/src/eu/rebelcorp/parse/ParseModuleBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.json.JSONObject;

import android.os.Bundle;
import java.util.Set;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.titanium.TiApplication;

Expand All @@ -30,65 +29,69 @@ public class ParseModuleBroadcastReceiver extends ParsePushBroadcastReceiver {
@Override
public void onPushOpen(Context context, Intent intent) {
Log.d("onPushOpen", "Clicked");

if(ParseModule.getInstance() != null) {
Log.d("onPushOpen", "App is running");

if (ParseModule.getInstance() != null && ParseModule.getInstance().isModuleRunning()) {
Log.d("onPushOpen", "App is running in foreground");
return;
}
else {
Log.d("onPushOpen", "App is not running");

Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationContext().getPackageName());
i.putExtras(intent.getExtras());
context.startActivity(i);


Log.d("onPushOpen", "App is not running or is in background. Now resume the app.");
Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationContext().getPackageName());
i.putExtras(intent.getExtras());
context.startActivity(i);

/* Now, the module should be opened */
if (ParseModule.getInstance() != null) {
try {
Log.d("onPushOpen", "Open a notification");
KrollDict data = new KrollDict(new JSONObject(intent.getExtras().getString("com.parse.Data")));
ParseModule.getInstance().fireEvent("notificationopen", data);
} catch (Exception e) {
Log.d("JSON Failure", e.getMessage());
}
}
}

@Override
public void onReceive(Context context, Intent intent) {
// super.onReceive(context, intent);

try {
if (intent == null) {
Log.d("onReceive", "Receiver intent null");
} else {
String action = intent.getAction();
Log.d("onReceive", "got action " + action );

if(ParseModule.getInstance() != null) {

if (action.equals("com.parse.push.intent.RECEIVE")) {

String data = intent.getExtras().getString("com.parse.Data");
Log.d("onReceive", "and data " + data);

JSONObject json = new JSONObject(data);
KrollDict dict = new KrollDict(json);

Log.d("onReceive", "in notification.");
ParseModule.getInstance().fireEvent("notificationreceive", dict);

}
else if(action.equals("com.parse.push.intent.OPEN")) {
String data = intent.getExtras().getString("com.parse.Data");
Log.d("onReceive", "and data " + data);

JSONObject json = new JSONObject(data);
KrollDict dict = new KrollDict(json);

Log.d("onReceive", "opened.");
ParseModule.getInstance().fireEvent("notificationopen", dict);
}
return;
}

if (ParseModule.getInstance() == null) {
Log.d("onReceive", "no instance of ParseModule found");
return;
}

String action = intent.getAction();
KrollDict data = new KrollDict(new JSONObject(intent.getExtras().getString("com.parse.Data")));

Log.d("onReceive", "got action " + action );
if (action.equals("com.parse.push.intent.RECEIVE")) {
/* The notification is received by the device */
Log.d("onReceive", "New notification received");

if (ParseModule.getInstance().isModuleRunning()) {
ParseModule.getInstance().fireEvent("notificationreceive", data);
} else {
Log.d("onReceive", "App is in background, the event won't be triggered");
}
else {
Log.d("onReceive", "no instance of ParseModule found");
} else if (action.equals("com.parse.push.intent.OPEN")) {
/* The user has clicked on the notification */
Log.d("onReceive", "Notification opened");

if (ParseModule.getInstance().isModuleRunning()) {
ParseModule.getInstance().fireEvent("notificationopen", data);
} else {
Log.d("onReceive", "App is in background, the event will be triggered later.");
}
}

} catch (Exception e) {
Log.e("Push", "Exception: " + e.toString());
}

super.onReceive(context, intent);

}
}
}

0 comments on commit 558cd36

Please sign in to comment.