StatusBarUtils.kt 42.7 KB
Newer Older
1
package com.ydl.ydlcommon.utils
konghaorui committed
2 3 4 5 6 7 8

import android.annotation.TargetApi
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Build
YKai committed
9 10 11 12 13 14
import androidx.annotation.ColorInt
import androidx.annotation.IntRange
import androidx.annotation.LayoutRes
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
konghaorui committed
15 16 17
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
18
import android.widget.FrameLayout
konghaorui committed
19
import android.widget.LinearLayout
20 21
import com.ydl.ydlcommon.R
import com.ydl.ydlcommon.bean.StatusBarOptions
konghaorui committed
22
import com.yidianling.common.tools.RxImageTool
konghaorui committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67


/**
 * 状态栏相关工具类
 * Created by hg
 * w on 2017/7/2.
 */
class StatusBarUtils {
    companion object {

        fun setWindowStatusBarColor(activity: Activity, colorResId: Int) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    val window = activity.window
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                    window.statusBarColor = ContextCompat.getColor(activity, colorResId)

                    //底部导航栏
                    //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }

        fun setWindowStatusBarColor(dialog: Dialog, colorResId: Int) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    val window = dialog.window
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                    window.statusBarColor = ContextCompat.getColor(dialog.context, colorResId)

                    //底部导航栏
                    //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        //====================添加其他状态栏处理方法 2019/3/3 haorui ====================
        //====================来源 GitHub: https://github.com/laobie/StatusBarUtil====================

        val DEFAULT_STATUS_BAR_ALPHA = 112
konghaorui committed
68 69
        private val FAKE_STATUS_BAR_VIEW_ID = R.id.platform_statusbarutil_fake_status_bar_view
        private val FAKE_TRANSLUCENT_VIEW_ID = R.id.platform_statusbarutil_translucent_view
konghaorui committed
70 71 72 73 74 75 76 77 78
        private val TAG_KEY_HAVE_SET_OFFSET = -123

        /**
         * 设置状态栏颜色
         *
         * @param activity 需要设置的 activity
         * @param color    状态栏颜色值
         */
        fun setColor(activity: Activity, @ColorInt color: Int) {
79 80 81 82 83
            setColor(
                activity,
                color,
                DEFAULT_STATUS_BAR_ALPHA
            )
konghaorui committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
        }

        /**
         * 设置状态栏颜色
         *
         * @param activity       需要设置的activity
         * @param color          状态栏颜色值
         * @param statusBarAlpha 状态栏透明度
         */

        fun setColor(activity: Activity, @ColorInt color: Int, @IntRange(from = 0, to = 255) statusBarAlpha: Int) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                activity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
98 99
                activity.window.statusBarColor =
                    calculateStatusColor(color, statusBarAlpha)
konghaorui committed
100 101 102 103 104 105 106 107
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                val decorView = activity.window.decorView as ViewGroup
                val fakeStatusBarView = decorView.findViewById<View>(FAKE_STATUS_BAR_VIEW_ID)
                if (fakeStatusBarView != null) {
                    if (fakeStatusBarView.visibility == View.GONE) {
                        fakeStatusBarView.visibility = View.VISIBLE
                    }
108 109 110 111 112 113
                    fakeStatusBarView.setBackgroundColor(
                        calculateStatusColor(
                            color,
                            statusBarAlpha
                        )
                    )
konghaorui committed
114
                } else {
115 116 117 118 119 120 121
                    decorView.addView(
                        createStatusBarView(
                            activity,
                            color,
                            statusBarAlpha
                        )
                    )
konghaorui committed
122 123 124 125 126 127 128 129 130 131 132 133
                }
                setRootView(activity)
            }
        }

        /**
         * 为滑动返回界面设置状态栏颜色
         *
         * @param activity 需要设置的activity
         * @param color    状态栏颜色值
         */
        fun setColorForSwipeBack(activity: Activity, color: Int) {
134 135 136 137 138
            setColorForSwipeBack(
                activity,
                color,
                DEFAULT_STATUS_BAR_ALPHA
            )
konghaorui committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        }

        /**
         * 为滑动返回界面设置状态栏颜色
         *
         * @param activity       需要设置的activity
         * @param color          状态栏颜色值
         * @param statusBarAlpha 状态栏透明度
         */
        fun setColorForSwipeBack(
            activity: Activity, @ColorInt color: Int,
            @IntRange(from = 0, to = 255) statusBarAlpha: Int
        ) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                val contentView = activity.findViewById<View>(android.R.id.content) as ViewGroup
                val rootView = contentView.getChildAt(0)
