Skip to content

Commit

Permalink
Merge pull request #1433 from billdawson/timob-3286
Browse files Browse the repository at this point in the history
TIMOB-3286 Enable services to be re-startable
  • Loading branch information
Opie Cyrus committed Feb 17, 2012
2 parents ebeaa6a + d78e8b9 commit e412ad2
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 25 deletions.
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 All @@ -12,16 +12,19 @@
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.IntentProxy;
import org.appcelerator.titanium.proxy.RProxy;
import org.appcelerator.titanium.proxy.ServiceProxy;
import org.appcelerator.titanium.util.TiConvert;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
Expand Down Expand Up @@ -217,6 +220,9 @@ public class AndroidModule extends KrollModule
@Kroll.constant public static final int FLAG_SHOW_LIGHTS = Notification.FLAG_SHOW_LIGHTS;
@Kroll.constant public static final int STREAM_DEFAULT = Notification.STREAM_DEFAULT;

@Kroll.constant public static final int START_NOT_STICKY = Service.START_NOT_STICKY;
@Kroll.constant public static final int START_REDELIVER_INTENT = Service.START_REDELIVER_INTENT;

@Kroll.constant public static final int STREAM_ALARM = AudioManager.STREAM_ALARM;
@Kroll.constant public static final int STREAM_MUSIC = AudioManager.STREAM_MUSIC;
@Kroll.constant public static final int STREAM_NOTIFICATION = AudioManager.STREAM_NOTIFICATION;
Expand Down Expand Up @@ -250,6 +256,10 @@ public IntentProxy createServiceIntent(Object[] args)
IntentProxy intent = new IntentProxy();
intent.setType(IntentProxy.TYPE_SERVICE);
intent.handleCreationArgs(this, args);
Object startMode = intent.getProperty(TiC.INTENT_PROPERTY_START_MODE);
if (startMode != null) {
intent.putExtra(TiC.INTENT_PROPERTY_START_MODE, TiConvert.toInt(startMode));
}
return intent;
}

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 @@ -41,15 +41,16 @@ private void finalizeUrl(Intent intent)
}

@Override
public void onStart(Intent intent, int startId)
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStart(intent, startId);
if (DBG) {
Log.d(LCAT, "onStart");
Log.d(LCAT, "onStartCommand");
}
finalizeUrl(intent);
ServiceProxy proxy = createProxy(intent);
start(proxy);

return intent.getIntExtra(TiC.INTENT_PROPERTY_START_MODE, AndroidModule.START_REDELIVER_INTENT);
}

