Package webworks.system.display

Source Code of webworks.system.display.SystemDisplayFeature$BacklightTask

package webworks.system.display;

import net.rim.device.api.script.Scriptable;
import net.rim.device.api.system.Display;

import java.lang.Boolean;
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.system.Backlight;

public final class SystemDisplayFeature extends Scriptable
{
  private String  PROPERTY_ALLOW_SLEEP = "allowSleep";
 
  private int MAX_BACKLIGHT_TIMEOUT = 255;    //Max 255 seconds allowed by Backlight.setTimeout class

  private Timer t;
  private boolean isTimerActive;

 
  /**
   *  Constructor
   */ 
  public SystemDisplayFeature()
  {
    this.t = new Timer();
    isTimerActive = false;
  }

 
  /**
   *  getField - Called when WebWorks application tries to get the value of a property
   */ 
  public Object getField(String name) throws Exception
  {
    try
    {
      if (name.equals(PROPERTY_ALLOW_SLEEP))
      {
        // WebWorks application has requested to read the property 'allowSleep' 
        // return true if and only if the BacklightTask is NOT currently running.
       
        return new Boolean(isTimerActive == false);    //must return an object, not a value.
      }
    }
    catch (Exception e)
    {
      System.out.println("Exception in SystemDisplayFeature.getField: " + e.toString());
    }
   
    return super.getField(name);
  }


  /**
   *  putField - Called when WebWorks application tries to change the value of a property
   */ 
  public boolean putField(String name, Object value) throws Exception
  {
    try
    {
     
      if (name.equals(PROPERTY_ALLOW_SLEEP))
      {
        boolean allowSleep = (value.toString() == "true");
         
        if (allowSleep)
        {
          // WebWorks application has requested to set the property 'allowSleep' to 'false'
          // ==> Stop the timer that is currently calling Backlight.enable()
         
          if (isTimerActive == true)
          {
            isTimerActive = false;
            t.cancel();
            Backlight.setTimeout(Backlight.getTimeoutDefault())//restore the backlight timeout default value
          }
        }
        else
        {
          // WebWorks application has requested to set the property 'allowSleep' to 'true'
          // ==> Start a timer that will call Backlight.enable() every 255 seconds.
         
          if (isTimerActive == false)
          {
            isTimerActive = true;
            BacklightTask task = new BacklightTask();
            t.scheduleAtFixedRate(task, 0, MAX_BACKLIGHT_TIMEOUT * 1000);    //Start the timer task immediately, and have it run every 255 seconds.
          }
        }

        return true;
      }
    }
    catch (Exception e)
    {
      System.out.println("Exception in SystemDisplayFeature.putField: " + e.toString());
    }
   
    return false;
  }

 
 
  //The purpose of this timer task is to reset the display backlight every 255 seconds.
  protected class BacklightTask extends TimerTask {
 
    /**
     * Execute the Timer's Task
     */
    public void run()
    {
      try
      {
        //http://supportforums.blackberry.com/t5/Java-Development/Enable-the-backlight-and-prevent-the-BlackBerry-device-screen/ta-p/444706

        //The security timer cannot be directly reset programmatically.  Instead, this action is performed via the Backlight.enable() API.
        //If the application is not permitted to modify the security timer, then Backlight.enable() will silently skip this step.
        //No indication is given to the calling application as to whether a security timer reset was performed.

        //Two conditions must be satisfied for an application to be able to reset the security timer:
        
        //  1) If an IT Policy is present, the ALLOW_RESETTING_OF_IDLE_TIMER policy in the Security group must be set to True. 
        //    (This permits Blackberry Enterprise Server administrators to ban security timer resets entirely.) 
        //    This policy defaults to False.
        
        //  2) The calling application must hold the PERMISSION_IDLE_TIMER permission (also called the �Security Timer Reset� permission). 
        //    This permission defaults to Deny.
       
        if (( Display.getProperties() & Display.DISPLAY_PROPERTY_REQUIRES_BACKLIGHT) != 0 )
        {
          Backlight.enable( true, MAX_BACKLIGHT_TIMEOUT );
        }
       
      }
      catch (Exception e)
      {
        System.out.println("Exception in BacklightTask.run: " + e.toString());
//        EventLogger.logEvent(EVENT_LOG_GUID, "Exception in BacklightTask.run: " + e.toString(), EventLogger.ERROR);
      }
    }
 
  } 

 
}
TOP

Related Classes of webworks.system.display.SystemDisplayFeature$BacklightTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.