156 157
                val statusBarHeight =
                    getStatusBarHeight(activity)
konghaorui committed
158 159 160
                if (rootView != null && rootView is CoordinatorLayout) {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                        rootView.fitsSystemWindows = false
161 162 163 164 165 166
                        contentView.setBackgroundColor(
                            calculateStatusColor(
                                color,
                                statusBarAlpha
                            )
                        )
konghaorui committed
167 168 169 170 171 172
                        val isNeedRequestLayout = contentView.paddingTop < statusBarHeight
                        if (isNeedRequestLayout) {
                            contentView.setPadding(0, statusBarHeight, 0, 0)
                            rootView.post { rootView.requestLayout() }
                        }
                    } else {
173 174 175 176 177 178
                        rootView.setStatusBarBackgroundColor(
                            calculateStatusColor(
                                color,
                                statusBarAlpha
                            )
                        )
konghaorui committed
179 180 181
                    }
                } else {
                    contentView.setPadding(0, statusBarHeight, 0, 0)
182 183 184 185 186 187
                    contentView.setBackgroundColor(
                        calculateStatusColor(
                            color,
                            statusBarAlpha
                        )
                    )
konghaorui committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                }
                setTransparentForWindow(activity)
            }
        }

        /**
         * 设置状态栏纯色 不加半透明效果
         *
         * @param activity 需要设置的 activity
         * @param color    状态栏颜色值
         */
        fun setColorNoTranslucent(activity: Activity, @ColorInt color: Int) {
            setColor(activity, color, 0)
        }

        /**
         * 设置状态栏颜色(5.0以下无半透明效果,不建议使用)
         *
         * @param activity 需要设置的 activity
         * @param color    状态栏颜色值
         */
        @Deprecated("")
        fun setColorDiff(activity: Activity, @ColorInt color: Int) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            transparentStatusBar(activity)
            val contentView = activity.findViewById<View>(android.R.id.content) as ViewGroup
            // 移除半透明矩形,以免叠加
            val fakeStatusBarView = contentView.findViewById<View>(FAKE_STATUS_BAR_VIEW_ID)
            if (fakeStatusBarView != null) {
                if (fakeStatusBarView.visibility == View.GONE) {
                    fakeStatusBarView.visibility = View.VISIBLE
                }
                fakeStatusBarView.setBackgroundColor(color)
            } else {
224 225 226 227 228 229
                contentView.addView(
                    createStatusBarView(
                        activity,
                        color
                    )
                )
konghaorui committed
230 231 232 233 234 235 236 237 238 239 240 241 242
            }
            setRootView(activity)
        }

        /**
         * 使状态栏半透明
         *
         *
         * 适用于图片作为背景的界面,此时需要图片填充到状态栏
         *
         * @param activity 需要设置的activity
         */
        fun setTranslucent(activity: Activity) {
243 244 245 246
            setTranslucent(
                activity,
                DEFAULT_STATUS_BAR_ALPHA
            )
konghaorui committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        }

        /**
         * 使状态栏半透明
         *
         *
         * 适用于图片作为背景的界面,此时需要图片填充到状态栏
         *
         * @param activity       需要设置的activity
         * @param statusBarAlpha 状态栏透明度
         */
        fun setTranslucent(activity: Activity, @IntRange(from = 0, to = 255) statusBarAlpha: Int) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            setTransparent(activity)
            addTranslucentView(activity, statusBarAlpha)
        }

        /**
         * 针对根布局是 CoordinatorLayout, 使状态栏半透明
         *
         *
         * 适用于图片作为背景的界面,此时需要图片填充到状态栏
         *
         * @param activity       需要设置的activity
         * @param statusBarAlpha 状态栏透明度
         */
        fun setTranslucentForCoordinatorLayout(activity: Activity, @IntRange(from = 0, to = 255) statusBarAlpha: Int) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            transparentStatusBar(activity)
            addTranslucentView(activity, statusBarAlpha)
        }

        /**
         * 设置状态栏全透明
         *
         * @param activity 需要设置的activity
         */
        fun setTransparent(activity: Activity) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            transparentStatusBar(activity)
            setRootView(activity)
        }

        /**
         * 使状态栏透明(5.0以上半透明效果,不建议使用)
         *
         *
         * 适用于图片作为背景的界面,此时需要图片填充到状态栏
         *
         * @param activity 需要设置的activity
         */
        @Deprecated("")
        fun setTranslucentDiff(activity: Activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                // 设置状态栏透明
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                setRootView(activity)
            }
        }

        /**
         * 为DrawerLayout 布局设置状态栏变色
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         * @param color        状态栏颜色值
         */
        fun setColorForDrawerLayout(activity: Activity, drawerLayout: DrawerLayout, @ColorInt color: Int) {
321 322 323 324 325 326
            setColorForDrawerLayout(
                activity,
                drawerLayout,
                color,
                DEFAULT_STATUS_BAR_ALPHA
            )
konghaorui committed
327 328 329 330 331 332 333 334 335 336
        }

        /**
         * 为DrawerLayout 布局设置状态栏颜色,纯色
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         * @param color        状态栏颜色值
         */
        fun setColorNoTranslucentForDrawerLayout(activity: Activity, drawerLayout: DrawerLayout, @ColorInt color: Int) {
337 338 339 340 341 342
            setColorForDrawerLayout(
                activity,
                drawerLayout,
                color,
                0
            )
konghaorui committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        }

        /**
         * 为DrawerLayout 布局设置状态栏变色
         *
         * @param activity       需要设置的activity
         * @param drawerLayout   DrawerLayout
         * @param color          状态栏颜色值
         * @param statusBarAlpha 状态栏透明度
         */
        fun setColorForDrawerLayout(
            activity: Activity, drawerLayout: DrawerLayout, @ColorInt color: Int,
            @IntRange(from = 0, to = 255) statusBarAlpha: Int
        ) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                activity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                activity.window.statusBarColor = Color.TRANSPARENT
            } else {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            }
            // 生成一个状态栏大小的矩形
            // 添加 statusBarView 到布局中
            val contentLayout = drawerLayout.getChildAt(0) as ViewGroup
            val fakeStatusBarView = contentLayout.findViewById<View>(FAKE_STATUS_BAR_VIEW_ID)
            if (fakeStatusBarView != null) {
                if (fakeStatusBarView.visibility == View.GONE) {
                    fakeStatusBarView.visibility = View.VISIBLE
                }
                fakeStatusBarView.setBackgroundColor(color)
            } else {
377 378 379 380 381
                contentLayout.addView(
                    createStatusBarView(
                        activity,
                        color
                    ), 0)
konghaorui committed
382 383 384 385 386
            }
            // 内容布局不是 LinearLayout 时,设置padding top
            if (contentLayout !is LinearLayout && contentLayout.getChildAt(1) != null) {
                contentLayout.getChildAt(1)
                    .setPadding(
387 388 389
                        contentLayout.paddingLeft, getStatusBarHeight(
                            activity
                        ) + contentLayout.paddingTop,
konghaorui committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                        contentLayout.paddingRight, contentLayout.paddingBottom
                    )
            }
            // 设置属性
            setDrawerLayoutProperty(drawerLayout, contentLayout)
            addTranslucentView(activity, statusBarAlpha)
        }

        /**
         * 设置 DrawerLayout 属性
         *
         * @param drawerLayout              DrawerLayout
         * @param drawerLayoutContentLayout DrawerLayout 的内容布局
         */
        private fun setDrawerLayoutProperty(drawerLayout: DrawerLayout, drawerLayoutContentLayout: ViewGroup) {
            val drawer = drawerLayout.getChildAt(1) as ViewGroup
            drawerLayout.fitsSystemWindows = false
            drawerLayoutContentLayout.fitsSystemWindows = false
            drawerLayoutContentLayout.clipToPadding = true
            drawer.fitsSystemWindows = false
        }

        /**
         * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         * @param color        状态栏颜色值
         */
        @Deprecated("")
        fun setColorForDrawerLayoutDiff(activity: Activity, drawerLayout: DrawerLayout, @ColorInt color: Int) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                // 生成一个状态栏大小的矩形
                val contentLayout = drawerLayout.getChildAt(0) as ViewGroup
                val fakeStatusBarView = contentLayout.findViewById<View>(FAKE_STATUS_BAR_VIEW_ID)
                if (fakeStatusBarView != null) {
                    if (fakeStatusBarView.visibility == View.GONE) {
                        fakeStatusBarView.visibility = View.VISIBLE
                    }
430 431 432 433 434 435
                    fakeStatusBarView.setBackgroundColor(
                        calculateStatusColor(
                            color,
                            DEFAULT_STATUS_BAR_ALPHA
                        )
                    )
konghaorui committed
436 437
                } else {
                    // 添加 statusBarView 到布局中
438 439 440 441 442
                    contentLayout.addView(
                        createStatusBarView(
                            activity,
                            color
                        ), 0)
konghaorui committed
443 444 445
                }
                // 内容布局不是 LinearLayout 时,设置padding top
                if (contentLayout !is LinearLayout && contentLayout.getChildAt(1) != null) {
446 447
                    contentLayout.getChildAt(1).setPadding(0,
                        getStatusBarHeight(activity), 0, 0)
konghaorui committed
448 449
                }
                // 设置属性
