Merge branch 'pluginService' of lightning-dev/lightning_launcher into master

This commit is contained in:
Pierrot 2019-02-08 19:09:50 +01:00 committed by Gogs
commit f431ac6059
4 changed files with 143 additions and 1 deletions

View file

@ -27,6 +27,11 @@
<!-- Tasker -->
<uses-permission android:name="net.dinglisch.android.tasker.PERMISSION_RUN_TASKS"/>
<permission
android:name="net.pierrox.lightning_launcher.USE_PLUGIN_SERVICE"
android:protectionLevel="dangerous"/>
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
@ -351,6 +356,10 @@
</intent-filter>
</activity>
<service android:name="net.pierrox.lightning_launcher.util.PluginService"
android:permission="net.pierrox.lightning_launcher.USE_PLUGIN_SERVICE"
android:exported="true"/>
</application>
</manifest>

View file

@ -0,0 +1,25 @@
package net.pierrox.lightning_launcher.plugin;
interface IPluginService {
/**
* @returns id of the newly created script, or -1 if the name/path combination already exists
*/
int createScript(String code, String name, String path, int flags);
/**
* @returns id of the newly created or overwritten script
*/
int createOrOverwriteScript(String code, String name, String path, int flags);
/**
* runs the script with the given id and optional data
*/
void runScript(int id, String data);
/**
* runs the given code. Code must end with a return statement
*/
String runCode(String code);
}

View file

@ -0,0 +1,108 @@
package net.pierrox.lightning_launcher.util;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.faendir.rhino_android.RhinoAndroidHelper;
import net.pierrox.lightning_launcher.LLApp;
import net.pierrox.lightning_launcher.engine.LightningEngine;
import net.pierrox.lightning_launcher.engine.Screen;
import net.pierrox.lightning_launcher.plugin.IPluginService;
import net.pierrox.lightning_launcher.script.Script;
import net.pierrox.lightning_launcher.script.ScriptExecutor;
import net.pierrox.lightning_launcher.script.ScriptManager;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import static net.pierrox.lightning_launcher.script.ScriptExecutor.PROPERTY_EVENT_SCREEN;
/**
* @author lukas
* @since 05.02.19
*/
public class PluginService extends Service {
private LightningEngine engine;
private final IPluginService service = new IPluginService.Stub() {
@Override
public int createScript(String code, String name, String path, int flags) {
ScriptManager scriptManager = engine.getScriptManager();
path = ScriptManager.sanitizeRelativePath(path);
if (scriptManager.getOrLoadScript(path, name) == null) {
Script script = scriptManager.createScriptForFile(name, path);
script.setSourceText(code);
script.flags = flags;
scriptManager.saveScript(script);
return script.id;
}
return Script.NO_ID;
}
@Override
public int createOrOverwriteScript(String code, String name, String path, int flags) {
ScriptManager scriptManager = engine.getScriptManager();
path = ScriptManager.sanitizeRelativePath(path);
Script script = scriptManager.getOrLoadScript(path, name);
if (script == null) {
script = scriptManager.createScriptForFile(name, path);
}
script.setSourceText(code);
script.flags = flags;
scriptManager.saveScript(script);
return script.id;
}
@Override
public void runScript(int id, String data) {
Screen screen = LLApp.get().getActiveScreen();
if (screen == null) {
LLApp.get().getScreen(Screen.Identity.BACKGROUND);
}
engine.getScriptExecutor().runScript(screen, id, "PLUGIN", data);
}
@Override
public String runCode(String code) {
Screen screen = LLApp.get().getActiveScreen();
if (screen == null) {
LLApp.get().getScreen(Screen.Identity.BACKGROUND);
}
ScriptExecutor executor = engine.getScriptExecutor();
if (executor.canRunScriptGlobally()) {
Scriptable scope = executor.prepareScriptScope();
org.mozilla.javascript.Context cx = RhinoAndroidHelper.prepareContext();
ScriptableObject.putProperty(scope, PROPERTY_EVENT_SCREEN, screen);
ScriptableObject.putProperty(scope, "ev_se", "DIRECT_PLUGIN");
ScriptableObject.putProperty(scope, "ev_d", null);
ScriptableObject.putProperty(scope, "ev_t", System.currentTimeMillis());
ScriptableObject.putProperty(scope, "ev_il", null);
ScriptableObject.putProperty(scope, "ev_iv", null);
try {
String script = "javascript:(function() {var _event = createEvent(ev_sc, ev_se, ev_d, ev_t, ev_il, ev_iv); var getEvent = function() { return _event;};\n" + code + "\n})();";
cx.setOptimizationLevel(-1);
org.mozilla.javascript.Script compiledScript = cx.compileString(script, null, 0, null);
if (compiledScript != null) {
return String.valueOf(compiledScript.exec(cx, scope));
}
} catch (RhinoException e) {
e.printStackTrace();
} finally {
// Exit from the context.
org.mozilla.javascript.Context.exit();
}
}
return null;
}
};
@Override
public void onCreate() {
engine = LLApp.get().getAppEngine();
}
@Override
public IBinder onBind(Intent intent) {
return service.asBinder();
}
}

View file

@ -752,7 +752,7 @@ public class ScriptExecutor {
return null;
}
private boolean canRunScriptGlobally() {
public boolean canRunScriptGlobally() {
return mEngine.canRunScripts() && !LLApp.get().isFreeVersion();
}