Android service support for app.nit centered around the class Service

Introduced classes

extern class NativeService

android :: NativeService

Wrapper of Java class
class Service

android :: Service

Android service with its life-cycle callbacks

Redefined classes

redef class App

android :: service $ App

App subclasses are cross-platform applications
redef extern class NativeContext

android :: service $ NativeContext

An Android activity context
redef class Sys

android :: service $ Sys

The main class of the program.

All class definitions

redef class App

android :: service $ App

App subclasses are cross-platform applications
redef extern class NativeContext

android :: service $ NativeContext

An Android activity context
extern class NativeService

android $ NativeService

Wrapper of Java class
class Service

android $ Service

Android service with its life-cycle callbacks
redef class Sys

android :: service $ Sys

The main class of the program.
package_diagram android::service service android::nit_activity nit_activity android::service->android::nit_activity android::log log android::nit_activity->android::log android::key_event key_event android::nit_activity->android::key_event android\>bundle\> bundle android::nit_activity->android\>bundle\> android::dalvik dalvik android::nit_activity->android::dalvik ...>android::log ...>android::key_event\>bundle\> ...\>bundle\>->android\>bundle\> ...>android::dalvik android::at_boot at_boot android::at_boot->android::service a_star-m a_star-m a_star-m->android::at_boot a_star-m... ... a_star-m...->a_star-m


module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module activities

android :: activities

Android Activities wrapper
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

app :: assets

Portable services to load resources from the assets folder
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module bundle

android :: bundle

A mapping class of String to various value types used by the
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module collections

java :: collections

Basic Java collections
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module dalvik

android :: dalvik

Java related services specific to Android and its Dalvik VM
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

json :: error

Intro JsonParseError which is exposed by all JSON reading APIs
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module ffi_support

java :: ffi_support

Core supporting services for the FFI with Java
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module java

java :: java

Supporting services for the FFI with Java and to access Java libraries
module json

json :: json

Read and write JSON formatted text using the standard serialization services
module jvm

jvm :: jvm

Java Virtual Machine invocation API and others services from the JNI C API
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module log

android :: log

Advanced Android logging services
module math

core :: math

Mathematical operations
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module parser_base

parser_base :: parser_base

Simple base for hand-made parsers of all kinds
module platform

android :: platform

Triggers compilation for the android platform
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module safe

serialization :: safe

Services for safer deserialization engines
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module serialization_read

json :: serialization_read

Services to read JSON: deserialize_json and JsonDeserializer
module serialization_write

json :: serialization_write

Services to write Nit objects to JSON strings: serialize_to_json and JsonSerializer
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module static

json :: static

Static interface to read Nit objects from JSON strings
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O


module nit_activity

android :: nit_activity

Core implementation of app.nit on Android using a custom Java entry point


module at_boot

android :: at_boot

Import this module to launch Service at device boot


module a_star-m


# Android service support for _app.nit_ centered around the class `Service`
module service is
	extra_java_files ""
	android_manifest_application """<service android:name=""></service>"""

import android::nit_activity

in "C" `{
	// Nit's App running instance, declared in `nit_activity`
	extern App global_app;

	JNIEXPORT jlong JNICALL Java_nit_app_NitService_nitNewService
		(JNIEnv *env, jobject java_service)
		// Pin a ref to the Java service in the Java GC
		java_service = (*env)->NewGlobalRef(env, java_service);

		// Create the service in Nit and pin it in the Nit GC
		Service nit_service = App_register_service(global_app, java_service);

		// FIXME replace the previous call to register_service by
		// the following line when #1941 is fixed:
		//Service nit_service = new_Service(java_service);


		return (jlong)(void*)nit_service;

	JNIEXPORT jint JNICALL Java_nit_app_NitService_nitOnStartCommand
		(JNIEnv *env, jobject java_service, jlong nit_service, jobject intent, jint flags, jint id)
		return Service_on_start_command((Service)nit_service, intent, flags, id);

	JNIEXPORT void JNICALL Java_nit_app_NitService_nitOnCreate
		(JNIEnv *env, jobject java_service, jlong nit_service)

	JNIEXPORT void JNICALL Java_nit_app_NitService_nitOnDestroy
		(JNIEnv *env, jobject java_service, jlong nit_service)

		// Unpin the service instances in both Nit and Java
		java_service = Service_native((Service)nit_service);
		(*env)->DeleteGlobalRef(env, java_service);

redef class App

	# Current instance of `Service`, if any
	var service: nullable Service = null

	# Launch `Service` in the background, it will be set as `service` when ready
	fun start_service do native_context.start_service

	# Register a service from Java/C
	# FIXME remove when #1941 is fixed
	private fun register_service(java_service: NativeService): Service
		return new Service(java_service.new_global_ref)

	# Prioritize an activity context if one is running, fallback on a service
	redef fun native_context
		if activities.not_empty then return super

		var service = service
		assert service != null
		return service.native

	# Dummy method to force the compilation of callbacks from C
	# The callbacks are used in the launch of a Nit service from C code.
	private fun force_service_callbacks_in_c import Service, register_service,
		Service.on_start_command, Service.on_create, Service.on_destroy, Service.native `{ `}

	redef fun setup

		# Call the dummy method to force their compilation in global compilation

# Android service with its life-cycle callbacks
class Service
	# Java service with the Android context of this service
	var native: NativeService

	# The service has been created
	fun on_create do app.service = self

	# The service received a start command (may happen more then once per service)
	# Should return either `start_sticky`, `start_not_sticky` or `start_redeliver_intent`.
	fun on_start_command(intent: JavaObject, flags, id: Int): Int do return start_sticky

	# The service is being destroyed
	fun on_destroy do app.service = null

# Wrapper of Java class ``
extern class NativeService in "Java" `{ `}
	super NativeContext

	redef fun new_global_ref import sys, Sys.jni_env `{
		Sys sys = NativeService_sys(self);
		JNIEnv *env = Sys_jni_env(sys);
		return (*env)->NewGlobalRef(env, self);

redef class NativeContext
	private fun start_service in "Java" `{
		android.content.Intent intent =
			new android.content.Intent(self,;

# The service is explicitly started and stopped, it is restarted if stopped by the system
# Return value of `Service::on_start_command`.
fun start_sticky: Int in "Java" `{ return; `}

# The service may be stopped by the system and will not be restarted
# Return value of `Service::on_start_command`.
fun start_not_sticky: Int in "Java" `{ return; `}

# The service is explicitly started and stopped, it is restarted with the last intent if stopped by the system
# Return value of `Service::on_start_command`.
fun start_redeliver_intent: Int in "Java" `{ return; `}