450 451 452 453
                setDrawerLayoutProperty(
                    drawerLayout,
                    contentLayout
                )
konghaorui committed
454 455 456 457 458 459 460 461 462 463
            }
        }

        /**
         * 为 DrawerLayout 布局设置状态栏透明
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         */
        fun setTranslucentForDrawerLayout(activity: Activity, drawerLayout: DrawerLayout) {
464 465 466 467 468
            setTranslucentForDrawerLayout(
                activity,
                drawerLayout,
                DEFAULT_STATUS_BAR_ALPHA
            )
konghaorui committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
        }

        /**
         * 为 DrawerLayout 布局设置状态栏透明
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         */
        fun setTranslucentForDrawerLayout(
            activity: Activity, drawerLayout: DrawerLayout,
            @IntRange(from = 0, to = 255) statusBarAlpha: Int
        ) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
484 485 486 487
            setTransparentForDrawerLayout(
                activity,
                drawerLayout
            )
konghaorui committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
            addTranslucentView(activity, statusBarAlpha)
        }

        /**
         * 为 DrawerLayout 布局设置状态栏透明
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         */
        fun setTransparentForDrawerLayout(activity: Activity, drawerLayout: DrawerLayout) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                activity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                activity.window.statusBarColor = Color.TRANSPARENT
            } else {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            }

            val contentLayout = drawerLayout.getChildAt(0) as ViewGroup
            // 内容布局不是 LinearLayout 时,设置padding top
            if (contentLayout !is LinearLayout && contentLayout.getChildAt(1) != null) {
512 513
                contentLayout.getChildAt(1).setPadding(0,
                    getStatusBarHeight(activity), 0, 0)
konghaorui committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
            }

            // 设置属性
            setDrawerLayoutProperty(drawerLayout, contentLayout)
        }

        /**
         * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用)
         *
         * @param activity     需要设置的activity
         * @param drawerLayout DrawerLayout
         */
        @Deprecated("")
        fun setTranslucentForDrawerLayoutDiff(activity: Activity, drawerLayout: DrawerLayout) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                // 设置状态栏透明
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                // 设置内容布局属性
                val contentLayout = drawerLayout.getChildAt(0) as ViewGroup
                contentLayout.fitsSystemWindows = true
                contentLayout.clipToPadding = true
                // 设置抽屉布局属性
                val vg = drawerLayout.getChildAt(1) as ViewGroup
                vg.fitsSystemWindows = false
                // 设置 DrawerLayout 属性
                drawerLayout.fitsSystemWindows = false
            }
        }

        /**
         * 为头部是 ImageView 的界面设置状态栏全透明
         *
         * @param activity       需要设置的activity
         * @param needOffsetView 需要向下偏移的 View
         */
        fun setTransparentForImageView(activity: Activity, needOffsetView: View?) {
550 551 552 553 554
            setTranslucentForImageView(
                activity,
                0,
                needOffsetView
            )
konghaorui committed
555 556 557 558 559 560 561 562 563
        }

        /**
         * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度)
         *
         * @param activity       需要设置的activity
         * @param needOffsetView 需要向下偏移的 View
         */
        fun setTranslucentForImageView(activity: Activity, needOffsetView: View) {
564 565 566 567 568
            setTranslucentForImageView(
                activity,
                DEFAULT_STATUS_BAR_ALPHA,
                needOffsetView
            )
konghaorui committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
        }

        /**
         * 为头部是 ImageView 的界面设置状态栏透明
         *
         * @param activity       需要设置的activity
         * @param statusBarAlpha 状态栏透明度
         * @param needOffsetView 需要向下偏移的 View
         */
        fun setTranslucentForImageView(
            activity: Activity, @IntRange(from = 0, to = 255) statusBarAlpha: Int,
            needOffsetView: View?
        ) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return
            }
            setTransparentForWindow(activity)
            addTranslucentView(activity, statusBarAlpha)
            if (needOffsetView != null) {
                val haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET)
                if (haveSetOffset != null && haveSetOffset as Boolean) {
                    return
                }
                val layoutParams = needOffsetView.layoutParams as ViewGroup.MarginLayoutParams
                layoutParams.setMargins(
594 595 596
                    layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(
                        activity
                    ),
konghaorui committed
597 598 599 600 601 602 603 604 605 606 607 608 609
                    layoutParams.rightMargin, layoutParams.bottomMargin
                )
                needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true)
            }
        }

        /**
         * 为 fragment 头部是 ImageView 的设置状态栏透明
         *
         * @param activity       fragment 对应的 activity
         * @param needOffsetView 需要向下偏移的 View
         */
        fun setTranslucentForImageViewInFragment(activity: Activity, needOffsetView: View) {
610 611 612 613 614
            setTranslucentForImageViewInFragment(
                activity,
                DEFAULT_STATUS_BAR_ALPHA,
                needOffsetView
            )
konghaorui committed
615 616 617 618 619 620 621 622 623
        }

        /**
         * 为 fragment 头部是 ImageView 的设置状态栏透明
         *
         * @param activity       fragment 对应的 activity
         * @param needOffsetView 需要向下偏移的 View
         */
        fun setTransparentForImageViewInFragment(activity: Activity, needOffsetView: View) {
624 625 626 627 628
            setTranslucentForImageViewInFragment(
                activity,
                0,
                needOffsetView
            )
konghaorui committed
629 630 631 632 633 634 635 636 637 638 639 640 641
        }

        /**
         * 为 fragment 头部是 ImageView 的设置状态栏透明
         *
         * @param activity       fragment 对应的 activity
         * @param statusBarAlpha 状态栏透明度
         * @param needOffsetView 需要向下偏移的 View
         */
        fun setTranslucentForImageViewInFragment(
            activity: Activity, @IntRange(from = 0, to = 255) statusBarAlpha: Int,
            needOffsetView: View
        ) {
642 643 644 645 646
            setTranslucentForImageView(
                activity,
                statusBarAlpha,
                needOffsetView
            )
konghaorui committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                clearPreviousSetting(activity)
            }
        }

        /**
         * 隐藏伪状态栏 View
         *
         * @param activity 调用的 Activity
         */
        fun hideFakeStatusBarView(activity: Activity) {
            val decorView = activity.window.decorView as ViewGroup
            val fakeStatusBarView = decorView.findViewById<View>(FAKE_STATUS_BAR_VIEW_ID)
            if (fakeStatusBarView != null) {
                fakeStatusBarView.visibility = View.GONE
            }
            val fakeTranslucentView = decorView.findViewById<View>(FAKE_TRANSLUCENT_VIEW_ID)
            if (fakeTranslucentView != null) {
                fakeTranslucentView.visibility = View.GONE
            }
        }

        ///////////////////////////////////////////////////////////////////////////////////

        @TargetApi(Build.VERSION_CODES.KITKAT)
        private fun clearPreviousSetting(activity: Activity) {
            val decorView = activity.window.decorView as ViewGroup
            val fakeStatusBarView = decorView.findViewById<View>(FAKE_STATUS_BAR_VIEW_ID)
            if (fakeStatusBarView != null) {
                decorView.removeView(fakeStatusBarView)
                val rootView =
                    (activity.findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0) as ViewGroup
                rootView.setPadding(0, 0, 0, 0)
            }
        }

        /**
         * 添加半透明矩形条
         *
         * @param activity       需要设置的 activity
         * @param statusBarAlpha 透明值
         */
        private fun addTranslucentView(activity: Activity, @IntRange(from = 0, to = 255) statusBarAlpha: Int) {
            val contentView = activity.findViewById<View>(android.R.id.content) as ViewGroup
            val fakeTranslucentView = contentView.findViewById<View>(FAKE_TRANSLUCENT_VIEW_ID)
            if (fakeTranslucentView != null) {
                if (fakeTranslucentView.visibility == View.GONE) {
                    fakeTranslucentView.visibility = View.VISIBLE
                }
                fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0))
            } else {
698 699 700 701 702 703
                contentView.addView(
                    createTranslucentStatusBarView(
                        activity,
                        statusBarAlpha
                    )
                )
konghaorui committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
            }
        }

        /**
         * 生成一个和状态栏大小相同的彩色矩形条
         *
         * @param activity 需要设置的 activity
         * @param color    状态栏颜色值
         * @return 状态栏矩形条
         */
        private fun createStatusBarView(activity: Activity, @ColorInt color: Int): View {
            return createStatusBarView(activity, color, 0)
        }

        /**
         * 生成一个和状态栏大小相同的半透明矩形条
         *
         * @param activity 需要设置的activity
         * @param color    状态栏颜色值
         * @param alpha    透明值
         * @return 状态栏矩形条
         */
        private fun createStatusBarView(activity: Activity, @ColorInt color: Int, alpha: Int): View {
            // 绘制一个和状态栏一样高的矩形
            val statusBarView = View(activity)
729 730 731
            val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                getStatusBarHeight(activity)
            )
