소스 검색

1.创建基础模块coreLib

master
wangwanlei 9 달 전
부모
커밋
6db4769d86
26개의 변경된 파일1827개의 추가작업 그리고 0개의 파일을 삭제
  1. 6
    0
      .idea/kotlinc.xml
  2. 1
    0
      corelib/.gitignore
  3. 97
    0
      corelib/build.gradle.kts
  4. BIN
      corelib/libs/csdk_5.0.jar
  5. 24
    0
      corelib/src/androidTest/java/com/xhly/corelib/ExampleInstrumentedTest.kt
  6. 4
    0
      corelib/src/main/AndroidManifest.xml
  7. 39
    0
      corelib/src/main/java/com/xhly/corelib/Const.kt
  8. 117
    0
      corelib/src/main/java/com/xhly/corelib/base/ui/BaseActivity.kt
  9. 66
    0
      corelib/src/main/java/com/xhly/corelib/base/ui/CommonBaseActivity.kt
  10. 35
    0
      corelib/src/main/java/com/xhly/corelib/base/viewmodel/CommonBaseViewModel.kt
  11. 74
    0
      corelib/src/main/java/com/xhly/corelib/bean/AppInfo.java
  12. 64
    0
      corelib/src/main/java/com/xhly/corelib/eventbus/UIEvent.java
  13. 55
    0
      corelib/src/main/java/com/xhly/corelib/network/ResponseInterceptor.kt
  14. 61
    0
      corelib/src/main/java/com/xhly/corelib/network/RetrofitService.kt
  15. 3
    0
      corelib/src/main/java/com/xhly/corelib/network/bean/ResponseData.kt
  16. 29
    0
      corelib/src/main/java/com/xhly/corelib/network/bean/ResponseListData.java
  17. 43
    0
      corelib/src/main/java/com/xhly/corelib/utils/AppUtils.java
  18. 88
    0
      corelib/src/main/java/com/xhly/corelib/utils/GsonUtils.java
  19. 68
    0
      corelib/src/main/java/com/xhly/corelib/utils/MD5Utils.java
  20. 10
    0
      corelib/src/main/java/com/xhly/corelib/utils/ModelNameUtils.kt
  21. 250
    0
      corelib/src/main/java/com/xhly/corelib/utils/NetworkUtils.java
  22. 141
    0
      corelib/src/main/java/com/xhly/corelib/utils/SharedPreferencesUtils.java
  23. 132
    0
      corelib/src/main/java/com/xhly/corelib/utils/StatusBarUtil.java
  24. 129
    0
      corelib/src/main/java/com/xhly/corelib/utils/SystemUtil.java
  25. 274
    0
      corelib/src/main/java/com/xhly/corelib/utils/baseUtils.kt
  26. 17
    0
      corelib/src/test/java/com/xhly/corelib/ExampleUnitTest.kt

+ 6
- 0
.idea/kotlinc.xml 파일 보기

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="KotlinJpsPluginSettings">
4
+    <option name="version" value="1.8.10" />
5
+  </component>
6
+</project>

+ 1
- 0
corelib/.gitignore 파일 보기

@@ -0,0 +1 @@
1
+/build

+ 97
- 0
corelib/build.gradle.kts 파일 보기

@@ -0,0 +1,97 @@
1
+@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
2
+plugins {
3
+    alias(libs.plugins.androidLibrary)
4
+    alias(libs.plugins.kotlinAndroid)
5
+}
6
+
7
+android {
8
+    namespace = "com.xhly.corelib"
9
+    compileSdk = 33
10
+
11
+    defaultConfig {
12
+        minSdk = 24
13
+
14
+        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
15
+        consumerProguardFiles("consumer-rules.pro")
16
+    }
17
+
18
+    buildTypes {
19
+        release {
20
+            isMinifyEnabled = false
21
+            proguardFiles(
22
+                getDefaultProguardFile("proguard-android-optimize.txt"),
23
+                "proguard-rules.pro"
24
+            )
25
+        }
26
+    }
27
+    
28
+    viewBinding{
29
+        enable=true
30
+    }
31
+
32
+    compileOptions {
33
+        sourceCompatibility = JavaVersion.VERSION_1_8
34
+        targetCompatibility = JavaVersion.VERSION_1_8
35
+    }
36
+    kotlinOptions {
37
+        jvmTarget = "1.8"
38
+    }
39
+}
40
+
41
+dependencies {
42
+
43
+    implementation(libs.core.ktx)
44
+    implementation(libs.appcompat)
45
+    implementation(libs.material)
46
+    api(files("libs\\csdk_5.0.jar"))
47
+    testImplementation(libs.junit)
48
+    androidTestImplementation(libs.androidx.test.ext.junit)
49
+    androidTestImplementation(libs.espresso.core)
50
+    api(libs.androidx.multidex)
51
+    api(libs.lifecycle.runtime.ktx)
52
+    api (libs.lifecycle.livedata.ktx)
53
+    api (libs.androidx.lifecycle.viewmodel.ktx)
54
+    api(libs.androidx.constraintlayout)
55
+
56
+    //协程
57
+    api(libs.org.jetbrains.kotlinx.kotlinx.coroutines.core2)
58
+    api(libs.org.jetbrains.kotlinx.kotlinx.coroutines.android2)
59
+
60
+    //retrofit + okHttp3
61
+    api(libs.com.squareup.retrofit2.retrofit)
62
+    api(libs.com.squareup.retrofit2.adapter.rxjava2)
63
+    api(libs.com.squareup.retrofit2.converter.gson)
64
+    api(libs.com.squareup.retrofit2.converter.scalars)
65
+    api(libs.com.squareup.okhttp3.logging.interceptor)
66
+    api(libs.com.jakewharton.retrofit.retrofit2.kotlin.coroutines.adapter2)
67
+
68
+    //rxjava
69
+    api(libs.rxjava)
70
+    api(libs.rxandroid)
71
+    //动态权限框架
72
+    api(libs.rxpermissions)
73
+    //eventbus
74
+    api(libs.eventbus)
75
+    //万能适配器
76
+    api(libs.baseadapterhelper)
77
+
78
+    //下拉刷新框架
79
+    api(libs.smartrefresh.layout.kernel)     //核心必须依赖
80
+    api(libs.smartrefresh.header.classics)    //经典刷新头
81
+    api(libs.smartrefresh.header.radar)       //雷达刷新头
82
+    api(libs.smartrefresh.header.falsify)   //虚拟刷新头
83
+    api(libs.smartrefresh.header.material)    //谷歌刷新头
84
+    api(libs.smartrefresh.header.two.level)   //二级刷新头
85
+    api(libs.smartrefresh.footer.ball)        //球脉冲加载
86
+    api(libs.smartrefresh.footer.classics)    //经典加载
87
+// 吐司框架:https://github.com/getActivity/Toaster
88
+    api(libs.gittoaster)
89
+    //弹窗
90
+    api(libs.dialogx)
91
+    //今日头条适配
92
+    api(libs.jessyanandroidautosize)
93
+
94
+
95
+    //圆角imageview
96
+    api("com.makeramen:roundedimageview:2.3.0")
97
+}

BIN
corelib/libs/csdk_5.0.jar 파일 보기


+ 24
- 0
corelib/src/androidTest/java/com/xhly/corelib/ExampleInstrumentedTest.kt 파일 보기

@@ -0,0 +1,24 @@
1
+package com.xhly.corelib
2
+
3
+import androidx.test.platform.app.InstrumentationRegistry
4
+import androidx.test.ext.junit.runners.AndroidJUnit4
5
+
6
+import org.junit.Test
7
+import org.junit.runner.RunWith
8
+
9
+import org.junit.Assert.*
10
+
11
+/**
12
+ * Instrumented test, which will execute on an Android device.
13
+ *
14
+ * See [testing documentation](http://d.android.com/tools/testing).
15
+ */
16
+@RunWith(AndroidJUnit4::class)
17
+class ExampleInstrumentedTest {
18
+    @Test
19
+    fun useAppContext() {
20
+        // Context of the app under test.
21
+        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22
+        assertEquals("com.xhly.corelib.test", appContext.packageName)
23
+    }
24
+}

+ 4
- 0
corelib/src/main/AndroidManifest.xml 파일 보기

@@ -0,0 +1,4 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+
4
+</manifest>

+ 39
- 0
corelib/src/main/java/com/xhly/corelib/Const.kt 파일 보기

