Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/****************************************************************************
 * Copyright 2009 kraigs.android@gmail.com
 * Copyright 2012 akaiosorani(akaiosorani@gmail.com)
 * Licensed 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. 
 ****************************************************************************/

import android.content.ContentResolver;
import android.provider.Settings;

import android.view.Window;
import android.view.WindowManager;

public class Main {
    public static void setSystemBrightness(ContentResolver resolver, Window window, int brightnessUnits) {
        setSystemBrightness(resolver, brightnessUnits);
        setActivityBrightness(window, brightnessUnits);
    }

    /**
     * Sets the phone's global brightness level. This does not change the screen's
     * brightness immediately. Valid brightnesses range from 0 to 255.
     * 
     * @param resolver
     *          The ContentResolver.
     * @param brightnessUnits
     *          An integer between 0 and 255.
     */
    static void setSystemBrightness(ContentResolver resolver, int brightnessUnits) {
        // Change the system brightness setting. This doesn't change the
        // screen brightness immediately. (Scale 0 - 255).
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightnessUnits);
    }

    /**
     * Sets the screen brightness for this activity. The screen brightness will
     * change immediately. As soon as the activity terminates, the brightness will
     * return to the system brightness. Valid brightnesses range from 0 to 255.
     * 
     * @param window
     *          The activity window.
     * @param brightnessUnits
     *          An integer between 0 and 255.
     */
    static void setActivityBrightness(Window window, int brightnessUnits) {
        // Set the brightness of the current window. This takes effect immediately.
        // When the window is closed, the new system brightness is used.
        // (Scale 0.0 - 1.0).
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightnessUnits / 255.0f;
        window.setAttributes(lp);
    }
}