konghaorui committed
732
            statusBarView.layoutParams = params
733 734 735 736 737 738
            statusBarView.setBackgroundColor(
                calculateStatusColor(
                    color,
                    alpha
                )
            )
konghaorui committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
            statusBarView.id = FAKE_STATUS_BAR_VIEW_ID
            return statusBarView
        }

        /**
         * 设置根布局参数
         */
        private fun setRootView(activity: Activity) {
            val parent = activity.findViewById<View>(android.R.id.content) as ViewGroup
            var i = 0
            val count = parent.childCount
            while (i < count) {
                val childView = parent.getChildAt(i)
                if (childView is ViewGroup) {
                    childView.setFitsSystemWindows(true)
                    childView.clipToPadding = true
                }
                i++
            }
        }

        /**
         * 设置透明
         */
        private fun setTransparentForWindow(activity: Activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.window.statusBarColor = Color.TRANSPARENT
                activity.window
                    .decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                activity.window
                    .setFlags(
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    )
            }
        }

        /**
         * 使状态栏透明
         */
        @TargetApi(Build.VERSION_CODES.KITKAT)
        private fun transparentStatusBar(activity: Activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                activity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
                activity.window.statusBarColor = Color.TRANSPARENT
            } else {
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            }
        }

        /**
         * 创建半透明矩形 View
         *
         * @param alpha 透明值
         * @return 半透明 View
         */
        private fun createTranslucentStatusBarView(activity: Activity, alpha: Int): View {
            // 绘制一个和状态栏一样高的矩形
            val statusBarView = View(activity)
802 803 804
            val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                getStatusBarHeight(activity)
            )
