Skip to content

Commit

Permalink
Restructure BackgroundModeExt as an internal cordova plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Feb 11, 2019
1 parent 8fbf210 commit 2c4df33
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 150 deletions.
10 changes: 6 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BackgroundMode" >
<param name="android-package"
value="de.appplant.cordova.plugin.background.BackgroundMode"/>
<feature name="BackgroundMode">
<param name="android-package" value="de.appplant.cordova.plugin.background.BackgroundMode"/>
</feature>
<feature name="BackgroundModeExt">
<param name="android-package" value="de.appplant.cordova.plugin.background.BackgroundModeExt"/>
</feature>
</config-file>

Expand All @@ -82,7 +84,7 @@
target-dir="src/de/appplant/cordova/plugin/background" />

<source-file
src="src/android/BackgroundExt.java"
src="src/android/BackgroundModeExt.java"
target-dir="src/de/appplant/cordova/plugin/background" />

<source-file
Expand Down
49 changes: 19 additions & 30 deletions src/android/BackgroundMode.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Copyright 2013 Sebastián Katzer
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package de.appplant.cordova.plugin.background;
Expand All @@ -35,7 +35,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;

import static android.content.Context.BIND_AUTO_CREATE;
import static de.appplant.cordova.plugin.background.BackgroundExt.clearKeyguardFlags;
import static de.appplant.cordova.plugin.background.BackgroundModeExt.clearKeyguardFlags;

public class BackgroundMode extends CordovaPlugin {

Expand Down Expand Up @@ -104,17 +104,6 @@ public boolean execute (String action, JSONArray args,
case "disable":
disableMode();
break;
case "webviewoptimizations":
case "batteryoptimizations":
case "appstart":
case "background":
case "foreground":
case "tasklist":
case "dimmed":
case "wakeup":
case "unlock":
BackgroundExt.execute(this, action, args, callback);
break;
default:
validAction = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Copyright 2013 Sebastián Katzer
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package de.appplant.cordova.plugin.background;
Expand All @@ -37,9 +37,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.view.View;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
Expand All @@ -66,7 +64,7 @@ Licensed to the Apache Software Foundation (ASF) under one
* Implements extended functions around the main purpose
* of infinite execution in the background.
*/
class BackgroundExt {
public class BackgroundModeExt extends CordovaPlugin {

// List of intents for various manufactures to adjust the power saver mode.
private static final List<Intent> APPSTART_INTENTS = Arrays.asList(
Expand All @@ -91,57 +89,31 @@ class BackgroundExt {
new Intent().setComponent(new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity"))
);

// Reference to the cordova interface passed by the plugin
private final CordovaInterface cordova;

// Reference to the cordova web view passed by the plugin
private final CordovaWebView webView;

// To keep the device awake
private PowerManager.WakeLock wakeLock;

/**
* Initialize the extension to perform non-background related tasks.
*
* @param plugin The cordova plugin.
*/
private BackgroundExt (CordovaPlugin plugin)
{
this.cordova = plugin.cordova;
this.webView = plugin.webView;
}

/**
* Executes the request within a thread.
*
* @param action The action to execute.
* @param args The exec() arguments.
* @param callback The callback context used when
* calling back into JavaScript.
*/
static void execute (CordovaPlugin plugin, String action, JSONArray args,
CallbackContext callback)
{
plugin.cordova.getThreadPool().execute(() -> new BackgroundExt(plugin).execute(action, args, callback));
}

/**
* Executes the request.
*
* @param action The action to execute.
* @param args The exec() arguments.
* @param callback The callback context used when
* calling back into JavaScript.
*
* @return Returning false results in a "MethodNotFound" error.
*/
private void execute (String action, JSONArray args,
CallbackContext callback)
@Override
public boolean execute (String action, JSONArray args,
CallbackContext callback)
{
boolean validAction = true;

switch (action)
{
case "batteryoptimizations":
case "battery":
disableBatteryOptimizations();
break;
case "webviewoptimizations":
case "webview":
disableWebViewOptimizations();
break;
case "appstart":
Expand All @@ -166,7 +138,17 @@ private void execute (String action, JSONArray args,
wakeup();
unlock();
break;
default:
validAction = false;
}

if (validAction) {
callback.success();
} else {
callback.error("Invalid action: " + action);
}

return validAction;
}

/**
Expand Down
36 changes: 18 additions & 18 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Copyright 2013 Sebastián Katzer
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package de.appplant.cordova.plugin.background;
Expand Down
30 changes: 15 additions & 15 deletions src/browser/BackgroundModeProxy.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
Copyright 2013-2017 appPlant GmbH
Copyright 2013 Sebastián Katzer
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

/**
Expand Down
36 changes: 18 additions & 18 deletions src/windows/BackgroundModeProxy.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Copyright 2013 Sebastián Katzer
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

var plugin = cordova.plugins.backgroundMode;
Expand Down
Loading

0 comments on commit 2c4df33

Please sign in to comment.