Skip to content
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

[TIMOB-15443] Android: Enable proxies to get window/tabgroup activity lifecycle events #6179

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,6 @@ protected void handlePostOpen()
// Prevent any duplicate events from firing by marking
// this group has having focus.
isFocused = true;

// Setup the new tab activity like setting orientation modes.
onWindowActivityCreated();
}

@Override
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to onWindowActivityCreated is redundant since it's already called in TiBaseActivity.onCreate for both Windows and TabGroups. That function is not idempotent thus it's a bug.

Expand Down
65 changes: 64 additions & 1 deletion android/titanium/src/java/org/appcelerator/kroll/KrollProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.ActivityProxy;
import org.appcelerator.titanium.proxy.TiWindowProxy;
import org.appcelerator.titanium.TiLifecycle.OnLifecycleEvent;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiUrl;
Expand All @@ -42,7 +44,7 @@
* the view object is a proxy itself.
*/
@Kroll.proxy(name = "KrollProxy", propertyAccessors = { KrollProxy.PROPERTY_HAS_JAVA_LISTENER })
public class KrollProxy implements Handler.Callback, KrollProxySupport
public class KrollProxy implements Handler.Callback, KrollProxySupport, OnLifecycleEvent
{
private static final String TAG = "KrollProxy";
private static final int INDEX_NAME = 0;
Expand Down Expand Up @@ -182,6 +184,12 @@ public void setActivity(Activity activity)
this.activity = new WeakReference<Activity>(activity);
}

public void attachActivityLifecycle(Activity activity)
{
setActivity(activity);
((TiBaseActivity) activity).addOnLifecycleEventListener(this);
}

/**
* @return the activity associated with this proxy. It can be null.
* @module.api
Expand Down Expand Up @@ -384,6 +392,21 @@ public void handleCreationDict(KrollDict dict)
if (dict.containsKey(TiC.PROPERTY_BUBBLE_PARENT)) {
bubbleParent = TiConvert.toBoolean(dict, TiC.PROPERTY_BUBBLE_PARENT, true);
}

if (dict.containsKey(TiC.PROPERTY_LIFECYCLE_CONTAINER)) {
KrollProxy lifecycleProxy = (KrollProxy) dict.get(TiC.PROPERTY_LIFECYCLE_CONTAINER);
if (lifecycleProxy instanceof TiWindowProxy) {
ActivityProxy activityProxy = ((TiWindowProxy) lifecycleProxy).getWindowActivityProxy();
if (activityProxy != null) {
attachActivityLifecycle(activityProxy.getActivity());
} else {
((TiWindowProxy) lifecycleProxy).addProxyWaitingForActivity(this);
}
} else {
Log.e(TAG, TiC.PROPERTY_LIFECYCLE_CONTAINER + " must be a WindowProxy or TabGroupProxy (TiWindowProxy)");
}
}

properties.putAll(dict);
handleDefaultValues();
handleLocaleProperties();
Expand Down Expand Up @@ -1306,5 +1329,45 @@ public String getApiName()
{
return "Ti.Proxy";
}

/**
* A place holder for subclasses to extend. Its purpose is to receive native Android onResume life cycle events.
* @param activity the activity attached to this module.
* @module.api
*/
public void onResume(Activity activity) {
}

/**
* A place holder for subclasses to extend. Its purpose is to receive native Android onPause life cycle events.
* @param activity the activity attached to this module.
* @module.api
*/
public void onPause(Activity activity) {
}

/**
* A place holder for subclasses to extend. Its purpose is to receive native Android onDestroy life cycle events.
* @param activity the activity attached to this module.
* @module.api
*/
public void onDestroy(Activity activity) {
}

/**
* A place holder for subclasses to extend. Its purpose is to receive native Android onStart life cycle events.
* @param activity the activity attached to this module.
* @module.api
*/
public void onStart(Activity activity) {
}

/**
* A place holder for subclasses to extend. Its purpose is to receive native Android onStop life cycle events.
* @param activity the activity attached to this module.
* @module.api
*/
public void onStop(Activity activity) {
}
}

5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,11 @@ public class TiC
*/
public static final String PROPERTY_LAYOUT_ID = "layoutId";

/**
* @module.api
*/
public static final String PROPERTY_LIFECYCLE_CONTAINER = "lifecycleContainer";

/**
* @module.api
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiAnimation;
import org.appcelerator.titanium.view.TiUIView;
import org.appcelerator.titanium.util.TiWeakList;

import android.app.Activity;
import android.content.pm.ActivityInfo;
Expand All @@ -46,6 +47,7 @@ public abstract class TiWindowProxy extends TiViewProxy
protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;

private static WeakReference<TiWindowProxy> waitingForOpen;
private TiWeakList<KrollProxy> proxiesWaitingForActivity = new TiWeakList<KrollProxy>();

protected boolean opened, opening;
protected boolean focused;
Expand Down Expand Up @@ -189,6 +191,10 @@ public void closeFromActivity(boolean activityIsFinishing)
fireSyncEvent(TiC.EVENT_CLOSE, data);
}

public void addProxyWaitingForActivity(KrollProxy waitingProxy) {
proxiesWaitingForActivity.add(new WeakReference<KrollProxy>(waitingProxy));
}

protected void releaseViewsForActivityForcedToDestroy()
{
releaseViews();
Expand Down Expand Up @@ -240,6 +246,16 @@ public void onWindowActivityCreated()
{
windowActivityCreated = true;

synchronized (proxiesWaitingForActivity.synchronizedList()) {
for (KrollProxy proxy : proxiesWaitingForActivity.nonNull()) {
try {
proxy.attachActivityLifecycle(getActivity());
} catch (Throwable t) {
Log.e(TAG, "Error attaching activity to proxy: " + t.getMessage(), t);
}
}
}

// Make sure the activity opens according to any orientation modes
// set on the window before the activity was actually created.
if (orientationModes != null) {
Expand Down
10 changes: 10 additions & 0 deletions apidoc/Titanium/Proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,13 @@ properties:
type: String
platforms: [android, blackberry, iphone, ipad, mobileweb, tizen]
since: "3.2.0"

- name: lifecycleContainer
summary: The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
description: |
If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks
will also be called on the proxy. Proxies that require the activity lifecycle will need this property set
to the appropriate containing Window or TabGroup.
type: [Titanium.UI.Window, Titanium.UI.TabGroup]
platforms: [android]
since: "3.5.0"