diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml index 9f6b0ae..d6f55c9 100644 --- a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml +++ b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml @@ -6,6 +6,6 @@ \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml index 9f6b0ae..d6f55c9 100644 --- a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml +++ b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml @@ -6,6 +6,6 @@ \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_minus.xml b/app/src/main/res/drawable/ic_minus.xml index 98d3e0b..a399f5f 100644 --- a/app/src/main/res/drawable/ic_minus.xml +++ b/app/src/main/res/drawable/ic_minus.xml @@ -1,6 +1,6 @@ variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml index 9f6b0ae..d6f55c9 100644 --- a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml +++ b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml @@ -6,6 +6,6 @@ \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_minus.xml b/app/src/main/res/drawable/ic_minus.xml index 98d3e0b..a399f5f 100644 --- a/app/src/main/res/drawable/ic_minus.xml +++ b/app/src/main/res/drawable/ic_minus.xml @@ -1,6 +1,6 @@ variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml index 9f6b0ae..d6f55c9 100644 --- a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml +++ b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml @@ -6,6 +6,6 @@ \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_minus.xml b/app/src/main/res/drawable/ic_minus.xml index 98d3e0b..a399f5f 100644 --- a/app/src/main/res/drawable/ic_minus.xml +++ b/app/src/main/res/drawable/ic_minus.xml @@ -1,6 +1,6 @@ + android:layout_height="wrap_content" + android:background="@color/backgroundColor"> @@ -127,7 +128,7 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:layout_margin="@dimen/dp_10" + android:layout_margin="@dimen/dp_7" android:orientation="horizontal"> + android:layout_height="match_parent" + android:background="@color/backgroundColor" /> @@ -192,12 +194,13 @@ + android:background="@color/backgroundColor" + android:orientation="vertical"> @@ -251,6 +255,7 @@ @@ -324,6 +329,7 @@ @@ -343,7 +349,7 @@ android:gravity="center" app:ctrl_backgroundColor="@color/white" app:ctrl_borderColor="#64BFEB" - app:ctrl_borderStroke="@dimen/dp_5" + app:ctrl_borderStroke="@dimen/dp_3" app:ctrl_diameter="@dimen/dp_150" /> diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml index 9f6b0ae..d6f55c9 100644 --- a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml +++ b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml @@ -6,6 +6,6 @@ \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_minus.xml b/app/src/main/res/drawable/ic_minus.xml index 98d3e0b..a399f5f 100644 --- a/app/src/main/res/drawable/ic_minus.xml +++ b/app/src/main/res/drawable/ic_minus.xml @@ -1,6 +1,6 @@ + android:layout_height="wrap_content" + android:background="@color/backgroundColor"> @@ -127,7 +128,7 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:layout_margin="@dimen/dp_10" + android:layout_margin="@dimen/dp_7" android:orientation="horizontal"> + android:layout_height="match_parent" + android:background="@color/backgroundColor" /> @@ -192,12 +194,13 @@ + android:background="@color/backgroundColor" + android:orientation="vertical"> @@ -251,6 +255,7 @@ @@ -324,6 +329,7 @@ @@ -343,7 +349,7 @@ android:gravity="center" app:ctrl_backgroundColor="@color/white" app:ctrl_borderColor="#64BFEB" - app:ctrl_borderStroke="@dimen/dp_5" + app:ctrl_borderStroke="@dimen/dp_3" app:ctrl_diameter="@dimen/dp_150" /> diff --git a/app/src/main/res/layout/item_point_list_rv.xml b/app/src/main/res/layout/item_point_list_rv.xml new file mode 100644 index 0000000..57b2727 --- /dev/null +++ b/app/src/main/res/layout/item_point_list_rv.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 9158bd9..796cfcc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 33 @@ -46,6 +47,12 @@ viewBinding = true } + greendao { + schemaVersion 1//数据库版本号 + targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录 + daoPackage "${defaultConfig.applicationId}.greendao"//设置DaoMaster、DaoSession、Dao包名 + } + applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = "br_endoscope_" + getBuildDate() + "_" + defaultConfig.versionName + ".apk" @@ -87,4 +94,8 @@ implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' //TCP implementation 'io.netty:netty-all:4.1.23.Final' + //数据库框架 + implementation 'org.greenrobot:greendao:3.3.0' + //数据库升级 + implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1' } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt new file mode 100644 index 0000000..cd9bc1a --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/adapter/CameraPointAdapter.kt @@ -0,0 +1,78 @@ +package com.casic.endoscope.adapter + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.CheckBox +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import com.casic.endoscope.R +import com.casic.endoscope.bean.CameraPointBean +import com.pengxh.kt.lite.adapter.ViewHolder + +class CameraPointAdapter( + context: Context, private val dataRows: MutableList +) : RecyclerView.Adapter() { + + private var inflater: LayoutInflater = LayoutInflater.from(context) + private var multipleSelected = mutableSetOf() + private var selectedItems = ArrayList() + + override fun getItemCount(): Int = dataRows.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) + } + + @SuppressLint("NotifyDataSetChanged") + fun setRefreshData(dataRows: MutableList) { + this.dataRows.clear() + this.dataRows.addAll(dataRows) + notifyDataSetChanged() + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = dataRows[position] + holder.setText(R.id.stepNameView, "步骤 ${item.id}") + .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") + .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") + + val linearLayout = holder.itemView.findViewById(R.id.rootView) + //item背景色 + if (position % 2 == 0) { + linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) + } else { + linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) + } + + //多选 + val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) + + multiCheckBox.isSelected = multipleSelected.contains(position) + multiCheckBox.setOnClickListener { + if (multipleSelected.contains(position)) { + multipleSelected.remove(position) + selectedItems.remove(dataRows[position]) + holder.itemView.isSelected = false + } else { + multipleSelected.add(position) + selectedItems.add(dataRows[position]) + holder.itemView.isSelected = true + } + + itemCheckedListener?.onItemChecked(selectedItems) + } + } + + private var itemCheckedListener: OnItemCheckedListener? = null + + interface OnItemCheckedListener { + fun onItemChecked(items: ArrayList) + } + + fun setOnItemCheckedListener(listener: OnItemCheckedListener) { + itemCheckedListener = listener + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt index 830a0d9..6439217 100644 --- a/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt +++ b/app/src/main/java/com/casic/endoscope/base/BaseApplication.kt @@ -1,6 +1,9 @@ package com.casic.endoscope.base import android.app.Application +import com.casic.endoscope.greendao.DaoMaster +import com.casic.endoscope.greendao.DaoSession +import com.casic.endoscope.utils.EndoscopeDevOpenHelper import com.pengxh.kt.lite.utils.SaveKeyValues import kotlin.properties.Delegates @@ -12,11 +15,24 @@ private var application: BaseApplication by Delegates.notNull() fun get() = application + + private lateinit var daoSession: DaoSession } override fun onCreate() { super.onCreate() application = this SaveKeyValues.initSharedPreferences(this) + initDataBase() + } + + private fun initDataBase() { + val devOpenHelper = EndoscopeDevOpenHelper(this, "Endoscope.db", null) + val daoMaster = DaoMaster(devOpenHelper.writableDatabase) + daoSession = daoMaster.newSession() + } + + fun getDaoSession(): DaoSession { + return daoSession } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java new file mode 100644 index 0000000..009c2b8 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/bean/CameraPointBean.java @@ -0,0 +1,53 @@ +package com.casic.endoscope.bean; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +/** + * 必须是java,不能是kt + */ +@Entity +public class CameraPointBean { + + @Id(autoincrement = true) + private Long id;//主键自增 + + private int hAngle;//水平角度 + private int vAngle;//垂直角度 + + @Generated(hash = 1444859110) + public CameraPointBean(Long id, int hAngle, int vAngle) { + this.id = id; + this.hAngle = hAngle; + this.vAngle = vAngle; + } + + @Generated(hash = 44086073) + public CameraPointBean() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getHAngle() { + return this.hAngle; + } + + public void setHAngle(int hAngle) { + this.hAngle = hAngle; + } + + public int getVAngle() { + return this.vAngle; + } + + public void setVAngle(int vAngle) { + this.vAngle = vAngle; + } +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java new file mode 100644 index 0000000..b445947 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/CameraPointBeanDao.java @@ -0,0 +1,127 @@ +package com.casic.endoscope.greendao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.internal.DaoConfig; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "CAMERA_POINT_BEAN". +*/ +public class CameraPointBeanDao extends AbstractDao { + + public static final String TABLENAME = "CAMERA_POINT_BEAN"; + + /** + * Properties of entity CameraPointBean.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property HAngle = new Property(1, int.class, "hAngle", false, "H_ANGLE"); + public final static Property VAngle = new Property(2, int.class, "vAngle", false, "V_ANGLE"); + } + + + public CameraPointBeanDao(DaoConfig config) { + super(config); + } + + public CameraPointBeanDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"CAMERA_POINT_BEAN\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"H_ANGLE\" INTEGER NOT NULL ," + // 1: hAngle + "\"V_ANGLE\" INTEGER NOT NULL );"); // 2: vAngle + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAMERA_POINT_BEAN\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, CameraPointBean entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindLong(2, entity.getHAngle()); + stmt.bindLong(3, entity.getVAngle()); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public CameraPointBean readEntity(Cursor cursor, int offset) { + CameraPointBean entity = new CameraPointBean( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getInt(offset + 1), // hAngle + cursor.getInt(offset + 2) // vAngle + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, CameraPointBean entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setHAngle(cursor.getInt(offset + 1)); + entity.setVAngle(cursor.getInt(offset + 2)); + } + + @Override + protected final Long updateKeyAfterInsert(CameraPointBean entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(CameraPointBean entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(CameraPointBean entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java new file mode 100644 index 0000000..76125b5 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoMaster.java @@ -0,0 +1,96 @@ +package com.casic.endoscope.greendao; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.util.Log; + +import org.greenrobot.greendao.AbstractDaoMaster; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseOpenHelper; +import org.greenrobot.greendao.database.StandardDatabase; +import org.greenrobot.greendao.identityscope.IdentityScopeType; + + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * Master of DAO (schema version 1): knows all DAOs. + */ +public class DaoMaster extends AbstractDaoMaster { + public static final int SCHEMA_VERSION = 1; + + /** Creates underlying database table using DAOs. */ + public static void createAllTables(Database db, boolean ifNotExists) { + CameraPointBeanDao.createTable(db, ifNotExists); + } + + /** Drops underlying database table using DAOs. */ + public static void dropAllTables(Database db, boolean ifExists) { + CameraPointBeanDao.dropTable(db, ifExists); + } + + /** + * WARNING: Drops all table on Upgrade! Use only during development. + * Convenience method using a {@link DevOpenHelper}. + */ + public static DaoSession newDevSession(Context context, String name) { + Database db = new DevOpenHelper(context, name).getWritableDb(); + DaoMaster daoMaster = new DaoMaster(db); + return daoMaster.newSession(); + } + + public DaoMaster(SQLiteDatabase db) { + this(new StandardDatabase(db)); + } + + public DaoMaster(Database db) { + super(db, SCHEMA_VERSION); + registerDaoClass(CameraPointBeanDao.class); + } + + public DaoSession newSession() { + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); + } + + public DaoSession newSession(IdentityScopeType type) { + return new DaoSession(db, type, daoConfigMap); + } + + /** + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - + */ + public static abstract class OpenHelper extends DatabaseOpenHelper { + public OpenHelper(Context context, String name) { + super(context, name, SCHEMA_VERSION); + } + + public OpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(Database db) { + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + createAllTables(db, false); + } + } + + /** WARNING: Drops all table on Upgrade! Use only during development. */ + public static class DevOpenHelper extends OpenHelper { + public DevOpenHelper(Context context, String name) { + super(context, name); + } + + public DevOpenHelper(Context context, String name, CursorFactory factory) { + super(context, name, factory); + } + + @Override + public void onUpgrade(Database db, int oldVersion, int newVersion) { + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + dropAllTables(db, true); + onCreate(db); + } + } + +} diff --git a/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java new file mode 100644 index 0000000..ed2f9c0 --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/greendao/DaoSession.java @@ -0,0 +1,46 @@ +package com.casic.endoscope.greendao; + +import com.casic.endoscope.bean.CameraPointBean; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.AbstractDaoSession; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.internal.DaoConfig; + +import java.util.Map; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. + +/** + * {@inheritDoc} + * + * @see org.greenrobot.greendao.AbstractDaoSession + */ +public class DaoSession extends AbstractDaoSession { + + private final DaoConfig cameraPointBeanDaoConfig; + + private final CameraPointBeanDao cameraPointBeanDao; + + public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> + daoConfigMap) { + super(db); + + cameraPointBeanDaoConfig = daoConfigMap.get(CameraPointBeanDao.class).clone(); + cameraPointBeanDaoConfig.initIdentityScope(type); + + cameraPointBeanDao = new CameraPointBeanDao(cameraPointBeanDaoConfig, this); + + registerDao(CameraPointBean.class, cameraPointBeanDao); + } + + public void clear() { + cameraPointBeanDaoConfig.clearIdentityScope(); + } + + public CameraPointBeanDao getCameraPointBeanDao() { + return cameraPointBeanDao; + } + +} diff --git a/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt new file mode 100644 index 0000000..18a424b --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/DataBaseManager.kt @@ -0,0 +1,40 @@ +package com.casic.endoscope.utils + +import com.casic.endoscope.base.BaseApplication +import com.casic.endoscope.bean.CameraPointBean +import com.casic.endoscope.greendao.CameraPointBeanDao + +class DataBaseManager private constructor() { + + companion object { + //Kotlin委托模式双重锁单例 + val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { + DataBaseManager() + } + } + + private var cameraPointDao = BaseApplication.get().getDaoSession().cameraPointBeanDao + + fun cacheCameraPoint(h: Int, v: Int) { + val bean = CameraPointBean() + bean.hAngle = h + bean.vAngle = v + cameraPointDao.insert(bean) + } + + fun deleteCameraPoint(id: Long) { + cameraPointDao.delete(queryCameraPointById(id)) + } + + fun deleteAllCameraPoint() { + cameraPointDao.deleteAll() + } + + fun queryCameraPointById(id: Long): CameraPointBean { + return cameraPointDao.queryBuilder() + .where(CameraPointBeanDao.Properties.Id.eq(id)) + .unique() + } + + fun loadAllCameraPoint(): MutableList = cameraPointDao.loadAll() +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt new file mode 100644 index 0000000..507b1ff --- /dev/null +++ b/app/src/main/java/com/casic/endoscope/utils/EndoscopeDevOpenHelper.kt @@ -0,0 +1,28 @@ +package com.casic.endoscope.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CursorFactory +import com.casic.endoscope.greendao.CameraPointBeanDao +import com.casic.endoscope.greendao.DaoMaster +import com.github.yuweiguocn.library.greendao.MigrationHelper +import com.github.yuweiguocn.library.greendao.MigrationHelper.ReCreateAllTableListener +import org.greenrobot.greendao.database.Database + +class EndoscopeDevOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : + DaoMaster.DevOpenHelper(context, name, factory) { + + override fun onUpgrade(db: Database?, oldVersion: Int, newVersion: Int) { + MigrationHelper.migrate( + db, object : ReCreateAllTableListener { + override fun onCreateAllTables(db: Database, ifNotExists: Boolean) { + DaoMaster.createAllTables(db, ifNotExists) + } + + override fun onDropAllTables(db: Database, ifExists: Boolean) { + DaoMaster.dropAllTables(db, ifExists) + } + }, + CameraPointBeanDao::class.java + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt index 4d20b38..1da4409 100644 --- a/app/src/main/java/com/casic/endoscope/view/MainActivity.kt +++ b/app/src/main/java/com/casic/endoscope/view/MainActivity.kt @@ -3,15 +3,20 @@ import android.annotation.SuppressLint import android.graphics.PixelFormat import android.os.Bundle +import android.os.Handler +import android.os.Message import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import androidx.lifecycle.lifecycleScope +import com.casic.endoscope.adapter.CameraPointAdapter +import com.casic.endoscope.bean.CameraPointBean import com.casic.endoscope.databinding.ActivityMainBinding import com.casic.endoscope.extensions.createVideoFileDir import com.casic.endoscope.extensions.getChannel import com.casic.endoscope.extensions.toTime +import com.casic.endoscope.utils.DataBaseManager import com.casic.endoscope.utils.ProjectConstant import com.casic.endoscope.utils.hk.MessageCodeHub import com.casic.endoscope.utils.hk.SDKGuider @@ -25,6 +30,7 @@ import com.pengxh.kt.lite.extensions.getStatusBarHeight import com.pengxh.kt.lite.extensions.navigatePageTo import com.pengxh.kt.lite.extensions.show +import com.pengxh.kt.lite.utils.WeakReferenceHandler import com.pengxh.kt.lite.widget.SteeringWheelView import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -36,12 +42,14 @@ import java.util.TimerTask @SuppressLint("all") -class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback { +class MainActivity : KotlinBaseActivity(), SurfaceHolder.Callback, + Handler.Callback { private val kTag = "MainActivity" private val hkSDK by lazy { HCNetSDK.getInstance() } private val timeFormat by lazy { SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) } private val selectedSpeed = 7 + private val messageCode = 2024021901 private var clickTime = 0L private var previewHandle = -1 private var selectChannel = -1 @@ -60,13 +68,36 @@ private var timer: Timer? = null private var timerTask: TimerTask? = null private var seconds = 0L + private var dataBeans: MutableList = ArrayList() + private lateinit var weakReferenceHandler: WeakReferenceHandler + private lateinit var dataAdapter: CameraPointAdapter + private var selectedItems: MutableList = ArrayList() override fun initViewBinding(): ActivityMainBinding { return ActivityMainBinding.inflate(layoutInflater) } override fun initOnCreate(savedInstanceState: Bundle?) { + weakReferenceHandler = WeakReferenceHandler(this) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + + //显示数据 + weakReferenceHandler.sendEmptyMessage(messageCode) + } + + override fun handleMessage(msg: Message): Boolean { + if (msg.what == messageCode) { + //绑定数据 + dataAdapter = CameraPointAdapter(this, dataBeans) + binding.recyclerView.adapter = dataAdapter + dataAdapter.setOnItemCheckedListener(object : CameraPointAdapter.OnItemCheckedListener { + override fun onItemChecked(items: ArrayList) { + selectedItems = items + } + }) + } + return true } override fun initEvent() { @@ -112,6 +143,18 @@ true } + binding.addButton.setOnClickListener { + DataBaseManager.get.cacheCameraPoint(0, 0) + dataBeans = DataBaseManager.get.loadAllCameraPoint() + dataAdapter.setRefreshData(dataBeans) + } + + binding.deleteButton.setOnClickListener { + selectedItems.forEach { + Log.d(kTag, "initEvent => ${it.id}") + } + } + binding.steeringWheelView.setOnWheelTouchListener(object : SteeringWheelView.OnWheelTouchListener { override fun onCenterClicked() { diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml index 9f6b0ae..d6f55c9 100644 --- a/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml +++ b/app/src/main/res/drawable/bg_stroke_layout_blue_10.xml @@ -6,6 +6,6 @@ \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_minus.xml b/app/src/main/res/drawable/ic_minus.xml index 98d3e0b..a399f5f 100644 --- a/app/src/main/res/drawable/ic_minus.xml +++ b/app/src/main/res/drawable/ic_minus.xml @@ -1,6 +1,6 @@ + android:layout_height="wrap_content" + android:background="@color/backgroundColor"> @@ -127,7 +128,7 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:layout_margin="@dimen/dp_10" + android:layout_margin="@dimen/dp_7" android:orientation="horizontal"> + android:layout_height="match_parent" + android:background="@color/backgroundColor" /> @@ -192,12 +194,13 @@ + android:background="@color/backgroundColor" + android:orientation="vertical"> @@ -251,6 +255,7 @@ @@ -324,6 +329,7 @@ @@ -343,7 +349,7 @@ android:gravity="center" app:ctrl_backgroundColor="@color/white" app:ctrl_borderColor="#64BFEB" - app:ctrl_borderStroke="@dimen/dp_5" + app:ctrl_borderStroke="@dimen/dp_3" app:ctrl_diameter="@dimen/dp_150" /> diff --git a/app/src/main/res/layout/item_point_list_rv.xml b/app/src/main/res/layout/item_point_list_rv.xml new file mode 100644 index 0000000..57b2727 --- /dev/null +++ b/app/src/main/res/layout/item_point_list_rv.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 918d8c4..987ad74 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,7 @@ dependencies { classpath 'com.android.tools.build:gradle:4.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files