| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.app.Activity; |
| 4 | import android.graphics.Color; |
| 5 | import android.os.Build; |
| 6 | import android.view.View; |
| 7 | import android.view.WindowManager; |
| 8 | |
| 9 | import androidx.core.graphics.ColorUtils; |
| 10 | |
| 11 | import org.telegram.ui.ActionBar.Theme; |
| 12 | |
| 13 | public class UIHelper { |
| 14 | |
| 15 | static private void setStatusBarColor(Activity parentActivity) { |
| 16 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 17 | if (NekoConfig.transparentStatusBar && |
| 18 | (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || NekoConfig.navigationBarTint)) { |
| 19 | parentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
| 20 | parentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
| 21 | parentActivity.getWindow().setStatusBarColor(Color.TRANSPARENT); |
| 22 | setUIFlagLight(parentActivity); |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | static private void setUIFlagLight(Activity parentActivity) { |
| 28 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 29 | View view = parentActivity.getWindow().getDecorView(); |
| 30 | int colorCode = Theme.getColor(Theme.key_actionBarDefault); |
| 31 | int flags = view.getSystemUiVisibility(); |
| 32 | if (ColorUtils.calculateLuminance(colorCode) > 0.8 && NekoConfig.transparentStatusBar && |
| 33 | (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || NekoConfig.navigationBarTint)) { |
| 34 | flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 35 | view.setSystemUiVisibility(flags); |
| 36 | } else { |
| 37 | flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 38 | view.setSystemUiVisibility(flags); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static private void setNavigationBarColor(Activity parentActivity) { |
| 44 | int color; |
| 45 | switch (NekoConfig.navigationBarColor) { |
| 46 | case 3: |
| 47 | color = Theme.getColor(Theme.key_chat_messagePanelBackground); |
| 48 | break; |
| 49 | case 2: |
| 50 | color = Theme.getColor(Theme.key_actionBarDefault); |
| 51 | break; |
| 52 | case 1: |
| 53 | default: |
| 54 | color = 0xff000000; |
| 55 | } |
| 56 | setNavigationBarColor(parentActivity, color); |
| 57 | } |
| 58 | |
| 59 | static private void setNavigationBarColor(Activity parentActivity, int color) { |
| 60 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 61 | if (NekoConfig.navigationBarTint) { |
| 62 | parentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
| 63 | parentActivity.getWindow().setNavigationBarColor(color); |
| 64 | setUIFlagLightNavBar(parentActivity, color); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static private void setUIFlagLightNavBar(Activity parentActivity, int color) { |
| 70 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 71 | View view = parentActivity.getWindow().getDecorView(); |
| 72 | int flags = view.getSystemUiVisibility(); |
| 73 | if (ColorUtils.calculateLuminance(color) > 0.8 && NekoConfig.navigationBarTint) { |
| 74 | flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 75 | view.setSystemUiVisibility(flags); |
| 76 | } else { |
| 77 | flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 78 | view.setSystemUiVisibility(flags); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static private void clearColor(Activity parentActivity) { |
| 84 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 85 | parentActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
| 86 | parentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static public void updateStatusBarColor(Activity parentActivity) { |
| 91 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 92 | if (NekoConfig.transparentStatusBar) { |
| 93 | setStatusBarColor(parentActivity); |
| 94 | } else { |
| 95 | clearColor(parentActivity); |
| 96 | setNavigationBarColor(parentActivity); |
| 97 | } |
| 98 | setUIFlagLight(parentActivity); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public static void updateNavigationBarColor(Activity parentActivity) { |
| 103 | int color; |
| 104 | switch (NekoConfig.navigationBarColor) { |
| 105 | case 3: |
| 106 | color = Theme.getColor(Theme.key_chat_messagePanelBackground); |
| 107 | break; |
| 108 | case 2: |
| 109 | color = Theme.getColor(Theme.key_actionBarDefault); |
| 110 | break; |
| 111 | case 1: |
| 112 | default: |
| 113 | color = 0xff000000; |
| 114 | } |
| 115 | updateNavigationBarColor(parentActivity, color); |
| 116 | } |
| 117 | |
| 118 | public static void updateNavigationBarColor(Activity parentActivity, int color) { |
| 119 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 120 | if (NekoConfig.navigationBarTint) { |
| 121 | setNavigationBarColor(parentActivity, color); |
| 122 | } else { |
| 123 | clearColor(parentActivity); |
| 124 | setStatusBarColor(parentActivity); |
| 125 | } |
| 126 | setUIFlagLightNavBar(parentActivity, color); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |