BaseActivity.kt 6.13 KB
Newer Older
1
package com.ydl.ydlcommon.base
konghaorui committed
2 3 4

import android.app.Activity
import android.content.res.Resources
5
import android.graphics.Color
konghaorui committed
6
import android.os.Bundle
YKai committed
7 8
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
konghaorui committed
9
import android.util.Log
10
import android.view.View
11 12 13
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
konghaorui committed
14
import com.trello.rxlifecycle2.android.ActivityEvent
15
import com.ydl.ydlcommon.R
16 17 18 19 20 21
import com.ydl.ydlcommon.base.lifecycle.IActivityLifecycleable
import com.ydl.ydlcommon.bean.StatusBarOptions
import com.ydl.ydlcommon.ui.LoadingDialogFragment
import com.ydl.ydlcommon.utils.ActivityManager
import com.ydl.ydlcommon.utils.AndroidSystemHelper
import com.ydl.ydlcommon.utils.StatusBarUtils
22
import com.yidianling.common.tools.RxImageTool
konghaorui committed
23 24 25 26 27 28 29 30 31 32
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlin.properties.Delegates

/**
 * 基础 Activity,所有的 Activity 都继承此类
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/01/27
 */
33
abstract class BaseActivity : AppCompatActivity(), IActivityLifecycleable {
konghaorui committed
34 35 36

    private val mLifecycleSubject = BehaviorSubject.create<ActivityEvent>()
    var mContext: Activity by Delegates.notNull()
37
    var statusView: View? = null
38
    private var loadingDialogFragment: LoadingDialogFragment? = null
konghaorui committed
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

    override fun provideLifecycleSubject(): Subject<ActivityEvent> {
        return mLifecycleSubject;
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        //修复Android 8.0 崩溃
        AndroidSystemHelper.fixAndroidOrientationBug(this)
        super.onCreate(savedInstanceState)

        Log.d("TAG", javaClass.name)
        mContext = this

        val layoutResId = layoutResId()
        if (layoutResId != 0) setContentView(layoutResId())
        ActivityManager.getInstance().addStack(this)
        initDataAndEvent()
    }

    override fun setRequestedOrientation(requestedOrientation: Int) {
        if (AndroidSystemHelper.isAllowSetOrientation(this)) {
            super.setRequestedOrientation(requestedOrientation)
        }
    }

    override fun setContentView(@LayoutRes layoutResID: Int) {
65
        if (getStatusViewOptions()?.isAddStatusView) {
66 67 68 69
            val options = getStatusViewOptions()
            options.bottomStatusColor = getDefaultBottomColor()
            val returnViews =
                StatusBarUtils.initStatusBarView(this, layoutResID, getStatusViewOptions())
70

71
            if (returnViews.size > 1) {
72 73 74 75
                statusView = returnViews[1];
            }

            super.setContentView(returnViews[0])
konghaorui committed
76 77
            AndroidSystemHelper.fixAndroidBug5497Workaround(this)
        } else {
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

            //判定是否有底部小横条
            if (StatusBarUtils.hasBottomNavigatorLine(this)) {
                val containerView =
                    View.inflate(this, R.layout.platform_layout_bottom_fit_root, null) as ViewGroup
                val rootView = containerView.findViewById<FrameLayout>(R.id.ll_bottom_fit_root);
                val layoutView = View.inflate(this, layoutResID, null)

                val params =
                    ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )

                rootView.addView(layoutView, params)

                val bottomView = containerView.findViewById<View>(R.id.ll_bottom_fit_v)
                bottomView.visibility = View.VISIBLE
                bottomView.setBackgroundColor(getDefaultBottomColor())

                super.setContentView(containerView)
            } else {
                super.setContentView(layoutResID)
            }
konghaorui committed
102 103 104
        }
    }

105 106 107 108 109
    /**
     * 设置底部抬高的布局的颜色
     */
    open fun setBottomColor(color: String) {
        try {
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            if (getStatusViewOptions()?.isAddStatusView) {
                if (StatusBarUtils.hasBottomNavigatorLine(this)) {
                    findViewById<View>(R.id.ll_bottom_v).setBackgroundColor(
                        Color.parseColor(
                            color
                        )
                    )
                }
            } else {
                if (StatusBarUtils.hasBottomNavigatorLine(this)) {
                    findViewById<View>(R.id.ll_bottom_fit_v).setBackgroundColor(
                        Color.parseColor(
                            color
                        )
                    )
                }
126
            }
127 128
        } catch (e: Exception) {
        }
129 130
    }

131 132 133 134
    open fun getDefaultBottomColor(): Int {
        return Color.parseColor("#00000000")
    }

konghaorui committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    override fun onDestroy() {
        super.onDestroy()
        ActivityManager.getInstance().removeStack(this)
        AndroidSystemHelper.fixInputMethodManagerLeak(this)
    }

    @LayoutRes
    protected abstract fun layoutResId(): Int

    // 初始化数据和事件
    protected abstract fun initDataAndEvent()

    override fun getResources(): Resources {
        //解决 修改系统字体大小后 H5页面大小比例不正常现象
        val res = super.getResources()
        return AndroidSystemHelper.fixResourcesScale(res)
    }

153
    open fun getStatusViewOptions(): StatusBarOptions {
154
        return StatusBarOptions()
konghaorui committed
155
    }
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    fun showProgressDialog() {
        showProgressDialog("")
    }


    //显示dialog
    open fun showProgressDialog(str: String?) {
        if (loadingDialogFragment == null) {
            loadingDialogFragment = LoadingDialogFragment.newInstance(str)
        }
        if (loadingDialogFragment?.isAdded!!) {
            return
        }
        loadingDialogFragment?.show(supportFragmentManager, BaseActivity::class.java.simpleName)
172 173
    }

174 175 176 177 178 179 180 181 182 183 184 185 186
    //移除dialog
    open fun dismissProgressDialog() {
        try {
            if (loadingDialogFragment != null && loadingDialogFragment?.isAdded!!) {
                loadingDialogFragment?.hide()
                loadingDialogFragment = null
            }
        } catch (e: Exception) {
            //防止以下错误
            //java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        }

    }
187

konghaorui committed
188
}