Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package org.kivy.android;

import android.content.Context;
import android.content.Intent;

public class PythonServiceIntent {

private PythonServiceIntent() {}

public static Intent build(
Context ctx,
Class<?> serviceClass,
String serviceEntrypoint,
String serviceTitle,
String pythonName,
String serviceStartAsForeground,
String pythonServiceArgument,
String smallIconName,
String contentTitle,
String contentText) {
String appRoot = PythonUtil.getAppRoot(ctx);
return buildWithPaths(
ctx,
serviceClass,
ctx.getFilesDir().getAbsolutePath(),
appRoot,
null,
serviceEntrypoint,
serviceTitle,
null,
pythonName,
serviceStartAsForeground,
pythonServiceArgument,
smallIconName,
contentTitle,
contentText);
}

public static Intent build(
Context ctx,
Class<?> serviceClass,
String serviceEntrypoint,
String serviceTitle,
String pythonName,
boolean serviceStartAsForeground,
String pythonServiceArgument,
String smallIconName,
String contentTitle,
String contentText) {
return build(
ctx,
serviceClass,
serviceEntrypoint,
serviceTitle,
pythonName,
booleanToString(serviceStartAsForeground),
pythonServiceArgument,
smallIconName,
contentTitle,
contentText);
}

public static Intent buildActivityService(
Context ctx,
Class<?> serviceClass,
String serviceEntrypoint,
String serviceTitle,
String serviceDescription,
boolean serviceStartAsForeground,
String pythonServiceArgument) {
String appRoot = PythonUtil.getAppRoot(ctx);
return buildWithPaths(
ctx,
serviceClass,
ctx.getFilesDir().getAbsolutePath(),
appRoot,
null,
serviceEntrypoint,
serviceTitle,
serviceDescription,
"python",
booleanToString(serviceStartAsForeground),
pythonServiceArgument,
null,
null,
null);
}

public static Intent buildWithPaths(
Context ctx,
Class<?> serviceClass,
String androidPrivate,
String androidArgument,
String androidUnpack,
String serviceEntrypoint,
String serviceTitle,
String serviceDescription,
String pythonName,
String serviceStartAsForeground,
String pythonServiceArgument,
String smallIconName,
String contentTitle,
String contentText) {
Intent intent = new Intent(ctx, serviceClass);
intent.putExtra("androidPrivate", androidPrivate);
intent.putExtra("androidArgument", androidArgument);
intent.putExtra("serviceEntrypoint", serviceEntrypoint);
intent.putExtra("pythonName", pythonName);
intent.putExtra("serviceStartAsForeground", serviceStartAsForeground);
intent.putExtra("pythonHome", androidArgument);
intent.putExtra("pythonPath", androidArgument + ":" + androidArgument + "/lib");
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
intent.putExtra("serviceTitle", serviceTitle);

if (androidUnpack != null) {
intent.putExtra("androidUnpack", androidUnpack);
}
if (serviceDescription != null) {
intent.putExtra("serviceDescription", serviceDescription);
}
if (smallIconName != null) {
intent.putExtra("smallIconName", smallIconName);
}
if (contentTitle != null) {
intent.putExtra("contentTitle", contentTitle);
}
if (contentText != null) {
intent.putExtra("contentText", contentText);
}

return intent;
}

public static Intent buildWithPaths(
Context ctx,
Class<?> serviceClass,
String androidPrivate,
String androidArgument,
String androidUnpack,
String serviceEntrypoint,
String serviceTitle,
String serviceDescription,
String pythonName,
boolean serviceStartAsForeground,
String pythonServiceArgument,
String smallIconName,
String contentTitle,
String contentText) {
return buildWithPaths(
ctx,
serviceClass,
androidPrivate,
androidArgument,
androidUnpack,
serviceEntrypoint,
serviceTitle,
serviceDescription,
pythonName,
booleanToString(serviceStartAsForeground),
pythonServiceArgument,
smallIconName,
contentTitle,
contentText);
}

public static void stop(Context ctx, Class<?> serviceClass) {
ctx.stopService(new Intent(ctx, serviceClass));
}

private static String booleanToString(boolean value) {
return value ? "true" : "false";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Intent;
import android.content.Context;
import {{ args.service_class_name }};
import org.kivy.android.PythonServiceIntent;


public class Service{{ name|capitalize }} extends {{ base_service_class }} {
Expand Down Expand Up @@ -39,21 +40,17 @@ static public void start(Context ctx, String smallIconName,
static public Intent getDefaultIntent(Context ctx, String smallIconName,
String contentTitle, String contentText,
String pythonServiceArgument) {
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath());
intent.putExtra("androidArgument", argument);
intent.putExtra("serviceTitle", "{{ args.name }}");
intent.putExtra("serviceEntrypoint", "{{ entrypoint }}");
intent.putExtra("pythonName", "{{ name }}");
intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}");
intent.putExtra("pythonHome", argument);
intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
intent.putExtra("smallIconName", smallIconName);
intent.putExtra("contentTitle", contentTitle);
intent.putExtra("contentText", contentText);
return intent;
return PythonServiceIntent.build(
ctx,
Service{{ name|capitalize }}.class,
"{{ entrypoint }}",
"{{ args.name }}",
"{{ name }}",
"{{ foreground|lower }}",
pythonServiceArgument,
smallIconName,
contentTitle,
contentText);
}

@Override
Expand All @@ -63,7 +60,6 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument)
}

