將分割設定值存入Shared Preference內
This commit is contained in:
parent
87f731168f
commit
2e8d8a6381
@ -3,6 +3,7 @@
|
||||
package="com.ray650128.gstreamer_demo_app">
|
||||
|
||||
<application
|
||||
android:name=".MyApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
||||
@ -0,0 +1,48 @@
|
||||
package com.ray650128.gstreamer_demo_app
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class MyApplication : Application(), Application.ActivityLifecycleCallbacks {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
instance = this
|
||||
}
|
||||
|
||||
private var currentActivity: Activity? = null
|
||||
|
||||
override fun onActivityCreated(p0: Activity, p1: Bundle?) {}
|
||||
|
||||
override fun onActivityStarted(p0: Activity) {}
|
||||
|
||||
override fun onActivityResumed(p0: Activity) {
|
||||
currentActivity = p0
|
||||
}
|
||||
|
||||
override fun onActivityPaused(p0: Activity) {}
|
||||
|
||||
override fun onActivityStopped(p0: Activity) {}
|
||||
|
||||
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {}
|
||||
|
||||
override fun onActivityDestroyed(p0: Activity) {
|
||||
if (currentActivity?.equals(p0) == true) {
|
||||
Log.e("APP", "EXIT...")
|
||||
onTerminate()
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
var instance: Application? = null
|
||||
|
||||
fun getAppContext(): Context {
|
||||
return instance!!.applicationContext
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.ray650128.gstreamer_demo_app
|
||||
|
||||
import android.content.Context
|
||||
import com.ray650128.gstreamer_demo_app.ui.mainScreen.MainViewModel
|
||||
|
||||
/**
|
||||
* Shared Preferences 工具類別
|
||||
* @author Raymond Yang
|
||||
*/
|
||||
object PreferenceUtil {
|
||||
private const val MAIN_KEY = "GST_DEMO_APP"
|
||||
private const val IS_FIRST_OPEN_KEY = "IS_FIRST_OPEN_KEY"
|
||||
private const val LAST_SPLIT_MODE = "LAST_SPLIT_MODE"
|
||||
|
||||
private val sharedPreferences = MyApplication.getAppContext().getSharedPreferences(MAIN_KEY, Context.MODE_PRIVATE)
|
||||
|
||||
var isFirstOpen: Boolean
|
||||
get() = sharedPreferences.getBoolean(IS_FIRST_OPEN_KEY, true)
|
||||
set(value) = sharedPreferences.edit().putBoolean(IS_FIRST_OPEN_KEY, value).apply()
|
||||
|
||||
var lastSplitMode: Int
|
||||
get() = sharedPreferences.getInt(LAST_SPLIT_MODE, MainViewModel.PAGE_MODE_ONE)
|
||||
set(value) = sharedPreferences.edit().putInt(LAST_SPLIT_MODE, value).apply()
|
||||
}
|
||||
@ -57,14 +57,9 @@ class MainActivity : AppCompatActivity() {
|
||||
registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
|
||||
override fun onPageSelected(position: Int) {
|
||||
super.onPageSelected(position)
|
||||
/*splitVideoViewAdapter.stop(currentPage)*/
|
||||
currentPage = position
|
||||
splitViewModel.activePage.postValue(position)
|
||||
Log.d("Split", "currentPage: $currentPage")
|
||||
/*for (i in 0 until splitVideoViewAdapter.itemCount) {
|
||||
if (i == currentPage) continue
|
||||
splitVideoViewAdapter.stop(i)
|
||||
}*/
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.ray650128.gstreamer_demo_app.ui.mainScreen
|
||||
import androidx.lifecycle.MediatorLiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.ray650128.gstreamer_demo_app.PreferenceUtil
|
||||
import com.ray650128.gstreamer_demo_app.model.Device
|
||||
|
||||
class MainViewModel: ViewModel() {
|
||||
@ -17,7 +18,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "hs22601576",
|
||||
stream1 = "/media/video1",
|
||||
stream2 = "/media/video2",
|
||||
//rtspUrl = "rtsp://admin:hs22601576@@192.168.0.73:554/media/video2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.77",
|
||||
@ -27,7 +27,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "admin",
|
||||
stream1 = "/media/video1",
|
||||
stream2 = "/media/video2",
|
||||
//rtspUrl = "rtsp://admin:admin@192.168.0.77:554/media/video2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.79",
|
||||
@ -37,7 +36,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "1q2w3e4r!",
|
||||
stream1 = "/media/video1",
|
||||
stream2 = "/media/video2",
|
||||
//rtspUrl = "rtsp://admin:1q2w3e4r!@192.168.0.79:554/media/video2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.88",
|
||||
@ -47,7 +45,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "1q2w3e4r~",
|
||||
stream1 = "/media/video1",
|
||||
stream2 = "/media/video2",
|
||||
//rtspUrl = "rtsp://admin:1q2w3e4r~@211.23.78.226:8588/media/video2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.74",
|
||||
@ -57,7 +54,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "admin",
|
||||
stream1 = "/v01",
|
||||
stream2 = "/v02",
|
||||
//rtspUrl = "rtsp://admin:admin@211.23.78.226:8574/v02"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.75",
|
||||
@ -67,7 +63,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "admin",
|
||||
stream1 = "/v01",
|
||||
stream2 = "/v02",
|
||||
//rtspUrl = "rtsp://admin:admin@211.23.78.226:8575/v02"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.76",
|
||||
@ -77,7 +72,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "123456",
|
||||
stream1 = "/profile1",
|
||||
stream2 = "/profile2",
|
||||
//rtspUrl = "rtsp://admin:123456@211.23.78.226:8576/profile2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.82",
|
||||
@ -87,7 +81,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "123456",
|
||||
stream1 = "/profile1",
|
||||
stream2 = "/profile2",
|
||||
//rtspUrl = "rtsp://admin:123456@192.168.0.82:554/profile2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.84",
|
||||
@ -97,7 +90,6 @@ class MainViewModel: ViewModel() {
|
||||
password = "123456",
|
||||
stream1 = "/profile1",
|
||||
stream2 = "/profile2",
|
||||
//rtspUrl = "rtsp://admin:123456@192.168.0.84:554/profile2"
|
||||
),
|
||||
Device(
|
||||
deviceName = "192.168.0.95",
|
||||
@ -107,13 +99,10 @@ class MainViewModel: ViewModel() {
|
||||
password = "123456",
|
||||
stream1 = "/profile1",
|
||||
stream2 = "/profile2",
|
||||
//rtspUrl = "rtsp://admin:123456@192.168.0.95:554/profile2"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private var splitModeInt: Int = PAGE_MODE_ONE
|
||||
|
||||
val splitMode: MutableLiveData<Int> by lazy { MutableLiveData<Int>() }
|
||||
|
||||
val cameraList: MediatorLiveData<List<List<Device>>> by lazy {
|
||||
@ -125,21 +114,21 @@ class MainViewModel: ViewModel() {
|
||||
}
|
||||
|
||||
init {
|
||||
setSplitMode(PAGE_MODE_ONE)
|
||||
splitMode.postValue(PreferenceUtil.lastSplitMode)
|
||||
}
|
||||
|
||||
fun setSplitMode(mode: Int) {
|
||||
splitModeInt = mode
|
||||
splitMode.postValue(splitModeInt)
|
||||
PreferenceUtil.lastSplitMode = mode
|
||||
splitMode.postValue(PreferenceUtil.lastSplitMode)
|
||||
}
|
||||
|
||||
private fun updateCameraList(dbData: List<Device>?): List<List<Device>>? {
|
||||
if (dbData.isNullOrEmpty()) return null
|
||||
val tmpData = ArrayList<List<Device>>()
|
||||
for (index in uriList.indices step (splitModeInt)) {
|
||||
for (index in uriList.indices step (PreferenceUtil.lastSplitMode)) {
|
||||
if (index == uriList.size) break
|
||||
val tmpSubData = ArrayList<Device>()
|
||||
for (subIndex in 0 until (splitModeInt)) {
|
||||
for (subIndex in 0 until (PreferenceUtil.lastSplitMode)) {
|
||||
val dataIndex = index + subIndex
|
||||
if (dataIndex !in uriList.indices) break
|
||||
tmpSubData.add(uriList[dataIndex])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user