@@ -0,0 +1,39 @@
1
+package com.xhly.corelib
2
+
3
+
4
+object Const {
5
+    //接口地址
6
+    const val URL1: String = "http://49.4.26.249:8331/"
7
+
8
+    //标记app禁用
9
+    const val DISABLEAPP = "DISABLEAPP"
10
+
11
+    //作为存储白名单应用的key
12
+    const val WHITELISTAPP="WHITELISTAPP"
13
+
14
+    //401退出登录
15
+    const val LOGIN_OUT_401 = "LOGIN_OUT_401"
16
+
17
+    //通知websocket链接成功
18
+    const val UPDATE_CONNECTED_SUCCESS = "UPDATE_CONNECTED_SUCCESS"
19
+
20
+    //发送消息
21
+    const val CODE2001 = 2001
22
+
23
+    //限制使用
24
+    const val CODE2002 = 2002
25
+
26
+    //解除限制
27
+    const val CODE2003 = 2003
28
+
29
+    //更新策略
30
+    const val CODE2004 = 2004
31
+
32
+    //重启设备
33
+    const val CODE2005 = 2005
34
+
35
+    //恢复出厂
36
+    const val CODE2006 = 2006
37
+
38
+
39
+}

+ 117
- 0
corelib/src/main/java/com/xhly/corelib/base/ui/BaseActivity.kt 파일 보기

@@ -0,0 +1,117 @@
1
+import android.content.res.Resources
2
+import android.os.Bundle
3
+import android.util.Log
4
+import android.view.Gravity
5
+import androidx.lifecycle.ViewModelProvider
6
+import androidx.viewbinding.ViewBinding
7
+import com.hjq.toast.Toaster
8
+import com.scwang.smart.refresh.footer.ClassicsFooter
9
+import com.scwang.smart.refresh.layout.SmartRefreshLayout
10
+import com.xhly.corelib.base.ui.CommonBaseActivity
11
+import com.xhly.corelib.base.viewmodel.CommonBaseViewModel
12
+import com.xhly.corelib.eventbus.UIEvent
13
+import com.xhly.corelib.utils.LogShow
14
+import com.xhly.corelib.utils.SharedPreferencesUtils
15
+import com.xhly.corelib.utils.StatusBarUtil
16
+import com.xhly.corelib.utils.getClass
17
+import me.jessyan.autosize.AutoSizeCompat
18
+import me.jessyan.autosize.AutoSizeConfig
19
+import org.greenrobot.eventbus.EventBus
20
+import org.greenrobot.eventbus.Subscribe
21
+import org.greenrobot.eventbus.ThreadMode
22
+
23
+abstract class BaseActivity<VM : CommonBaseViewModel, VB : ViewBinding> : CommonBaseActivity() {
24
+    lateinit var viewModel: VM
25
+    lateinit var mBinding: VB
26
+    val WRITECODE = 10086
27
+    val spUtils by lazy { SharedPreferencesUtils.getInstance(this) }
28
+
29
+
30
+
31
+    override fun onCreate(savedInstanceState: Bundle?) {
32
+        super.onCreate(savedInstanceState)
33
+        mBinding = getBinding()
34
+        setContentView(mBinding.root)
35
+        viewModel = ViewModelProvider(this).get(getClass(this))
36
+        StatusBarUtil.StatusBarLightMode(this, true)
37
+        EventBus.getDefault().register(this)
38
+        startObserveError()
39
+        try {
40
+            initData()
41
+            initView()
42
+        } catch (e: Exception) {
43
+            LogShow(e.toString())
44
+        }
45
+    }
46
+
47
+    abstract fun getBinding(): VB
48
+    abstract fun initData()
49
+    abstract fun initView()
50
+
51
+    private fun startObserveError() {
52
+        viewModel.getError().observe(this) {
53
+            Log.d("错误日志", it.toString())
54
+        }
55
+        viewModel.netMessage.observe(this) {
56
+            //处理一些自定义的信息
57
+            useNetMessage(it)
58
+        }
59
+    }
60
+
61
+    open fun useNetMessage(s: String) {
62
+
63
+    }
64
+
65
+
66
+    public fun Toast(s: String) {
67
+        try {
68
+            Toaster.show(s)
69
+            Toaster.setGravity(Gravity.CENTER)
70
+        }catch (e:java.lang.Exception){
71
+            android.widget.Toast.makeText(this, s, android.widget.Toast.LENGTH_SHORT).show()
72
+        }
73
+    }
74
+
75
+
76
+
77
+
78
+    @Subscribe(threadMode = ThreadMode.MAIN)
79
+    open fun onUiEvent(uiEvent: UIEvent) {
80
+    }
81
+
82
+
83
+    override fun onRequestPermissionsResult(
84
+        requestCode: Int,
85
+        permissions: Array<out String>,
86
+        grantResults: IntArray
87
+    ) {
88
+        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
89
+        /*  if (requestCode == WRITECODE) {
90
+              if (grantResults[0] == -1) {
91
+                  Toast("没有权限")
92
+              } else {
93
+                  Toast("权限申请成功,请点击保存")
94
+              }
95
+          }*/
96
+    }
97
+
98
+    open fun initBaseRefreshLayout(smart: SmartRefreshLayout) {
99
+        smart.setRefreshFooter(ClassicsFooter(this))
100
+        smart.setEnableRefresh(false)
101
+        smart.setEnableAutoLoadMore(false)
102
+    }
103
+
104
+    override fun getResources(): Resources {
105
+        val width = AutoSizeConfig.getInstance().screenWidth
106
+        val height = AutoSizeConfig.getInstance().screenHeight
107
+        runOnUiThread {
108
+            AutoSizeCompat.autoConvertDensity(super.getResources(), 853f, width > height)
109
+        }
110
+        return super.getResources()
111
+    }
112
+
113
+    override fun onDestroy() {
114
+        super.onDestroy()
115
+        EventBus.getDefault().unregister(this)
116
+    }
117
+}

+ 66
- 0
corelib/src/main/java/com/xhly/corelib/base/ui/CommonBaseActivity.kt 파일 보기

@@ -0,0 +1,66 @@
1
+package com.xhly.corelib.base.ui
2
+
3
+import android.app.Activity
4
+import android.content.Intent
5
+import android.graphics.Rect
6
+import android.os.Build
7
+import android.os.Bundle
8
+import android.view.ViewGroup
9
+import androidx.appcompat.app.AppCompatActivity
10
+
11
+open class CommonBaseActivity : AppCompatActivity() {
12
+    var lastHeight: Int = 0
13
+    fun setSoftInput(rootLinearLayout: ViewGroup) {
14
+        //有edittext和srcollview时使用,多edittext可能需要搭配  android:windowSoftInputMode="adjustPan" 以及 <item name="android:windowBackground">@color/white</item>
15
+        if (Build.VERSION.SDK_INT >= 21) {
16
+            window.decorView.viewTreeObserver.addOnGlobalLayoutListener {
17
+                var r: Rect = Rect()
18
+                window.decorView.getWindowVisibleDisplayFrame(r)
19
+                if (lastHeight != r.bottom) {
20
+                    lastHeight = r.bottom
21
+                    var layayoutParams: ViewGroup.LayoutParams = rootLinearLayout.layoutParams
22
+                    layayoutParams.height = r.bottom - rootLinearLayout.top
23
+                    rootLinearLayout.layoutParams = layayoutParams
24
+                    windowChange()
25
+                }
26
+            }
27
+        }
28
+    }
29
+
30
+    open fun windowChange() {
31
+        //window内容变动时调用,可以用来处理edittext光标和检测输入法的隐藏与显示
32
+        try {
33
+            if (!isSoftShowing(this)) {
34
+
35
+            } else {
36
+
37
+            }
38
+        } catch (e: Exception) {
39
+
40
+        }
41
+    }
42
+
43
+    fun isSoftShowing(activity: Activity): Boolean {
44
+        //获取当前屏幕内容的高度
45
+        val screenHeight = activity.window.decorView.height
46
+        //获取View可见区域的bottom
47
+        val rect = Rect()
48
+        //DecorView即为activity的顶级view
49
+        activity.window.decorView.getWindowVisibleDisplayFrame(rect)
50
+        //考虑到虚拟导航栏的情况(虚拟导航栏情况下:screenHeight = rect.bottom + 虚拟导航栏高度)
51
+        //选取screenHeight*2/3进行判断
52
+        return screenHeight * 2 / 3 > rect.bottom
53
+    }
54
+
55
+    public fun startIntentActivity(activity: Class<AppCompatActivity>, bundle: Bundle? = null) {
56
+        var intent = Intent(this, activity)
57
+        try {
58
+            if (bundle != null) {
59
+                intent.putExtras(bundle)
60
+            }
61
+        } catch (e: Exception) {
62
+
63
+        }
64
+        startActivity(intent)
65
+    }
66
+}

+ 35
- 0
corelib/src/main/java/com/xhly/corelib/base/viewmodel/CommonBaseViewModel.kt 파일 보기