static public void stop(Context ctx) {
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
ctx.stopService(intent);
PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,21 @@ public static void _do_start_service(
String serviceDescription,
String pythonServiceArgument,
boolean showForegroundNotification) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
String app_root_dir = PythonActivity.mActivity.getAppRoot();
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
serviceIntent.putExtra("androidPrivate", argument);
serviceIntent.putExtra("androidArgument", app_root_dir);
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
serviceIntent.putExtra("pythonName", "python");
serviceIntent.putExtra("pythonHome", app_root_dir);
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
serviceIntent.putExtra(
"serviceStartAsForeground", (showForegroundNotification ? "true" : "false"));
serviceIntent.putExtra("serviceTitle", serviceTitle);
serviceIntent.putExtra("serviceDescription", serviceDescription);
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
Intent serviceIntent =
PythonServiceIntent.buildActivityService(
PythonActivity.mActivity,
PythonService.class,
"service/" + entry_point,
serviceTitle,
serviceDescription,
showForegroundNotification,
pythonServiceArgument);
PythonActivity.mActivity.startService(serviceIntent);
}

public static void stop_service() {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
PythonActivity.mActivity.stopService(serviceIntent);
PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,22 @@ public static void _do_start_service(
String serviceDescription,
String pythonServiceArgument,
boolean showForegroundNotification) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
String app_root_dir = PythonActivity.mActivity.getAppRoot();
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
serviceIntent.putExtra("androidPrivate", argument);
serviceIntent.putExtra("androidArgument", app_root_dir);
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
serviceIntent.putExtra("pythonName", "python");
serviceIntent.putExtra("pythonHome", app_root_dir);
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
serviceIntent.putExtra(
"serviceStartAsForeground", (showForegroundNotification ? "true" : "false"));
serviceIntent.putExtra("serviceTitle", serviceTitle);
serviceIntent.putExtra("serviceDescription", serviceDescription);
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
Intent serviceIntent =
PythonServiceIntent.buildActivityService(
PythonActivity.mActivity,
PythonService.class,
"service/" + entry_point,
serviceTitle,
serviceDescription,
showForegroundNotification,
pythonServiceArgument);
PythonActivity.mActivity.startService(serviceIntent);
}

public static void stop_service() {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
PythonActivity.mActivity.stopService(serviceIntent);
PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class);
}

/** Loading screen view * */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,27 +301,22 @@ public static void _do_start_service(
String serviceDescription,
String pythonServiceArgument,
boolean showForegroundNotification) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
String app_root_dir = PythonActivity.mActivity.getAppRoot();
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
serviceIntent.putExtra("androidPrivate", argument);
serviceIntent.putExtra("androidArgument", app_root_dir);
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
serviceIntent.putExtra("pythonName", "python");
serviceIntent.putExtra("pythonHome", app_root_dir);
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
serviceIntent.putExtra(
"serviceStartAsForeground", (showForegroundNotification ? "true" : "false"));
serviceIntent.putExtra("serviceTitle", serviceTitle);
serviceIntent.putExtra("serviceDescription", serviceDescription);
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
Intent serviceIntent =
PythonServiceIntent.buildActivityService(
PythonActivity.mActivity,
PythonService.class,
"service/" + entry_point,
serviceTitle,
serviceDescription,
showForegroundNotification,
pythonServiceArgument);
PythonActivity.mActivity.startService(serviceIntent);
}

public static void stop_service() {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
PythonActivity.mActivity.stopService(serviceIntent);
PythonServiceIntent.stop(PythonActivity.mActivity, PythonService.class);
}

/** Loading screen view * */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.util.Log;

import org.kivy.android.PythonService;
import org.kivy.android.PythonServiceIntent;
import org.kivy.android.PythonUtil;

public class Service{{ name|capitalize }} extends PythonService {
Expand Down Expand Up @@ -69,21 +70,21 @@ static public Intent getDefaultIntent(Context ctx, String smallIconName,
String contentText,
String pythonServiceArgument) {
String appRoot = PythonUtil.getAppRoot(ctx);
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
intent.putExtra("androidPrivate", appRoot);
intent.putExtra("androidArgument", appRoot);
intent.putExtra("serviceEntrypoint", "{{ entrypoint }}");
intent.putExtra("serviceTitle", "{{ name|capitalize }}");
intent.putExtra("pythonName", "{{ name }}");
intent.putExtra("serviceStartAsForeground", "{{ foreground|lower }}");
intent.putExtra("pythonHome", appRoot);
intent.putExtra("androidUnpack", appRoot);
intent.putExtra("pythonPath", appRoot + ":" + appRoot + "/lib");
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
intent.putExtra("smallIconName", smallIconName);
intent.putExtra("contentTitle", contentTitle);
intent.putExtra("contentText", contentText);
return intent;
return PythonServiceIntent.buildWithPaths(
ctx,
Service{{ name|capitalize }}.class,
appRoot,
appRoot,
appRoot,
"{{ entrypoint }}",
"{{ name|capitalize }}",
null,
"{{ name }}",
"{{ foreground|lower }}",
pythonServiceArgument,
smallIconName,
contentTitle,
contentText);
}

@Override
Expand All @@ -95,8 +96,7 @@ protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument)


static public void stop(Context ctx) {
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
ctx.stopService(intent);
PythonServiceIntent.stop(ctx, Service{{ name|capitalize }}.class);
}

}
Loading
Loading