/packages/SystemUI/src/com/android/systemui/statusbar/phone/BarTransitions.java

https://github.com/aizuzi/platform_frameworks_base · Java · 225 lines · 177 code · 29 blank · 19 comment · 38 complexity · a7b7835b03c54e2f4165aa418c8b9acd MD5 · raw file

  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.systemui.statusbar.phone;
  17. import android.animation.TimeInterpolator;
  18. import android.app.ActivityManager;
  19. import android.content.Context;
  20. import android.content.res.Resources;
  21. import android.graphics.Canvas;
  22. import android.graphics.Color;
  23. import android.graphics.ColorFilter;
  24. import android.graphics.PixelFormat;
  25. import android.graphics.Rect;
  26. import android.graphics.drawable.Drawable;
  27. import android.os.SystemClock;
  28. import android.util.Log;
  29. import android.view.View;
  30. import android.view.animation.LinearInterpolator;
  31. import com.android.systemui.R;
  32. public class BarTransitions {
  33. private static final boolean DEBUG = false;
  34. private static final boolean DEBUG_COLORS = false;
  35. public static final boolean HIGH_END = ActivityManager.isHighEndGfx();
  36. public static final int MODE_OPAQUE = 0;
  37. public static final int MODE_SEMI_TRANSPARENT = 1;
  38. public static final int MODE_TRANSLUCENT = 2;
  39. public static final int MODE_LIGHTS_OUT = 3;
  40. public static final int LIGHTS_IN_DURATION = 250;
  41. public static final int LIGHTS_OUT_DURATION = 750;
  42. public static final int BACKGROUND_DURATION = 200;
  43. private final String mTag;
  44. private final View mView;
  45. private final BarBackgroundDrawable mBarBackground;
  46. private int mMode;
  47. public BarTransitions(View view, int gradientResourceId) {
  48. mTag = "BarTransitions." + view.getClass().getSimpleName();
  49. mView = view;
  50. mBarBackground = new BarBackgroundDrawable(mView.getContext(), gradientResourceId);
  51. if (HIGH_END) {
  52. mView.setBackground(mBarBackground);
  53. }
  54. }
  55. public int getMode() {
  56. return mMode;
  57. }
  58. public void transitionTo(int mode, boolean animate) {
  59. // low-end devices do not support translucent modes, fallback to opaque
  60. if (!HIGH_END && (mode == MODE_SEMI_TRANSPARENT || mode == MODE_TRANSLUCENT)) {
  61. mode = MODE_OPAQUE;
  62. }
  63. if (mMode == mode) return;
  64. int oldMode = mMode;
  65. mMode = mode;
  66. if (DEBUG) Log.d(mTag, String.format("%s -> %s animate=%s",
  67. modeToString(oldMode), modeToString(mode), animate));
  68. onTransition(oldMode, mMode, animate);
  69. }
  70. protected void onTransition(int oldMode, int newMode, boolean animate) {
  71. if (HIGH_END) {
  72. applyModeBackground(oldMode, newMode, animate);
  73. }
  74. }
  75. protected void applyModeBackground(int oldMode, int newMode, boolean animate) {
  76. if (DEBUG) Log.d(mTag, String.format("applyModeBackground oldMode=%s newMode=%s animate=%s",
  77. modeToString(oldMode), modeToString(newMode), animate));
  78. mBarBackground.applyModeBackground(oldMode, newMode, animate);
  79. }
  80. public static String modeToString(int mode) {
  81. if (mode == MODE_OPAQUE) return "MODE_OPAQUE";
  82. if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT";
  83. if (mode == MODE_TRANSLUCENT) return "MODE_TRANSLUCENT";
  84. if (mode == MODE_LIGHTS_OUT) return "MODE_LIGHTS_OUT";
  85. throw new IllegalArgumentException("Unknown mode " + mode);
  86. }
  87. public void finishAnimations() {
  88. mBarBackground.finishAnimation();
  89. }
  90. public void setContentVisible(boolean visible) {
  91. // for subclasses
  92. }
  93. private static class BarBackgroundDrawable extends Drawable {
  94. private final int mOpaque;
  95. private final int mSemiTransparent;
  96. private final Drawable mGradient;
  97. private final TimeInterpolator mInterpolator;
  98. private int mMode = -1;
  99. private boolean mAnimating;
  100. private long mStartTime;
  101. private long mEndTime;
  102. private int mGradientAlpha;
  103. private int mColor;
  104. private int mGradientAlphaStart;
  105. private int mColorStart;
  106. public BarBackgroundDrawable(Context context, int gradientResourceId) {
  107. final Resources res = context.getResources();
  108. if (DEBUG_COLORS) {
  109. mOpaque = 0xff0000ff;
  110. mSemiTransparent = 0x7f0000ff;
  111. } else {
  112. mOpaque = res.getColor(R.color.system_bar_background_opaque);
  113. mSemiTransparent = res.getColor(R.color.system_bar_background_semi_transparent);
  114. }
  115. mGradient = res.getDrawable(gradientResourceId);
  116. mInterpolator = new LinearInterpolator();
  117. }
  118. @Override
  119. public void setAlpha(int alpha) {
  120. // noop
  121. }
  122. @Override
  123. public void setColorFilter(ColorFilter cf) {
  124. // noop
  125. }
  126. @Override
  127. protected void onBoundsChange(Rect bounds) {
  128. super.onBoundsChange(bounds);
  129. mGradient.setBounds(bounds);
  130. }
  131. public void applyModeBackground(int oldMode, int newMode, boolean animate) {
  132. if (mMode == newMode) return;
  133. mMode = newMode;
  134. mAnimating = animate;
  135. if (animate) {
  136. long now = SystemClock.elapsedRealtime();
  137. mStartTime = now;
  138. mEndTime = now + BACKGROUND_DURATION;
  139. mGradientAlphaStart = mGradientAlpha;
  140. mColorStart = mColor;
  141. }
  142. invalidateSelf();
  143. }
  144. @Override
  145. public int getOpacity() {
  146. return PixelFormat.TRANSLUCENT;
  147. }
  148. public void finishAnimation() {
  149. if (mAnimating) {
  150. mAnimating = false;
  151. invalidateSelf();
  152. }
  153. }
  154. @Override
  155. public void draw(Canvas canvas) {
  156. int targetGradientAlpha = 0, targetColor = 0;
  157. if (mMode == MODE_TRANSLUCENT) {
  158. targetGradientAlpha = 0xff;
  159. } else if (mMode == MODE_SEMI_TRANSPARENT) {
  160. targetColor = mSemiTransparent;
  161. } else {
  162. targetColor = mOpaque;
  163. }
  164. if (!mAnimating) {
  165. mColor = targetColor;
  166. mGradientAlpha = targetGradientAlpha;
  167. } else {
  168. final long now = SystemClock.elapsedRealtime();
  169. if (now >= mEndTime) {
  170. mAnimating = false;
  171. mColor = targetColor;
  172. mGradientAlpha = targetGradientAlpha;
  173. } else {
  174. final float t = (now - mStartTime) / (float)(mEndTime - mStartTime);
  175. final float v = Math.max(0, Math.min(mInterpolator.getInterpolation(t), 1));
  176. mGradientAlpha = (int)(v * targetGradientAlpha + mGradientAlphaStart * (1 - v));
  177. mColor = Color.argb(
  178. (int)(v * Color.alpha(targetColor) + Color.alpha(mColorStart) * (1 - v)),
  179. (int)(v * Color.red(targetColor) + Color.red(mColorStart) * (1 - v)),
  180. (int)(v * Color.green(targetColor) + Color.green(mColorStart) * (1 - v)),
  181. (int)(v * Color.blue(targetColor) + Color.blue(mColorStart) * (1 - v)));
  182. }
  183. }
  184. if (mGradientAlpha > 0) {
  185. mGradient.setAlpha(mGradientAlpha);
  186. mGradient.draw(canvas);
  187. }
  188. if (Color.alpha(mColor) > 0) {
  189. canvas.drawColor(mColor);
  190. }
  191. if (mAnimating) {
  192. invalidateSelf(); // keep going
  193. }
  194. }
  195. }
  196. }