protected void executeServiceCode(ServiceProxy proxy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ti.modules.titanium.app;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
Expand All @@ -20,9 +21,8 @@
@Kroll.module(parentModule=AppModule.class)
public class AndroidModule extends KrollModule
{
private static final String TAG = "AndroidModule";

protected RProxy r;
private static final String TAG = "App.AndroidModule";


public AndroidModule()
Expand All @@ -48,6 +48,11 @@ public RProxy getR()
@Kroll.method
public ActivityProxy getTopActivity()
{
if (KrollRuntime.getActivityRefCount() == 0) {
// No activity to wait for. This can be the case if, for example,
// the Application is being started for a Service, not an Activity.
return null;
}
TiApplication tiApp = TiApplication.getInstance();
Activity activity = tiApp.getCurrentActivity();
if (activity == null || !(activity instanceof TiBaseActivity)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 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 @@ -102,9 +102,6 @@ private void linkifyIfEnabled(TextView tv, Object autoLink)
@Override
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{
if (DBG) {
Log.d(LCAT, "Property: " + key + " old: " + oldValue + " new: " + newValue);
}
TextView tv = (TextView) getNativeView();
if (key.equals(TiC.PROPERTY_HTML)) {
tv.setText(Html.fromHtml(TiConvert.toString(newValue)), TextView.BufferType.SPANNABLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2011 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2011-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 @@ -250,6 +250,11 @@ public static void decrementActivityRefCount()
instance.dispose();
}

public static int getActivityRefCount()
{
return activityRefCount;
}

private void internalDispose()
{
doDispose();
Expand Down
16 changes: 6 additions & 10 deletions android/runtime/common/src/js/module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2011 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2011-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 @@ -65,16 +65,9 @@ Module.runModule = function (source, filename, activityOrService) {
return module;
}

// Run a module as the main entry point.
Module.runMainModule = function (source, filename) {
var mainModule = Module.main = new Module('.');
mainModule.load(filename, source);
return true;
}

// Attempts to load the module. If no file is found
// with the provided name an exception will be thrown.
// Once the contents of the file are read, it is ran
// Once the contents of the file are read, it is run
// in the current context. A sandbox is created by
// executing the code inside a wrapper function.
// This provides a speed boost vs creating a new context.
Expand Down Expand Up @@ -243,7 +236,10 @@ Module.prototype._runScript = function (source, filename) {
}
require.main = Module.main;

if (self.id == '.') {
// This "first time" run is really only for app.js, AFAICT, and needs
// an activity. If app was restarted for Service only, we don't want
// to go this route. So added currentActivity check. (bill)
if (self.id == '.' && self.context.currentActivity) {
global.require = require;
Titanium.Android.currentActivity = self.context.currentActivity;

Expand Down
3 changes: 2 additions & 1 deletion android/titanium/src/java/org/appcelerator/titanium/TiC.java
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 @@ -92,6 +92,7 @@ public class TiC
public static final String INTENT_PROPERTY_MESSENGER = "messenger";
public static final String INTENT_PROPERTY_MSG_ACTIVITY_CREATED_ID = "msgActivityCreatedId";
public static final String INTENT_PROPERTY_MSG_ID = "messageId";
public static final String INTENT_PROPERTY_START_MODE = "startMode";
public static final String INTENT_PROPERTY_USE_ACTIVITY_WINDOW = "useActivityWindow";
public static final String INTENT_PROPERTY_WINDOW_ID = "windowId";
public static final String LAYOUT_HORIZONTAL = "horizontal";
Expand Down
27 changes: 25 additions & 2 deletions apidoc/Titanium/Android/Android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ methods:
type: Titanium.Android.Intent
parameters:
- name: options
summary: Options dictionary -- pass in a `url` option to specify the path to a javascript-based service you create.
type: Object
summary: Options for the Service.
type: ServiceIntentOptions
- name: isServiceRunning
summary: Check on state of Service.
returns:
Expand Down Expand Up @@ -764,6 +764,14 @@ properties:
summary:
type: Number
permission: read-only
- name: START_NOT_STICKY
summary: A Service start mode indicating that if the host application is stopped by Android, the service should not be restarted automatically.
type: Number
permission: read-only
- name: START_REDELIVER_INTENT
summary: A Service start mode indicating that if the host application is stopped by Android, the service should be restarted automatically and the original Intent re-sent.
type: Number
permission: read-only
- name: STREAM_DEFAULT
summary: Use this constant as the value for audioStreamType to request that the default stream type for notifications be used.
type: Number
Expand All @@ -780,3 +788,18 @@ description: |
The most important page there will be the
[Intent reference](http://developer.android.com/reference/android/content/Intent.html),
which will give you the meaning of those ACTION, EXTRA and CATEGORY constants.
---
name: ServiceIntentOptions
summary: Options passed to <Titanium.Android.createServiceIntent>.
properties:
- name: url
summary: Url for the service's Javascript.
type: String
accessors: false
- name: startMode
summary: One of the `START_` constants from <Titanium.Android> to specify the "stickiness" of the Service when Android shuts down the host application.
type: Number
accessors: false
optional: true
default: Titanium.Android.START_REDELIVER_INTENT

0 comments on commit e412ad2

Please sign in to comment.