konghaorui committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
            statusBarView.layoutParams = params
            statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0))
            statusBarView.id = FAKE_TRANSLUCENT_VIEW_ID
            return statusBarView
        }

        /**
         * 获取状态栏高度
         *
         * @param context context
         * @return 状态栏高度
         */
        public fun getStatusBarHeight(context: Context): Int {
            // 获得状态栏高度
            val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
            return context.resources.getDimensionPixelSize(resourceId)
        }

        /**
         * 计算状态栏颜色
         *
         * @param color color值
         * @param alpha alpha值
         * @return 最终的状态栏颜色
         */
        private fun calculateStatusColor(@ColorInt color: Int, alpha: Int): Int {
            if (alpha == 0) {
                return color
            }
            val a = 1 - alpha / 255f
            var red = color shr 16 and 0xff
            var green = color shr 8 and 0xff
            var blue = color and 0xff
            red = (red * a + 0.5).toInt()
            green = (green * a + 0.5).toInt()
            blue = (blue * a + 0.5).toInt()
            return 0xff shl 24 or (red shl 16) or (green shl 8) or blue
        }

        //====================设置状态栏黑色字体 by haorui START====================
        val OTHER = -1
        val MIUI = 1
        val FLYME = 2
        val ANDROID_M = 3
        /**
         * 设置状态栏黑色字体图标,
         * 适配4.4以上版本MIUI、Flyme和6.0以上版本其他Android
         *
         * 原生方法不适配6.0的小米
         * 魅族方法不适配6.0以上的魅族
         *
         * @return 1:MIUI 2:Flyme 3:android6.0
         */
        fun statusBarLightMode(activity: Activity): Int {
            var result = 0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (setStatusBarLightMode(activity, true)) {
                    result = ANDROID_M
                }
                if (setMIUIStatusBarLightMode(activity, true)) {
                    result = MIUI
866 867 868 869 870
                } else if (setFlymeStatusBarLightMode(
                        activity,
                        true
                    )
                ) {
konghaorui committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
                    result = FLYME
                }
            }
            return result
        }

        /**
         * 已知系统类型时,设置状态栏黑色字体图标。
         * 适配4.4以上版本MIUI6、Flyme和6.0以上版本其他Android
         *
         * @param type 1:MIUI 2:Flyme 3:android6.0
         */
        fun statusBarLightMode(activity: Activity, type: Int) {
            statusBarMode(activity, type, true)

        }

        /**
         * 清除MIUI或flyme或6.0以上版本状态栏黑色字体
         */
        fun statusBarDarkMode(activity: Activity, type: Int) {
            statusBarMode(activity, type, false)
        }

        private fun statusBarMode(activity: Activity, type: Int, isFontColorDark: Boolean) {
            if (type == MIUI) {
897 898 899 900
                setMIUIStatusBarLightMode(
                    activity,
                    isFontColorDark
                )
konghaorui committed
901
            } else if (type == FLYME) {
902 903 904 905
                setFlymeStatusBarLightMode(
                    activity,
                    isFontColorDark
                )
konghaorui committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
            } else if (type == ANDROID_M) {
                setStatusBarLightMode(activity, isFontColorDark)
            }
        }

        fun setStatusBarLightMode(activity: Activity, isFontColorDark: Boolean): Boolean {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (isFontColorDark) {
                    // 沉浸式
                    activity.window.decorView.systemUiVisibility =
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    //非沉浸式
                    activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
                }
                return true
            }
            return false
        }

        fun setMIUIStatusBarLightMode(activity: Activity, isFontColorDark: Boolean): Boolean {
            val window = activity.window
            var result = false
            if (window != null) {
                val clazz = window.javaClass
                try {
                    var darkModeFlag = 0
                    val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
                    val field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE")
                    darkModeFlag = field.getInt(layoutParams)
                    val extraFlagField =
                        clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
                    if (isFontColorDark) {
                        extraFlagField.invoke(window, darkModeFlag, darkModeFlag)//状态栏透明且黑色字体
                    } else {
                        extraFlagField.invoke(window, 0, darkModeFlag)//清除黑色字体
                    }
                    result = true
                } catch (e: Exception) {
                    //                e.printStackTrace();
                }

            }
            return result
        }

        /**
         * 设置状态栏图标为深色和魅族特定的文字风格
         * 可以用来判断是否为Flyme用户
         *
         * @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
         * @return boolean 成功执行返回true
         */
        fun setFlymeStatusBarLightMode(activity: Activity, isFontColorDark: Boolean): Boolean {
            val window = activity.window
            var result = false
            if (window != null) {
                try {
                    val lp = window.attributes
                    val darkFlag = WindowManager.LayoutParams::class.java
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON")
                    val meizuFlags = WindowManager.LayoutParams::class.java
                        .getDeclaredField("meizuFlags")
                    darkFlag.isAccessible = true
                    meizuFlags.isAccessible = true
                    val bit = darkFlag.getInt(null)
                    var value = meizuFlags.getInt(lp)
                    if (isFontColorDark) {
                        value = value or bit
                    } else {
                        value = value and bit.inv()
                    }
                    meizuFlags.setInt(lp, value)
                    window.attributes = lp
                    result = true
                } catch (e: Exception) {
                    //                e.printStackTrace();
                    result = false
                }

            }
            return result
        }
        //====================设置状态栏黑色字体 by haorui END====================


        //====================设置Activity状态栏View START====================
        fun initStatusBarView(
            context: Activity, @LayoutRes layoutResID: Int,
            statusBarOptions: StatusBarOptions
996
        ): Array<View> {
konghaorui committed
997
            val child = View.inflate(context, layoutResID, null)
998 999 1000
            return initStatusBarView(
                context,
                child,
1001 1002
                statusBarOptions,
                false
1003
            )
konghaorui committed
1004 1005 1006
        }

        fun initStatusBarView(
1007 1008 1009 1010
            context: Activity,
            child:View,
            statusBarOptions: StatusBarOptions,
            isFragment: Boolean = false
1011 1012
        ): Array<View> {

1013
            var returnView  = arrayOfNulls<View>(2)
1014

1015
            val containerView = View.inflate(context, R.layout.platform_layout_root, null) as ViewGroup
1016
            val rootView = containerView.findViewById<FrameLayout>(R.id.ll_root)
1017
            val statusView =
1018
                initStatus(context, containerView, statusBarOptions)
1019

konghaorui committed
1020
            val params =
1021 1022 1023 1024
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
konghaorui committed
1025
            rootView.addView(child, params)
1026 1027 1028 1029 1030 1031 1032

            if (!isFragment && hasBottomNavigatorLine(context)) {
                val bottomView = containerView.findViewById<View>(R.id.ll_bottom_v)
                bottomView.visibility = View.VISIBLE
                bottomView.setBackgroundColor(statusBarOptions.bottomStatusColor)
            }

1033 1034 1035 1036 1037 1038
            returnView[0] = containerView

            if (statusView != null){
                returnView[1] = statusView
            }

1039
            return returnView as Array<View>
konghaorui committed
1040 1041
        }

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        /**
         * 判定是否含有底部小横条,当底部导航高度像素小于50时,认为是有小横条
         */
        fun hasBottomNavigatorLine(context: Context): Boolean {
            return try {
                val clazz = Class.forName("com.android.internal.R\$dimen")
                val `object` = clazz.newInstance()
                val heightStr = clazz.getField("navigation_bar_height").get(`object`).toString()
                val height = Integer.parseInt(heightStr)
                val bottomNavigatorHeight = context.resources.getDimensionPixelSize(height)
                bottomNavigatorHeight < 50
            } catch (e: Exception) {
                false
            }
        }