@@ -0,0 +1,35 @@
1
+package com.xhly.corelib.base.viewmodel
2
+
3
+import androidx.lifecycle.LiveData
4
+import androidx.lifecycle.MutableLiveData
5
+import androidx.lifecycle.ViewModel
6
+import androidx.lifecycle.viewModelScope
7
+import com.xhly.corelib.network.bean.ResponseData
8
+import com.xhly.corelib.utils.LogShow
9
+import kotlinx.coroutines.CoroutineScope
10
+import kotlinx.coroutines.launch
11
+
12
+open class CommonBaseViewModel : ViewModel() {
13
+    private val error by lazy { MutableLiveData<Exception>() }
14
+
15
+    //记录自定义的一些信息,用于数据处理
16
+    public val netMessage by lazy { MutableLiveData<String>() }
17
+    //记录自定义的一些信息,用于数据处理
18
+    public val responseData by lazy { MutableLiveData<ResponseData<Any>>() }
19
+    //运行在UI线程的协程
20
+    fun launchUI(block: suspend CoroutineScope.() -> Unit) = viewModelScope.launch {
21
+        try {
22
+            block()
23
+        } catch (e: Exception) {
24
+            error.value = e
25
+            LogShow(e.toString())
26
+        }
27
+    }
28
+
29
+    /**
30
+     * 请求失败,出现异常
31
+     */
32
+    fun getError(): LiveData<Exception> {
33
+        return error
34
+    }
35
+}

+ 74
- 0
corelib/src/main/java/com/xhly/corelib/bean/AppInfo.java 파일 보기

@@ -0,0 +1,74 @@
1
+package com.xhly.corelib.bean;
2
+
3
+import android.content.Intent;
4
+import android.graphics.drawable.Drawable;
5
+
6
+public class AppInfo {
7
+    private String packageName; //包名
8
+    private Drawable ico;       //图标
9
+    private String Name;        //应用标签
10
+    private Intent intent;     //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity
11
+
12
+    private long startTime;
13
+
14
+    private long endTime;
15
+
16
+    private long foregroundTime;
17
+ 
18
+    public Intent getIntent() {
19
+        return intent;
20
+    }
21
+ 
22
+    public void setIntent(Intent intent) {
23
+        this.intent = intent;
24
+    }
25
+ 
26
+ 
27
+    public String getPackageName() {
28
+        return packageName;
29
+    }
30
+ 
31
+    public void setPackageName(String packageName) {
32
+        this.packageName = packageName;
33
+    }
34
+ 
35
+    public Drawable getIco() {
36
+        return ico;
37
+    }
38
+ 
39
+    public void setIco(Drawable ico) {
40
+        this.ico = ico;
41
+    }
42
+ 
43
+    public String getName() {
44
+        return Name;
45
+    }
46
+ 
47
+    public void setName(String name) {
48
+        Name = name;
49
+    }
50
+
51
+    public long getForegroundTime() {
52
+        return foregroundTime;
53
+    }
54
+
55
+    public void setForegroundTime(long foregroundTime) {
56
+        this.foregroundTime = foregroundTime;
57
+    }
58
+
59
+    public long getStartTime() {
60
+        return startTime;
61
+    }
62
+
63
+    public void setStartTime(long startTime) {
64
+        this.startTime = startTime;
65
+    }
66
+
67
+    public long getEndTime() {
68
+        return endTime;
69
+    }
70
+
71
+    public void setEndTime(long endTime) {
72
+        this.endTime = endTime;
73
+    }
74
+}

+ 64
- 0
corelib/src/main/java/com/xhly/corelib/eventbus/UIEvent.java 파일 보기

@@ -0,0 +1,64 @@
1
+package com.xhly.corelib.eventbus;
2
+
3
+import android.os.Bundle;
4
+
5
+import org.greenrobot.eventbus.EventBus;
6
+
7
+import java.io.Serializable;
8
+
9
+
10
+public class UIEvent {
11
+
12
+    public String event;
13
+    private String message;
14
+
15
+    private Bundle bundle = new Bundle();
16
+
17
+    public UIEvent(String event) {
18
+        this.event = event;
19
+    }
20
+
21
+    public String getMessage() {
22
+        return message;
23
+    }
24
+
25
+    public UIEvent setMessage(String message) {
26
+        this.message = message;
27
+        return this;
28
+    }
29
+
30
+    public String getEvent() {
31
+        return event;
32
+    }
33
+
34
+    public void setEvent(String event) {
35
+        this.event = event;
36
+    }
37
+
38
+    public void post() {
39
+        EventBus.getDefault().post(this);
40
+    }
41
+
42
+    public void postSticky() {
43
+        EventBus.getDefault().postSticky(this);
44
+    }
45
+
46
+    public UIEvent putExtra(String key, Serializable value) {
47
+        bundle.putSerializable(key, value);
48
+        return this;
49
+    }
50
+
51
+    public UIEvent setBundle(Bundle bundle) {
52
+        this.bundle = bundle;
53
+        return this;
54
+    }
55
+
56
+    public Bundle getBundle() {
57
+        return bundle;
58
+    }
59
+
60
+    @Override
61
+    public String toString() {
62
+        return "UIEvent{" + "event='" + event + '\'' + ", message='" + message + '\'' + ", bundle=" + bundle + '}';
63
+    }
64
+}

+ 55
- 0
corelib/src/main/java/com/xhly/corelib/network/ResponseInterceptor.kt 파일 보기

@@ -0,0 +1,55 @@
1
+package com.xhly.corelib.network
2
+
3
+import android.util.Log
4
+import com.xhly.corelib.Const
5
+import com.xhly.corelib.eventbus.UIEvent
6
+import com.xhly.corelib.network.bean.ResponseData
7
+import com.xhly.corelib.utils.GsonUtils
8
+
9
+import okhttp3.Interceptor
10
+import okhttp3.Request
11
+import okhttp3.Response
12
+import java.io.IOException
13
+import java.net.URLDecoder
14
+import java.net.URLEncoder
15
+import java.nio.charset.Charset
16
+
17
+class ResponseInterceptor : Interceptor {
18
+    @Throws(IOException::class)
19
+    override fun intercept(chain: Interceptor.Chain): Response {
20
+        var request = chain.request()
21
+        return try {
22
+            val requestBuilder: Request.Builder = request.newBuilder()
23
+            //TODO 需要替换token
24
+         /*   requestBuilder.header("Authorization", "Bearer ${Const.token}")
25
+            requestBuilder.header("Accept-Language", "zh-Hans")*/
26
+            requestBuilder.header("XH-UserId","")
27
+            request = requestBuilder.build()
28
+            val originalResponse = chain.proceed(request)
29
+            val responseBody = originalResponse.body
30
+            val source = responseBody!!.source()
31
+            source.request(Long.MAX_VALUE)
32
+            val buffer = source.buffer()
33
+            var charset = Charset.forName("UTF8")
34
+            val contentType = responseBody.contentType()
35
+            if (contentType != null) {
36
+                charset = contentType.charset(charset)
37
+            }
38
+            val bodyString = buffer.clone().readString(charset)
39
+            val data = GsonUtils.parseJsonWithGson(bodyString, ResponseData::class.java)
40
+            if (data == null) {
41
+                throw RuntimeException("数据为空")
42
+            } else {
43
+                if (data.code == 401) {
44
+                    //强制退出
45
+                    UIEvent(Const.LOGIN_OUT_401).post()
46
+                }
47
+            }
48
+            originalResponse
49
+        } catch (e: Exception) {
50
+            e.printStackTrace()
51
+            chain.proceed(request)
52
+        }
53
+    }
54
+
55
+}

+ 61
- 0
corelib/src/main/java/com/xhly/corelib/network/RetrofitService.kt 파일 보기

