package com.samples.hardware.powermanager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class PowerManagerActivity extends Activity 
		implements OnClickListener{
    
	PowerManager manager;
	WakeLock wakeLock;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button bPartialWL = (Button)findViewById(R.id.bPartialWL);
        final Button bScreenBrightWL = (Button)findViewById(R.id.bScreenBrightWL);
        final Button bScreenDimWL = (Button)findViewById(R.id.bScreenDimWL);
        final Button bFullWL = (Button)findViewById(R.id.bFullWL);
        
        bPartialWL.setOnClickListener(this);
        bScreenBrightWL.setOnClickListener(this);
        bScreenDimWL.setOnClickListener(this);
        bFullWL.setOnClickListener(this);
        
        manager = (PowerManager)getSystemService(
        		Context.POWER_SERVICE);
    }

	@Override
	public void onClick(View v) {
		if (wakeLock != null) {
			wakeLock.release();
		}
	    String wlName = "";
	    
		switch (v.getId()) {
		case R.id.bPartialWL:
			wlName = getResources().getString(
					R.string.partial_wake_lock);
			wakeLock = manager.newWakeLock(
					PowerManager.PARTIAL_WAKE_LOCK, wlName);
			break;
		case R.id.bScreenBrightWL:
			wlName = getResources().getString(
					R.string.screen_bright_wake_lock);
			wakeLock = manager.newWakeLock(
					PowerManager.SCREEN_BRIGHT_WAKE_LOCK, wlName);
			break;
		case R.id.bScreenDimWL:
			wlName = getResources().getString(
					R.string.screen_dim_wake_lock);
			wakeLock = manager.newWakeLock(
					PowerManager.SCREEN_DIM_WAKE_LOCK, wlName);		
			break;
		case R.id.bFullWL:
			wlName = getResources().getString(
					R.string.full_wake_lock);
			wakeLock = manager.newWakeLock(
					PowerManager.FULL_WAKE_LOCK, wlName);		
			break;
		}
		
		Toast.makeText(this, wlName + " ON", Toast.LENGTH_LONG).show();
		wakeLock.acquire();
	}
}