konghaorui committed
1058 1059 1060

        fun initStatus(
            context: Activity,
1061
            containerView: View,
1062 1063
            statusBarOptions: StatusBarOptions
        ):View? {
1064
            var statusView:View ?=null
konghaorui committed
1065
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
1066
                statusView = containerView.findViewById<View>(R.id.view_status_place_holder)
konghaorui committed
1067
                statusView?.setBackgroundColor(Color.parseColor(statusBarOptions.statusColor))
1068 1069
                val statusBarHeight =
                    getStatusBarHeight(context)
1070 1071 1072 1073 1074
                statusView?.let {
                    val layoutParams = statusView?.getLayoutParams() as LinearLayout.LayoutParams
                    layoutParams.height = statusBarHeight
                    statusView?.layoutParams = layoutParams
                }
konghaorui committed
1075 1076 1077 1078 1079
                setTransparentForImageView(context, null)
            }
            if (statusBarOptions.statusBarDarkMode) {
                statusBarLightMode(context)
            }
1080
            return statusView
konghaorui committed
1081
        }
konghaorui committed
1082 1083 1084
        /**
         * 设置自定义状态栏位置
         */
konghaorui committed
1085 1086 1087 1088 1089 1090

        fun setCustomStatusView(context: Activity,statusView:View){
            setCustomStatusView(context,statusView,false)
        }

        fun setCustomStatusView(context: Activity,statusView:View,isDarkMode:Boolean = false){
fengquan committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
            val params = statusView.layoutParams
            setTransparentForImageView(context, null)
            val statusBarHeight = StatusBarUtils.getStatusBarHeight(context)
            params.height = RxImageTool.dp2px(48f) + statusBarHeight
            statusView.setPadding(0, statusBarHeight, 0, 0)
            if (isDarkMode) {
                statusBarLightMode(context)
            }
        }

        /**
         *  适配 One Plus手机顶部返回按钮被截取问题
         */
        fun setCustomStatusViewForMembers(context: Activity,statusView:View,isDarkMode:Boolean = false) {
konghaorui committed
1105
            val params = statusView.layoutParams
konghaorui committed
1106 1107 1108
            setTransparentForImageView(context, null)
            val statusBarHeight = StatusBarUtils.getStatusBarHeight(context)
            params.height = RxImageTool.dp2px(48f) + statusBarHeight
1109
            statusView.setPadding(0, statusBarHeight - 25, 0, 0)
konghaorui committed
1110 1111 1112
            if (isDarkMode) {
                statusBarLightMode(context)
            }
konghaorui committed
1113 1114
        }
    }
konghaorui committed
1115 1116 1117 1118
    //====================设置Activity状态栏View END====================


}