@@ -0,0 +1,61 @@
1
+package com.xhly.corelib.network
2
+
3
+import android.util.Log
4
+import com.xhly.corelib.Const
5
+import io.reactivex.internal.subscribers.DeferredScalarSubscriber
6
+import io.reactivex.schedulers.Schedulers
7
+import okhttp3.OkHttpClient
8
+import okhttp3.logging.HttpLoggingInterceptor
9
+import retrofit2.Retrofit
10
+import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
11
+import retrofit2.converter.gson.GsonConverterFactory
12
+import retrofit2.converter.scalars.ScalarsConverterFactory
13
+import java.net.Proxy
14
+import java.net.URLDecoder
15
+import java.net.URLEncoder
16
+import java.util.concurrent.TimeUnit
17
+
18
+object RetrofitService {
19
+    private const val READ_TIMEOUT = 60L
20
+    private const val WRITE_TIMEOUT = 60L
21
+    private const val CONNECT_TIMEOUT = 30L
22
+
23
+    var BASE_URL = Const.URL1
24
+
25
+    private var mRetrofit: Retrofit? = null
26
+
27
+    private fun getClient(): OkHttpClient {
28
+
29
+        val interceptor = HttpLoggingInterceptor { message ->
30
+            try {
31
+                val text: String = URLDecoder.decode(message, "utf-8")
32
+                Log.e("OKHttp-----", text)
33
+            } catch (t: Exception) {
34
+                try {
35
+                    var encode = URLEncoder.encode(message, "utf-8")
36
+                    val text: String = URLDecoder.decode(encode, "utf-8")
37
+                    Log.e("OKHttp-----DEBUG", text)
38
+                } catch (e: Exception) {
39
+                    t.printStackTrace()
40
+                    e.printStackTrace()
41
+                }
42
+            }
43
+        }
44
+        interceptor.level = HttpLoggingInterceptor.Level.BODY
45
+        return OkHttpClient.Builder().proxy(Proxy.NO_PROXY).retryOnConnectionFailure(true)
46
+            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
47
+            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
48
+            .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS).addInterceptor(ResponseInterceptor())
49
+            .addInterceptor(interceptor).build()
50
+    }
51
+
52
+    fun getRetrofit(): Retrofit {
53
+        return mRetrofit ?: Retrofit.Builder().baseUrl(BASE_URL).validateEagerly(true)
54
+            .addConverterFactory(ScalarsConverterFactory.create())
55
+            .addConverterFactory(GsonConverterFactory.create())
56
+            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
57
+            .client(getClient()).build().also { mRetrofit = it }
58
+    }
59
+
60
+    inline fun <reified T> create() = getRetrofit().create(T::class.java)
61
+}

+ 3
- 0
corelib/src/main/java/com/xhly/corelib/network/bean/ResponseData.kt 파일 보기

@@ -0,0 +1,3 @@
1
+package com.xhly.corelib.network.bean
2
+
3
+data class ResponseData<T>(val code: Int, var msg: String, val obj: T)

+ 29
- 0
corelib/src/main/java/com/xhly/corelib/network/bean/ResponseListData.java 파일 보기

@@ -0,0 +1,29 @@
1
+/**
2
+ * Copyright 2023 bejson.com
3
+ */
4
+package com.xhly.corelib.network.bean;
5
+
6
+import java.util.ArrayList;
7
+
8
+public class ResponseListData<T> {
9
+
10
+    private int count;
11
+    private ArrayList<T> list;
12
+
13
+    public void setCount(int count) {
14
+        this.count = count;
15
+    }
16
+
17
+    public int getCount() {
18
+        return count;
19
+    }
20
+
21
+    public void setList(ArrayList<T> list) {
22
+        this.list = list;
23
+    }
24
+
25
+    public ArrayList<T> getList() {
26
+        return list;
27
+    }
28
+
29
+}

+ 43
- 0
corelib/src/main/java/com/xhly/corelib/utils/AppUtils.java 파일 보기

@@ -0,0 +1,43 @@
1
+package com.xhly.corelib.utils;
2
+
3
+import static com.xhly.corelib.utils.BaseUtilsKt.LogShow;
4
+
5
+import android.content.ComponentName;
6
+import android.content.Context;
7
+import android.content.Intent;
8
+import android.content.pm.PackageManager;
9
+import android.content.pm.ResolveInfo;
10
+
11
+
12
+import com.xhly.corelib.bean.AppInfo;
13
+
14
+import java.util.ArrayList;
15
+import java.util.List;
16
+
17
+public class AppUtils {
18
+    public static List<AppInfo> GetAppList1(Context context) {
19
+        List<AppInfo> list = new ArrayList<>();
20
+        PackageManager pm = context.getPackageManager();
21
+        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
22
+        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
23
+        List<ResolveInfo> activities = pm.queryIntentActivities(mainIntent, 0);
24
+        for (ResolveInfo info : activities) {
25
+            String packName = info.activityInfo.packageName;
26
+            LogShow("数据","数据类名" + packName+"|||"+info.activityInfo.name);
27
+            if (packName.equals(context.getPackageName())) {
28
+                continue;
29
+            }
30
+            AppInfo mInfo = new AppInfo();
31
+            mInfo.setIco(info.activityInfo.applicationInfo.loadIcon(pm));
32
+            mInfo.setName(info.activityInfo.applicationInfo.loadLabel(pm).toString());
33
+            mInfo.setPackageName(packName);
34
+            // 为应用程序的启动Activity 准备Intent
35
+            Intent launchIntent = new Intent();
36
+            launchIntent.setComponent(new ComponentName(packName,
37
+                    info.activityInfo.name));
38
+            mInfo.setIntent(launchIntent);
39
+            list.add(mInfo);
40
+        }
41
+        return list;
42
+    }
43
+}

+ 88
- 0
corelib/src/main/java/com/xhly/corelib/utils/GsonUtils.java 파일 보기

@@ -0,0 +1,88 @@
1
+package com.xhly.corelib.utils;
2
+
3
+import android.text.TextUtils;
4
+
5
+import com.google.gson.Gson;
6
+import com.google.gson.JsonArray;
7
+import com.google.gson.JsonElement;
8
+import com.google.gson.JsonParser;
9
+import com.xhly.corelib.network.bean.ResponseData;
10
+
11
+import java.lang.reflect.ParameterizedType;
12
+import java.lang.reflect.Type;
13
+import java.util.ArrayList;
14
+
15
+public class GsonUtils {
16
+    //将Json数据解析成相应的映射对象
17
+    public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
18
+        if (TextUtils.isEmpty(jsonData) || jsonData.length() < 6) {
19
+            return null;
20
+        }
21
+        Gson gson = new Gson();
22
+        T result = null;
23
+        try {
24
+            result = gson.fromJson(jsonData, type);
25
+        } catch (Exception e) {
26
+            LogUtils.d(GsonUtils.class.getName(), "解析异常:"+e.getMessage() + "\n 内容是:" + jsonData);
27
+            e.printStackTrace();
28
+            return null;
29
+        }
30
+        return result;
31
+    }
32
+
33
+    /**
34
+     * 将Json数组解析成相应的映射对象列表
35
+     */
36
+    public static <T> ArrayList<T> parseJsonArrayWithGson(String jsonData,
37
+                                                          Class<T> type) {
38
+        if (TextUtils.isEmpty(jsonData) || jsonData.length() < 6) {
39
+            return null;
40
+        }
41
+        ArrayList<T> mList = new ArrayList<>();
42
+        JsonArray array = null;
43
+        try {
44
+            array = new JsonParser().parse(jsonData).getAsJsonArray();
45
+        } catch (Exception e) {
46
+            e.printStackTrace();
47
+            return null;
48
+        }
49
+        for (final JsonElement elem : array) {
50
+            mList.add(new Gson().fromJson(elem, type));
51
+        }
52
+        return mList;
53
+    }
54
+
55
+    public static String parseClassToJson(Object object) {
56
+        if (null == object) {
57
+            return "";
58
+        }
59
+        Gson gson = new Gson();
60
+        return gson.toJson(object);
61
+    }
62
+    //将Json数据解析成ResponseData
63
+    public static ResponseData fromJson(String json, Class clazz) {
64
+        Gson gson = new Gson();
65
+        Type objectType = type(ResponseData.class, clazz);
66
+        return gson.fromJson(json, objectType);
67
+    }
68
+
69
+
70
+    static ParameterizedType type(final Class raw, final Type... args) {
71
+        return new ParameterizedType() {
72
+            @Override
73
+            public Type getRawType() {
74
+                return raw;
75
+            }
76
+
77
+            @Override
78
+            public Type[] getActualTypeArguments() {
79
+                return args;
80
+            }
81
+
82
+            @Override
83
+            public Type getOwnerType() {
84
+                return null;
85
+            }
86
+        };
87
+    }
88
+}

+ 68
- 0
corelib/src/main/java/com/xhly/corelib/utils/MD5Utils.java 파일 보기

