package com.backfire.doudizhu.common;

import android.content.Context;
import android.os.PowerManager;

import com.backfire.utils.Log;

public class WakeLock
{
	private static final String TAG = "WakeLock";
	private static PowerManager.WakeLock sCpuWakeLock = null;
	// level = -1 (auto)
	// level = 0  (dim)
	// level = 1  (bright)
	public static void acquireCpuWakeLock(Context context,int level)
	{
		//if (sCpuWakeLock != null) { return; }
		int flag;
		switch(level)
		{
		case 0:
			flag = PowerManager.SCREEN_DIM_WAKE_LOCK;
			break;
		case 1:
			flag = PowerManager.SCREEN_BRIGHT_WAKE_LOCK;
			break;
/*			
		case 2:
			flag = PowerManager.FULL_WAKE_LOCK;
			break;
*/			
		default:
			flag = PowerManager.PARTIAL_WAKE_LOCK;
			break;
		}
		releaseCpuLock();
		
		Log.v("acpWakeLock", "Lock_" + Integer.toString(level));
		
		PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		sCpuWakeLock = pm.newWakeLock(flag | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG);
		sCpuWakeLock.setReferenceCounted(false);
		sCpuWakeLock.acquire();
	}

	public static void releaseCpuLock()
	{
		if (sCpuWakeLock != null)
		{
			Log.v("acpWakeLock", "Release");
			sCpuWakeLock.release();
			sCpuWakeLock = null;
		}
	}
}
