Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android customizations #420

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Deprecated by the original author

This fork lives on here: https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background

This readme is left for historical purposes.


<p align="left">
<b><a href="https://github.com/katzer/cordova-plugin-background-mode/tree/example">SAMPLE APP</a> :point_right:</b>
Expand Down Expand Up @@ -145,11 +151,18 @@ The title, text and icon for that notification can be customized as below. Also,
cordova.plugins.backgroundMode.setDefaults({
title: String,
text: String,
icon: 'icon' // this will look for icon.png in platforms/android/res/drawable|mipmap
color: String // hex format like 'F14F4D'
subText: String, // see https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSubText(java.lang.CharSequence)
icon: 'icon', // this will look for icon.png in platforms/android/res/drawable|mipmap
color: String, // hex format like 'F14F4D'
resume: Boolean,
hidden: Boolean,
bigText: Boolean
bigText: Boolean,
channelName: String, // Shown when the user views the app's notification settings
channelDescription: String, // Shown when the user views the channel's settings
allowClose: Boolean, // add a "Close" action to the notification
closeIcon: 'power', // An icon shown for the close action
closeTitle: 'Close', // The text for the close action
showWhen: Boolean //(Default: true) Show the time since the notification was created
})
```

Expand Down Expand Up @@ -205,4 +218,4 @@ Made with :yum: from Leipzig
[changelog]: CHANGELOG.md
[apache2_license]: http://opensource.org/licenses/Apache-2.0
[appplant]: http://appplant.de
[meshfields]: http://meshfields.de
[meshfields]: http://meshfields.de
35 changes: 8 additions & 27 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<engines>
<engine name="cordova" version=">=3.0.0" />
<engine name="android-sdk" version=">=16" />
<engine name="windows-sdk" version=">=10.0.14393.0" />
</engines>

<!-- js -->
Expand Down Expand Up @@ -77,6 +76,7 @@

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please align those indents with the rest of the files (use spaces)

</config-file>

<source-file
Expand All @@ -90,33 +90,14 @@
<source-file
src="src/android/ForegroundService.java"
target-dir="src/de/appplant/cordova/plugin/background" />
</platform>

<!-- windows
<platform name="windows">
<config-file target="config.xml" parent="/*">
<feature name="BackgroundMode" >
<param name="windows-package" value="BackgroundMode"/>
</feature>
</config-file>

<config-file target="package.appxmanifest" parent="/Package/Capabilities" device-target="windows">
<Capability Name="backgroundMediaPlayback" />
</config-file>

<config-file target="config.xml" parent="/*">
<preference name="windows-target-version" value="UAP" />
<preference name="uap-target-min-version" value="10.0.14393.0" />
<preference name="Windows.Universal-MinVersion" value="10.0.14393.0" />
<preference name="Windows.Universal" value="10.0.14393.0" />
</config-file>

<resource-file src="appbeep.wma" target="appbeep.wma" />

<js-module src="src/windows/BackgroundModeProxy.js" name="BackgroundMode.Proxy">
<runs />
</js-module>
</platform> -->
<framework src="com.android.support:support-compat:27.1.1" />
<resource-file src="src/android/res/drawable/power.xml" target="res/drawable/power.xml" />
<resource-file src="src/android/res/drawable-hdpi/power.png" target="res/drawable-hdpi/power.png" />
<resource-file src="src/android/res/drawable-mdpi/power.png" target="res/drawable-mdpi/power.png" />
<resource-file src="src/android/res/drawable-xhdpi/power.png" target="res/drawable-xhdpi/power.png" />
<resource-file src="src/android/res/drawable-xxhdpi/power.png" target="res/drawable-xxhdpi/power.png" />
</platform>

<!-- browser -->
<platform name="browser">
Expand Down
27 changes: 23 additions & 4 deletions src/android/BackgroundMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ Licensed to the Apache Software Foundation (ASF) under one
package de.appplant.cordova.plugin.background;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.*;
import android.os.Bundle;
import android.os.IBinder;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -77,6 +78,24 @@ public void onServiceDisconnected (ComponentName name)
}
};

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
IntentFilter filter = new IntentFilter();
filter.addAction("com.backgroundmode.close" + cordova.getContext().getPackageName());
cordova.getActivity().registerReceiver(receiver, filter);

}

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
cordova.getActivity().finish();

}
};

/**
* Executes the request.
*
Expand Down Expand Up @@ -284,7 +303,7 @@ private void fireEvent (Event event, String params)
String str = String.format("%s._setActive(%b)",
JS_NAMESPACE, active);

str = String.format("%s;%s.on%s(%s)",
str = String.format("%s;%s.on('%s', %s)",
str, JS_NAMESPACE, eventName, params);

str = String.format("%s;%s.fireEvent('%s',%s);",
Expand Down
93 changes: 56 additions & 37 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Icon;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.app.NotificationChannel;

import org.json.JSONObject;
import android.support.v4.app.NotificationCompat;

import static android.os.PowerManager.PARTIAL_WAKE_LOCK;

Expand Down Expand Up @@ -173,38 +170,49 @@ private Notification makeNotification (JSONObject settings)
{
// use channelid for Oreo and higher
String CHANNEL_ID = "cordova-plugin-background-mode-id";
if(Build.VERSION.SDK_INT >= 26){
// The user-visible name of the channel.
CharSequence name = "cordova-plugin-background-mode";
// The user-visible description of the channel.
String description = "cordova-plugin-background-moden notification";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The user-visible name of the channel.
CharSequence name = settings.optString("channelName", "cordova-plugin-background-mode");
// The user-visible description of the channel.
String description = settings.optString("channelDescription", "cordova-plugin-background-moden notification");

int importance = NotificationManager.IMPORTANCE_LOW;
int importance = NotificationManager.IMPORTANCE_LOW;

NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,importance);
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

// Configure the notification channel.
mChannel.setDescription(description);
// Configure the notification channel.
mChannel.setDescription(description);

getNotificationManager().createNotificationChannel(mChannel);
getNotificationManager().createNotificationChannel(mChannel);
}
String title = settings.optString("title", NOTIFICATION_TITLE);
String text = settings.optString("text", NOTIFICATION_TEXT);
boolean bigText = settings.optBoolean("bigText", false);
String subText = settings.optString("subText", "");

Context context = getApplicationContext();
String pkgName = context.getPackageName();
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(pkgName);

Notification.Builder notification = new Notification.Builder(context)
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setOngoing(true)
.setSmallIcon(getIconResId(settings));
.setSmallIcon(getIconResId(settings))
.setShowWhen(settings.optBoolean("showWhen", true));

if (!subText.equals("")) {
notification.setSubText(subText);
}

if (settings.optBoolean("allowClose", false)) {

if(Build.VERSION.SDK_INT >= 26){
notification.setChannelId(CHANNEL_ID);
final Intent clostAppIntent = new Intent("com.backgroundmode.close" + pkgName);
final PendingIntent closeIntent = PendingIntent.getBroadcast(context, 1337, clostAppIntent, 0);
final String closeIconName = settings.optString("closeIcon", "power");
NotificationCompat.Action.Builder closeAction = new NotificationCompat.Action.Builder(getIconResId(closeIconName), settings.optString("closeTitle", "Close"), closeIntent);
notification.addAction(closeAction.build());
}

if (settings.optBoolean("hidden", true)) {
Expand All @@ -213,7 +221,7 @@ private Notification makeNotification (JSONObject settings)

if (bigText || text.contains("\n")) {
notification.setStyle(
new Notification.BigTextStyle().bigText(text));
new NotificationCompat.BigTextStyle().bigText(text));
}

setColor(notification, settings);
Expand Down Expand Up @@ -251,23 +259,40 @@ protected void updateNotification (JSONObject settings)
}

/**
* Retrieves the resource ID of the app icon.
* Retrieves the resource ID of the sent icon name
*
* @param settings A JSON dict containing the icon name.
* @param name Name of the resource to return
*/
private int getIconResId (JSONObject settings)
{
String icon = settings.optString("icon", NOTIFICATION_ICON);
private int getIconResId(String name) {
int resId = getIconResId(name, "mipmap");

int resId = getIconResId(icon, "mipmap");
if (resId == 0) {
resId = getIconResId(name, "drawable");
}

if (resId == 0) {
resId = getIconResId(icon, "drawable");
resId = getIconResId("icon", "mipmap");
}

if (resId == 0) {
resId = getIconResId("icon", "drawable");
}


return resId;
}

/**
* Retrieves the resource ID of the app icon.
*
* @param settings A JSON dict containing the icon name.
*/
private int getIconResId(JSONObject settings) {
String icon = settings.optString("icon", NOTIFICATION_ICON);

return getIconResId(icon);
}

/**
* Retrieve resource id of the specified icon.
*
Expand All @@ -281,13 +306,7 @@ private int getIconResId (String icon, String type)
Resources res = getResources();
String pkgName = getPackageName();

int resId = res.getIdentifier(icon, type, pkgName);

if (resId == 0) {
resId = res.getIdentifier("icon", type, pkgName);
}

return resId;
return res.getIdentifier(icon, type, pkgName);
}

/**
Expand All @@ -297,8 +316,8 @@ private int getIconResId (String icon, String type)
* @param settings A JSON dict containing the color definition (red: FF0000)
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setColor (Notification.Builder notification, JSONObject settings)
{
private void setColor(NotificationCompat.Builder notification,
JSONObject settings) {

String hex = settings.optString("color", null);

Expand Down
Binary file added src/android/res/drawable-hdpi/power.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/android/res/drawable-mdpi/power.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/android/res/drawable-xhdpi/power.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/android/res/drawable-xxhdpi/power.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/android/res/drawable/power.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M13,3h-2v10h2L13,3zM17.83,5.17l-1.42,1.42C17.99,7.86 19,9.81 19,12c0,3.87 -3.13,7 -7,7s-7,-3.13 -7,-7c0,-2.19 1.01,-4.14 2.58,-5.42L6.17,5.17C4.23,6.82 3,9.26 3,12c0,4.97 4.03,9 9,9s9,-4.03 9,-9c0,-2.74 -1.23,-5.18 -3.17,-6.83z"/>
</vector>
2 changes: 1 addition & 1 deletion src/ios/APPBackgroundMode.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ - (void) fireEvent:(NSString*)event
*/
+ (NSString*) wkProperty
{
NSString* str = @"X2Fsd2F5c1J1bnNBdEZvcmVncm91bmRQcmlvcml0eQ==";
NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5";
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];

return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Expand Down
27 changes: 17 additions & 10 deletions www/background-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,23 @@ exports._isActive = false;
*
* Default values of all available options.
*/
exports._defaults =
{
title: 'App is running in background',
text: 'Doing heavy tasks.',
bigText: false,
resume: true,
silent: false,
hidden: true,
color: undefined,
icon: 'icon'

exports._defaults = {
title: 'App is running in background',
text: 'Doing heavy tasks.',
subText: '',
bigText: false,
resume: true,
silent: false,
hidden: true,
color: undefined,
icon: 'icon',
channelName: 'cordova-plugin-background-mode',
channelDescription: 'cordova-plugin-background-moden notification',
allowClose: false,
closeIcon: 'power',
closeTitle: 'Close',
showWhen: true
};

/**
Expand Down