@@ -0,0 +1,68 @@
1
+package com.xhly.corelib.utils;
2
+
3
+/**
4
+ * Created by newbiechen on 2018/1/1.
5
+ */
6
+
7
+import java.security.MessageDigest;
8
+import java.security.NoSuchAlgorithmException;
9
+
10
+/**
11
+ *@Description: 将字符串转化为MD5
12
+ */
13
+
14
+public class MD5Utils {
15
+
16
+    public static String strToMd5By32(String str){
17
+        String reStr = null;
18
+        try {
19
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
20
+            byte[] bytes = md5.digest(str.getBytes());
21
+            StringBuffer stringBuffer = new StringBuffer();
22
+            for (byte b : bytes){
23
+                int bt = b&0xff;
24
+                if (bt < 16){
25
+                    stringBuffer.append(0);
26
+                }
27
+                stringBuffer.append(Integer.toHexString(bt));
28
+            }
29
+            reStr = stringBuffer.toString();
30
+        } catch (NoSuchAlgorithmException e) {
31
+            e.printStackTrace();
32
+        }
33
+        return reStr;
34
+    }
35
+
36
+    public static String strToMd5By16(String str){
37
+        String reStr = strToMd5By32(str);
38
+        if (reStr != null){
39
+            reStr = reStr.substring(8, 24);
40
+        }
41
+        return reStr;
42
+    }
43
+
44
+    public static  String getJsMD5(String password) {
45
+        try {
46
+            // 得到一个信息摘要器
47
+            MessageDigest digest = MessageDigest.getInstance("md5");
48
+            byte[] result = digest.digest(password.getBytes());
49
+            StringBuffer buffer = new StringBuffer();
50
+            // 把没一个byte 做一个与运算 0xff;
51
+            for (byte b : result) {
52
+                // 与运算
53
+                int number = b & 0xff;// 加盐
54
+                String str = Integer.toHexString(number);
55
+                if (str.length() == 1) {
56
+                    buffer.append("0");
57
+                }
58
+                buffer.append(str);
59
+            }
60
+
61
+            // 标准的md5加密后的结果
62
+            return buffer.toString();
63
+        } catch (NoSuchAlgorithmException e) {
64
+            e.printStackTrace();
65
+            return "";
66
+        }
67
+    }
68
+}

+ 10
- 0
corelib/src/main/java/com/xhly/corelib/utils/ModelNameUtils.kt 파일 보기

@@ -0,0 +1,10 @@
1
+package com.xhly.corelib.utils
2
+
3
+class ModelNameUtils {
4
+    companion object{
5
+        fun IS_LianxX505f(): Boolean{
6
+            val systemModel: String = SystemUtil.getSystemModel()
7
+            return "Lenovo TB-X505F" == systemModel
8
+        }
9
+    }
10
+}

+ 250
- 0
corelib/src/main/java/com/xhly/corelib/utils/NetworkUtils.java 파일 보기

@@ -0,0 +1,250 @@
1
+package com.xhly.corelib.utils;
2
+
3
+import android.content.Context;
4
+import android.content.Intent;
5
+import android.net.ConnectivityManager;
6
+import android.net.NetworkInfo;
7
+import android.provider.Settings;
8
+import android.telephony.TelephonyManager;
9
+
10
+public class NetworkUtils {
11
+
12
+    private NetworkUtils() {
13
+        throw new UnsupportedOperationException("");
14
+    }
15
+
16
+    public static final int NETWORK_WIFI = 1;    // wifi network
17
+    public static final int NETWORK_4G = 4;    // "4G" networks
18
+    public static final int NETWORK_3G = 3;    // "3G" networks
19
+    public static final int NETWORK_2G = 2;    // "2G" networks
20
+    public static final int NETWORK_UNKNOWN = 5;    // unknown network
21
+    public static final int NETWORK_NO = -1;   // no network
22
+
23
+    private static final int NETWORK_TYPE_GSM = 16;
24
+    private static final int NETWORK_TYPE_TD_SCDMA = 17;
25
+    private static final int NETWORK_TYPE_IWLAN = 18;
26
+
27
+    /**
28
+     * 打开网络设置界面
29
+     * <p>3.0以下打开设置界面</p>
30
+     *
31
+     * @param context 上下文
32
+     */
33
+    public static void openWirelessSettings(Context context) {
34
+        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
35
+        context.startActivity(intent);
36
+    }
37
+
38
+    /**
39
+     * 获取活动网络信息
40
+     *
41
+     * @param context 上下文
42
+     * @return NetworkInfo
43
+     */
44
+    private static NetworkInfo getActiveNetworkInfo(Context context) {
45
+        ConnectivityManager cm = (ConnectivityManager) context
46
+                .getSystemService(Context.CONNECTIVITY_SERVICE);
47
+        return cm.getActiveNetworkInfo();
48
+    }
49
+
50
+    /**
51
+     * 判断网络是否可用
52
+     * <p>需添加权限 {@code <uses-permission android:name="android.permission
53
+     * .ACCESS_NETWORK_STATE"/>}</p>
54
+     *
55
+     * @param context 上下文
56
+     * @return {@code true}: 可用<br>{@code false}: 不可用
57
+     */
58
+    public static boolean isAvailable(Context context) {
59
+        ConnectivityManager connectivity = (ConnectivityManager) context
60
+                .getSystemService(Context.CONNECTIVITY_SERVICE);
61
+        if (connectivity != null) {
62
+            NetworkInfo info = connectivity.getActiveNetworkInfo();
63
+            if (info != null && info.isConnected()) {
64
+                // 当前网络是连接的
65
+                if (info.getState() == NetworkInfo.State.CONNECTED) {
66
+                    // 当前所连接的网络可用
67
+                    return true;
68
+                }
69
+            }
70
+        }
71
+        return false;
72
+    }
73
+
74
+    /**
75
+     * 判断网络是否连接
76
+     * <p>需添加权限 {@code <uses-permission android:name="android.permission
77
+     * .ACCESS_NETWORK_STATE"/>}</p>
78
+     *
79
+     * @param context 上下文
80
+     * @return {@code true}: 是<br>{@code false}: 否
81
+     */
82
+    public static boolean isConnected(Context context) {
83
+        NetworkInfo info = getActiveNetworkInfo(context);
84
+        return info != null && info.isAvailable();
85
+    }
86
+
87
+    /**
88
+     * 判断网络是否是4G
89
+     * <p>需添加权限 {@code <uses-permission android:name="android.permission
90
+     * .ACCESS_NETWORK_STATE"/>}</p>
91
+     *
92
+     * @param context 上下文
93
+     * @return {@code true}: 是<br>{@code false}: 不是
94
+     */
95
+    public static boolean is4G(Context context) {
96
+        NetworkInfo info = getActiveNetworkInfo(context);
97
+        return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager
98
+                .NETWORK_TYPE_LTE;
99
+    }
100
+
101
+    /**
102
+     * 判断wifi是否连接状态
103
+     * <p>需添加权限 {@code <uses-permission android:name="android.permission
104
+     * .ACCESS_NETWORK_STATE"/>}</p>
105
+     *
106
+     * @param context 上下文
107
+     * @return {@code true}: 连接<br>{@code false}: 未连接
108
+     */
109
+    public static boolean isWifiConnected(Context context) {
110
+        ConnectivityManager cm = (ConnectivityManager) context
111
+                .getSystemService(Context.CONNECTIVITY_SERVICE);
112
+        return cm != null && cm.getActiveNetworkInfo() != null
113
+                && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
114
+    }
115
+
116
+    /**
117
+     * 获取移动网络运营商名称
118
+     * <p>如中国联通、中国移动、中国电信</p>
119
+     *
120
+     * @param context 上下文
121
+     * @return 移动网络运营商名称
122
+     */
123
+    public static String getNetworkOperatorName(Context context) {
124
+        TelephonyManager tm = (TelephonyManager) context
125
+                .getSystemService(Context.TELEPHONY_SERVICE);
126
+        return tm != null ? tm.getNetworkOperatorName() : null;
127
+    }
128
+
129
+    /**
130
+     * 获取移动终端类型
131
+     *
132
+     * @param context 上下文
133
+     * @return 手机制式
134
+     * <ul>
135
+     * <li>{@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知</li>
136
+     * <li>{@link TelephonyManager#PHONE_TYPE_GSM  } : 1 手机制式为GSM,移动和联通</li>
137
+     * <li>{@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为CDMA,电信</li>
138
+     * <li>{@link TelephonyManager#PHONE_TYPE_SIP  } : 3</li>
139
+     * </ul>
140
+     */
141
+    public static int getPhoneType(Context context) {
142
+        TelephonyManager tm = (TelephonyManager) context
143
+                .getSystemService(Context.TELEPHONY_SERVICE);
144
+        return tm != null ? tm.getPhoneType() : -1;
145
+    }
146
+
147
+
148
+    /**
149
+     * 获取当前的网络类型(WIFI,2G,3G,4G)
150
+     * <p>需添加权限 {@code <uses-permission android:name="android.permission
151
+     * .ACCESS_NETWORK_STATE"/>}</p>
152
+     *
153
+     * @param context 上下文
154
+     * @return 网络类型
155
+     * <ul>
156
+     * <li>{@link #NETWORK_WIFI   } = 1;</li>
157
+     * <li>{@link #NETWORK_4G     } = 4;</li>
158
+     * <li>{@link #NETWORK_3G     } = 3;</li>
159
+     * <li>{@link #NETWORK_2G     } = 2;</li>
160
+     * <li>{@link #NETWORK_UNKNOWN} = 5;</li>
161
+     * <li>{@link #NETWORK_NO     } = -1;</li>
162
+     * </ul>
163
+     */
164
+    public static int getNetWorkType(Context context) {
165
+        int netType = NETWORK_NO;
166
+        NetworkInfo info = getActiveNetworkInfo(context);
167
+        if (info != null && info.isAvailable()) {
168
+
169
+            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
170
+                netType = NETWORK_WIFI;
171
+            } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
172
+                switch (info.getSubtype()) {
173
+
174
+                    case NETWORK_TYPE_GSM:
175
+                    case TelephonyManager.NETWORK_TYPE_GPRS:
176
+                    case TelephonyManager.NETWORK_TYPE_CDMA:
177
+                    case TelephonyManager.NETWORK_TYPE_EDGE:
178
+                    case TelephonyManager.NETWORK_TYPE_1xRTT:
179
+                    case TelephonyManager.NETWORK_TYPE_IDEN:
180
+                        netType = NETWORK_2G;
181
+                        break;
182
+
183
+                    case NETWORK_TYPE_TD_SCDMA:
184
+                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
185
+                    case TelephonyManager.NETWORK_TYPE_UMTS:
186
+                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
187
+                    case TelephonyManager.NETWORK_TYPE_HSDPA:
188
+                    case TelephonyManager.NETWORK_TYPE_HSUPA:
189
+                    case TelephonyManager.NETWORK_TYPE_HSPA:
190
+                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
191
+                    case TelephonyManager.NETWORK_TYPE_EHRPD:
192
+                    case TelephonyManager.NETWORK_TYPE_HSPAP:
193
+                        netType = NETWORK_3G;
194
+                        break;
195
+
196
+                    case NETWORK_TYPE_IWLAN:
197
+                    case TelephonyManager.NETWORK_TYPE_LTE:
198
+                        netType = NETWORK_4G;
199
+                        break;
200
+                    default:
201
+
202
+                        String subtypeName = info.getSubtypeName();
203
+                        if (subtypeName.equalsIgnoreCase("TD-SCDMA")
204
+                                || subtypeName.equalsIgnoreCase("WCDMA")
205
+                                || subtypeName.equalsIgnoreCase("CDMA2000")) {
206
+                            netType = NETWORK_3G;
207
+                        } else {
208
+                            netType = NETWORK_UNKNOWN;
209
+                        }
210
+                        break;
211
+                }
212
+            } else {
213
+                netType = NETWORK_UNKNOWN;
214
+            }
215
+        }
216
+        return netType;
217
+    }
218
+
219
+    /**
220
+     * 获取当前的网络类型(WIFI,2G,3G,4G)
221
+     * <p>依赖上面的方法</p>
222
+     *
223
+     * @param context 上下文
224
+     * @return 网络类型名称
225
+     * <ul>
226
+     * <li>NETWORK_WIFI   </li>
227
+     * <li>NETWORK_4G     </li>
228
+     * <li>NETWORK_3G     </li>
229
+     * <li>NETWORK_2G     </li>
230
+     * <li>NETWORK_UNKNOWN</li>
231
+     * <li>NETWORK_NO     </li>
232
+     * </ul>
233
+     */
234
+    public static String getNetWorkTypeName(Context context) {
235
+        switch (getNetWorkType(context)) {
236
+            case NETWORK_WIFI:
237
+                return "NETWORK_WIFI";
238
+            case NETWORK_4G:
239
+                return "NETWORK_4G";
240
+            case NETWORK_3G:
241
+                return "NETWORK_3G";
242
+            case NETWORK_2G:
243
+                return "NETWORK_2G";
244
+            case NETWORK_NO:
245
+                return "NETWORK_NO";
246
+            default:
247
+                return "NETWORK_UNKNOWN";
248
+        }
249
+    }
250
+}

