cordova-plugin-keepalive-aj 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "cordova-plugin-keepalive-aj",
3
+ "version": "1.0.0",
4
+ "description": "Prevents Android from killing the app during Web Audio playback.",
5
+ "cordova": {
6
+ "id": "cordova-plugin-keepalive-aj",
7
+ "platforms": [
8
+ "android"
9
+ ]
10
+ },
11
+ "keywords": [
12
+ "ecosystem:cordova",
13
+ "cordova-android"
14
+ ],
15
+ "author": "Ajibola Ayanwola",
16
+ "license": "MIT"
17
+ }
package/plugin.xml ADDED
@@ -0,0 +1,19 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <plugin id="cordova-plugin-keepalive-aj" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0">
3
+ <name>KeepAlive</name>
4
+ <js-module name="KeepAlive" src="www/KeepAlive.js">
5
+ <clobbers target="KeepAlive" />
6
+ </js-module>
7
+ <platform name="android">
8
+ <config-file parent="/*" target="res/xml/config.xml">
9
+ <feature name="KeepAlive">
10
+ <param name="android-package" value="com.example.keepalive.KeepAlive" />
11
+ </feature>
12
+ </config-file>
13
+ <config-file target="AndroidManifest.xml" parent="/manifest">
14
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
15
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
16
+ </config-file>
17
+ <source-file src="src/android/KeepAlive.java" target-dir="src/com/example/keepalive" />
18
+ </platform>
19
+ </plugin>
@@ -0,0 +1,54 @@
1
+ package com.example.keepalive;
2
+
3
+ import android.app.Notification;
4
+ import android.app.NotificationChannel;
5
+ import android.app.NotificationManager;
6
+ import android.content.Context;
7
+ import android.content.Intent;
8
+ import android.os.Build;
9
+ import org.apache.cordova.CordovaPlugin;
10
+ import org.apache.cordova.CallbackContext;
11
+ import org.json.JSONArray;
12
+
13
+ public class KeepAlive extends CordovaPlugin {
14
+ private static final String CHANNEL_ID = "audio_service_channel";
15
+
16
+ @Override
17
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
18
+ if (action.equals("start")) {
19
+ startService();
20
+ callbackContext.success();
21
+ return true;
22
+ } else if (action.equals("stop")) {
23
+ stopService();
24
+ callbackContext.success();
25
+ return true;
26
+ }
27
+ return false;
28
+ }
29
+
30
+ private void startService() {
31
+ Context context = cordova.getActivity().getApplicationContext();
32
+ NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
33
+
34
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
35
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Audio Playback", NotificationManager.IMPORTANCE_LOW);
36
+ manager.createNotificationChannel(channel);
37
+ }
38
+
39
+ Notification notification = null;
40
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
41
+ notification = new Notification.Builder(context, CHANNEL_ID)
42
+ .setContentTitle("Audio Running")
43
+ .setContentText("Your app is playing audio in the background")
44
+ .setSmallIcon(context.getApplicationInfo().icon)
45
+ .build();
46
+ }
47
+
48
+ cordova.getActivity().startForegroundService(new Intent(context, ForegroundService.class)); // Simplified for example
49
+ }
50
+
51
+ private void stopService() {
52
+ // Logic to stop service
53
+ }
54
+ }
@@ -0,0 +1,9 @@
1
+ var exec = require('cordova/exec');
2
+
3
+ exports.start = function(success, error) {
4
+ exec(success, error, 'KeepAlive', 'start', []);
5
+ };
6
+
7
+ exports.stop = function(success, error) {
8
+ exec(success, error, 'KeepAlive', 'stop', []);
9
+ };