1

I am trying to control the screen timeout from my cordova app. The app plays videos and while the app is playing a video I want to turn off the screen timeout. While a video is paused or they are doing something else I want to turn it back on. If I set the KeepScreenOn flag in OnCreate it works fine however if I call it from my plugin nothing changes. I have tried both

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

and

this.webView.setKeepScreenOn(true); 

Here is my plugin code.

package com.Kidobi.plugins;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.view.WindowManager;

public class KeepScreenOn extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    System.out.println("Im in the plugin");
    if (action.equals("KeepScreenOn")) {
        System.out.println("KeepScreenOn");
        this.webView.setKeepScreenOn(true);
        //cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //callbackContext.success(action);
        return true;
    } else if (action.equals("CancelKeepScreenOn")){
        System.out.println("CancelKeepScreenOn");
        this.webView.setKeepScreenOn(false);
           //cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //callbackContext.success(action);
        return true;
    } else {
        System.out.println("UNKNOWN");
        callbackContext.error("unknown action" + action);
        return false;
    }
}

}
Leo
  • 1,495
  • 23
  • 41

1 Answers1

5

I have added a plugin to gihub using this code. to install it using the cli sudo cordova plugins add https://github.com/leohenning/KeepScreenOnPlugin this has been tested for cordova 3.1

It has to do with the thread. Need to be running on the UI thread. http://cordova.apache.org/docs/en/2.8.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

see the section on threading

so the code that works looks like

package com.MyPlug.plugins;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.view.WindowManager;

public class KeepScreenOn extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        System.out.println("Im in the plugin");
        if (action.equalsIgnoreCase("KeepScreenOn")) {
            System.out.println("Start KeepScreenOn");
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    System.out.println("Screen will be kept on. KeepScreenOn");
                }
            });
            //cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //callbackContext.success(action);
            return true;
        } else if (action.equalsIgnoreCase("CancelKeepScreenOn")){
            System.out.println("CancelKeepScreenOn");
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    System.out.println("Screen will not be kept on. Cancel KeepScreenOn");
                }
            });
            //cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            //callbackContext.success(action);
            return true;
        } else {
            System.out.println("UNKNOWN");
            callbackContext.error("unknown action" + action);
            return false;
        }
    }

}

then from the javascript I call

cordova.exec(null, null, "KeepScreenOn", "CancelKeepScreenOn", [""]);

the config.xml

<feature name="KeepScreenOn">
  <param name="android-package" value="com.MyPlug.plugins.KeepScreenOn"/>
</feature>

with thanks to this problem Android & PhoneGap -- Error calling method on NPObject

Community
  • 1
  • 1
Leo
  • 1,495
  • 23
  • 41