+ 141
- 0
corelib/src/main/java/com/xhly/corelib/utils/SharedPreferencesUtils.java 파일 보기

@@ -0,0 +1,141 @@
1
+package com.xhly.corelib.utils;
2
+
3
+import android.content.Context;
4
+import android.content.SharedPreferences;
5
+import android.text.TextUtils;
6
+import android.util.Log;
7
+
8
+import com.google.gson.Gson;
9
+
10
+public class SharedPreferencesUtils {
11
+    /**
12
+     * 保存在手机里面的文件名
13
+     */
14
+    private static final String FILE_NAME = "share_data";
15
+    private SharedPreferences preferences;
16
+    private static SharedPreferencesUtils instance;
17
+
18
+
19
+    private SharedPreferencesUtils(Context context) {
20
+        preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
21
+    }
22
+
23
+    /**
24
+     * 单例模式的SharedPreferencesUtil
25
+     */
26
+    public static SharedPreferencesUtils getInstance(Context context) {
27
+        if (instance == null) {
28
+            synchronized (SharedPreferencesUtils.class) {
29
+                if (instance == null) {
30
+                    instance = new SharedPreferencesUtils(context);
31
+                }
32
+            }
33
+        }
34
+        return instance;
35
+    }
36
+
37
+
38
+    //    *
39
+//     * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
40
+//     * @param context
41
+//     * @param key
42
+//     * @param object
43
+//
44
+    public boolean setParam(String key, Object object) {
45
+
46
+        String type = object.getClass().getSimpleName();
47
+        SharedPreferences.Editor editor = preferences.edit();
48
+
49
+        if ("String".equals(type)) {
50
+            editor.putString(key, (String) object);
51
+        } else if ("Integer".equals(type)) {
52
+            editor.putInt(key, (Integer) object);
53
+        } else if ("Boolean".equals(type)) {
54
+            editor.putBoolean(key, (Boolean) object);
55
+        } else if ("Float".equals(type)) {
56
+            editor.putFloat(key, (Float) object);
57
+        } else if ("Long".equals(type)) {
58
+            editor.putLong(key, (Long) object);
59
+        }
60
+
61
+        editor.apply();
62
+        return false;
63
+    }
64
+
65
+
66
+    //   *
67
+//     * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
68
+//     * @param context
69
+//     * @param key
70
+//     * @param defaultObject
71
+//     * @return
72
+    public Object getParam(String key, Object defaultObject) {
73
+        String type = defaultObject.getClass().getSimpleName();
74
+        SharedPreferences sp = preferences;
75
+
76
+        if ("String".equals(type)) {
77
+            return sp.getString(key, (String) defaultObject);
78
+        } else if ("Integer".equals(type)) {
79
+            return sp.getInt(key, (Integer) defaultObject);
80
+        } else if ("Boolean".equals(type)) {
81
+            return sp.getBoolean(key, (Boolean) defaultObject);
82
+        } else if ("Float".equals(type)) {
83
+            return sp.getFloat(key, (Float) defaultObject);
84
+        } else if ("Long".equals(type)) {
85
+            return sp.getLong(key, (Long) defaultObject);
86
+        }
87
+
88
+        return null;
89
+    }
90
+
91
+
92
+    /**
93
+     * 保存对象到json对象
94
+     *
95
+     * @param object 播放列表对象
96
+     */
97
+    public void saveJson(String key, Object object) {
98
+        String strJson = "";
99
+        Gson gson = new Gson();
100
+        strJson = gson.toJson(object);
101
+        SharedPreferences.Editor edit = preferences.edit();
102
+        if (edit != null) {
103
+            edit.putString(key, strJson);
104
+            edit.apply();
105
+        }
106
+    }
107
+
108
+    public void clearJson(String key) {
109
+        SharedPreferences.Editor edit = preferences.edit();
110
+        if (edit != null) {
111
+            edit.putString(key, "");
112
+            edit.apply();
113
+        }
114
+    }
115
+
116
+    /**
117
+     * 读取object对象
118
+     *
119
+     * @return
120
+     */
121
+    public <T> Object getFromJson(String key, Class<T> cls) {
122
+        try {
123
+            String json = preferences.getString(key, "");
124
+            if (TextUtils.isEmpty(json)) {
125
+                Log.d("转换失败", "数据为空");
126
+                return null;
127
+            }
128
+            Gson gson = new Gson();
129
+            return gson.fromJson(json, cls);
130
+        } catch (Exception e) {
131
+            Log.d("转换失败", e.toString());
132
+            return null;
133
+        }
134
+    }
135
+
136
+
137
+    public String get(String key) {
138
+        return preferences.getString(key, "");
139
+    }
140
+
141
+}

+ 132
- 0
corelib/src/main/java/com/xhly/corelib/utils/StatusBarUtil.java 파일 보기

