优化配置界面
This commit is contained in:
parent
3804ec39a6
commit
d3e4a6de53
|
|
@ -47,6 +47,7 @@ android {
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
|
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
|
||||||
implementation("com.jakewharton.timber:timber:5.0.1")
|
implementation("com.jakewharton.timber:timber:5.0.1")
|
||||||
|
implementation("androidx.cardview:cardview:1.0.0")
|
||||||
implementation("io.github.justson:agentweb-core:v5.1.1-androidx")
|
implementation("io.github.justson:agentweb-core:v5.1.1-androidx")
|
||||||
implementation("com.github.Justson:Downloader:v5.0.4-androidx")
|
implementation("com.github.Justson:Downloader:v5.0.4-androidx")
|
||||||
implementation("com.google.android.material:material:1.12.0")
|
implementation("com.google.android.material:material:1.12.0")
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,14 @@ import android.content.SharedPreferences
|
||||||
object AppConfig {
|
object AppConfig {
|
||||||
private const val PREF_NAME = "app_settings"
|
private const val PREF_NAME = "app_settings"
|
||||||
private const val KEY_SERVER_URL = "server_url"
|
private const val KEY_SERVER_URL = "server_url"
|
||||||
|
private const val KEY_SERIAL_PORT = "serial_port"
|
||||||
private const val KEY_REMEMBER_LOGIN = "remember_login"
|
private const val KEY_REMEMBER_LOGIN = "remember_login"
|
||||||
private const val KEY_USERNAME = "saved_username"
|
private const val KEY_USERNAME = "saved_username"
|
||||||
private const val KEY_PASSWORD = "saved_password"
|
private const val KEY_PASSWORD = "saved_password"
|
||||||
|
|
||||||
// 默认服务器地址
|
// 默认配置
|
||||||
const val DEFAULT_SERVER_URL = "https://template.gyouzhe.com"
|
const val DEFAULT_SERVER_URL = "https://template.gyouzhe.com"
|
||||||
|
const val DEFAULT_SERIAL_PORT = "/dev/ttyACM0"
|
||||||
|
|
||||||
private fun getPrefs(context: Context): SharedPreferences {
|
private fun getPrefs(context: Context): SharedPreferences {
|
||||||
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||||
|
|
@ -25,6 +27,24 @@ object AppConfig {
|
||||||
getPrefs(context).edit().putString(KEY_SERVER_URL, url).apply()
|
getPrefs(context).edit().putString(KEY_SERVER_URL, url).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getSerialPort(context: Context): String {
|
||||||
|
return getPrefs(context).getString(KEY_SERIAL_PORT, DEFAULT_SERIAL_PORT) ?: DEFAULT_SERIAL_PORT
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setSerialPort(context: Context, port: String) {
|
||||||
|
getPrefs(context).edit().putString(KEY_SERIAL_PORT, port).apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 还原默认配置
|
||||||
|
*/
|
||||||
|
fun resetToDefault(context: Context) {
|
||||||
|
getPrefs(context).edit()
|
||||||
|
.putString(KEY_SERVER_URL, DEFAULT_SERVER_URL)
|
||||||
|
.putString(KEY_SERIAL_PORT, DEFAULT_SERIAL_PORT)
|
||||||
|
.apply()
|
||||||
|
}
|
||||||
|
|
||||||
fun isRememberLogin(context: Context): Boolean {
|
fun isRememberLogin(context: Context): Boolean {
|
||||||
return getPrefs(context).getBoolean(KEY_REMEMBER_LOGIN, false)
|
return getPrefs(context).getBoolean(KEY_REMEMBER_LOGIN, false)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -143,31 +143,55 @@ class LoginActivity : AppCompatActivity() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 弹出服务器地址配置对话框
|
|
||||||
*/
|
|
||||||
private fun showServerConfigDialog() {
|
private fun showServerConfigDialog() {
|
||||||
val container = LinearLayout(this)
|
val dialogView = layoutInflater.inflate(R.layout.dialog_settings, null)
|
||||||
container.orientation = LinearLayout.VERTICAL
|
val etServerUrl = dialogView.findViewById<EditText>(R.id.et_server_url)
|
||||||
container.setPadding(60, 20, 60, 0)
|
val etSerialPort = dialogView.findViewById<EditText>(R.id.et_serial_port)
|
||||||
|
val btnReset = dialogView.findViewById<TextView>(R.id.btn_reset)
|
||||||
|
|
||||||
val editText = EditText(this)
|
// 初始化数据
|
||||||
editText.setText(AppConfig.getServerUrl(this))
|
etServerUrl.setText(AppConfig.getServerUrl(this))
|
||||||
editText.hint = "例如: http://192.168.1.100:8080"
|
etSerialPort.setText(AppConfig.getSerialPort(this))
|
||||||
container.addView(editText)
|
|
||||||
|
|
||||||
AlertDialog.Builder(this)
|
val dialog = AlertDialog.Builder(this, R.style.CustomDialogTheme)
|
||||||
.setTitle("服务器地址配置")
|
.setView(dialogView)
|
||||||
.setView(container)
|
.setCancelable(false)
|
||||||
.setPositiveButton("保存") { _, _ ->
|
.create()
|
||||||
val newUrl = editText.text.toString().trim()
|
|
||||||
if (newUrl.isNotEmpty()) {
|
btnReset.setOnClickListener {
|
||||||
AppConfig.setServerUrl(this, newUrl)
|
etServerUrl.setText(AppConfig.DEFAULT_SERVER_URL)
|
||||||
Toast.makeText(this, "服务器配置已更新", Toast.LENGTH_SHORT).show()
|
etSerialPort.setText(AppConfig.DEFAULT_SERIAL_PORT)
|
||||||
|
Toast.makeText(this, "已重置数值 (待保存)", Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dialogView.findViewById<TextView>(R.id.btn_cancel).setOnClickListener {
|
||||||
|
dialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
dialogView.findViewById<TextView>(R.id.btn_save).setOnClickListener {
|
||||||
|
val serverUrl = etServerUrl.text.toString().trim()
|
||||||
|
val serialPort = etSerialPort.text.toString().trim()
|
||||||
|
|
||||||
|
if (serverUrl.isEmpty()) {
|
||||||
|
etServerUrl.error = "地址不能为空"
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
|
AppConfig.setServerUrl(this, serverUrl)
|
||||||
|
AppConfig.setSerialPort(this, serialPort)
|
||||||
|
|
||||||
|
Toast.makeText(this, "参数已生效", Toast.LENGTH_SHORT).show()
|
||||||
|
dialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
dialog.show()
|
||||||
|
|
||||||
|
// 强制设置对话框宽度
|
||||||
|
dialog.window?.let { window ->
|
||||||
|
val params = window.attributes
|
||||||
|
params.width = (resources.displayMetrics.widthPixels * 0.75).toInt() // 占据屏幕 75% 宽度
|
||||||
|
window.attributes = params
|
||||||
}
|
}
|
||||||
.setNegativeButton("取消", null)
|
|
||||||
.show()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import com.dp.dp_serialportlist.Serialport_Factory;
|
import com.dp.dp_serialportlist.Serialport_Factory;
|
||||||
import com.stand.standapp.R;
|
import com.stand.standapp.R;
|
||||||
|
import com.stand.standapp.AppConfig;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
@ -63,6 +64,11 @@ public class PrintTestActivity extends AppCompatActivity implements View.OnClick
|
||||||
initViews();
|
initViews();
|
||||||
|
|
||||||
sf = Serialport_Factory.getSerialport_Factory(this, mHandler);
|
sf = Serialport_Factory.getSerialport_Factory(this, mHandler);
|
||||||
|
|
||||||
|
// 预填配置好的串口地址
|
||||||
|
String savedPort = AppConfig.INSTANCE.getSerialPort(this);
|
||||||
|
etManualPath.setText(savedPort);
|
||||||
|
|
||||||
loadSerialPorts();
|
loadSerialPorts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:color="#40FFFFFF">
|
||||||
|
<item>
|
||||||
|
<shape>
|
||||||
|
<solid android:color="#004A8F" />
|
||||||
|
<corners android:radius="14dp" />
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
</ripple>
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:color="#20000000">
|
||||||
|
<item>
|
||||||
|
<shape>
|
||||||
|
<solid android:color="#F1F5F9" />
|
||||||
|
<corners android:radius="14dp" />
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
</ripple>
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<solid android:color="#FFFFFF" />
|
||||||
|
<corners android:radius="24dp" />
|
||||||
|
</shape>
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<solid android:color="#F8FAFC" />
|
||||||
|
<corners android:radius="12dp" />
|
||||||
|
<stroke android:width="1.5dp" android:color="#E2E8F0" />
|
||||||
|
</shape>
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_focused="true">
|
||||||
|
<shape>
|
||||||
|
<solid android:color="#FFFFFF" />
|
||||||
|
<corners android:radius="12dp" />
|
||||||
|
<stroke android:width="2dp" android:color="#3B82F6" />
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<shape>
|
||||||
|
<solid android:color="#F1F5F9" />
|
||||||
|
<corners android:radius="12dp" />
|
||||||
|
<stroke android:width="1dp" android:color="#E2E8F0" />
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
</selector>
|
||||||
|
|
@ -0,0 +1,140 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:padding="28dp"
|
||||||
|
android:background="@drawable/bg_dialog_white">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="参数配置"
|
||||||
|
android:textColor="#1E293B"
|
||||||
|
android:textSize="22sp"
|
||||||
|
android:fontFamily="sans-serif-medium"
|
||||||
|
android:layout_marginBottom="4dp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="设置后端服务器及硬件通讯接口"
|
||||||
|
android:textColor="#64748B"
|
||||||
|
android:textSize="13sp"
|
||||||
|
android:layout_marginBottom="32dp" />
|
||||||
|
|
||||||
|
<!-- Server URL -->
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_marginBottom="20dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="服务器地址"
|
||||||
|
android:textColor="#475569"
|
||||||
|
android:textSize="12sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:textAllCaps="true"
|
||||||
|
android:letterSpacing="0.05"
|
||||||
|
android:layout_marginBottom="8dp" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_server_url"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="56dp"
|
||||||
|
android:background="@drawable/bg_input_field"
|
||||||
|
android:hint="https://api.example.com"
|
||||||
|
android:inputType="textUri"
|
||||||
|
android:paddingHorizontal="16dp"
|
||||||
|
android:textColor="#0F172A"
|
||||||
|
android:textSize="15sp" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<!-- Serial Port -->
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_marginBottom="12dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="打印机串口路径"
|
||||||
|
android:textColor="#475569"
|
||||||
|
android:textSize="12sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:textAllCaps="true"
|
||||||
|
android:letterSpacing="0.05"
|
||||||
|
android:layout_marginBottom="8dp" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_serial_port"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="56dp"
|
||||||
|
android:background="@drawable/bg_input_field"
|
||||||
|
android:hint="/dev/ttyACM0"
|
||||||
|
android:inputType="text"
|
||||||
|
android:paddingHorizontal="16dp"
|
||||||
|
android:textColor="#0F172A"
|
||||||
|
android:textSize="15sp" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/btn_reset"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="恢复默认数值"
|
||||||
|
android:textColor="#3B82F6"
|
||||||
|
android:textSize="13sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:paddingVertical="8dp"
|
||||||
|
android:layout_gravity="end"
|
||||||
|
android:layout_marginBottom="32dp"
|
||||||
|
android:clickable="true"
|
||||||
|
android:focusable="true" />
|
||||||
|
|
||||||
|
<!-- Action Buttons -->
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/btn_cancel"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="56dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="取消"
|
||||||
|
android:textColor="#64748B"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="@drawable/bg_btn_secondary"
|
||||||
|
android:clickable="true"
|
||||||
|
android:focusable="true" />
|
||||||
|
|
||||||
|
<Space
|
||||||
|
android:layout_width="12dp"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/btn_save"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="56dp"
|
||||||
|
android:layout_weight="1.4"
|
||||||
|
android:text="保存配置"
|
||||||
|
android:textColor="@android:color/white"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="@drawable/bg_btn_primary"
|
||||||
|
android:clickable="true"
|
||||||
|
android:focusable="true" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
@ -2,8 +2,15 @@
|
||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
<style name="Theme.StandAPP" parent="Theme.AppCompat.Light.NoActionBar">
|
<style name="Theme.StandAPP" parent="Theme.AppCompat.Light.NoActionBar">
|
||||||
<!-- Primary brand color. -->
|
<!-- Primary brand color. -->
|
||||||
<item name="colorPrimary">#6200EE</item>
|
<item name="colorPrimary">#004A8F</item>
|
||||||
<item name="colorPrimaryDark">#3700B3</item>
|
<item name="colorPrimaryDark">#003366</item>
|
||||||
<item name="colorAccent">#03DAC5</item>
|
<item name="colorAccent">#3B82F6</item>
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog">
|
||||||
|
<item name="android:windowBackground">@android:color/transparent</item>
|
||||||
|
<item name="android:windowNoTitle">true</item>
|
||||||
|
<item name="android:windowIsTranslucent">true</item>
|
||||||
|
<item name="android:backgroundDimEnabled">true</item>
|
||||||
</style>
|
</style>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue