react-native-navigation的迁移库

Presenter.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package com.reactnativenavigation.presentation;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.graphics.drawable.ColorDrawable;
  5. import android.graphics.drawable.Drawable;
  6. import android.graphics.drawable.LayerDrawable;
  7. import android.os.Build;
  8. import android.view.View;
  9. import android.view.ViewGroup.MarginLayoutParams;
  10. import android.view.Window;
  11. import com.reactnativenavigation.parse.NavigationBarOptions;
  12. import com.reactnativenavigation.parse.Options;
  13. import com.reactnativenavigation.parse.OrientationOptions;
  14. import com.reactnativenavigation.parse.StatusBarOptions;
  15. import com.reactnativenavigation.parse.StatusBarOptions.TextColorScheme;
  16. import com.reactnativenavigation.parse.params.Bool;
  17. import com.reactnativenavigation.utils.StatusBarUtils;
  18. import com.reactnativenavigation.viewcontrollers.ParentController;
  19. import com.reactnativenavigation.viewcontrollers.ViewController;
  20. import com.reactnativenavigation.viewcontrollers.navigator.Navigator;
  21. import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
  22. @SuppressWarnings("FieldCanBeLocal")
  23. public class Presenter {
  24. private Activity activity;
  25. private Options defaultOptions;
  26. public Presenter(Activity activity, Options defaultOptions) {
  27. this.activity = activity;
  28. this.defaultOptions = defaultOptions;
  29. }
  30. public void setDefaultOptions(Options defaultOptions) {
  31. this.defaultOptions = defaultOptions;
  32. }
  33. public void mergeOptions(View view, Options options) {
  34. mergeStatusBarOptions(view, options.statusBar);
  35. mergeNavigationBarOptions(options.navigationBar);
  36. }
  37. public void applyOptions(ViewController view, Options options) {
  38. Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
  39. applyOrientation(withDefaultOptions.layout.orientation);
  40. applyViewOptions(view, withDefaultOptions);
  41. applyStatusBarOptions(withDefaultOptions);
  42. applyNavigationBarOptions(withDefaultOptions.navigationBar);
  43. }
  44. public void onViewBroughtToFront(Options options) {
  45. Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
  46. applyStatusBarOptions(withDefaultOptions);
  47. }
  48. private void applyOrientation(OrientationOptions options) {
  49. activity.setRequestedOrientation(options.getValue());
  50. }
  51. private void applyViewOptions(ViewController view, Options options) {
  52. applyBackgroundColor(view, options);
  53. applyTopMargin(view.getView(), options);
  54. }
  55. private void applyTopMargin(View view, Options options) {
  56. if (view.getLayoutParams() instanceof MarginLayoutParams && options.layout.topMargin.hasValue()) {
  57. ((MarginLayoutParams) view.getLayoutParams()).topMargin = options.layout.topMargin.get(0);
  58. }
  59. }
  60. private void applyBackgroundColor(ViewController view, Options options) {
  61. if (options.layout.backgroundColor.hasValue()) {
  62. if (view instanceof Navigator) return;
  63. LayerDrawable ld = new LayerDrawable(new Drawable[]{new ColorDrawable(options.layout.backgroundColor.get())});
  64. int top = view.resolveCurrentOptions().statusBar.drawBehind.isTrue() ? 0 : StatusBarUtils.getStatusBarHeight(view.getActivity());
  65. if (!(view instanceof ParentController)) {
  66. MarginLayoutParams lp = (MarginLayoutParams) view.getView().getLayoutParams();
  67. if (lp.topMargin != 0) top = 0;
  68. }
  69. ld.setLayerInset(0, 0, top, 0, 0);
  70. view.getView().setBackground(ld);
  71. }
  72. }
  73. private void applyStatusBarOptions(Options options) {
  74. StatusBarOptions statusBar = options.copy().withDefaultOptions(defaultOptions).statusBar;
  75. setStatusBarBackgroundColor(statusBar);
  76. setTextColorScheme(statusBar.textColorScheme);
  77. setTranslucent(statusBar);
  78. setStatusBarVisible(statusBar.visible);
  79. }
  80. private void setTranslucent(StatusBarOptions options) {
  81. Window window = activity.getWindow();
  82. if (options.translucent.isTrue()) {
  83. window.setFlags(FLAG_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS);
  84. } else if (StatusBarUtils.isTranslucent(window)) {
  85. window.clearFlags(FLAG_TRANSLUCENT_STATUS);
  86. }
  87. }
  88. private void setStatusBarVisible(Bool visible) {
  89. View decorView = activity.getWindow().getDecorView();
  90. int flags = decorView.getSystemUiVisibility();
  91. if (visible.isFalse()) {
  92. flags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
  93. } else {
  94. flags &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
  95. }
  96. decorView.setSystemUiVisibility(flags);
  97. }
  98. private void setStatusBarBackgroundColor(StatusBarOptions statusBar) {
  99. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && statusBar.backgroundColor.canApplyValue()) {
  100. int defaultColor = statusBar.visible.isTrueOrUndefined() ? Color.BLACK : Color.TRANSPARENT;
  101. activity.getWindow().setStatusBarColor(statusBar.backgroundColor.get(defaultColor));
  102. }
  103. }
  104. private void setTextColorScheme(TextColorScheme scheme) {
  105. final View view = activity.getWindow().getDecorView();
  106. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
  107. if (scheme == TextColorScheme.Dark) {
  108. int flags = view.getSystemUiVisibility();
  109. flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  110. view.setSystemUiVisibility(flags);
  111. } else {
  112. clearDarkTextColorScheme(view);
  113. }
  114. }
  115. private void clearDarkTextColorScheme(View view) {
  116. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
  117. int flags = view.getSystemUiVisibility();
  118. flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  119. view.setSystemUiVisibility(flags);
  120. }
  121. private void mergeStatusBarOptions(View view, StatusBarOptions statusBar) {
  122. mergeStatusBarBackgroundColor(statusBar);
  123. mergeTextColorScheme(statusBar.textColorScheme);
  124. mergeTranslucent(statusBar);
  125. mergeStatusBarVisible(view, statusBar.visible, statusBar.drawBehind);
  126. }
  127. private void mergeStatusBarBackgroundColor(StatusBarOptions statusBar) {
  128. if (statusBar.backgroundColor.hasValue() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  129. activity.getWindow().setStatusBarColor(statusBar.backgroundColor.get(Color.BLACK));
  130. }
  131. }
  132. private void mergeTextColorScheme(TextColorScheme scheme) {
  133. if (!scheme.hasValue() || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
  134. final View view = activity.getWindow().getDecorView();
  135. if (scheme == TextColorScheme.Dark) {
  136. int flags = view.getSystemUiVisibility();
  137. flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  138. view.setSystemUiVisibility(flags);
  139. } else {
  140. clearDarkTextColorScheme(view);
  141. }
  142. }
  143. private void mergeTranslucent(StatusBarOptions options) {
  144. Window window = activity.getWindow();
  145. if (options.translucent.isTrue()) {
  146. window.setFlags(FLAG_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS);
  147. } else if (options.translucent.isFalse() && StatusBarUtils.isTranslucent(window)) {
  148. window.clearFlags(FLAG_TRANSLUCENT_STATUS);
  149. }
  150. }
  151. private void mergeStatusBarVisible(View view, Bool visible, Bool drawBehind) {
  152. if (visible.hasValue()) {
  153. int flags = view.getSystemUiVisibility();
  154. if (visible.isTrue()) {
  155. flags &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN & ~View.SYSTEM_UI_FLAG_FULLSCREEN;
  156. } else {
  157. flags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;
  158. }
  159. if (flags != view.getSystemUiVisibility()) view.requestLayout();
  160. view.setSystemUiVisibility(flags);
  161. } else if (drawBehind.hasValue()) {
  162. if (drawBehind.isTrue()) {
  163. view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  164. } else {
  165. view.setSystemUiVisibility(~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  166. }
  167. }
  168. }
  169. private void applyNavigationBarOptions(NavigationBarOptions options) {
  170. setNavigationBarBackgroundColor(options);
  171. }
  172. private void mergeNavigationBarOptions(NavigationBarOptions options) {
  173. setNavigationBarBackgroundColor(options);
  174. }
  175. private void setNavigationBarBackgroundColor(NavigationBarOptions navigationBar) {
  176. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && navigationBar.backgroundColor.canApplyValue()) {
  177. int defaultColor = activity.getWindow().getNavigationBarColor();
  178. int color = navigationBar.backgroundColor.get(defaultColor);
  179. activity.getWindow().setNavigationBarColor(color);
  180. setNavigationBarButtonsColor(color);
  181. }
  182. }
  183. private void setNavigationBarButtonsColor(int color) {
  184. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  185. View decorView = activity.getWindow().getDecorView();
  186. int flags = decorView.getSystemUiVisibility();
  187. if (isColorLight(color)) {
  188. flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
  189. } else {
  190. flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
  191. }
  192. decorView.setSystemUiVisibility(flags);
  193. }
  194. }
  195. private boolean isColorLight(int color) {
  196. double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
  197. return darkness < 0.5;
  198. }
  199. }