@@ -0,0 +1,132 @@
1
+package com.xhly.corelib.utils;
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.Window;
8
+import android.view.WindowManager;
9
+
10
+import com.xhly.corelib.R;
11
+
12
+import java.lang.reflect.Field;
13
+import java.lang.reflect.Method;
14
+
15
+public class StatusBarUtil {
16
+
17
+    /**
18
+     * 设置状态栏黑色字体图标,
19
+     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
20
+     *
21
+     * @param activity
22
+     * @return 1:MIUUI 2:Flyme 3:android6.0
23
+     */
24
+    public static int StatusBarLightMode(Activity activity, Boolean dark) {
25
+        setStatusBarColor(activity);
26
+        int result = 0;
27
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
28
+            if (MIUISetStatusBarLightMode(activity.getWindow(), dark)) {
29
+                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
30
+                result = 1;
31
+            } else if (FlymeSetStatusBarLightMode(activity.getWindow(), dark)) {
32
+                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
33
+                result = 2;
34
+            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
35
+                if (dark)
36
+                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
37
+                else
38
+                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
39
+                result = 3;
40
+            }
41
+        }
42
+        return result;
43
+    }
44
+
45
+    /**
46
+     * 修改状态栏颜色,支持4.4以上版本
47
+     * @param activity
48
+     * @param colorId
49
+     */
50
+    public static void setStatusBarColor(Activity activity, int colorId) {
51
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
52
+            Window window = activity.getWindow();
53
+            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
54
+            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
55
+            window.setStatusBarColor(activity.getResources().getColor(colorId));
56
+        }
57
+    }
58
+
59
+    public static void setStatusBarColor(Activity activity) {
60
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
61
+            Window window = activity.getWindow();
62
+            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
63
+            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
64
+            window.setStatusBarColor(Color.TRANSPARENT);
65
+        }
66
+    }
67
+    /**
68
+     * 设置状态栏字体图标为深色,需要MIUIV6以上
69
+     *
70
+     * @param window 需要设置的窗口
71
+     * @param dark   是否把状态栏字体及图标颜色设置为深色
72
+     * @return boolean 成功执行返回true
73
+     */
74
+    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
75
+        boolean result = false;
76
+        if (window != null) {
77
+            Class clazz = window.getClass();
78
+            try {
79
+                int darkModeFlag = 0;
80
+                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
81
+                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
82
+                darkModeFlag = field.getInt(layoutParams);
83
+                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
84
+                if (dark) {
85
+                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
86
+                } else {
87
+                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
88
+                }
89
+                result = true;
90
+            } catch (Exception e) {
91
+
92
+            }
93
+        }
94
+        return result;
95
+    }
96
+
97
+    /**
98
+     * 设置状态栏图标为深色和魅族特定的文字风格
99
+     * 可以用来判断是否为Flyme用户
100
+     *
101
+     * @param window 需要设置的窗口
102
+     * @param dark   是否把状态栏字体及图标颜色设置为深色
103
+     * @return boolean 成功执行返回true
104
+     */
105
+    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
106
+        boolean result = false;
107
+        if (window != null) {
108
+            try {
109
+                WindowManager.LayoutParams lp = window.getAttributes();
110
+                Field darkFlag = WindowManager.LayoutParams.class
111
+                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
112
+                Field meizuFlags = WindowManager.LayoutParams.class
113
+                        .getDeclaredField("meizuFlags");
114
+                darkFlag.setAccessible(true);
115
+                meizuFlags.setAccessible(true);
116
+                int bit = darkFlag.getInt(null);
117
+                int value = meizuFlags.getInt(lp);
118
+                if (dark) {
119
+                    value |= bit;
120
+                } else {
121
+                    value &= ~bit;
122
+                }
123
+                meizuFlags.setInt(lp, value);
124
+                window.setAttributes(lp);
125
+                result = true;
126
+            } catch (Exception e) {
127
+
128
+            }
129
+        }
130
+        return result;
131
+    }
132
+}

+ 129
- 0
corelib/src/main/java/com/xhly/corelib/utils/SystemUtil.java 파일 보기

@@ -0,0 +1,129 @@
1
+package com.xhly.corelib.utils;
2
+
3
+import android.os.Build;
4
+import android.text.TextUtils;
5
+
6
+import java.lang.reflect.Method;
7
+import java.util.Locale;
8
+
9
+/**
10
+ * 作者:NB的LittleWhite
11
+ * <p>
12
+ * 邮箱:zhan_0313@163.com
13
+ */
14
+public class SystemUtil {
15
+
16
+
17
+    /**
18
+     * 获取当前手机系统语言。
19
+     *
20
+     * @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
21
+     */
22
+    public static String getSystemLanguage() {
23
+        return Locale.getDefault().getLanguage();
24
+    }
25
+
26
+    /**
27
+     * 获取当前系统上的语言列表(Locale列表)
28
+     *
29
+     * @return  语言列表
30
+     */
31
+    public static Locale[] getSystemLanguageList() {
32
+        return Locale.getAvailableLocales();
33
+    }
34
+
35
+    /**
36
+     * 获取当前手机系统版本号
37
+     *
38
+     * @return  系统版本号
39
+     */
40
+    public static String getSystemVersion() {
41
+        String version ="";
42
+        if (isHarmonyOs()) {
43
+            version = "Harmony    " + getHarmonyVersion();
44
+        } else {
45
+            version = "Android    " + Build.VERSION.RELEASE;
46
+        }
47
+        return version;
48
+    }
49
+
50
+    /**
51
+     * 获取手机型号
52
+     *
53
+     * @return  手机型号
54
+     */
55
+    public static String getSystemModel() {
56
+        return Build.MODEL;
57
+    }
58
+
59
+
60
+    /**
61
+     * 获取手机版本号
62
+     *
63
+     * @return  手机型号
64
+     */
65
+    public static String getSystemDisplay() {
66
+        return Build.DISPLAY;
67
+    }
68
+
69
+    /**
70
+     * 获取手机厂商
71
+     *
72
+     * @return  手机厂商
73
+     */
74
+    public static String getDeviceBrand() {
75
+        return Build.BRAND;
76
+    }
77
+
78
+
79
+    /**
80
+     * 是否为鸿蒙系统
81
+     *
82
+     * @return true为鸿蒙系统
83
+     */
84
+    public static boolean isHarmonyOs() {
85
+        try {
86
+            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
87
+            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
88
+            return "Harmony".equalsIgnoreCase(osBrand.toString());
89
+        } catch (Throwable x) {
90
+            return false;
91
+        }
92
+    }
93
+
94
+    /**
95
+     * 获取鸿蒙系统版本号
96
+     *
97
+     * @return 版本号
98
+     */
99
+    public static String getHarmonyVersion() {
100
+        return getProp("hw_sc.build.platform.version", "");
101
+    }
102
+
103
+    private static String getProp(String property, String defaultValue) {
104
+        try {
105
+            Class spClz = Class.forName("android.os.SystemProperties");
106
+            Method method = spClz.getDeclaredMethod("get", String.class);
107
+            String value = (String) method.invoke(spClz, property);
108
+            if (TextUtils.isEmpty(value)) {
109
+                return defaultValue;
110
+            }
111
+            return value;
112
+        } catch (Throwable e) {
113
+            e.printStackTrace();
114
+        }
115
+        return defaultValue;
116
+    }
117
+
118
+    public static String getCpuName() {
119
+        String[] abis = Build.SUPPORTED_ABIS;
120
+        StringBuilder abiStr = new StringBuilder();
121
+        for (String abi : abis) {
122
+            abiStr.append(abi);
123
+            abiStr.append(',');
124
+        }
125
+        return abis[0];
126
+    }
127
+
128
+
129
+}

+ 274
- 0
corelib/src/main/java/com/xhly/corelib/utils/baseUtils.kt 파일 보기

