Просмотр исходного кода

新增一键恢复所有MCU控制板参数到默认出厂设置的功能

niujiuru 6 месяцев назад
Родитель
Сommit
e11ef57931

+ 7 - 5
app/build.gradle

@@ -3,7 +3,7 @@ plugins {
 }
 
 android {
-    namespace 'com.example.yunfeiapplication'
+    namespace = 'com.example.yunfeiapplication'
     compileSdk 34
 
     defaultConfig {
@@ -35,13 +35,16 @@ android {
     }
     lint {
         baseline = file("lint-baseline.xml")
-        // 添加这行,即使有错误也继续构建
-        abortOnError = false
+        // 添加这行,false: 即使有错误也继续构建, ture: 直接让编译失败, 中止构建
+        abortOnError = true
     }
 }
 
-dependencies {
+tasks.withType(JavaCompile).configureEach {
+    //options.compilerArgs += ["-Xlint:deprecation"]
+}
 
+dependencies {
     implementation libs.org.eclipse.paho.android.service
     implementation libs.org.eclipse.paho.client.mqttv3
     implementation libs.fastjson
@@ -60,6 +63,5 @@ dependencies {
     testImplementation libs.junit
     androidTestImplementation libs.ext.junit
     androidTestImplementation libs.espresso.core
-
 }
 

+ 28 - 3
app/src/main/java/com/example/yunfeiapplication/MainActivity.java

@@ -206,9 +206,12 @@ public class MainActivity extends AppCompatActivity implements CustomJsonProcess
 
     UserMqttManager customerMqttManager = null;
 
+    public static MainActivity Instance;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        Instance = this;
         EdgeToEdge.enable(this);
         setContentView(R.layout.activity_main);
         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
@@ -242,8 +245,14 @@ public class MainActivity extends AppCompatActivity implements CustomJsonProcess
         /*获取定位、手机状态、存储权限*/
         GetSelfPermissionPhoneState();
 
-        /* 程序运行起始界面 */
-        getSupportFragmentManager().beginTransaction().replace(R.id.mainFragmentContainerView, FragmentMac0.class,null).commit();
+        /* 程序运行起始界面, Modified by NiuJiuRu @2025-05-20 */
+        FragmentMac0 fragment = new FragmentMac0();
+
+        getSupportFragmentManager()
+                .beginTransaction()
+                .replace(R.id.mainFragmentContainerView, fragment)
+                .commitNow();
+        //getSupportFragmentManager().beginTransaction().replace(R.id.mainFragmentContainerView, FragmentMac0.class,null).commit();
         OldFragmentIndex = 0;
 
         /* 侧边栏初始化 */
@@ -824,6 +833,10 @@ public class MainActivity extends AppCompatActivity implements CustomJsonProcess
 
     /******************************************** 侧边栏功能 ********************************************/
     /*** 界面切换 ***/
+    public void SwitchToFragment(int index){
+        MacReplaceFragment(index);
+    }
+
     private void MacReplaceFragment(int index) {
 
         if (index < 1 || index > 7 || index == OldFragmentIndex) {
@@ -991,6 +1004,9 @@ public class MainActivity extends AppCompatActivity implements CustomJsonProcess
             String logString = "";
 
             switch (string) {
+                case "0": // Added by NiuJiuRu @2025-05-20
+                    logString = "执行一键恢复所有参数到默认出厂设置";
+                    break;
 
                 case "1":
                     jsonObjectEXT.put("ts", jsonObject.getJSONObject("ext").get("ts"));
@@ -1084,7 +1100,16 @@ public class MainActivity extends AppCompatActivity implements CustomJsonProcess
 
             int commandType = Integer.parseInt(string);
 
-            if (commandType >= 1 && commandType <= 50) {
+            if (commandType == 0) { // Added by NiuJiuRu @2025-05-20
+                String resetCmd = "{\"cmd\": \"ctrl\", \"ext\": {\"RC\": \"Force_Reset\"}}";
+
+                Log.i("TAG", "serialSend: " + resetCmd);
+
+                if (!SerialManager.getInstance().getUpgradingFlag()) {
+                    SerialManager.getInstance().write(resetCmd.getBytes());
+                }
+            }
+            else if (commandType >= 1 && commandType <= 50) {
                 jsonMainObject.put("cmd","paramconf");
 //                jsonObjectEXT.put("no_reply", "1");
                 jsonMainObject.put("ext",jsonObjectEXT);

+ 94 - 0
app/src/main/java/com/example/yunfeiapplication/fragments/FragmentMac1.java

@@ -12,12 +12,14 @@ import androidx.fragment.app.FragmentResultListener;
 
 import android.os.Handler;
 import android.os.Looper;
+import android.text.InputType;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.CompoundButton;
+import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.RadioButton;
 import android.widget.RadioGroup;
@@ -26,6 +28,7 @@ import android.widget.Switch;
 import android.widget.TextView;
 import android.widget.Toast;
 
+import com.example.yunfeiapplication.MainActivity;
 import com.example.yunfeiapplication.R;
 import com.example.yunfeiapplication.variable.UserGlobalVariable;
 
@@ -56,6 +59,7 @@ public class FragmentMac1 extends Fragment {
     Button FMac1ParaSetBtn1;
     Button FMac1ParaSetBtn2;
     Button FMac1ParaSetBtn3;
+    Button FMac1ParaSetBtn4; // 一键恢复所有参数到默认出厂设置 Added by NiuJiuRu @2025-05-20
 
     ImageView FMac1ParameterSetHelpIv;
     ImageView FMac1WorkStatusHelpIv;
@@ -183,6 +187,7 @@ public class FragmentMac1 extends Fragment {
         FMac1ParaSetBtn1 = view.findViewById(R.id.FMac1ParaSetBtn1);
         FMac1ParaSetBtn2 = view.findViewById(R.id.FMac1ParaSetBtn2);
         FMac1ParaSetBtn3 = view.findViewById(R.id.FMac1ParaSetBtn3);
+        FMac1ParaSetBtn4 = view.findViewById(R.id.FMac1ResetPara2DefaultBtn); // Added by NiuJiuRu @2025-05-20
 
         FMac1TimeLightRBGroup = view.findViewById(R.id.FMac1TimeLightRBGroup);
 
@@ -509,6 +514,72 @@ public class FragmentMac1 extends Fragment {
         FMac1ModeCtrlSw2.setOnCheckedChangeListener(onCheckedChangeListener);
     }
 
+    private static final String ADMIN_PWD = "000000";
+
+    /* 请求密码校验, Added by NiuJiuRu @2025-05-20 */
+    private void requestPassword(Runnable onSuccess) {
+        EditText input = new EditText(requireContext());
+
+        input.setInputType(
+                InputType.TYPE_CLASS_NUMBER
+                        | InputType.TYPE_TEXT_VARIATION_PASSWORD
+        );
+
+        input.setSingleLine(true);
+        input.setHint("请输入管理员密码");
+
+        AlertDialog dialog = new AlertDialog.Builder(requireContext())
+                .setTitle("权限校验")
+                .setIcon(android.R.drawable.ic_lock_idle_lock)
+                .setView(input)
+                .setCancelable(false)
+                .setPositiveButton("确认", null)
+                .setNegativeButton("取消", null)
+                .create();
+
+        dialog.setOnShowListener(d -> {
+            input.requestFocus();
+            dialog.getButton(AlertDialog.BUTTON_POSITIVE)
+                    .setOnClickListener(v -> {
+                        String pwd = input.getText()
+                                .toString()
+                                .trim();
+                        if (ADMIN_PWD.equals(pwd)) { // 密码正确,执行后续动作
+                            dialog.dismiss();
+                            onSuccess.run();
+                        } else {
+                            Toast.makeText(
+                                    requireContext(),
+                                    "密码错误",
+                                    Toast.LENGTH_SHORT
+                            ).show();
+                            // 清空重新输入
+                            input.setText("");
+                            input.requestFocus();
+                        }
+                    });
+        });
+
+        dialog.show();
+    }
+
+    /* 确认执行, Added by NiuJiuRu @2025-05-20 */
+    private void confirmExecute(
+            String title,
+            String msg,
+            Runnable action
+    ) {
+        new AlertDialog.Builder(requireContext())
+                .setTitle(title)
+                .setMessage(msg)
+                .setIcon(android.R.drawable.ic_dialog_alert)
+                .setCancelable(false)
+                .setPositiveButton("是",
+                        (d, w) -> action.run())
+                .setNegativeButton("否", null)
+                .show();
+    }
+
     /* 设置按钮检测器 */
     private  void FMac1ParaSetBtnClickListenerInit(View view) {
 
@@ -538,6 +609,27 @@ public class FragmentMac1 extends Fragment {
                 FMac1ParaSetBtnClickDeal(R.id.FMac1ParaSetBtn3);
             }
         });
+
+        view.findViewById(R.id.FMac1ResetPara2DefaultBtn).setOnClickListener(new View.OnClickListener() { // Added by NiuJiuRu @2025-05-20
+            @Override
+            public void onClick(View v) {
+                requestPassword(() ->
+                        confirmExecute(
+                                "恢复出厂参数设置",
+                                "此操作将清除所有用户配置并恢复出厂默认参数,是否继续?",
+                                () -> {
+                                    // 1. 通知MCU恢复出厂参数
+                                    FMac1ParaSetBtnClickDeal(R.id.FMac1ResetPara2DefaultBtn);
+                                    // 2. 退回到登录前的界面状态
+                                    if (MainActivity.Instance != null) {
+                                        MainActivity.Instance.SwitchToFragment(6);
+                                    }
+                                }
+                        )
+                );
+                //FMac1ParaSetBtnClickDeal(R.id.FMac1ResetPara2DefaultBtn);
+            }
+        });
     }
 
     public void FMac1SeekBarClickListenerInit() {
@@ -701,6 +793,8 @@ public class FragmentMac1 extends Fragment {
                     /*获取工作温度下限输入值*/
                     tempString = Integer.toString(FMac1SeekBar05.getProgress());
                     jsonObject.getJSONObject("ext").put("tpl",tempString);
+                }else if(buttonId == R.id.FMac1ResetPara2DefaultBtn){ // Added by NiuJiuRu @2025-05-20
+                    setBtnOptBundle.putString("OptCommand","0");
                 }
 
                 fragmentMap.put("JsonMainObject",jsonObject.toString());

+ 55 - 46
app/src/main/res/layout/fragment_mac1.xml

@@ -55,6 +55,11 @@
                     />
             </LinearLayout>
 
+            <ScrollView
+                android:layout_width="match_parent"
+                android:layout_height="0dp"
+                android:layout_weight="1"
+                android:fillViewport="true">
 
             <LinearLayout
                 android:layout_width="match_parent"
@@ -341,36 +346,32 @@
 
                 <LinearLayout
                     android:layout_width="match_parent"
-                    android:layout_height="match_parent"
-                    android:orientation="vertical"
-                    android:background="@drawable/background_style_test"
+                    android:layout_height="wrap_content"
                     android:layout_marginTop="5dp"
+                    android:background="@drawable/background_style_test"
+                    android:orientation="vertical"
                     android:paddingHorizontal="10dp"
-                    android:paddingVertical="5dp"
-                    >
+                    android:paddingVertical="5dp">
 
                     <LinearLayout
                         android:layout_width="match_parent"
-                        android:layout_height="wrap_content"
-                        >
+                        android:layout_height="wrap_content">
 
                         <ImageView
                             android:layout_width="32dp"
                             android:layout_height="32dp"
                             android:layout_gravity="center"
                             android:layout_marginRight="5dp"
-                            android:src="@drawable/fmac1control2"
-                            />
+                            android:src="@drawable/fmac1control2" />
 
                         <TextView
                             android:layout_width="@dimen/user_const_size_120dp"
                             android:layout_height="wrap_content"
                             android:layout_gravity="center"
                             android:text="@string/user_const_string_07"
-                            android:textSize="20sp"
-                            android:textStyle="bold"
                             android:textColor="#14A478"
-                            />
+                            android:textSize="20sp"
+                            android:textStyle="bold" />
 
                         <Switch
                             android:id="@+id/FMac1ModeCtrlSw2"
@@ -382,23 +383,21 @@
                             android:textOff="关"
                             android:textOn="开"
                             android:thumb="@drawable/thumb_style_1"
-                            android:track="@drawable/track_style_1"
-                            />
+                            android:track="@drawable/track_style_1" />
 
                     </LinearLayout>
 
                     <LinearLayout
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content"
-                        android:orientation="vertical"
-
                         android:divider="@drawable/divider_space_hor_10dp"
+
+                        android:orientation="vertical"
                         android:showDividers="middle">
 
                         <LinearLayout
                             android:layout_width="wrap_content"
-                            android:layout_height="wrap_content"
-                            >
+                            android:layout_height="wrap_content">
 
                             <TextView
                                 android:id="@+id/FMac1TemperatureInfoTv1"
@@ -406,7 +405,7 @@
                                 android:layout_height="wrap_content"
                                 android:layout_gravity="center"
                                 android:text="@string/user_const_string_08"
-                                android:textSize="20sp"/>
+                                android:textSize="20sp" />
 
                             <TextView
                                 android:id="@+id/FMac1SeekBar04ValueTv"
@@ -415,8 +414,7 @@
                                 android:layout_gravity="center"
                                 android:gravity="center"
                                 android:text="50~70"
-                                android:textSize="20sp"
-                                />
+                                android:textSize="20sp" />
 
                             <TextView
                                 android:id="@+id/FMac1TemperatureInfoTv2"
@@ -430,19 +428,17 @@
 
                         <LinearLayout
                             android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            >
+                            android:layout_height="wrap_content">
 
                             <SeekBar
                                 android:id="@+id/FMac1SeekBar04"
                                 android:layout_width="0dp"
                                 android:layout_height="wrap_content"
-                                android:layout_weight="1"
-                                android:layout_marginVertical="5dp"
                                 android:layout_gravity="center"
+                                android:layout_marginVertical="5dp"
+                                android:layout_weight="1"
                                 android:max="70"
-                                android:min="50"
-                                />
+                                android:min="50" />
 
                             <TextView
                                 android:id="@+id/FMac1SeekBar04ValueSetTv"
@@ -450,8 +446,7 @@
                                 android:layout_height="wrap_content"
                                 android:layout_gravity="center"
                                 android:text="50"
-                                android:textSize="16sp"
-                                />
+                                android:textSize="16sp" />
 
                         </LinearLayout>
 
@@ -460,9 +455,9 @@
                     <LinearLayout
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content"
-                        android:orientation="vertical"
-
                         android:divider="@drawable/divider_space_hor_10dp"
+
+                        android:orientation="vertical"
                         android:showDividers="middle">
 
                         <LinearLayout
@@ -475,7 +470,7 @@
                                 android:layout_height="wrap_content"
                                 android:layout_gravity="center"
                                 android:text="@string/user_const_string_09"
-                                android:textSize="20sp"/>
+                                android:textSize="20sp" />
 
                             <TextView
                                 android:id="@+id/FMac1SeekBar05ValueTv"
@@ -484,8 +479,7 @@
                                 android:layout_gravity="center"
                                 android:gravity="center"
                                 android:text="0~20"
-                                android:textSize="20sp"
-                                />
+                                android:textSize="20sp" />
 
                             <TextView
                                 android:id="@+id/FMac1TemperatureInfoTv4"
@@ -493,25 +487,23 @@
                                 android:layout_height="wrap_content"
                                 android:layout_gravity="center"
                                 android:text="℃"
-                                android:textSize="20sp"/>
+                                android:textSize="20sp" />
 
                         </LinearLayout>
 
                         <LinearLayout
                             android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            >
+                            android:layout_height="wrap_content">
 
                             <SeekBar
                                 android:id="@+id/FMac1SeekBar05"
                                 android:layout_width="0dp"
                                 android:layout_height="wrap_content"
-                                android:layout_weight="1"
                                 android:layout_gravity="center"
                                 android:layout_marginVertical="5dp"
+                                android:layout_weight="1"
                                 android:max="20"
-                                android:min="0"
-                                />
+                                android:min="0" />
 
                             <TextView
                                 android:id="@+id/FMac1SeekBar05ValueSetTv"
@@ -527,32 +519,49 @@
 
                     <LinearLayout
                         android:layout_width="match_parent"
-                        android:layout_height="wrap_content"
-                        >
+                        android:layout_height="wrap_content">
 
                         <TextView
                             android:id="@+id/FMac1TemperatureErrInfoTv"
                             android:layout_width="0dp"
                             android:layout_height="match_parent"
                             android:layout_weight="1"
-                            android:gravity="center_vertical"
-                            />
+                            android:gravity="center_vertical" />
 
                         <Button
                             android:id="@+id/FMac1ParaSetBtn3"
                             android:layout_width="wrap_content"
                             android:layout_height="45dp"
                             android:layout_gravity="end"
-                            android:text="设置"
                             android:backgroundTint="#00A54B"
-                            />
+                            android:text="设置" />
 
                     </LinearLayout>
 
                 </LinearLayout>
 
+                <LinearLayout
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:layout_marginTop="10dp"
+                    android:layout_marginHorizontal="10dp"
+                    android:background="@drawable/background_style_test"
+                    android:padding="10dp">
+
+                    <Button
+                        android:id="@+id/FMac1ResetPara2DefaultBtn"
+                        android:layout_width="match_parent"
+                        android:layout_height="50dp"
+                        android:text="一键恢复所有参数到默认出厂设置"
+                        android:textColor="#FFFFFF"
+                        android:textStyle="bold"
+                        android:backgroundTint="#D32F2F"/>
+                </LinearLayout>
+
             </LinearLayout>
 
+            </ScrollView>
+
         </LinearLayout>
 
         <!-- 右侧布局开始  -->

+ 23 - 0
gradle.properties

@@ -0,0 +1,23 @@
+# Project-wide Gradle settings.
+# IDE (e.g. Android Studio) users:
+# Gradle settings configured through the IDE *will override*
+# any settings specified in this file.
+# For more details on how to configure your build environment visit
+# http://www.gradle.org/docs/current/userguide/build_environment.html
+# Specifies the JVM arguments used for the daemon process.
+# The setting is particularly useful for tweaking memory settings.
+org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
+# When configured, Gradle will run in incubating parallel mode.
+# This option should only be used with decoupled projects. For more details, visit
+# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
+# org.gradle.parallel=true
+# AndroidX package structure to make it clearer which packages are bundled with the
+# Android operating system, and which are packaged with your app's APK
+# https://developer.android.com/topic/libraries/support-library/androidx-rn
+android.useAndroidX=true
+# Kotlin code style for this project: "official" or "obsolete":
+kotlin.code.style=official
+# Enables namespacing of each library's R class so that its R class includes only the
+# resources declared in the library itself and none from the library's dependencies,
+# thereby reducing the size of the R class for that library
+android.nonTransitiveRClass=true