feat: 添加Realm-Java依赖

This commit is contained in:
xiaoyan 2023-03-31 11:37:33 +08:00
commit 80a42c8387
18 changed files with 386 additions and 75 deletions

View File

@ -3,9 +3,8 @@ plugins {
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
id 'io.realm.kotlin'
}
apply plugin: "realm-android"
android {
namespace 'com.navinfo.omqs'
compileSdk 33
@ -27,8 +26,8 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
@ -67,10 +66,6 @@ dependencies {
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")
// Realm相关依赖
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'io.realm.kotlin:library-base:1.7.0'
}
//
kapt {

View File

@ -54,6 +54,8 @@
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="@style/Theme.OMQualityInspection" />
<meta-data android:name="ScopedStorage" android:value="true" />
</application>
</manifest>

View File

@ -1,7 +1,16 @@
package com.navinfo.omqs
import io.realm.Realm
class Constant {
companion object {
/**
* sd卡根目录
*/
lateinit var ROOT_PATH: String
lateinit var MAP_PATH: String
lateinit var DATA_PATH: String
/**
* 服务器地址
*/
@ -14,6 +23,7 @@ class Constant {
const val message_version_right_off = "1" //立即发送
const val MESSAGE_PAGE_SIZE = 30 //消息列表一页最多数量
lateinit var realm: Realm
}
}

View File

@ -2,7 +2,12 @@ package com.navinfo.omqs
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
import io.realm.Realm
@HiltAndroidApp
class OMQSApplication : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
}
}

View File

@ -1,10 +0,0 @@
package com.navinfo.omqs
import android.app.Application
class QAApplication: Application() {
override fun onCreate() {
super.onCreate()
}
}

View File

@ -1,28 +1,37 @@
package com.navinfo.omqs.bean
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import io.realm.RealmObject
enum class StatusEnum(val status: Int) {
NONE(0), WAITING(1), LOADING(2), PAUSE(3),
ERROR(4), DONE(5), UPDATE(6)
}
open class OfflineMapCityBean : RealmObject{
var id: String = ""
var fileName: String = ""
var name: String = ""
var url: String = ""
var version: Long = 0L
var fileSize: Long = 0L
var currentSize:Long = 0L
var status: Int = StatusEnum.NONE.status
// status的转换对象
var statusEnum:StatusEnum
get() {
return try {
StatusEnum.values().find { it.status == status }!!
} catch (e: IllegalArgumentException) {
StatusEnum.NONE
}
}
set(value) {
status = value.status
}
constructor() : super()
data class OfflineMapCityBean(
@PrimaryKey
val id: String,
val fileName: String,
val name: String,
val url: String,
val version: Long,
val fileSize: Long,
var currentSize:Long = 0,
var status:Int = NONE
): RealmObject {
companion object Status{
const val NONE = 0 //无状态
const val WAITING = 1 //等待中
const val LOADING = 2 //下载中
const val PAUSE = 3 //暂停
const val ERROR = 4 //错误
const val DONE = 5 //完成
const val UPDATE = 6 //有新版本要更新
}
fun getFileSizeText(): String {
return if (fileSize < 1024.0)
"$fileSize B"

View File

@ -0,0 +1,17 @@
package com.navinfo.omqs.bean
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class OfflineMapCityRealmObject(): RealmObject() {
@PrimaryKey
var id: String = ""
var fileName: String=""
var name: String = ""
var url: String = ""
var version: Long = 0
var fileSize: Long = 0
var currentSize:Long = 0
var status:Int = 0
}

View File

@ -25,11 +25,11 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
class GlobalModule {
@Singleton
@Provides
fun provideApplication(application: Application): OMQSApplication {
return application as OMQSApplication
}
// @Singleton
// @Provides
// fun provideApplication(application: Application): OMQSApplication {
// return application as OMQSApplication
// }
/**
* 注入 网络OKHttp 对象

View File

@ -1,22 +1,34 @@
package com.navinfo.omqs.hilt
import android.util.Log
import android.content.Context
import com.navinfo.collect.library.map.NIMapController
import com.navinfo.omqs.ui.activity.map.MainViewModel
import com.navinfo.omqs.http.offlinemapdownload.OfflineMapDownloadManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.hilt.android.qualifiers.ActivityContext
import dagger.hilt.android.scopes.ActivityRetainedScoped
@InstallIn(ActivityRetainedComponent::class)
@Module
class MainActivityModule {
/**
* 注入地图控制器在activity范围内使用单例
*/
@ActivityRetainedScoped
@Provides
fun providesMapController(): NIMapController = NIMapController()
/**
* 注入离线地图下载管理在activity范围内使用单例
*/
@ActivityRetainedScoped
@Provides
fun providesOfflineMapDownloadManager(@ActivityContext context: Context): OfflineMapDownloadManager =
OfflineMapDownloadManager(context)
/**
* 实验失败这样创建viewmodel不会在activity销毁的时候同时销毁
*/

View File

@ -0,0 +1,48 @@
package com.navinfo.omqs.http.offlinemapdownload
import android.content.Context
import android.os.Environment
import android.text.TextUtils
import com.navinfo.omqs.Constant
import com.navinfo.omqs.bean.OfflineMapCityBean
import dagger.hilt.android.qualifiers.ActivityContext
import java.io.Serializable
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
/**
* 管理离线地图下载
*/
class OfflineMapDownloadManager @Inject constructor(@ActivityContext context: Context) {
/**
* 最多同时下载数量
*/
private val MAX_SCOPE = 5
/**
* 存储有哪些城市需要下载
*/
private val scopeMap: ConcurrentHashMap<String, OfflineMapDownloadScope> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
ConcurrentHashMap<String, OfflineMapDownloadScope>()
}
val downloadFolder: String? by lazy {
Constant.MAP_PATH + "/offline/"
}
/**
* 请求一个下载任务[OfflineMapDownloadScope]
* 这是创建[OfflineMapDownloadScope]的唯一途径,请不要通过其他方式创建[OfflineMapDownloadScope]
*/
fun request(cityBean: OfflineMapCityBean): OfflineMapDownloadScope? {
//没有下载连接的不能下载
if (TextUtils.isEmpty(cityBean.url)) return null
// if(scopeMap.containsKey())
var downloadScope = scopeMap[cityBean.id]
if (downloadScope == null) {
scopeMap[cityBean.id] = OfflineMapDownloadScope(cityBean)
}
return downloadScope
}
}

View File

@ -0,0 +1,26 @@
package com.navinfo.omqs.http.offlinemapdownload
import androidx.lifecycle.MutableLiveData
import com.navinfo.omqs.bean.OfflineMapCityBean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlin.coroutines.EmptyCoroutineContext
/**
* 代表一个下载任务
* [OfflineMapCityBean.id]将做为下载任务的唯一标识
* 不要直接在外部直接创建此对象,那样就可能无法统一管理下载任务,请通过[OfflineMapDownloadManager.download]获取此对象
* 这是一个协程作用域
* EmptyCoroutineContext 表示一个不包含任何元素的协程上下文它通常用于创建新的协程上下文或者作为协程上下文的基础
*/
class OfflineMapDownloadScope(cityBean: OfflineMapCityBean) : CoroutineScope by CoroutineScope(EmptyCoroutineContext) {
/**
*
*/
private var downloadJob: Job? = null
/**
*
*/
private val downloadData = MutableLiveData<OfflineMapCityBean>()
}

View File

@ -2,12 +2,16 @@ package com.navinfo.omqs.ui.activity.login
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.navinfo.omqs.R
import com.navinfo.omqs.databinding.ActivityLoginBinding
import com.navinfo.omqs.ui.activity.map.MainActivity
import com.navinfo.omqs.ui.activity.PermissionsActivity
import com.navinfo.omqs.ui.activity.map.MainActivity
/**
* 登陆页面
@ -16,18 +20,71 @@ class LoginActivity : PermissionsActivity() {
private lateinit var binding: ActivityLoginBinding
private val viewModel by viewModels<LoginViewModel>()
private var loginDialog: AlertDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
binding.loginUserModel = viewModel
binding.lifecycleOwner = this
binding.activity = this
initView()
}
private fun initView() {
//登录校验,初始化成功
viewModel.loginStatus.observe(this) {
when (it) {
LoginStatus.LOGIN_STATUS_NET_LOADING -> {
loginDialog("验证用户信息...")
}
LoginStatus.LOGIN_STATUS_FOLDER_INIT -> {
loginDialog("检查本地数据...")
}
LoginStatus.LOGIN_STATUS_FOLDER_FAILURE -> {
Toast.makeText(this, "文件夹初始化失败", Toast.LENGTH_SHORT).show()
loginDialog?.dismiss()
loginDialog = null
}
LoginStatus.LOGIN_STATUS_NET_FAILURE -> {
Toast.makeText(this, "网络访问失败", Toast.LENGTH_SHORT).show()
loginDialog?.dismiss()
loginDialog = null
}
LoginStatus.LOGIN_STATUS_SUCCESS -> {
val intent = Intent(this@LoginActivity, MainActivity::class.java)
startActivity(intent)
// finish()
loginDialog?.dismiss()
loginDialog = null
}
LoginStatus.LOGIN_STATUS_CANCEL -> {
loginDialog?.dismiss()
loginDialog = null
}
}
}
}
/**
* 登录dialog
*/
private fun loginDialog(message: String) {
if (loginDialog == null) {
loginDialog = MaterialAlertDialogBuilder(
this, com.google.android.material.R.style.MaterialAlertDialog_Material3
).setTitle("登录").setMessage(message).show()
loginDialog!!.setCanceledOnTouchOutside(true)
loginDialog!!.setOnCancelListener {
viewModel.cancelLogin()
}
} else {
loginDialog!!.setMessage(message)
}
}
//进应用根本不调用,待查
override fun onPermissionsGranted() {
// 获取权限后初始化Realm设置
viewModel
Log.e("jingo","调用了吗")
}
override fun onPermissionsDenied() {
@ -37,8 +94,8 @@ class LoginActivity : PermissionsActivity() {
* 处理登录按钮
*/
fun onClickLoginButton() {
val intent = Intent(this@LoginActivity, MainActivity::class.java)
startActivity(intent)
// finish()
viewModel.onClickLoginBtn(
this, binding.loginUsername.text.toString(), binding.loginPassword.text.toString()
)
}
}
}

View File

@ -1,23 +1,79 @@
package com.navinfo.omqs.ui.activity.login
import android.content.Context
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.navinfo.omqs.Constant
import com.navinfo.omqs.bean.LoginUserBean
import com.navinfo.omqs.bean.OfflineMapCityBean
import io.realm.kotlin.RealmConfiguration
import com.navinfo.omqs.bean.OfflineMapCityRealmObject
import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.*
import okio.ByteString.Companion.toByteString
import okio.IOException
import java.io.File
import java.math.BigInteger
class LoginViewModel : ViewModel() {
enum class LoginStatus {
/**
* 访问服务器登陆中
*/
LOGIN_STATUS_NET_LOADING,
/**
* 初始化文件夹
*/
LOGIN_STATUS_FOLDER_INIT,
/**
* 创建文件夹失败
*/
LOGIN_STATUS_FOLDER_FAILURE,
/**
* 网络访问失败
*/
LOGIN_STATUS_NET_FAILURE,
/**
* 成功
*/
LOGIN_STATUS_SUCCESS,
/**
* 取消
*/
LOGIN_STATUS_CANCEL,
}
class LoginViewModel(
) : ViewModel() {
//用户信息
val loginUser: MutableLiveData<LoginUserBean> = MutableLiveData()
//是不是登录成功
val loginStatus: MutableLiveData<LoginStatus> = MutableLiveData()
var jobLogin: Job? = null;
init {
loginUser.value = LoginUserBean(username = "admin", password = "123456")
}
fun initRealm() {
val config = RealmConfiguration.Builder(schema = setOf(OfflineMapCityBean::class)).directory(
)
val password = "password".encodeToByteArray().copyInto(ByteArray(64))
// 1110000011000010111001101110011011101110110111101110010011001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Log.d("", "密码是: ${BigInteger(1, password).toString(2).padStart(64, '0')}")
val config = RealmConfiguration.Builder()
.directory(File(Constant.DATA_PATH))
.name("HDData")
// .encryptionKey(password)
.build()
Constant.realm = Realm.getInstance(config)
}
/**
@ -27,4 +83,87 @@ class LoginViewModel : ViewModel() {
loginUser.value!!.username = "admin2"
loginUser.postValue(loginUser.value)
}
/**
* 点击
*/
fun onClickLoginBtn(context: Context, userName: String, password: String) {
if (userName.isEmpty()) {
Toast.makeText(context, "请输入用户名", Toast.LENGTH_SHORT).show()
}
if (password.isEmpty()) {
Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show()
}
//不指定IO会在主线程里运行
jobLogin = viewModelScope.launch(Dispatchers.IO) {
loginCheck(context, userName, password)
Log.e("jingo", "运行完了1${Thread.currentThread().name}")
}
}
/**
* 如果不用挂起函数的方式直接把下面这段代码替换到上面在delay之后线程和delay之前不是同一个有啥影响未知
*/
private suspend fun loginCheck(context: Context, userName: String, password: String) {
Log.e("jingo", "我在哪个线程里?${Thread.currentThread().name}")
//上面调用了线程切换,这里不用调用,即使调用了还是在同一个线程中,除非自定义协程域?(待验证)
// withContext(Dispatchers.IO) {
Log.e("jingo", "delay之前${Thread.currentThread().name}")
//网络访问
loginStatus.postValue(LoginStatus.LOGIN_STATUS_NET_LOADING)
//假装网络访问等待3秒
delay(3000)
//文件夹初始化
try {
loginStatus.postValue(LoginStatus.LOGIN_STATUS_FOLDER_INIT)
createRootFolder(context)
// 初始化Realm
initRealm()
} catch (e: IOException) {
loginStatus.postValue(LoginStatus.LOGIN_STATUS_FOLDER_FAILURE)
}
//假装解压文件等
delay(1000)
loginStatus.postValue(LoginStatus.LOGIN_STATUS_SUCCESS)
Log.e("jingo", "delay之后${Thread.currentThread().name}")
// }
}
@Throws(IOException::class)
private fun createRootFolder(context: Context) {
// 在SD卡创建项目目录
val sdCardPath = context.getExternalFilesDir(null)
sdCardPath?.let {
Constant.ROOT_PATH = sdCardPath.absolutePath
Constant.MAP_PATH = Constant.ROOT_PATH + "/map/"
Constant.DATA_PATH = Constant.ROOT_PATH + "/data/"
with(File(Constant.MAP_PATH)) {
if(!this.exists()) this.mkdirs()
}
with(File(Constant.DATA_PATH)) {
if(!this.exists()) this.mkdirs()
}
}
}
/**
* 取消登录
*/
fun cancelLogin() {
Log.e("jingo", "取消了?${Thread.currentThread().name}")
jobLogin?.let {
it.cancel()
loginStatus.postValue(LoginStatus.LOGIN_STATUS_CANCEL)
}
}
override fun onCleared() {
super.onCleared()
cancelLogin()
}
}

View File

@ -1,12 +1,14 @@
package com.navinfo.omqs.ui.activity.map
import android.os.Bundle
import android.provider.ContactsContract.Contacts
import android.util.Log
import androidx.activity.viewModels
import androidx.core.view.WindowCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.viewModelScope
import com.navinfo.collect.library.map.NIMapController
import com.navinfo.omqs.Constant
import com.navinfo.omqs.R
import com.navinfo.omqs.databinding.ActivityMainBinding
import com.navinfo.omqs.ui.activity.BaseActivity
@ -32,7 +34,12 @@ class MainActivity : BaseActivity() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
//初始化地图
mapController.init(this, binding.mapView.mainActivityMap)
mapController.init(
this,
binding.mapView.mainActivityMap,
null,
Constant.ROOT_PATH + "/map/"
)
//关联生命周期
binding.lifecycleOwner = this
//给xml转递对象
@ -59,7 +66,7 @@ class MainActivity : BaseActivity() {
super.onDestroy()
mapController.mMapView.onDestroy()
mapController.locationLayerHandler.stopLocation()
Log.e("jingo","MainActivity 销毁")
Log.e("jingo", "MainActivity 销毁")
}
override fun onResume() {

View File

@ -9,6 +9,5 @@ plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'io.realm.kotlin' version '1.7.0' apply false
id 'com.google.dagger.hilt.android' version '2.44' apply false
}

View File

@ -5,6 +5,7 @@ import android.util.Log
import com.navinfo.collect.library.map.handler.*
import com.navinfo.collect.library.map.maphandler.MeasureLayerHandler
import com.navinfo.collect.library.map.handler.ViewportHandler
import com.navinfo.collect.library.system.Constant
/**
* 地图控制器
@ -22,7 +23,8 @@ class NIMapController {
lateinit var measureLayerHandler: MeasureLayerHandler
fun init(context: Context, mapView: NIMapView, options: NIMapOptions? = null) {
fun init(context: Context, mapView: NIMapView, options: NIMapOptions? = null, mapPath: String) {
Constant.MAP_PATH = mapPath
layerManagerHandler = LayerManagerHandler(context, mapView)
locationLayerHandler = LocationLayerHandler(context, mapView)
animationHandler = AnimationHandler(context, mapView)

View File

@ -5,6 +5,7 @@ import android.os.Environment
import com.navinfo.collect.library.map.NIMapView
import com.navinfo.collect.library.map.NIMapView.LAYER_GROUPS
import com.navinfo.collect.library.map.source.NavinfoMapRastorTileSource
import com.navinfo.collect.library.system.Constant
import okhttp3.Cache
import okhttp3.OkHttpClient
import org.oscim.layers.Layer
@ -18,7 +19,6 @@ import java.io.File
*/
class LayerManagerHandler(context: Context, mapView: NIMapView) :
BaseHandler(context, mapView) {
lateinit var mLocationLayer: LocationLayer
private var baseRasterLayer: Layer? = null
init {
@ -62,7 +62,7 @@ class LayerManagerHandler(context: Context, mapView: NIMapView) :
// 如果使用缓存
if (useCache) {
val cacheDirectory: File =
File(Environment.getExternalStorageState() + "/" + "lalalal", "tiles-raster")
File(Constant.MAP_PATH, "tiles-raster")
val cacheSize = 300 * 1024 * 1024 // 300 MB
val cache = Cache(cacheDirectory, cacheSize.toLong())
builder.cache(cache)

View File

@ -11,14 +11,7 @@ import java.util.Map;
public class Constant {
public static String SD_PATH = Environment.getExternalStorageDirectory() + "";
public static String ROOT_PATH = SD_PATH + "/NavinfoCollect";
public static String PHOTO_PATH = ROOT_PATH + "/image";
public static double CONVERSION_FACTOR = 1000000d;
public static String MAP_PATH = Environment.getExternalStorageDirectory() + "/map/";
public static void setVisibleTypeMap(Map<String, Boolean> visibleTypeMap) {
Map<String, Boolean> HD_LAYER_VISIABLE_MAP= new HashMap<>();