LightManager.java

/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.explonoid.effects;

import com.nokia.mid.ui.DeviceControl;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Responsible for the background light related
 * visual effects such as screen dimming and the pulse effect.
 * 
 * For usage, see the class Game.
 * 
 * @see com.nokia.example.explonoid.game.Game
 */
public class LightManager {

    private static final int NORMAL_LIGHT = 60;
    private static final int BRIGHT_LIGHT = 100;
    private static final int PULSE_DURATION = 60;
    private static Timer flasher = new Timer();
    private static Timer avoider;

    public static void avoidDimming() {
        if (avoider == null) {
            try {
                avoider = new Timer();
                avoider.schedule(new TimerTask() {

                    public void run() {
                        DeviceControl.setLights(0, NORMAL_LIGHT);
                    }
                }, 0, 5000);
            }
            catch (Exception e) {
            }
        }
    }

    public static void allowDimming() {
        if (avoider != null) {
            avoider.cancel();
            avoider = null;
        }
    }

    public static void pulse(int repeats) {
        try {
            flasher = new Timer();
            for (int i = 0; i < repeats; i++) {
                flasher.schedule(new TimerTask() {

                    public void run() {
                        DeviceControl.setLights(0, BRIGHT_LIGHT);
                    }
                }, 2 * i * PULSE_DURATION);
                flasher.schedule(new TimerTask() {

                    public void run() {
                        DeviceControl.setLights(0, NORMAL_LIGHT);
                    }
                }, PULSE_DURATION + 2 * i * PULSE_DURATION);
            }
        }
        catch (Exception e) {
        }
    }
}