Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. package com.android.server.status.widget;
  2.  
  3. import com.android.internal.R;
  4. import com.android.server.status.widget.PowerButton;
  5.  
  6. import android.content.ContentResolver;
  7. import android.content.Context;
  8. import android.content.SharedPreferences;
  9. import android.os.IPowerManager;
  10. import android.os.RemoteException;
  11. import android.os.ServiceManager;
  12. import android.provider.Settings;
  13. import android.util.Log;
  14.  
  15. public class BrightnessButton extends PowerButton {
  16.  
  17. Context mContext;
  18.  
  19. /**
  20. * Minimum and maximum brightnesses. Don't go to 0 since that makes the
  21. * display unusable
  22. */
  23. private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
  24. private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;
  25. private static int DEFAULT_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.4f);
  26.  
  27. private static int LOW_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.25f);
  28. private static int MID_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.5f);
  29. private static int HIGH_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.75f);
  30.  
  31. private static final int AUTO_BACKLIGHT = -1;
  32.  
  33. private static final int MODE_AUTO_MIN_DEF_MAX=0;
  34. private static final int MODE_AUTO_MIN_LOW_MID_HIGH_MAX=1;
  35. private static final int MODE_AUTO_LOW_MAX=2;
  36. private static final int MODE_MIN_MAX=3;
  37.  
  38. private static final int DEFAULT_SETTING = 0;
  39.  
  40. private static Boolean supportsAutomaticMode=null;
  41.  
  42. static BrightnessButton ownButton=null;
  43.  
  44. private static int currentMode;
  45.  
  46. public static int getMinBacklight(Context context) {
  47. return MINIMUM_BACKLIGHT;
  48. }
  49.  
  50. private static boolean isAutomaticModeSupported(Context context) {
  51. if (supportsAutomaticMode == null) {
  52. if (context
  53. .getResources()
  54. .getBoolean(
  55. com.android.internal.R.bool.config_automatic_brightness_available)) {
  56. supportsAutomaticMode=true;
  57. } else {
  58. supportsAutomaticMode=false;
  59. }
  60. }
  61. return supportsAutomaticMode;
  62. }
  63.  
  64. /**
  65. * Gets state of brightness mode.
  66. *
  67. * @param context
  68. * @return true if auto brightness is on.
  69. */
  70. private static boolean isBrightnessSetToAutomatic(Context context) {
  71. try {
  72. IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
  73. .getService("power"));
  74. if (power != null) {
  75. int brightnessMode = Settings.System.getInt(context
  76. .getContentResolver(),
  77. Settings.System.SCREEN_BRIGHTNESS_MODE);
  78. return brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
  79. }
  80. } catch (Exception e) {
  81. Log.d("PowerWidget", "getBrightnessMode: " + e);
  82. }
  83. return false;
  84. }
  85.  
  86. private int getNextBrightnessValue(Context context) {
  87. int brightness = Settings.System.getInt(context.getContentResolver(),
  88. Settings.System.SCREEN_BRIGHTNESS,0);
  89.  
  90. if (isAutomaticModeSupported(context) && isBrightnessSetToAutomatic(context)) {
  91. if (currentMode == MODE_AUTO_LOW_MAX) {
  92. return LOW_BACKLIGHT;
  93. } else {
  94. return getMinBacklight(context);
  95. }
  96. } else if (brightness < LOW_BACKLIGHT) {
  97. if (currentMode == MODE_AUTO_LOW_MAX) {
  98. return LOW_BACKLIGHT;
  99. } else if (currentMode == MODE_MIN_MAX) {
  100. return MAXIMUM_BACKLIGHT;
  101. } else {
  102. return DEFAULT_BACKLIGHT;
  103. }
  104. } else if (brightness < DEFAULT_BACKLIGHT) {
  105. if (currentMode == MODE_AUTO_MIN_DEF_MAX) {
  106. return DEFAULT_BACKLIGHT;
  107. } else if (currentMode == MODE_AUTO_LOW_MAX || currentMode == MODE_MIN_MAX) {
  108. return MAXIMUM_BACKLIGHT;
  109. } else {
  110. return MID_BACKLIGHT;
  111. }
  112. } else if (brightness < MID_BACKLIGHT) {
  113. if (currentMode == MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
  114. return MID_BACKLIGHT;
  115. } else {
  116. return MAXIMUM_BACKLIGHT;
  117. }
  118. } else if (brightness < HIGH_BACKLIGHT) {
  119. if (currentMode == MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
  120. return HIGH_BACKLIGHT;
  121. } else {
  122. return MAXIMUM_BACKLIGHT;
  123. }
  124. } else if (brightness < MAXIMUM_BACKLIGHT) {
  125. return MAXIMUM_BACKLIGHT;
  126. } else if (isAutomaticModeSupported(context) && currentMode!=MODE_MIN_MAX) {
  127. return AUTO_BACKLIGHT;
  128. } else if(currentMode == MODE_AUTO_LOW_MAX){
  129. return LOW_BACKLIGHT;
  130. } else {
  131. return getMinBacklight(context);
  132. }
  133. }
  134.  
  135. /**
  136. * Increases or decreases the brightness.
  137. *
  138. * @param context
  139. */
  140. public void toggleState(Context context) {
  141. try {
  142. IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
  143. .getService("power"));
  144. if (power != null) {
  145. int brightness = getNextBrightnessValue(context);
  146. ContentResolver contentResolver = context.getContentResolver();
  147. if (brightness == AUTO_BACKLIGHT) {
  148. Settings.System.putInt(contentResolver,
  149. Settings.System.SCREEN_BRIGHTNESS_MODE,
  150. Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  151. } else {
  152. if (isAutomaticModeSupported(context)) {
  153. Settings.System.putInt(contentResolver,
  154. Settings.System.SCREEN_BRIGHTNESS_MODE,
  155. Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  156. }
  157. power.setBacklightBrightness(brightness);
  158. Settings.System.putInt(contentResolver,
  159. Settings.System.SCREEN_BRIGHTNESS, brightness);
  160. }
  161. }
  162. } catch (RemoteException e) {
  163. Log.d("PowerWidget", "toggleBrightness: " + e);
  164. }
  165. }
  166.  
  167. public boolean launchActivity(Context context) {
  168. return false;
  169. }
  170.  
  171. public static BrightnessButton getInstance() {
  172. if (ownButton == null) ownButton = new BrightnessButton();
  173.  
  174. return ownButton;
  175. }
  176.  
  177. @Override
  178. void initButton(int position) {
  179. }
  180.  
  181. @Override
  182. public void updateState(Context context) {
  183. mContext = context;
  184. boolean useCustomExp = Settings.System.getInt(mContext.getContentResolver(),
  185. Settings.System.NOTIF_EXPANDED_BAR_CUSTOM, 0) == 1;
  186.  
  187. currentMode = Settings.System.getInt(context.getContentResolver(),
  188. Settings.System.EXPANDED_BRIGHTNESS_MODE, DEFAULT_SETTING);
  189.  
  190. if (isBrightnessSetToAutomatic(context)) {
  191. if (useCustomExp) {
  192. currentIcon = R.drawable.stat_brightness_auto_cust;
  193. } else {
  194. currentIcon = R.drawable.stat_brightness_auto;
  195. }
  196. currentState = PowerButton.STATE_ENABLED;
  197. } else if (getBrightnessState(context) == PowerButton.STATE_ENABLED) {
  198. if (useCustomExp) {
  199. currentIcon = R.drawable.stat_brightness_on_cust;
  200. } else {
  201. currentIcon = R.drawable.stat_brightness_on;
  202. }
  203. currentState = PowerButton.STATE_ENABLED;
  204. } else if (getBrightnessState(context) == PowerButton.STATE_TURNING_ON) {
  205. if (useCustomExp) {
  206. currentIcon = R.drawable.stat_brightness_on_cust;
  207. } else {
  208. currentIcon = R.drawable.stat_brightness_on;
  209. }
  210. currentState = PowerButton.STATE_INTERMEDIATE;
  211. } else if (getBrightnessState(context) == PowerButton.STATE_TURNING_OFF) {
  212. if (useCustomExp) {
  213. currentIcon = R.drawable.stat_brightness_off_cust;
  214. } else {
  215. currentIcon = R.drawable.stat_brightness_off;
  216. }
  217. currentState = PowerButton.STATE_INTERMEDIATE;
  218. } else {
  219. if (useCustomExp) {
  220. currentIcon = R.drawable.stat_brightness_off_cust;
  221. } else {
  222. currentIcon = R.drawable.stat_brightness_off;
  223. }
  224. currentState = PowerButton.STATE_DISABLED;
  225. }
  226. }
  227.  
  228. private int getBrightnessState(Context context) {
  229. int brightness = Settings.System.getInt(context.getContentResolver(),
  230. Settings.System.SCREEN_BRIGHTNESS,0);
  231.  
  232. if (brightness < LOW_BACKLIGHT) {
  233. return PowerButton.STATE_DISABLED;
  234. } else if (brightness < DEFAULT_BACKLIGHT) {
  235. return PowerButton.STATE_DISABLED;
  236. } else if (brightness < MID_BACKLIGHT) {
  237. if (currentMode == MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
  238. return PowerButton.STATE_DISABLED;
  239. } else {
  240. return PowerButton.STATE_TURNING_OFF;
  241. }
  242. } else if (brightness < HIGH_BACKLIGHT) {
  243. if (currentMode == MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
  244. return PowerButton.STATE_TURNING_OFF;
  245. } else {
  246. return PowerButton.STATE_TURNING_ON;
  247. }
  248. } else if (brightness < MAXIMUM_BACKLIGHT) {
  249. return PowerButton.STATE_TURNING_ON;
  250. } else {
  251. return PowerButton.STATE_ENABLED;
  252. }
  253. }
  254. }
Add Comment
Please, Sign In to add comment