Android Open Source - dttv-android Control Lightness






From Project

Back to project page dttv-android.

License

The source code is released under:

GNU General Public License

If you think the Android project dttv-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package dttv.app.utils;
/*from   w  ww.ja va  2 s.c  om*/
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.view.WindowManager;

public class ControlLightness {
  
  private static ControlLightness controlLightness;
  
  private ControlLightness() {
    // TODO Auto-generated constructor stub
  }
  
  public static ControlLightness getInstance(){
    if(controlLightness==null)
      controlLightness = new ControlLightness();
    return controlLightness;
  }
  
  /**
   * judge if auto open brightness
   * @param context
   * @return
   */
  public boolean isAutoBrightness(Context context){
    boolean automicBrightness = false;
    ContentResolver ctr = context.getContentResolver();
    try {
      automicBrightness = Settings.System.getInt(ctr, 
      Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
    } catch (SettingNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return automicBrightness;
  }
  
  /**
   * 
   * @param context
   * @param value
   */
  public void setLightness(Activity act,int value){
    System.putInt(act.getContentResolver(), System.SCREEN_BRIGHTNESS, value);
    try {
      WindowManager.LayoutParams lp = act.getWindow().getAttributes();
      lp.screenBrightness = (value < 0 ? 1 : value)/255f;
      act.getWindow().setAttributes(lp);
      
      //saveBrightness(act,value);
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
  
  /**
   * get lightness
   * @param cr
   * @return
   */
  public int getLightness(Activity cr){
    return Settings.System.getInt(cr.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,255);
  }
  
  /**
   * get current brightness
   * @param cr
   * @return
   */
  public int getCurrentBrightness(Activity cr){
    int currentNess = Settings.System.getInt(cr.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, 255);
    return currentNess >20 ? currentNess : 20;
  }
  
  /**
   * stop auto change
   * brightness
   * @param cr
   */
  public void stopAutoBrightness(Activity cr) {
    Settings.System.putInt(cr.getContentResolver(),
        System.SCREEN_BRIGHTNESS, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  }
  
  /**
   * start auto change
   * brightness
   * @param activity
   */
  public void startAutoBrightness(Activity activity) {
    Settings.System.putInt(activity.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS_MODE,
        Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  }
  
  /**
   * save brightness value for entire
   * system
   * @param activity
   * @param value
   */
  public void saveBrightness(Activity activity,int value){
    Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
    Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value);
    activity.getContentResolver().notifyChange(uri, null);
  }
  
  /**
   * just this method
   * @param cr
   * @param value
   */
  public void setBrightness(Activity cr,int value){
    if (0 <= value && value <= 255) {
      Settings.System.putInt(cr.getContentResolver(),
          Settings.System.SCREEN_BRIGHTNESS,  value); // 0-255
      value = Settings.System.getInt(cr.getContentResolver(),
          Settings.System.SCREEN_BRIGHTNESS, -1);
      // Cupcake way..... sucks
      WindowManager.LayoutParams lp = cr.getWindow().getAttributes();
      lp.screenBrightness = value;
      cr.getWindow().setAttributes(lp);
    }
  }
}




Java Source Code List

dttv.app.DtPlayer.java
dttv.app.FileBrowserActivity.java
dttv.app.FileShowActivity.java
dttv.app.IndexActivity.java
dttv.app.MainActivity.java
dttv.app.SettingActivity.java
dttv.app.VideoPlayerActivity.java
dttv.app.adapter.FileAdapter.java
dttv.app.compnent.Compnent.java
dttv.app.compnent.MethodPlayerCompnent.java
dttv.app.compnent.PopWindowCompnent.java
dttv.app.impl.ICallBack.java
dttv.app.impl.I_Async.java
dttv.app.impl.I_KeyIntercept.java
dttv.app.impl.I_OnMyKey.java
dttv.app.impl.I_PopWindow.java
dttv.app.model.Item.java
dttv.app.multithread.DataAsyncTask.java
dttv.app.utils.Constant.java
dttv.app.utils.ControlLightness.java
dttv.app.utils.FileUtil.java
dttv.app.utils.Log.java
dttv.app.utils.MultiMediaTypeUtil.java
dttv.app.utils.MusicUtils.java
dttv.app.utils.PlayerUtil.java
dttv.app.utils.SettingUtil.java
dttv.app.utils.TimesUtil.java
dttv.app.utils.VolumeUtil.java
dttv.app.view.SimpleDialog.java
dttv.app.widget.AudioUIFragment.java
dttv.app.widget.EffectWindow.java
dttv.app.widget.FilesUIFragment.java
dttv.app.widget.GlVideoView.java
dttv.app.widget.OnTouchMoveListener.java
dttv.app.widget.SlideTabsFragment.java
dttv.app.widget.VideoUIFragment.java