@@ -0,0 +1,274 @@
1
+package com.xhly.corelib.utils
2
+
3
+import android.content.Context
4
+import android.graphics.Bitmap
5
+import android.graphics.BitmapFactory
6
+import android.icu.math.BigDecimal
7
+import android.util.Base64
8
+import java.lang.reflect.ParameterizedType
9
+import java.text.DecimalFormat
10
+import java.text.SimpleDateFormat
11
+import java.util.Calendar
12
+import java.util.Date
13
+
14
+fun <T> getClass(t: Any): Class<T> {
15
+    // 通过反射 获取父类泛型 (T) 对应 Class类
16
+    return (t.javaClass.genericSuperclass as ParameterizedType)
17
+        .actualTypeArguments[0]
18
+            as Class<T>
19
+}
20
+
21
+fun convertBase64ToPic(base64: String): Bitmap? {
22
+    var value = base64
23
+    if (base64.contains(",")) {
24
+        value = base64.split(",")[1]
25
+    }
26
+    val decode = Base64.decode(value, Base64.DEFAULT)
27
+    return BitmapFactory.decodeByteArray(decode, 0, decode.size)
28
+}
29
+
30
+fun Date.toHourDate(format: String = "yyyy-MM-dd HH:mm:ss"): String {
31
+    try {
32
+        var format = SimpleDateFormat(format)
33
+        return format.format(this)
34
+    } catch (e: Exception) {
35
+        return this.toString()
36
+    }
37
+}
38
+
39
+fun Long.toFormat(format: String = "yyyy-MM-dd HH:mm:ss"): String {
40
+    try {
41
+        var format = SimpleDateFormat(format)
42
+        return format.format(this)
43
+    } catch (e: Exception) {
44
+        return this.toString()
45
+    }
46
+}
47
+
48
+fun String.hourToCurrentDate(formatType: String = "HH:mm:ss"): Date {
49
+    try {
50
+        var simpleDateFormat = SimpleDateFormat(formatType)
51
+        var parse = simpleDateFormat.parse(this)
52
+        var hours = parse.hours
53
+        var minutes = parse.minutes
54
+        var seconds = parse.seconds
55
+        var calendar = Calendar.getInstance()
56
+        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH))
57
+        calendar.set(Calendar.HOUR_OF_DAY, hours)
58
+        calendar.set(Calendar.MINUTE, minutes)
59
+        calendar.set(Calendar.SECOND, seconds)
60
+        return calendar.time
61
+    } catch (e: Exception) {
62
+        return Date()
63
+    }
64
+}
65
+
66
+fun Date.getBetweenDay(date: Date): String {
67
+    try {
68
+        var l = (this.time - date.time) / 1000 / 60 / 60 / 24
69
+        return l.toInt().toString()
70
+    } catch (e: Exception) {
71
+        return "0"
72
+    }
73
+}
74
+
75
+
76
+fun String?.NoEmpty(): String {
77
+    if (this == null) {
78
+        return ""
79
+    } else {
80
+        return this
81
+    }
82
+}
83
+
84
+fun Any.LogTag(): String {
85
+    return this.javaClass.name
86
+}
87
+
88
+fun Any.LogShow(s: String) {
89
+    LogUtils.d(LogTag(), s)
90
+}
91
+
92
+fun Double.getScaleSize(
93
+    DECIMAL_POINT_NUMBER: Int = 2,
94
+    roundType: Int = BigDecimal.ROUND_DOWN
95
+): Double {
96
+    return BigDecimal(this.toString()).setScale(DECIMAL_POINT_NUMBER, roundType).toDouble()
97
+}
98
+
99
+fun Double.getScaleSizeString(
100
+    DECIMAL_POINT_NUMBER: Int = 2,
101
+    roundType: Int = BigDecimal.ROUND_DOWN
102
+): String {
103
+    return BigDecimal(this.toString()).setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
104
+        .stripTrailingZeros().toPlainString()
105
+}
106
+
107
+fun String.getScaleSizeString(
108
+    DECIMAL_POINT_NUMBER: Int = 2,
109
+    roundType: Int = BigDecimal.ROUND_DOWN
110
+): String {
111
+    try {
112
+        return BigDecimal(this).setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
113
+            .stripTrailingZeros().toPlainString()
114
+    } catch (e: Exception) {
115
+        return this
116
+    }
117
+}
118
+
119
+fun String.getBigNumString(): String {
120
+    try {
121
+        return BigDecimal(this).toBigDecimal().stripTrailingZeros().toPlainString()
122
+    } catch (e: Exception) {
123
+        return this
124
+    }
125
+}
126
+
127
+fun Double.getBigNumString(): String {
128
+    try {
129
+        return BigDecimal(this).toBigDecimal().stripTrailingZeros().toPlainString()
130
+    } catch (e: Exception) {
131
+        return this.toString()
132
+    }
133
+}
134
+
135
+//对比double大小
136
+fun String.getContrastDouble(
137
+    s: String
138
+): Int {
139
+    return try {
140
+        BigDecimal(this).compareTo(BigDecimal(s))
141
+    } catch (e: Exception) {
142
+        0
143
+    }
144
+}
145
+
146
+fun Double.getMulString(
147
+    d: Double,
148
+    DECIMAL_POINT_NUMBER: Int = 2,
149
+    roundType: Int = BigDecimal.ROUND_DOWN
150
+): String {
151
+    try {
152
+        return BigDecimal(this.toString()).multiply(BigDecimal(d))
153
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
154
+            .stripTrailingZeros().toPlainString()
155
+    } catch (e: Exception) {
156
+        return "0"
157
+    }
158
+}
159
+
160
+fun Double.getSubString(
161
+    s: String,
162
+    DECIMAL_POINT_NUMBER: Int = 2,
163
+    roundType: Int = BigDecimal.ROUND_DOWN
164
+): String {
165
+    return try {
166
+        BigDecimal(this.toString()).subtract(BigDecimal(s))
167
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
168
+            .stripTrailingZeros().toPlainString()
169
+    } catch (e: Exception) {
170
+        "0"
171
+    }
172
+}
173
+
174
+fun Double.getMulString(
175
+    d: String,
176
+    DECIMAL_POINT_NUMBER: Int = 2,
177
+    roundType: Int = BigDecimal.ROUND_DOWN
178
+): String {
179
+    try {
180
+        return BigDecimal(this.toString()).multiply(BigDecimal(d))
181
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
182
+            .stripTrailingZeros().toPlainString()
183
+    } catch (e: Exception) {
184
+        return "0"
185
+    }
186
+}
187
+
188
+fun String.getMulString(
189
+    d: String,
190
+    DECIMAL_POINT_NUMBER: Int = 2,
191
+    roundType: Int = BigDecimal.ROUND_DOWN
192
+): String {
193
+    try {
194
+        return BigDecimal(this).multiply(BigDecimal(d))
195
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
196
+            .stripTrailingZeros().toPlainString()
197
+    } catch (e: Exception) {
198
+        return "0"
199
+    }
200
+}
201
+
202
+
203
+fun Double.getDivString(
204
+    d: Double,
205
+    DECIMAL_POINT_NUMBER: Int = 2,
206
+    roundType: Int = BigDecimal.ROUND_DOWN
207
+): String {
208
+    try {
209
+        return BigDecimal(this.toString()).divide(BigDecimal(d))
210
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
211
+            .stripTrailingZeros().toPlainString()
212
+    } catch (e: Exception) {
213
+        return "0"
214
+    }
215
+}
216
+
217
+
218
+fun String.getDivString(
219
+    d: String,
220
+    DECIMAL_POINT_NUMBER: Int = 2,
221
+    roundType: Int = BigDecimal.ROUND_DOWN
222
+): String {
223
+    try {
224
+        return BigDecimal(this).divide(BigDecimal(d))
225
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
226
+            .stripTrailingZeros().toPlainString()
227
+    } catch (e: Exception) {
228
+        return "0"
229
+    }
230
+}
231
+
232
+fun Double.getDivString(
233
+    d: String,
234
+    DECIMAL_POINT_NUMBER: Int = 2,
235
+    roundType: Int = BigDecimal.ROUND_DOWN
236
+): String {
237
+    try {
238
+        return BigDecimal(this.toString()).divide(BigDecimal(d))
239
+            .setScale(DECIMAL_POINT_NUMBER, roundType).toBigDecimal()
240
+            .stripTrailingZeros().toPlainString()
241
+    } catch (e: Exception) {
242
+        return "0"
243
+    }
244
+}
245
+
246
+fun Double.getPercentageString(
247
+): String {
248
+    val formatter = DecimalFormat("#.##%")
249
+    return formatter.format(this)
250
+}
251
+
252
+fun Context.getVersionName(): String {
253
+    var packageManager = this.packageManager
254
+    var packageInfo = packageManager.getPackageInfo(this.packageName, 0)
255
+    return packageInfo.versionName
256
+}
257
+
258
+//判断是否是链接
259
+fun String.isUrl():Boolean{
260
+    var rgex="^http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?".toRegex()
261
+   return  rgex.containsMatchIn(this)
262
+}
263
+//移除标点
264
+fun String.removePunctuation():String{
265
+    return try {
266
+        if (this.isNotBlank()){
267
+            this.replace(Regex("[^\\w\\s]"), "")
268
+        }else{
269
+            ""
270
+        }
271
+    }catch (e:java.lang.Exception){
272
+        ""
273
+    }
274
+}

+ 17
- 0
corelib/src/test/java/com/xhly/corelib/ExampleUnitTest.kt 파일 보기

@@ -0,0 +1,17 @@
1
+package com.xhly.corelib
2
+
3
+import org.junit.Test
4
+
5
+import org.junit.Assert.*
6
+
7
+/**
8
+ * Example local unit test, which will execute on the development machine (host).
9
+ *
10
+ * See [testing documentation](http://d.android.com/tools/testing).
11
+ */
12
+class ExampleUnitTest {
13
+    @Test
14
+    fun addition_isCorrect() {
15
+        assertEquals(4, 2 + 2)
16
+    }
17
+}

Loading…
취소
저장