Skip to content

Commit

Permalink
Merge branch 'master' of github.com:appcelerator/titanium_mobile into…
Browse files Browse the repository at this point in the history
… timob-7589
  • Loading branch information
marshall committed Feb 13, 2012
2 parents 65e7909 + a9219b3 commit cb70ac8
Show file tree
Hide file tree
Showing 24 changed files with 903 additions and 285 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.app;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiContext;
Expand Down Expand Up @@ -41,19 +48,22 @@ public RProxy getR()
@Kroll.method
public ActivityProxy getTopActivity()
{
try {
TiApplication.getInstance().rootActivityLatch.await();

} catch (InterruptedException e) {
e.printStackTrace();
}

Activity activity = TiApplication.getInstance().getCurrentActivity();
TiApplication tiApp = TiApplication.getInstance();
Activity activity = tiApp.getCurrentActivity();
if (activity == null || !(activity instanceof TiBaseActivity)) {
activity = TiApplication.getInstance().getRootActivity();
try {
tiApp.rootActivityLatch.await();
activity = tiApp.getRootActivity();
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted awaiting rootActivityLatch");
}
}

return ((TiBaseActivity)activity).getActivityProxy();
if (activity instanceof TiBaseActivity) {
return ((TiBaseActivity)activity).getActivityProxy();
} else {
return null;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public void setWindow(TiWindowProxy window)

this.win.setTabProxy(this);
this.win.setTabGroupProxy(tabGroupProxy);
//Send out a sync event to indicate window is added to tab
this.win.fireSyncEvent(TiC.EVENT_ADDED_TO_TAB, null);
}

@Kroll.method @Kroll.getProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand Down Expand Up @@ -47,6 +47,24 @@ public void setTabGroupProxy(TabGroupProxy proxy) {
@Override
protected void onCreate(Bundle savedInstanceState)
{
TiApplication tiApp = getTiApp();

if (tiApp.isRestartPending()) {
super.onCreate(savedInstanceState);
if (!isFinishing()) {
finish();
}
return;
}

if (TiBaseActivity.isUnsupportedReLaunch(this, savedInstanceState)) {
Log.w(LCAT, "Unsupported, out-of-order activity creation. Finishing.");
super.onCreate(savedInstanceState);
tiApp.scheduleRestart(250);
finish();
return;
}

TiApplication.addToActivityStack(this);
KrollRuntime.incrementActivityRefCount();

Expand Down Expand Up @@ -185,22 +203,51 @@ public void finish()
protected void onPause()
{
super.onPause();
((TiApplication) getApplication()).setCurrentActivity(this, null);

TiApplication tiApp = getTiApp();

if (tiApp.isRestartPending()) {
if (!isFinishing()) {
finish();
}
return;
}

tiApp.setCurrentActivity(this, null);
}

@Override
protected void onResume()
{
super.onResume();
((TiApplication) getApplication()).setCurrentActivity(this, this);

TiApplication tiApp = getTiApp();

if (tiApp.isRestartPending()) {
if (!isFinishing()) {
finish();
}
return;
}

tiApp.setCurrentActivity(this, this);
}

@Override
protected void onDestroy()
{

TiApplication.removeFromActivityStack(this);
super.onDestroy();

TiApplication tiApp = getTiApp();

if (tiApp.isRestartPending()) {
if (!isFinishing()) {
finish();
}
return;
}

if (!isFinishing())
{
// Our Activities are currently unable to recover from Android-forced restarts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,16 @@ protected void setActivityBackground(final Drawable drawable, boolean post)
if (post) {
proxy.getMainHandler().post(new Runnable() {
public void run() {
windowActivity.getWindow().setBackgroundDrawable(drawable);
/*
*This is a check to prevent a race condition- when user execute open, open, close on the window.
*setActivityBackground is being called in KrollRuntime thread, which may cause a race condition: windowActivity
*is set to null in the middle of closing process, while the 2nd call of open gets here. In the case of
*"open, open, close, open", this would work b/c the assigning of windowActivity and setting it to null are both being done
*on the same thread.
*/
if (windowActivity != null) {
windowActivity.getWindow().setBackgroundDrawable(drawable);
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,31 @@ public void release() {
viewModel = null;
itemClickListener = null;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// To prevent undesired "focus" and "blur" events during layout caused
// by ListView temporarily taking focus, we will disable focus events until
// layout has finished.
OnFocusChangeListener focusListener = null;
View focusedView = listView.findFocus();
if (focusedView != null) {
OnFocusChangeListener listener = focusedView.getOnFocusChangeListener();
if (listener != null && listener instanceof TiUIView) {
focusedView.setOnFocusChangeListener(null);
focusListener = listener;
}
}

super.onLayout(changed, left, top, right, bottom);

// Layout is finished, re-enable focus events.
if (focusListener != null) {
focusedView.setOnFocusChangeListener(focusListener);
// If the configuration changed, we manually fire the blur event
if (changed) {
focusListener.onFocusChange(focusedView, false);
}
}
}
}
57 changes: 31 additions & 26 deletions android/modules/ui/src/js/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ exports.bootstrapWindow = function(Titanium) {
Window.prototype.getWindowPixelFormat = windowPixelFormatGetter;
Window.prototype.setWindowPixelFormat = windowPixelFormatSetter;
Object.defineProperty(Window.prototype, "windowPixelFormat", { get: windowPixelFormatGetter, set: windowPixelFormatSetter});

// Helper method to keep the window alive until it gets closed.
var rememberWindowAndAddCloseListener = function(value){
if (value == null){
return;
}
var index = windows.indexOf(value);
if(index < 0){
windows.push(value);
var self = value;
value.on('close', function () {
var index = windows.indexOf(self);
if (index >= 0) {
windows.splice(index, index);
} else {
kroll.log(TAG, "Unable to release window reference.");
}
});
}
}

Window.prototype.open = function(options) {
var self = this;
Expand All @@ -178,16 +198,8 @@ exports.bootstrapWindow = function(Titanium) {
}
this.currentState = this.state.opening;

// Keep the window alive until it gets closed.
windows.push(this);
this.on('close', function () {
var index = windows.indexOf(self);
if (index >= 0) {
windows.splice(index, index);
} else {
kroll.log(TAG, "Unable to release window reference.");
}
});
rememberWindowAndAddCloseListener(this);


if (!options) {
options = {};
Expand Down Expand Up @@ -255,7 +267,6 @@ exports.bootstrapWindow = function(Titanium) {
if (this.propertyCache) {
kroll.extend(this._properties, this.propertyCache);
}

this.window = existingWindow;
this.view = this.window;
this.setWindowView(this.view);
Expand All @@ -280,15 +291,15 @@ exports.bootstrapWindow = function(Titanium) {
for (var event in this._events) {
var listeners = this.listeners(event);
for (var i = 0; i < listeners.length; i++) {
this.addWrappedListener(event, listeners[i]);
this.view.addEventListener(event, listeners[i].listener, this);
}
}
var self = this;
this.addWrappedListener("closeFromActivity", function(e) {
this.view.addEventListener("closeFromActivity", function(e) {
self.window = null;
self.view = null;
self.currentState = self.state.closed;
});
}, this);

if (this.cachedActivityProxy) {
this.window._internalActivity.extend(this.cachedActivityProxy);
Expand Down Expand Up @@ -463,21 +474,10 @@ exports.bootstrapWindow = function(Titanium) {
EventEmitter.prototype.addEventListener.call(this, event, listener);

} else {
this.addWrappedListener(event, listener);
this.view.addEventListener(event, listener, this);
}
}

// Add event listener to this.window and update the source of event to this.
Window.prototype.addWrappedListener = function(event, listener) {
var self = this;
self.view.addEventListener(event, function(e) {
if (e.source == self.view) {
e.source = self;
}
listener(e);
});
}

Window.prototype.removeEventListener = function(event, listener) {
if (["open", "close"].indexOf(event) >= 0 || this.window == null) {
EventEmitter.prototype.removeEventListener.call(this, event, listener);
Expand Down Expand Up @@ -516,8 +516,13 @@ exports.bootstrapWindow = function(Titanium) {
window._module = scopeVars.module;
window._children = [];
window._postOpenChildren = [];
var self = window;
window.on('addedToTab', function () {
rememberWindowAndAddCloseListener(self);
});

return window;

}

return Window;
Expand Down

0 comments on commit cb70ac8

Please sign in to comment.