package com.example.myapplication;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.TimePickerDialog;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ExifInterface;
import android.media.MediaRecorder;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;

import android.os.Looper;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.Manifest;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;


import com.example.myapplication.DataBase.AppDatabase;
import com.example.myapplication.Service.FloatingWindowService;
import com.example.myapplication.Service.RecordingService;
import com.example.myapplication.Tool.BackgroundToast;
import com.example.myapplication.model.AudioEntity;
import com.example.myapplication.model.ImageEntity;
import com.example.myapplication.model.ImageInfo;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.google.android.material.switchmaterial.SwitchMaterial;

import okhttp3.*;


import java.io.File;
import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;
// 在MainActivity类中添加以下变量



public class MainActivity extends AppCompatActivity {



    private static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 102;

    private boolean hasClickedRecord = false;
    private TextView tvUser;
    private TextView tvProject;
    private TextView tvProjectId;
    private Button btnAdmin;
    // 在类变量声明部分添加
    private Button btnUploadVideos;
    private Button btnUploadVideos2;

    private FloatingActionButton floatingActionButton;
    private boolean isRecording = false;
    private MediaRecorder mediaRecorder;
    private String currentAudioPath;
    private final OkHttpClient httpClient = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)  // 连接超时
            .readTimeout(60, TimeUnit.SECONDS)     // 读取超时
            .writeTimeout(60, TimeUnit.SECONDS)    // 写入超时
            .build();

    private Button selectTimeButton;


    private TextView startTimeText;
    private TextView endTimeText;
    private long startTime = 0;
    private long endTime = 0;

    private TextView statusText;
    private ContentObserver contentObserver;
    private long lastPhotoId = -1;
    private boolean isMonitoring = false;
    private SwitchMaterial switchMonitor;
    private Button btnSelectPhotos;
    private static final int REQUEST_CODE_SELECT_PHOTOS = 1001;


    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    private AppDatabase db;
    private static final int PERMISSION_REQUEST_CODE = 100;
    private static final int PERMISSIONS_REQUEST_CODE = 109;

    private AppDatabase db2;
    private Button btnHistory;
    private Button btnFailedAudios;
    private Button btnHistory2;
    private AlertDialog currentDialog;
    private AlertDialog currentDialog2;
    private static final int THUMBNAIL_SIZE = 200;
    private static final int DECODE_SAMPLE_SIZE = 4;


    private final ExecutorService executor = Executors.newFixedThreadPool(20);
    // 添加位置相关变量

    private LocationManager locationManager;
    private LocationListener locationListener;
    private Location lastKnownLocation;

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1002;
    private TextView locationText;
    private  Button btnSetUser ;
    private static final int AUDIO_PERMISSION_REQUEST_CODE = 1030;
    private EditText etUserInfo;
    private Button btnUploadQueue;
    private Button btnStopUpload;
    private volatile boolean isUploading = false;
    private final Object uploadLock = new Object();
    private final Object monitoringLock = new Object();
    private EditText etUploadInterval;
    private Button btnSetInterval;
    private Button btnStartTimedUpload;
    private Button btnStopTimedUpload;
    private Handler timedUploadHandler;
    private Runnable timedUploadRunnable;
    private static final int OVERLAY_PERMISSION_REQUEST_CODE = 1003;
    private int uploadIntervalMinutes = 0; // 默认上传间隔(分钟)
    private boolean isTimedUploadRunning = false;
    private static final int REQUEST_VIDEO_PERMISSION = 1005;
    private static final int REQUEST_CODE_SELECT_VIDEOS = 1004;
    // 在类变量声明部分添加
    private ActivityResultLauncher<Intent> videoPickerLauncher;
    private InputMethodManager inputMethodManager;

    private ActivityResultLauncher<String[]> requestPermissionLauncher;
    private static final int REQUEST_CODE_OVERLAY = 101;
    private TextView tvUnit, tvBlade,tvUnitName;
    private static final String ACTION_UPDATE_VALUES = "com.example.myapplication.UPDATE_VALUES";
    private BroadcastReceiver updateReceiver;
    private String currentUsername;
    private String projectName ="0";
    private String projectId="0";
    private String  unit = "0";
    private String  unitName = "0";
    private String imgpath ="0";
    private int blade =0;
    private ActivityResultLauncher<Intent> overlayPermissionLauncher;
    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeViews();
        initializeDatabase();
        initializeReceivers();
        initializePermissionLaunchers();
        initialProject();
        requestInitialPermissions();
        initFloatingActionButton();
        setupButtonListeners();
        setupTimedUpload();

    }

    private void initializeViews() {
        lastPhotoId = getLastPhotoId();
        selectTimeButton = findViewById(R.id.selectTimeButton);
        startTimeText = findViewById(R.id.startTimeText);
        endTimeText = findViewById(R.id.endTimeText);
        btnUploadVideos2 = findViewById(R.id.btn_upload_videos2);
        btnUploadVideos = findViewById(R.id.btn_upload_videos);
        etUploadInterval = findViewById(R.id.et_upload_interval);
        etUploadInterval.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                showKeyboard(v);
            }
        });
        btnFailedAudios = findViewById(R.id.btn_failed_audios);
        etUploadInterval.setOnClickListener(this::showKeyboard);
        btnSetInterval = findViewById(R.id.btn_set_interval);
        btnStartTimedUpload = findViewById(R.id.btn_start_timed_upload);
        btnStopTimedUpload = findViewById(R.id.btn_stop_timed_upload);
        btnUploadQueue = findViewById(R.id.btn_upload_queue);
        btnStopUpload = findViewById(R.id.btn_stop_upload);
        btnHistory = findViewById(R.id.btn_history);
        btnHistory2 = findViewById(R.id.btn_history2);
        statusText = findViewById(R.id.status_text);
        switchMonitor = findViewById(R.id.switch_monitor);
        locationText = findViewById(R.id.location_text);
        btnAdmin = findViewById(R.id.btnAdmin);
        tvProject = findViewById(R.id.tv_project);
        tvProjectId = findViewById(R.id.tv_projectId);
        tvUser = findViewById(R.id.tv_user);
        tvUnit = findViewById(R.id.tvUnit);
        tvBlade = findViewById(R.id.tvBlade);
        tvUnitName = findViewById(R.id.tvUnitName);
    }

    private void initializeDatabase() {
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "image-database").build();
        db2 = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "image-database2").build();
    }

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    private void initializeReceivers() {
        updateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                unit = intent.getStringExtra("unit");
                blade = intent.getIntExtra("blade", 0);
                unitName = intent.getStringExtra("unitName");
                updateValues(unit, blade, unitName);
            }
        };

        IntentFilter filter = new IntentFilter(ACTION_UPDATE_VALUES);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            registerReceiver(updateReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
        } else {
            registerReceiver(updateReceiver, filter);
        }
    }
    private void showKeyboard(View view) {
        if (view.requestFocus()) {
            view.postDelayed(() -> {
                if (inputMethodManager != null) {
                    inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                }
            }, 100);
        }
    }
    private void hideKeyboard() {
        View view = this.getCurrentFocus();
        if (view != null) {
            if (inputMethodManager != null) {
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }
    private void initializePermissionLaunchers() {
        // Video picker launcher
        videoPickerLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                        handleVideoSelection(result.getData());
                    }
                });

        // Permission request launcher
        requestPermissionLauncher = registerForActivityResult(
                new ActivityResultContracts.RequestMultiplePermissions(),
                permissions -> {
                    if (permissions.values().stream().allMatch(Boolean::booleanValue)) {
                        openVideoPicker();
                    } else {
                        Toast.makeText(this, "需要权限才能选择视频", Toast.LENGTH_SHORT).show();
                    }
                });

        // Overlay permission launcher
        overlayPermissionLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    new Handler(Looper.getMainLooper()).postDelayed(() -> {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (Settings.canDrawOverlays(MainActivity.this)) {
                                startFloatingService();
                            } else {
                                Toast.makeText(MainActivity.this,
                                        "需要悬浮窗权限才能显示悬浮窗", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }, 500);
                });
    }
    private void initialProject()
    {
        Intent intent = getIntent();
        currentUsername = intent.getStringExtra("username");
        projectName = intent.getStringExtra("projectName");
        projectId=intent.getStringExtra("projectId");
        tvProject.setText(projectName);
        tvProjectId.setText(projectId);
        tvUser.setText(currentUsername);
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    private void requestInitialPermissions() {
        // Check and request all required permissions in order
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            // For Android 13+
            String[] permissions = {
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.POST_NOTIFICATIONS
            };

            if (checkSelfPermission(permissions)) {
                // All permissions granted, proceed
                initLocationManager();
            } else {
                // Request missing permissions
                ActivityCompat.requestPermissions(this, permissions, PERMISSIONS_REQUEST_CODE);
            }
        } else {
            // For older versions
            String[] permissions = {
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.ACCESS_FINE_LOCATION
            };

            if (checkSelfPermission(permissions)) {
                // All permissions granted, proceed
                initLocationManager();
            } else {
                // Request missing permissions
                ActivityCompat.requestPermissions(this, permissions, PERMISSIONS_REQUEST_CODE);
            }
        }

        // Special handling for overlay permission
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            requestOverlayPermission();
        }
    }

    private boolean checkSelfPermission(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    private void setupButtonListeners() {
        // Set up all button click listeners here
        btnUploadVideos2.setOnClickListener(v -> openVideoPicker());
        btnUploadVideos.setOnClickListener(v -> showVideoDateTimePicker());
        selectTimeButton.setOnClickListener(v -> showDateTimePicker());

        btnSetInterval.setOnClickListener(v -> {
            hideKeyboard(); // 先隐藏键盘
            setUploadInterval(); // 然后执行设置逻辑
        });
        btnStartTimedUpload.setOnClickListener(v -> startTimedUpload());
        btnStopTimedUpload.setOnClickListener(v -> stopTimedUpload());

        btnUploadQueue.setOnClickListener(v -> startUploadQueue());
        btnStopUpload.setOnClickListener(v -> stopUploadQueue());
        btnFailedAudios.setOnClickListener(v -> showFailedAudios());
        btnHistory.setOnClickListener(v -> showHistory(db, "历史记录（点击查看大图）", "删除所有记录", "确定要删除所有历史记录吗？"));
        btnHistory2.setOnClickListener(v -> showHistory(db2, "缓存队列（点击查看大图）", "删除所有缓存", "确定要删除所有缓存吗？"));
        switchMonitor.setChecked(false);
        switchMonitor.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(
                            this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            LOCATION_PERMISSION_REQUEST_CODE);
                } else {
                    checkRecordPermission();
                }
            } else {
                stopMonitoring();
            }
        });

        findViewById(R.id.btnShowFloating).setOnClickListener(v -> {
            if (checkOverlayPermission2()) {
                startFloatingService();
            }
            else {
                BackgroundToast.show(MainActivity.this,"需要悬浮窗权限");
            }
        });

        btnAdmin.setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, AdminActivity.class));
        });
    }
    private void showFailedAudios() {
        new Thread(() -> {
            List<AudioEntity> failedAudios = db.AudioDao().getAllAudios(); // Assuming you have getAll() in AudioDao

            runOnUiThread(() -> {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("上传失败的录音");

                if (failedAudios == null || failedAudios.isEmpty()) {
                    builder.setMessage("没有上传失败的录音");
                    builder.setPositiveButton("确定", null);
                    builder.show();
                    return;
                }

                // Create a list of display strings
                List<String> displayItems = new ArrayList<>();
                for (AudioEntity audio : failedAudios) {
                    displayItems.add(String.format("录音路径: %s\n图片路径: %s\n时间: %s",
                            audio.getAudioPath(),
                            audio.getImagePath(),
                            audio.getTime()));
                }

                // Convert to array for the adapter
                String[] items = displayItems.toArray(new String[0]);

                builder.setAdapter(new ArrayAdapter<>(this,
                                android.R.layout.simple_list_item_1, items),
                        (dialog, which) -> {

                        });

                // Add delete and retry buttons
                builder.setNegativeButton("一键删除", (dialog, which) -> deleteAllFailedAudios(failedAudios));
                builder.setNeutralButton("一键上传", (dialog, which) -> retryAllFailedAudios(failedAudios));

                builder.show();
            });
        }).start();
    }
    private void setupTimedUpload() {
        timedUploadHandler = new Handler(Looper.getMainLooper());
        SharedPreferences prefs = getSharedPreferences("UploadPrefs", MODE_PRIVATE);
        uploadIntervalMinutes = prefs.getInt("upload_interval", 0);
        if (uploadIntervalMinutes > 0) {
            etUploadInterval.setText(String.valueOf(uploadIntervalMinutes));
        }
    }
    private void deleteAllFailedAudios(List<AudioEntity> audios) {
        new Thread(() -> {
            for (AudioEntity audio : audios) {

                db.AudioDao().delete(audio);
            }

            runOnUiThread(() -> {
                Toast.makeText(this, "已删除所有失败录音", Toast.LENGTH_SHORT).show();
            });
        }).start();
    }

    private void retryAllFailedAudios(List<AudioEntity> audios) {
        new Thread(() -> {
            for (AudioEntity audio : audios) {
                // Implement your upload logic here
                // This is just a placeholder - replace with your actual upload code
                uploadAudio(audio.ImagePath,audio.AudioPath,audio.time);
                db.AudioDao().deleteByPath(audio.AudioPath);

            }

            BackgroundToast.show(MainActivity.this,"录音手动上传成功");
        }).start();
    }

    // Placeholder for your actual upload method

    public void checkAndshowNotifation()
    {
        if (checkAndRequestNotificationPermission()) {
            // 权限已授予，可以显示通知
            showNotification("准备录音...");
        }
    }

    public void updateValues(String unit, int blade,String unitName) {
        runOnUiThread(() -> {
            tvUnit.setText( unit);
            tvBlade.setText( String.valueOf(blade));
            tvUnitName.setText(unitName);

        });
    }
    private boolean checkOverlayPermission2() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                requestOverlayPermission2();
                return false;
            }
        }
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void requestOverlayPermission2() {
        try {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + getPackageName()));
            overlayPermissionLauncher.launch(intent);
        } catch (Exception e) {
            // 兼容厂商 ROM
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
        }
    }


    private void startFloatingService() {
        Intent serviceIntent = new Intent(this, FloatingWindowService.class);
        serviceIntent.putExtra("PROJECT_ID", projectId);  // 添加projectID到Intent

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(serviceIntent);
        } else {
            startService(serviceIntent);
        }

    }
    private void openVideoPicker() {
        String[] requiredPermissions;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // Android 14+
            requiredPermissions = new String[]{
                    Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.READ_MEDIA_IMAGES
            };
        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            requiredPermissions = new String[]{Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.READ_MEDIA_IMAGES };
        } else {
            requiredPermissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
        }

        if (checkPermissions(requiredPermissions)) {
            launchVideoPicker();
        } else {
            requestPermissionLauncher.launch(requiredPermissions);
        }
    }

    private boolean checkPermissions(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    private void launchVideoPicker() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("video/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        videoPickerLauncher.launch(Intent.createChooser(intent, "选择视频"));
    }
    private void handleVideoSelection(Intent data) {
        List<Uri> videoUris = new ArrayList<>();

        if (data.getClipData() != null) {
            // 多选
            ClipData clipData = data.getClipData();
            for (int i = 0; i < clipData.getItemCount(); i++) {
                videoUris.add(clipData.getItemAt(i).getUri());
            }
        } else if (data.getData() != null) {
            // 单选
            videoUris.add(data.getData());
        }

        // 显示确认对话框
        showQualificationDialog(videoUris);
    }
    private void showQualificationDialog(List<Uri> videoUris) {
        new AlertDialog.Builder(this)
                .setTitle("机组合格确认")
                .setMessage("这批上传的机组视频是否合格？")
                .setPositiveButton("合格", (dialog, which) -> {
                    processVideosWithQualification(videoUris, 1);
                })
                .setNegativeButton("不合格", (dialog, which) -> {
                    processVideosWithQualification(videoUris, 0);
                })
                .setCancelable(false)
                .show();
    }
    private void processVideosWithQualification(List<Uri> videoUris, int qualified) {
        for (Uri videoUri : videoUris) {
            processVideoUri(videoUri, qualified);
        }
    }

    private void processVideoUri(Uri videoUri, int qualified) {
        String[] projection = {
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DATE_TAKEN,
                MediaStore.Video.Media.DURATION
        };

        try (Cursor cursor = getContentResolver().query(videoUri, projection, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                long time = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN));
                long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));

                String user = getCurrentUser();
                uploadVideo(path, time, duration, lastKnownLocation, user, qualified);
            }
        }
    }


    private void showVideoEndDateTimePicker() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(startTime);
        calendar.add(Calendar.HOUR, 1);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                (view, year, month, day) -> {
                    calendar.set(year, month, day);
                    TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                            (view1, hour, minute) -> {
                                calendar.set(Calendar.HOUR_OF_DAY, hour);
                                calendar.set(Calendar.MINUTE, minute);
                                endTime = calendar.getTimeInMillis();

                                if (endTime <= startTime) {
                                    Toast.makeText(MainActivity.this, "结束时间必须晚于开始时间", Toast.LENGTH_SHORT).show();
                                    showVideoEndDateTimePicker();
                                    return;
                                }

                                updateTimeDisplay();
                                showVideoConfirmationDialog();
                            }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                    timePickerDialog.setTitle("选择视频结束时间");
                    timePickerDialog.show();
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setTitle("选择视频结束日期");
        datePickerDialog.show();
    }
    private void showVideoDateTimePicker() {
        final Calendar calendar = Calendar.getInstance();
        Toast.makeText(this, "请选择视频开始时间", Toast.LENGTH_SHORT).show();

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                (view, year, month, day) -> {
                    calendar.set(year, month, day);
                    TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                            (view1, hour, minute) -> {
                                calendar.set(Calendar.HOUR_OF_DAY, hour);
                                calendar.set(Calendar.MINUTE, minute);
                                startTime = calendar.getTimeInMillis();
                                updateTimeDisplay();
                                Toast.makeText(MainActivity.this, "请选择视频结束时间", Toast.LENGTH_SHORT).show();
                                showVideoEndDateTimePicker();
                            }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                    timePickerDialog.setTitle("选择视频开始时间");
                    timePickerDialog.show();
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setTitle("选择视频开始日期");
        datePickerDialog.show();
    }

    private void showVideoConfirmationDialog() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        String message = "确认上传以下时间范围内的视频吗？\n\n" +
                "开始时间: " + sdf.format(new Date(startTime)) + "\n" +
                "结束时间: " + sdf.format(new Date(endTime));

        new AlertDialog.Builder(this)
                .setTitle("确认视频查询")
                .setMessage(message)
                .setPositiveButton("确认", (dialog, which) -> checkVideoPermissionAndQuery())
                .setNegativeButton("取消", (dialog, which) -> Toast.makeText(MainActivity.this, "已取消视频查询", Toast.LENGTH_SHORT).show())
                .setNeutralButton("重新选择", (dialog, which) -> showVideoDateTimePicker())
                .show();
    }
    private void checkVideoPermissionAndQuery() {
        String[] permissions;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // Android 14+
            permissions = new String[]{
                    Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.READ_MEDIA_IMAGES
            };
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Android 13
            permissions = new String[]{Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.READ_MEDIA_IMAGES };
        } else { // Android 12-
            permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
        }

        if (checkAnyPermissionGranted(permissions)) {
            queryVideos();
        } else {
            ActivityCompat.requestPermissions(this, permissions, REQUEST_VIDEO_PERMISSION);

        }
    }




    private void queryVideos() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            List<VideoInfo> videoList = new ArrayList<>();
            ContentResolver contentResolver = getContentResolver();
            Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

            String[] projection = {
                    MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.DATE_TAKEN,
                    MediaStore.Video.Media.DURATION
            };

            String selection = MediaStore.Video.Media.DATE_TAKEN + " >= ? AND " +
                    MediaStore.Video.Media.DATE_TAKEN + " <= ?";
            String[] selectionArgs = {String.valueOf(startTime), String.valueOf(endTime)};

            String sortOrder = MediaStore.Video.Media.DATE_TAKEN + " DESC";

            try (Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)) {
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        String path = cursor.getString(1);
                        long time = cursor.getLong(2);
                        long duration = cursor.getLong(3);

                        videoList.add(new VideoInfo(path, time, duration));
                    }
                }
            } catch (Exception e) {
                Log.e("VideoQuery", "Error querying videos", e);
            } finally {
                executorService.shutdown();
            }

            final int count = videoList.size();
            runOnUiThread(() -> {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
                String message = "找到 " + count + " 个视频 (" +
                        sdf.format(new Date(startTime)) + " 到 " +
                        sdf.format(new Date(endTime)) + ")";

                if (count > 0) {
                    // 显示确认对话框
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("视频上传确认")
                            .setMessage(message + "\n\n这些视频是否合格？")
                            .setPositiveButton("合格", (dialog, which) -> {
                                uploadVideosWithQualification(videoList, 1);
                            })
                            .setNegativeButton("不合格", (dialog, which) -> {
                                uploadVideosWithQualification(videoList, 0);
                            })
                            .show();
                } else {
                    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
                }
            });
        });
    }

    // 辅助类，用于存储视频信息
    private static class VideoInfo {
        String path;
        long time;
        long duration;

        VideoInfo(String path, long time, long duration) {
            this.path = path;
            this.time = time;
            this.duration = duration;
        }
    }

    // 批量上传视频并设置合格标记
    private void uploadVideosWithQualification(List<VideoInfo> videoList, int qualified) {
        String user = getCurrentUser();
        for (VideoInfo video : videoList) {
            uploadVideo(video.path, video.time, video.duration, lastKnownLocation, user, qualified);
        }
    }
    // 添加视频上传方法
    private void uploadVideo(String filePath, long time, long duration, Location location, String user,int q) {
        File file = new File(filePath);
        double latitude = location != null ? location.getLatitude() : 0;
        double longitude = location != null ? location.getLongitude() : 0;
        double altitude = location != null ? location.getAltitude() : 0;
        String finalunit = unit==null?"0":unit;
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(),
                        RequestBody.create(file, MediaType.parse("video/*")))
                .addFormDataPart("shootingTime", String.valueOf(time))
                .addFormDataPart("duration", String.valueOf(duration))
                .addFormDataPart("latitude", String.valueOf(latitude))
                .addFormDataPart("longitude", String.valueOf(longitude))
                .addFormDataPart("altitude", String.valueOf(altitude))
                .addFormDataPart("workerUserId", user)
                .addFormDataPart("partId",finalunit)
                .addFormDataPart("qualified", String.valueOf(q))
                .build();

        Request request = new Request.Builder()
                .url("http://pms.dtyx.net:9158/video-file-info/batch-upload") // 修改为您的视频上传URL
                .post(requestBody)
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                BackgroundToast.show(
                        MainActivity.this,
                        "视频上传失败: " + e.getMessage()
                );
            }

            @Override
            public void onResponse(Call call, Response response) {
                runOnUiThread(() -> {
                    if (response.isSuccessful()) {
                        BackgroundToast.show(
                                MainActivity.this,
                                "视频上传成功: " + response.code()
                        );
                    } else {
                        BackgroundToast.show(
                                MainActivity.this,
                                "视频上传失败: " + response.code()
                        );
                    }
                });
            }
        });
    }

    private void initFloatingActionButton() {
        if (!checkOverlayPermission()) {
            requestOverlayPermission();
            return;
        }

        floatingActionButton = new FloatingActionButton(this);
        floatingActionButton.setSize(FloatingActionButton.SIZE_MINI);
        floatingActionButton.setImageResource(R.drawable.ic_mic_off);
        floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(Color.RED));

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                        WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.START;
        params.x = 0;
        params.y = 100;

        try {
            WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            windowManager.addView(floatingActionButton, params);
        } catch (Exception e) {
            Toast.makeText(this, "需要悬浮窗权限才能显示悬浮球", Toast.LENGTH_SHORT).show();
            return;
        }

        floatingActionButton.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
                WindowManager.LayoutParams params = (WindowManager.LayoutParams) floatingActionButton.getLayoutParams();

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(floatingActionButton, params);
                        return true;

                    case MotionEvent.ACTION_UP:
                        if (Math.abs(event.getRawX() - initialTouchX) < 10 &&
                                Math.abs(event.getRawY() - initialTouchY) < 10) {
                            floatingActionButton.performClick();
                        }
                        return true;
                }
                return false;
            }
        });

        floatingActionButton.setOnClickListener(v -> {
            if (isRecording) {
                stopVoiceRecording();
                floatingActionButton.setImageResource(R.drawable.ic_mic_off);
                showNotification("录音已停止");
            } else {
                if (checkRecordPermission1()) {
                    currentAudioPath = startVoiceRecording();
                    if (currentAudioPath != null) {

                        isRecording = true;
                        floatingActionButton.setImageResource(R.drawable.ic_mic_on);
                        showNotification("录音中...");
                        Toast.makeText(this,"开始录音了"+currentAudioPath,Toast.LENGTH_SHORT).show();
                    }
                } else {
                    requestRecordPermission();
                }
            }
        });
    }
    private boolean checkAndRequestNotificationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{Manifest.permission.POST_NOTIFICATIONS},
                        NOTIFICATION_PERMISSION_REQUEST_CODE
                );
                return false;
            }
        }
        return true;
    }
    private void showNotification(String message) {
        // 确保通知渠道已创建（Android 8.0+ 必须）
        createNotificationChannel();

        // 检查通知权限（Android 13+）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
                    != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "请授予通知权限以显示录音状态", Toast.LENGTH_SHORT).show();
                return; // 不显示通知，但不崩溃
            }
        }

        try {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "recording_channel")
                    .setSmallIcon(R.drawable.ic_mic_on) // 确保图标有效
                    .setContentTitle("录音状态")
                    .setContentText(message)
                    .setPriority(NotificationCompat.PRIORITY_LOW);

            NotificationManagerCompat.from(this).notify(1, builder.build());
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "通知显示失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "recording_channel",
                    "Recording Service",
                    NotificationManager.IMPORTANCE_LOW
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }
    private boolean checkOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.canDrawOverlays(this);
        }
        return true;
    }

    private void requestOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, OVERLAY_PERMISSION_REQUEST_CODE);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OVERLAY_PERMISSION_REQUEST_CODE) {
            if (checkOverlayPermission()) {
                // Permission granted, initialize floating button
                initFloatingActionButton();
            } else {
                Toast.makeText(this, "悬浮窗权限被拒绝，无法显示悬浮球", Toast.LENGTH_SHORT).show();
            }
        }
    }
    private boolean checkRecordPermission1() {
        return ContextCompat.checkSelfPermission(this,
                Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
    }

    private void requestRecordPermission() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.RECORD_AUDIO},
                AUDIO_PERMISSION_REQUEST_CODE);
    }
    private String startVoiceRecording() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {
            return null;
        }

        // 启动前台服务
        Intent serviceIntent = new Intent(this, RecordingService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            startForegroundService(serviceIntent);
        } else {
            startService(serviceIntent);
        }

        String audioFilePath = Objects.requireNonNull(getExternalCacheDir()).getAbsolutePath()
                + "/" + System.currentTimeMillis() + ".3gp";

        try {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.setOutputFile(audioFilePath);
            // 添加异常捕获
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
                Toast.makeText(this,"AudioRecordprepare() failed: " + e.getMessage(),Toast.LENGTH_SHORT).show();
                return null;
            }
            mediaRecorder.start();
            return audioFilePath;
        } catch (Exception  e) {
            e.printStackTrace();
            Toast.makeText(this, "录音初始化失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            return null;
        }

    }

    private void stopVoiceRecording() {
        if (mediaRecorder != null) {
            try {
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder = null;
                isRecording = false;
                // 停止服务
                stopService(new Intent(this, RecordingService.class));
                BackgroundToast.show(this, "录音已保存"+currentAudioPath);

                uploadAudio(imgpath,currentAudioPath,currentUsername);
                currentAudioPath=null;
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    }


    private void queryPhotos() {
        // 定义查询时间范围的条件（示例，可根据需要修改）
        String selection = MediaStore.Images.Media.DATE_TAKEN + " BETWEEN ? AND ? AND"+
                "(" + MediaStore.Images.Media.DATA + " LIKE '%/DCIM/%' OR " +
                MediaStore.Images.Media.DATA + " LIKE '%/Camera/%')" ;;

        // 使用Executor替代AsyncTask
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            int count = 0;
            ContentResolver contentResolver = getContentResolver();
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            String[] projection = {
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DATE_TAKEN,
                    MediaStore.Images.Media.LATITUDE,
                    MediaStore.Images.Media.LONGITUDE
            };

            String[] selectionArgs = {String.valueOf(startTime), String.valueOf(endTime)};
            String sortOrder = MediaStore.Images.Media.DATE_TAKEN + " DESC";

            try (Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)) {
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
                        String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                        long time = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN));

                        // 初始化位置信息
                        double latitude = 0.0;
                        double longitude = 0.0;
                        double altitude = 0.0;

                        // 从媒体库读取位置信息（如果有）
                        int latIndex = cursor.getColumnIndex(MediaStore.Images.Media.LATITUDE);
                        int lngIndex = cursor.getColumnIndex(MediaStore.Images.Media.LONGITUDE);
                        if (latIndex != -1 && lngIndex != -1) {
                            latitude = cursor.getDouble(latIndex);
                            longitude = cursor.getDouble(lngIndex);
                        }

                        // 改进位置获取
                        try {
                            if (locationManager != null && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                                Location lastLocation = locationManager.getLastKnownLocation(
                                        LocationManager.GPS_PROVIDER);
                                if (lastLocation != null) {
                                    lastKnownLocation = lastLocation;
                                    runOnUiThread(() -> updateLocationText(lastLocation));
                                }
                            }
                        } catch (SecurityException e) {
                            e.printStackTrace();
                        }

                        if (lastKnownLocation != null) {
                            latitude = lastKnownLocation.getLatitude();
                            longitude = lastKnownLocation.getLongitude();
                            altitude = lastKnownLocation.getAltitude();
                        }

                        // 改进EXIF高度读取
                        try {
                            ExifInterface exif = new ExifInterface(path);
                            String altitudeStr = exif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
                            if (altitudeStr != null) {
                                String[] parts = altitudeStr.split("/");
                                if (parts.length == 2) {
                                    double alt = Double.parseDouble(parts[0]) /
                                            Double.parseDouble(parts[1]);
                                    altitude = alt;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        String user = getCurrentUser();
                        Log.d("PhotoQuery", "Processing photo for user: " + user);

                        // 使用更高效的方式生成缩略图
                        Bitmap thumbnail = null;
                        try {
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inJustDecodeBounds = true;
                            BitmapFactory.decodeFile(path, options);

                            options.inSampleSize = calculateInSampleSize(options, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
                            options.inJustDecodeBounds = false;
                            thumbnail = BitmapFactory.decodeFile(path, options);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        count++;
                        if (thumbnail != null) {
                            ImageInfo imageInfo = new ImageInfo(path, time, thumbnail,
                                    latitude, longitude, altitude, user, "0");

                            // 确保线程安全
                            synchronized (this) {
                                lastPhotoId = Math.max(lastPhotoId, id);
                            }

                            double finalAltitude = altitude;
                            double finalLongitude = longitude;
                            double finalLatitude = latitude;
                            String finalproject=getProject();
                            String finalunit=getUnit();
                            int finalblade=getBlade();
                            // 使用另一个线程执行上传或保存操作
                            Executors.newSingleThreadExecutor().execute(() -> {
                                if (isNetworkAvailable()) {
                                    uploadPhoto(path, time, lastKnownLocation, user, "0",finalproject,finalunit,finalblade);

                                } else {
                                    db2.imageDao().insert(new ImageEntity(path, time,
                                            finalLatitude, finalLongitude,
                                            finalAltitude, user, "0",finalproject,finalunit,finalblade,false));
                                }
                            });
                        }
                    }
                    cursor.close();
                }
            } catch (Exception e) {
                Log.e("PhotoQuery", "Error querying photos", e);
            } finally {
                executorService.shutdown();
            }

            // 更新UI需要在主线程执行
            final int finalCount = count;
            runOnUiThread(() -> {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
                Toast.makeText(MainActivity.this,
                        "找到 " + finalCount + " 张照片 (" +
                                sdf.format(new Date(startTime)) + " 到 " +
                                sdf.format(new Date(endTime)) + ")上传中",
                        Toast.LENGTH_LONG).show();
            });
        });
    }

    // 辅助方法计算采样大小
    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    private void showDateTimePicker() {
        final Calendar calendar = Calendar.getInstance();

        // Show prompt for start time selection
        Toast.makeText(this, "请选择开始时间", Toast.LENGTH_SHORT).show();

        // 日期选择器
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        calendar.set(year, month, day);

                        // 时间选择器
                        TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                                new TimePickerDialog.OnTimeSetListener() {
                                    @Override
                                    public void onTimeSet(TimePicker view, int hour, int minute) {
                                        calendar.set(Calendar.HOUR_OF_DAY, hour);
                                        calendar.set(Calendar.MINUTE, minute);
                                        startTime = calendar.getTimeInMillis();

                                        // Update start time display immediately
                                        updateTimeDisplay();

                                        // Show prompt for end time selection
                                        Toast.makeText(MainActivity.this, "请选择结束时间", Toast.LENGTH_SHORT).show();

                                        // 选择结束时间
                                        showEndDateTimePicker();
                                    }
                                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                        timePickerDialog.setTitle("选择开始时间");
                        timePickerDialog.show();
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setTitle("选择开始日期");
        datePickerDialog.show();
    }

    private void showEndDateTimePicker() {
        final Calendar calendar = Calendar.getInstance();
        // Set initial time to start time + 1 hour as a suggestion
        calendar.setTimeInMillis(startTime);
        calendar.add(Calendar.HOUR, 1);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        calendar.set(year, month, day);

                        TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                                new TimePickerDialog.OnTimeSetListener() {
                                    @Override
                                    public void onTimeSet(TimePicker view, int hour, int minute) {
                                        calendar.set(Calendar.HOUR_OF_DAY, hour);
                                        calendar.set(Calendar.MINUTE, minute);
                                        endTime = calendar.getTimeInMillis();

                                        // 验证结束时间是否晚于开始时间
                                        if (endTime <= startTime) {
                                            Toast.makeText(MainActivity.this, "结束时间必须晚于开始时间", Toast.LENGTH_SHORT).show();
                                            showEndDateTimePicker(); // 重新选择
                                            return;
                                        }

                                        // 更新UI显示选择的时间
                                        updateTimeDisplay();

                                        // 显示确认对话框
                                        showConfirmationDialog();
                                    }
                                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                        timePickerDialog.setTitle("选择结束时间");
                        timePickerDialog.show();
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setTitle("选择结束日期");
        datePickerDialog.show();
    }
    private void showConfirmationDialog() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        String message = "确认上传以下时间范围内的照片吗？\n\n" +
                "开始时间: " + sdf.format(new Date(startTime)) + "\n" +
                "结束时间: " + sdf.format(new Date(endTime));

        new AlertDialog.Builder(this)
                .setTitle("确认查询")
                .setMessage(message)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户确认，开始查询照片
                        queryPhotos();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户取消，可以重新选择时间或不做任何操作
                        Toast.makeText(MainActivity.this, "已取消查询", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("重新选择", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户想重新选择时间
                        showDateTimePicker();
                    }
                })
                .show();
    }
    private void updateTimeDisplay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());

        if (startTime > 0) {
            startTimeText.setText("开始时间：" + sdf.format(new Date(startTime)));
        }

        if (endTime > 0) {
            endTimeText.setText("结束时间：" + sdf.format(new Date(endTime)));
        }
    }








    // 设置上传间隔
    private void setUploadInterval() {
        try {
            int minutes = Integer.parseInt(etUploadInterval.getText().toString());
            if (minutes <= 0) {
                Toast.makeText(this, "请输入大于0的分钟数", Toast.LENGTH_SHORT).show();
                return;
            }

            uploadIntervalMinutes = minutes;

            // 保存到SharedPreferences
            SharedPreferences.Editor editor = getSharedPreferences("UploadPrefs", MODE_PRIVATE).edit();
            editor.putInt("upload_interval", uploadIntervalMinutes);
            editor.apply();

            Toast.makeText(this, "上传间隔已设置为: " + minutes + "分钟", Toast.LENGTH_SHORT).show();

            // 如果定时上传正在运行，重新启动以应用新间隔
            if (isTimedUploadRunning) {
                stopTimedUpload();
                startTimedUpload();
            }
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }

    // 开始定时上传
    private void startTimedUpload() {
        if (uploadIntervalMinutes <= 0) {
            Toast.makeText(this, "请先设置上传间隔", Toast.LENGTH_SHORT).show();
            return;
        }

        if (isTimedUploadRunning) {
            Toast.makeText(this, "定时上传已经在运行", Toast.LENGTH_SHORT).show();
            return;
        }

        isTimedUploadRunning = true;
        btnStartTimedUpload.setEnabled(false);
        btnStopTimedUpload.setEnabled(true);
        etUploadInterval.setEnabled(false);
        btnSetInterval.setEnabled(false);



        // 设置定时任务
        timedUploadRunnable = new Runnable() {
            @Override
            public void run() {
                if (!isTimedUploadRunning) return;

                startUploadQueue();
                timedUploadHandler.postDelayed(this, uploadIntervalMinutes * 60 * 1000L);
            }
        };

        timedUploadHandler.postDelayed(timedUploadRunnable, uploadIntervalMinutes * 60 * 1000L);
        statusText.setText("定时上传已启动，间隔: " + uploadIntervalMinutes + "分钟");
    }

    // 停止定时上传
    private void stopTimedUpload() {
        if (!isTimedUploadRunning) return;

        isTimedUploadRunning = false;
        if (timedUploadRunnable != null) {
            timedUploadHandler.removeCallbacks(timedUploadRunnable);
        }

        btnStartTimedUpload.setEnabled(true);
        btnStopTimedUpload.setEnabled(false);
        etUploadInterval.setEnabled(true);
        btnSetInterval.setEnabled(true);

        statusText.setText("定时上传已停止");
    }


    private void uploadPhotoSync(String filePath, long time, Location location, String user, String audioPath,String project,String unit,int blade) {
        File file = new File(filePath);

        double latitude = location != null ? location.getLatitude() : 0;
        double longitude = location != null ? location.getLongitude() : 0;
        double altitude = location != null ? location.getAltitude() : 0;

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(),
                        RequestBody.create(file, MediaType.parse("image/*")))
                .addFormDataPart("time", String.valueOf(time))
                .addFormDataPart("latitude", String.valueOf(latitude))
                .addFormDataPart("longitude", String.valueOf(longitude))
                .addFormDataPart("altitude", String.valueOf(altitude))
                .addFormDataPart("uploadUser", user)
                .addFormDataPart("projectId",project)
                .addFormDataPart("turbineId",unit)
                .addFormDataPart("partId",String.valueOf(blade));

        //

        RequestBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url("http://pms.dtyx.net:9158/common/upload-image/file")
                .post(requestBody)
                .build();

        try (Response response = httpClient.newCall(request).execute()) {
            if (response.isSuccessful()) {
                BackgroundToast.show(
                        MainActivity.this,
                        "上传成功: " + response.code() + filePath
                );
                uploadAudio(filePath,audioPath,currentUsername);

                db.imageDao().insert(new ImageEntity(filePath, time,
                        latitude, longitude, altitude, user, audioPath, project, unit, blade, true));
            } else {
                BackgroundToast.show(MainActivity.this,
                        "上传失败: " + response.message()
                );
            }
        } catch (IOException e) {
            if (isUploading) { // 只有主动上传时才重新加入队列
                db2.imageDao().insert(new ImageEntity(filePath, time,
                        latitude, longitude, altitude, user, audioPath, project, unit, blade, false));
            }
            BackgroundToast.show(MainActivity.this,
                    "上传失败: " + e.getMessage()
            );

        }
    }


    private void stopUploadQueue() {
        isUploading = false;
        // 中断当前上传
        synchronized (uploadLock) {
            // 这里会中断当前上传
            uploadLock.notifyAll(); // 唤醒可能正在等待的上传线程
        }
        btnStopUpload.setEnabled(false);
        btnUploadQueue.setEnabled(true);
        // 更新状态显示
        runOnUiThread(() -> {
            statusText.setText(isTimedUploadRunning ? "定时上传已暂停" : "上传已停止");
            Toast.makeText(MainActivity.this, "上传队列已停止", Toast.LENGTH_SHORT).show();
        });
    }
    private void checkRecordPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {
            // 请求录音权限
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.RECORD_AUDIO},
                    AUDIO_PERMISSION_REQUEST_CODE);
        }else {
            // 已经有录音权限，继续检查图片权限
            checkPermissionAndPickImage();
        }
    }



    private void initLocationManager() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        try {
            Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (lastLocation != null) {
                lastKnownLocation = lastLocation;
                updateLocationText(lastLocation);
                Log.d("LocationUpdate", "最新已知位置: " + locationToString(lastLocation));
            }
        } catch (SecurityException e) {
            e.printStackTrace();
            BackgroundToast.show( MainActivity.this,
                    " 获取位置失败"
            );
        }
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                lastKnownLocation = location;
                Toast.makeText(MainActivity.this,
                        "位置更新: " + location.getLatitude() + ", " + location.getLongitude(),
                        Toast.LENGTH_SHORT).show();
                // 打印到Logcat
                Log.d("LocationUpdate", "新位置: " + locationToString(location));
                updateLocationText(lastKnownLocation);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}

            @Override
            public void onProviderEnabled(String provider) {}

            @Override
            public void onProviderDisabled(String provider) {}
        };

        try {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this,"没有授予定位权限",Toast.LENGTH_SHORT).show();
                return;
            }
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    60000,  // 5秒更新间隔
                    10,    // 10米距离变化
                    locationListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void updateLocationText(Location location) {
        runOnUiThread(() -> {
            if (location == null) {
                locationText.setText("当前位置: 无法获取位置信息");
                return;
            }
            String locationInfo = String.format(Locale.getDefault(),
                    "当前位置:\n纬度: %.6f\n经度: %.6f\n海拔: %.2f米\n精度: %.2f米\n更新时间: %s",
                    location.getLatitude(),
                    location.getLongitude(),
                    location.getAltitude(),
                    location.getAccuracy(),
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(location.getTime())));

            locationText.setText(locationInfo);
        });
    }

    // 将位置转换为字符串用于打印
    private String locationToString(Location location) {
        return String.format(Locale.getDefault(),
                "纬度: %.6f, 经度: %.6f, 海拔: %.2f, 精度: %.2f, 时间: %s",
                location.getLatitude(),
                location.getLongitude(),
                location.getAltitude(),
                location.getAccuracy(),
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(location.getTime())));
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSIONS_REQUEST_CODE) {
            // 检查所有权限是否被授予
            boolean allPermissionsGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allPermissionsGranted = false;
                    break;
                }
            }

            if (allPermissionsGranted) {
                // 所有权限都已授予，继续初始化位置管理器
                initLocationManager();
            } else {
                // 处理权限被拒绝的情况
                handlePermissionDenial();
            }
        }
        else if (requestCode == REQUEST_VIDEO_PERMISSION) {
            boolean allGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }

            if(allGranted) {
                Toast.makeText(this, "媒体权限已授权", Toast.LENGTH_SHORT).show();

                queryVideos();
            }
            else {
                Toast.makeText(this, "需要媒体权限才能查询视频", Toast.LENGTH_SHORT).show();
            }


        }
        else if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                showNotification("通知权限已授予");
                initFloatingActionButton();
            } else {
                Toast.makeText(this, "通知权限被拒绝，无法显示录音状态", Toast.LENGTH_SHORT).show();
            }
        }
        else if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 位置权限已授予，继续检查图片权限
                checkRecordPermission();
                initLocationManager();
                Toast.makeText(this, "位置权限已授予", Toast.LENGTH_SHORT).show();
            } else {
                // 位置权限被拒绝，关闭开关
                switchMonitor.setChecked(false);
                Toast.makeText(this, "需要位置权限才能获取位置信息", Toast.LENGTH_SHORT).show();
            }
        }
        else if (requestCode == AUDIO_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 录音权限已授予，继续检查位置权限
                checkPermissionAndPickImage();
                Toast.makeText(this, "语音权限已授予", Toast.LENGTH_SHORT).show();
            } else {
                // 录音权限被拒绝，关闭开关
                switchMonitor.setChecked(false);
                Toast.makeText(this, "需要录音权限才能录制语音", Toast.LENGTH_SHORT).show();
            }
        }
        else if (requestCode == PERMISSION_REQUEST_CODE) {

            boolean allGranted=true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }

            if (allGranted) {
                Toast.makeText(this, "媒体权限已授与", Toast.LENGTH_SHORT).show();
                checkAndshowNotifation();
                lastPhotoId=getLastPhotoId();
                startMonitoring();
            } else {
                switchMonitor.setChecked(false);
                Toast.makeText(this, "需要媒体权限以选择图片", Toast.LENGTH_SHORT).show();


                if (shouldShowRequestPermissionRationale(permissions)) {
                    showPermissionExplanationDialog(() -> {
                        // 重新请求权限
                        requestPermissions(permissions);
                    });
                }
            }
        }
    }
    private void handlePermissionDenial() {
        // 检查哪些关键权限被拒绝
        boolean showRationale = false;

        // 检查是否有权限被永久拒绝（用户勾选了"不再询问"）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            for (String permission : new String[]{
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.POST_NOTIFICATIONS}) {
                if (!shouldShowRequestPermissionRationale(permission)
                        && ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    showRationale = true;
                    break;
                }
            }
        } else {
            for (String permission : new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.ACCESS_FINE_LOCATION}) {
                if (!shouldShowRequestPermissionRationale(permission)
                        && ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    showRationale = true;
                    break;
                }
            }
        }

        if (showRationale) {
            // 显示解释为什么需要权限，并引导用户去设置页面
            showPermissionExplanationDialog();
        } else {
            // 部分权限被拒绝，但用户没有选择"不再询问"
            Toast.makeText(this, "部分功能可能无法正常使用", Toast.LENGTH_SHORT).show();
            // 仍然尝试初始化位置管理器（如果位置权限被授予）
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                initLocationManager();
            }
        }
    }

    // 显示权限解释对话框
    private void showPermissionExplanationDialog() {
        new AlertDialog.Builder(this)
                .setTitle("需要权限")
                .setMessage("某些功能需要权限才能正常工作。请前往设置授予权限。")
                .setPositiveButton("去设置", (dialog, which) -> {
                    // 跳转到应用设置页面
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void checkPermissionAndPickImage() {
        String[] permissions;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // Android 14+
            permissions = new String[]{
                    Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED,
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO
            };
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Android 13
            permissions = new String[]{Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO};
        } else { // Android 12-
            permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
        }

        if (checkAnyPermissionGranted(permissions)) {
            startMonitoring();
        } else {
            requestPermissions(permissions);
        }
    }

    private boolean checkAnyPermissionGranted(String[] permissions) {
        for (String perm : permissions) {
            if (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }

    private void requestPermissions(String[] permissions) {
        if (shouldShowRequestPermissionRationale(permissions)) {
            showPermissionExplanationDialog(() ->
                    ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE));
        } else {
            ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
        }
    }

    private boolean shouldShowRequestPermissionRationale(String[] permissions) {
        for (String perm : permissions) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
                return true;
            }
        }
        return false;
    }

    private void openAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", getPackageName(), null));
        startActivity(intent);
    }

    private void showPermissionExplanationDialog(Runnable onContinue) {
        new AlertDialog.Builder(this)
                .setTitle("需要权限")
                .setMessage("此功能需要访问图片权限以监控相册变化")
                .setPositiveButton("继续", (dialog, which) -> onContinue.run())
                .setNegativeButton("取消", null)
                .show();
    }

    private void showHistory(AppDatabase database, String title, String deleteBtnText, String deleteMessage) {
        new Thread(() -> {
            List<ImageEntity> history = database.imageDao().getAll();
            runOnUiThread(() -> {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(title);

                LinearLayout mainContainer = createDialogContainer(deleteBtnText, database, deleteMessage, history);
                ScrollView scrollContainer = createScrollContainer(history, database);

                mainContainer.addView(scrollContainer);
                builder.setView(mainContainer);
                builder.setPositiveButton("关闭", null);

                AlertDialog dialog = builder.create();
                updateCurrentDialog(database, dialog);
                dialog.show();
            });
        }).start();
    }


    private LinearLayout createDialogContainer(String deleteBtnText, AppDatabase database, String deleteMessage, List<ImageEntity> history) {
        LinearLayout mainContainer = new LinearLayout(this);
        mainContainer.setOrientation(LinearLayout.VERTICAL);

        // 顶部按钮布局
        LinearLayout buttonLayout = new LinearLayout(this);
        buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
        buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        // 批量删除按钮
        Button batchDeleteButton = new Button(this);
        batchDeleteButton.setText("批量删除");
        batchDeleteButton.setOnClickListener(v -> showBatchDeleteDialog(database, history));
        buttonLayout.addView(batchDeleteButton);

        // 时间段删除按钮
        Button timeRangeDeleteButton = new Button(this);
        timeRangeDeleteButton.setText("时间段删除");
        timeRangeDeleteButton.setOnClickListener(v -> showTimeRangeDeleteDialog(database));
        buttonLayout.addView(timeRangeDeleteButton);

        // 删除所有按钮
        Button deleteAllButton = new Button(this);
        deleteAllButton.setText(deleteBtnText);
        deleteAllButton.setOnClickListener(v -> confirmDelete(database, deleteMessage));
        buttonLayout.addView(deleteAllButton);

        mainContainer.addView(buttonLayout);

        return mainContainer;
    }




    private ScrollView createScrollContainer(List<ImageEntity> history, AppDatabase database) {
        ScrollView scrollContainer = new ScrollView(this);
        LinearLayout contentContainer = new LinearLayout(this);
        contentContainer.setOrientation(LinearLayout.VERTICAL);

        if (history.isEmpty()) {
            addEmptyView(contentContainer, database);
        } else {
            addHistoryItems(contentContainer, history, database);
        }

        scrollContainer.addView(contentContainer);
        return scrollContainer;
    }

    private void addEmptyView(LinearLayout contentContainer, AppDatabase database) {
        TextView emptyText = new TextView(this);
        emptyText.setText(database == db ? "暂无历史记录" : "暂无缓存记录");
        contentContainer.addView(emptyText);
    }

    private void addHistoryItems(LinearLayout contentContainer, List<ImageEntity> history, AppDatabase database) {
        for (ImageEntity item : history) {
            View itemView = LayoutInflater.from(this).inflate(R.layout.item_history, contentContainer, false);

            // 添加复选框
            CheckBox checkBox = new CheckBox(this);
            checkBox.setTag(item.id); // 使用记录的ID作为tag

            LinearLayout itemLayout = (LinearLayout) itemView.findViewById(R.id.item_layout);
            if (itemLayout != null) {
                itemLayout.addView(checkBox, 0); // 将复选框添加到最前面
            }

            setupItemView(itemView, item);
            contentContainer.addView(itemView);
        }
    }
    private void showBatchDeleteDialog(AppDatabase database, List<ImageEntity> history) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("批量删除");

        // 获取所有复选框的选中状态
        List<Integer> selectedIds = new ArrayList<>();
        ViewGroup container = (ViewGroup) ((ViewGroup) currentDialog.getWindow().getDecorView()).getChildAt(0);
        findSelectedCheckBoxes(container, selectedIds);

        if (selectedIds.isEmpty()) {
            builder.setMessage("请先勾选要删除的项");
            builder.setPositiveButton("确定", null);
        } else {
            builder.setMessage("确定要删除选中的 " + selectedIds.size() + " 项记录吗？");
            builder.setPositiveButton("删除", (dialog, which) -> {
                deleteSelectedRecords(database, selectedIds);
            });
            builder.setNegativeButton("取消", null);
        }

        builder.show();
    }
    // 查找选中的复选框
    private void findSelectedCheckBoxes(ViewGroup container, List<Integer> selectedIds) {
        for (int i = 0; i < container.getChildCount(); i++) {
            View child = container.getChildAt(i);
            if (child instanceof ViewGroup) {
                findSelectedCheckBoxes((ViewGroup) child, selectedIds);
            } else if (child instanceof CheckBox) {
                CheckBox checkBox = (CheckBox) child;
                if (checkBox.isChecked()) {
                    selectedIds.add((Integer) checkBox.getTag());
                }
            }
        }
    }

    // 删除选中的记录
    private void deleteSelectedRecords(AppDatabase database, List<Integer> selectedIds) {
        executor.execute(() -> {
            database.imageDao().deleteByIds(selectedIds);
            runOnUiThread(() -> {
                Toast.makeText(this, "已删除选中的 " + selectedIds.size() + " 项记录", Toast.LENGTH_SHORT).show();
                dismissCurrentDialog(database);
            });
        });
    }
    // 显示时间段删除对话框
    // 显示时间段删除对话框 - 优化后的版本
    private void showTimeRangeDeleteDialog(AppDatabase database) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("按时间段删除");

        // 使用ScrollView确保内容可滚动
        ScrollView scrollView = new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(32, 32, 32, 32); // 增加内边距

        // 开始时间部分
        TextView startText = new TextView(this);
        startText.setText("开始时间:");
        startText.setTextSize(18);
        layout.addView(startText);

        // 开始日期和时间选择器
        LinearLayout startTimeLayout = new LinearLayout(this);
        startTimeLayout.setOrientation(LinearLayout.VERTICAL);

        final DatePicker startDatePicker = new DatePicker(this);
        startDatePicker.setCalendarViewShown(false); // 使用spinner模式更紧凑
        startTimeLayout.addView(startDatePicker);

        final TimePicker startTimePicker = new TimePicker(this);
        startTimePicker.setIs24HourView(true);
        startTimeLayout.addView(startTimePicker);

        layout.addView(startTimeLayout);

        // 添加分隔空间
        View space = new View(this);
        space.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, 32));
        layout.addView(space);

        // 结束时间部分
        TextView endText = new TextView(this);
        endText.setText("结束时间:");
        endText.setTextSize(18);
        layout.addView(endText);

        // 结束日期和时间选择器
        LinearLayout endTimeLayout = new LinearLayout(this);
        endTimeLayout.setOrientation(LinearLayout.VERTICAL);

        final DatePicker endDatePicker = new DatePicker(this);
        endDatePicker.setCalendarViewShown(false);
        endTimeLayout.addView(endDatePicker);

        final TimePicker endTimePicker = new TimePicker(this);
        endTimePicker.setIs24HourView(true);
        endTimeLayout.addView(endTimePicker);

        layout.addView(endTimeLayout);

        scrollView.addView(layout);
        builder.setView(scrollView);

        builder.setPositiveButton("删除", (dialog, which) -> {
            // 获取开始时间
            Calendar startCal = Calendar.getInstance();
            startCal.set(startDatePicker.getYear(),
                    startDatePicker.getMonth(),
                    startDatePicker.getDayOfMonth(),
                    startTimePicker.getHour(),
                    startTimePicker.getMinute(),
                    0);
            long startTime = startCal.getTimeInMillis();

            // 获取结束时间
            Calendar endCal = Calendar.getInstance();
            endCal.set(endDatePicker.getYear(),
                    endDatePicker.getMonth(),
                    endDatePicker.getDayOfMonth(),
                    endTimePicker.getHour(),
                    endTimePicker.getMinute(),
                    59);
            long endTime = endCal.getTimeInMillis();

            if (startTime > endTime) {
                Toast.makeText(this, "开始时间不能晚于结束时间", Toast.LENGTH_SHORT).show();
                return;
            }

            confirmTimeRangeDelete(database, startTime, endTime);
        });

        builder.setNegativeButton("取消", null);

        // 创建并显示对话框
        AlertDialog dialog = builder.create();

        // 设置对话框大小
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(window.getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }

        dialog.show();
    }

    // 确认时间段删除
    private void confirmTimeRangeDelete(AppDatabase database, long startTime, long endTime) {
        new AlertDialog.Builder(this)
                .setTitle("确认删除")
                .setMessage(String.format("确定要删除 %s 至 %s 的所有记录吗?",
                        sdf.format(new Date(startTime)),
                        sdf.format(new Date(endTime))))
                .setPositiveButton("删除", (d, w) -> deleteRecordsByTimeRange(database, startTime, endTime))
                .setNegativeButton("取消", null)
                .show();
    }

    // 删除时间段内的记录
    private void deleteRecordsByTimeRange(AppDatabase database, long startTime, long endTime) {
        executor.execute(() -> {
            int deletedCount = database.imageDao().deleteByTimeRange(startTime, endTime);
            runOnUiThread(() -> {
                Toast.makeText(this, "已删除 " + deletedCount + " 条记录", Toast.LENGTH_SHORT).show();
                dismissCurrentDialog(database);
            });
        });
    }
    private void setupItemView(View itemView, ImageEntity item) {
        ImageView iv = itemView.findViewById(R.id.iv_thumbnail);
        TextView tv = itemView.findViewById(R.id.tv_info);

        executor.execute(() -> loadThumbnail(iv, item.path));

        tv.setText(String.format(
                Locale.US,
                "时间: %s\n" +
                        "路径: %s\n"+
                        "纬度: %.6f\n" +   // 保留6位小数
                        "经度: %.6f\n" +
                        "高度: %.2f 米\n" + // 保留2位小数
                        "参与人名: %s\n"+
                        "项目名字:%s\n"+
                        "机组号:%s\n"+
                        "叶片号:%d\n",

                sdf.format(new Date(item.time)),  // 时间
                item.path,                       // 路径
                item.latitude,                    // 纬度
                item.longitude,                  // 经度
                item.altitude,                   // 高度（单位：米）
                item.user,// 用户信息
                projectName,
                unitName,
                item.blade

        ));
        itemView.setOnClickListener(v -> openFullImage(item.path));
    }

    private void loadThumbnail(ImageView iv, String path) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = DECODE_SAMPLE_SIZE;
            Bitmap src = BitmapFactory.decodeFile(path, options);
            if (src != null) {
                Bitmap thumbnail = Bitmap.createScaledBitmap(src, THUMBNAIL_SIZE, THUMBNAIL_SIZE, true);
                runOnUiThread(() -> iv.setImageBitmap(thumbnail));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void confirmDelete(AppDatabase database, String message) {

        new AlertDialog.Builder(this)
                .setTitle("确认删除")
                .setMessage(message)
                .setPositiveButton("删除", (d, w) -> deleteAllRecords(database))
                .setNegativeButton("取消", null)
                .show();

    }


    private void deleteAllRecords(AppDatabase database) {
        executor.execute(() -> {
            database.imageDao().deleteAll();
            runOnUiThread(() -> {
                Toast.makeText(this, "已删除所有记录", Toast.LENGTH_SHORT).show();
                dismissCurrentDialog(database);
            });
        });
    }

    private void updateCurrentDialog(AppDatabase database, AlertDialog dialog) {
        if (database == db) {
            currentDialog = dialog;
        } else {
            currentDialog2 = dialog;
        }
    }

    private void dismissCurrentDialog(AppDatabase database) {
        if (database == db && currentDialog != null && currentDialog.isShowing()) {
            currentDialog.dismiss();
        } else if (database == db2 && currentDialog2 != null && currentDialog2.isShowing()) {
            currentDialog2.dismiss();
        }
    }


    private void startMonitoring() {
        synchronized (monitoringLock) {
            if (isMonitoring) return;

            if (isNetworkAvailable()) {
                executor.execute(() -> {
                    // 使用上传锁保护数据库操作
                    // 在后台线程中执行数据库操作
                    List<ImageEntity> history = db2.imageDao().getAll();

                    if (history != null && !history.isEmpty()) {
                        synchronized (uploadLock) {
                            for (ImageEntity image : history) {
                                uploadPhoto(image.path, image.time, lastKnownLocation, image.user, image.audioPath,image.project,image.unit,image.blade);
                            }
                        }
                    }

                    // 删除所有记录（仍在后台线程）
                    db2.imageDao().deleteAll();

                });
            }

            // 创建内容观察者
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            contentObserver = new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);
                    checkNewPhotos();
                }
            };

            // 注册观察者
            getContentResolver().registerContentObserver(uri, true, contentObserver);
            isMonitoring = true;
            runOnUiThread(() -> statusText.setText("当前状态：正在监听相册变化"));
        }
    }
    private boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) return false;

        NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
        return capabilities != null &&
                (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                        capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                        capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
    }
    private void stopMonitoring() {
        if (!isMonitoring) return;

        // 注销观察者
        if (contentObserver != null) {
            getContentResolver().unregisterContentObserver(contentObserver);
            contentObserver = null;
        }
        isMonitoring = false;
        statusText.setText("当前状态：未监听");
    }
    private void startUploadQueue() {
        if (isUploading) {
            Toast.makeText(this, "上传正在进行中", Toast.LENGTH_SHORT).show();
            return;
        }

        executor.execute(() -> {
            isUploading = true;
            runOnUiThread(() -> {
                btnUploadQueue.setEnabled(false);
                btnStopUpload.setEnabled(true);
                statusText.setText("正在上传缓存队列..." + (isTimedUploadRunning ? "(定时上传)" : ""));
            });

            try {
                while (isUploading) {
                    List<ImageEntity> queue = db2.imageDao().getAll();
                    if (queue.isEmpty()) {
                        break;
                    }

                    for (ImageEntity image : queue) {
                        if (!isUploading) break;

                        synchronized (uploadLock) {
                            uploadPhotoSync(image.path, image.time,
                                    new Location("") {{
                                        setLatitude(image.latitude);
                                        setLongitude(image.longitude);
                                        setAltitude(image.altitude);
                                    }},
                                    image.user, image.audioPath,image.project,image.unit,image.blade);
                        }

                        if (isUploading) {
                            db2.imageDao().delete(image);
                        }
                    }
                }
            } finally {
                runOnUiThread(() -> {
                    btnUploadQueue.setEnabled(true);
                    btnStopUpload.setEnabled(false);
                    String status = isUploading ?
                            (isTimedUploadRunning ? "定时上传完成" : "手动上传完成") :
                            "上传已终止";
                    statusText.setText(status);
                });
                isUploading = false;
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (updateReceiver != null) {
            unregisterReceiver(updateReceiver);
        }
        stopMonitoring();
        executor.shutdown();
        if (httpClient != null) {
            httpClient.dispatcher().executorService().shutdown();
            httpClient.connectionPool().evictAll();
        }
        stopTimedUpload();
        if (!executor.isTerminated()) {
            executor.shutdownNow();
        }
        // 移除悬浮球
        if (floatingActionButton != null) {
            WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            windowManager.removeView(floatingActionButton);
        }
        if(isRecording)
            stopVoiceRecording();

    }


    //
    private void checkNewPhotos() {

        String[] projection = {
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DATE_ADDED,
                MediaStore.Images.Media.DATE_TAKEN,  // 拍摄时间（相机照片才有）
                MediaStore.Images.Media.LATITUDE,
                MediaStore.Images.Media.LONGITUDE
        };

// 查询条件：ID > lastPhotoId 且 DATE_TAKEN > 0（确保是相机拍摄的照片）
        String selection = MediaStore.Images.Media._ID + " > ? AND " +
                MediaStore.Images.Media.DATE_TAKEN + " > 0 AND " +
                "(" + MediaStore.Images.Media.DATA + " LIKE '%/DCIM/%' OR " +
                MediaStore.Images.Media.DATA + " LIKE '%/Camera/%')";
        String[] selectionArgs = new String[]{
                String.valueOf(lastPhotoId),

        };
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                MediaStore.Images.Media._ID + " DESC"
        );

        if (cursor != null) {
            while (cursor.moveToNext()) {
                long id = cursor.getLong(0);
                String path = cursor.getString(1);
                imgpath=path;
                long time = cursor.getLong(2) * 1000;
                double latitude = cursor.getDouble(3);
                double longitude = cursor.getDouble(4);
                double altitude = 0.0; // 默认值
                if(locationManager!=null) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                        break;
                    }
                    else {
                        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (lastLocation != null) {
                            lastKnownLocation = lastLocation;
                            updateLocationText(lastLocation);
                        }
                    }

                }
                if (lastKnownLocation != null) {
                    latitude = lastKnownLocation.getLatitude();
                    longitude = lastKnownLocation.getLongitude();
                    altitude = lastKnownLocation.getAltitude();
                }

                // 从EXIF获取高度信息
                try {
                    ExifInterface exif = new ExifInterface(path);
                    String altitudeStr = exif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
                    if (altitudeStr != null) {
                        altitude = Double.parseDouble(altitudeStr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String user = getCurrentUser();
                Log.e("用户是", user);

                // 生成缩略图
                Bitmap bitmap = null;
                try {
                    bitmap = BitmapFactory.decodeFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (bitmap != null) {
                    Bitmap thumbnail = Bitmap.createScaledBitmap(bitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, true);
                    String audioPath = currentAudioPath ==null?"0":currentAudioPath;
                    ImageInfo imageInfo = new ImageInfo(path, time, thumbnail, latitude, longitude, altitude, user, audioPath);

                    // 保护imageList和lastPhotoId的修改

                    lastPhotoId = Math.max(lastPhotoId, id);


                    double finalAltitude = altitude;
                    double finalLongitude = longitude;
                    double finalLatitude = latitude;



                        if(isNetworkAvailable()) {

                            executor.execute(() -> {
                                List<ImageEntity> history = db2.imageDao().getAll();
                                if (history != null && !history.isEmpty()) {
                                    synchronized (uploadLock) {
                                        for (ImageEntity image : history) {
                                            uploadPhoto(image.path, image.time, lastKnownLocation,
                                                    image.user, image.audioPath, image.project,
                                                    image.unit, image.blade);
                                        }
                                    }
                                }
                                db2.imageDao().deleteAll();
                          });
                            executor.execute(() -> {
                                uploadPhoto(path, time, lastKnownLocation, user,
                                        audioPath, projectId, unit, blade);
                            });

                        }
                        else {
                            executor.execute(() -> {
                                db2.imageDao().insert(new ImageEntity(path, time,finalLatitude,finalLongitude,finalAltitude, user, audioPath,projectId,unit,blade,false));

                            });
                        }

                    };

                hasClickedRecord=false;
                currentAudioPath=null;
            }
            cursor.close();
        }



    }


    private String getCurrentUser() {
        String user = currentUsername;
        // 如果用户没有输入信息，则使用设备型号作为默认值
        if (user.isEmpty()) {
            user = Build.MODEL;
        }
        return user;
    }
    private String getProject(){

        return projectId.isEmpty()?"项目ID未填写":projectId;
    }
    private String getUnit()
    {
        return unit.isEmpty()?"机组未填写":unit;
    }
    private int getBlade()
    {
        return  blade;
    }
    private void openFullImage(String imagePath) {
        File file = new File(imagePath);
        Uri uri = FileProvider.getUriForFile(
                this,
                getPackageName() + ".fileprovider",
                file
        );

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/*");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }


    private void uploadPhoto(String filePath, long time, Location lastKnownLocation, String user, String audioPath, String project, String unit, int blade) {
        synchronized (uploadLock) {
            if (isUploading) {
                // 如果正在执行队列上传，则直接加入队列不单独上传
                double latitude = 0;
                double longitude = 0;
                double altitude = 0;
                if (lastKnownLocation != null) {
                    latitude = lastKnownLocation.getLatitude();
                    longitude = lastKnownLocation.getLongitude();
                    altitude = lastKnownLocation.getAltitude();
                }

                db2.imageDao().insert(new ImageEntity(filePath, time, latitude, longitude, altitude, user, audioPath, project, unit, blade, false));
                return;
            }
        }

        File imageFile = new File(filePath);

        double latitude;
        double longitude;
        double altitude;
        if (lastKnownLocation != null) {
            latitude = lastKnownLocation.getLatitude();
            longitude = lastKnownLocation.getLongitude();
            altitude = lastKnownLocation.getAltitude();
        } else {
            altitude = 0;
            longitude = 0;
            latitude = 0;
        }

        // 创建包含图片文件、录音文件和元数据的请求体
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", imageFile.getName(),
                        RequestBody.create(imageFile, MediaType.parse("image/*")))
                .addFormDataPart("time", String.valueOf(time))
                .addFormDataPart("latitude", String.valueOf(latitude))
                .addFormDataPart("longitude", String.valueOf(longitude))
                .addFormDataPart("altitude", String.valueOf(altitude))
                .addFormDataPart("uploadUser", user)
                .addFormDataPart("projectId", project)
                .addFormDataPart("turbineId", unit)
                .addFormDataPart("partId", String.valueOf(blade));




        RequestBody requestBody = builder.build();

        // 构建请求
        Request request = new Request.Builder()
                .url("http://pms.dtyx.net:9158/common/upload-image/file") // 修改为你的实际URL
                .post(requestBody)
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                BackgroundToast.show( MainActivity.this,
                        "上传失败: " + e.getMessage() + "已存入缓冲队列"
                );
                executor.execute(() -> {
                    db2.imageDao().insert(new ImageEntity(filePath, time, latitude, longitude, altitude, user, audioPath, project, unit, blade, false));
                });
            }

            @Override
            public void onResponse(Call call, Response response) {
                runOnUiThread(() -> {
                    if (response.isSuccessful()) {
                        BackgroundToast.show(
                                MainActivity.this,
                                "上传成功: " + response.code() + filePath);
                        executor.execute(() -> {
                            db.imageDao().insert(new ImageEntity(filePath, time, latitude, longitude, altitude, user, audioPath, project, unit, blade, true));


                        });



                    } else {
                        BackgroundToast.show(
                                MainActivity.this,
                                "上传失败: " + response.code() +response.message()+ "已存入缓冲队列"
                        );
                        executor.execute(() -> {
                            db2.imageDao().insert(new ImageEntity(filePath, time, latitude, longitude, altitude, user, audioPath, project, unit, blade, false));
                        });
                    }
                });
            }
        });
    }

    private void uploadAudio(String imageId, String audioPath, String token) {
        if (audioPath == null || audioPath.equals("0")) {
            BackgroundToast.show(MainActivity.this,"文件路径为空");

        }

        File audioFile = new File(audioPath);
        if (!audioFile.exists()) {
            BackgroundToast.show(MainActivity.this,"文件不存在");

        }

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", audioFile.getName(),
                        RequestBody.create(audioFile, MediaType.parse("audio/*")))
                .build();

        Request.Builder requestBuilder = new Request.Builder()
                .url( "http://pms.dtyx.net:9158/audio/upload/" + imageId)
                .post(requestBody);

        if (token != null && !token.isEmpty()) {
            requestBuilder.addHeader("Authorization", token);
        }

        httpClient.newCall(requestBuilder.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                BackgroundToast.show(
                        MainActivity.this,
                        "录音上传失败: " + e.getMessage()
                );
                String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                        .format(new Date());

                executor.execute(() -> {
                    db.AudioDao().insert(new AudioEntity(audioPath,imageId,currentTime));
                });

            }

            @Override
            public void onResponse(Call call, Response response) {
                // Handle response
                if (!response.isSuccessful()) {
                    // Handle upload failure

                    BackgroundToast.show(
                            MainActivity.this,
                            "录音上传失败: " + response.code() + response.message()
                    );
                    String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                            .format(new Date());

                    executor.execute(() -> {
                        db.AudioDao().insert(new AudioEntity(audioPath,imageId,currentTime));
                    });
                }
                else {
                    BackgroundToast.show(
                            MainActivity.this,
                            "录音上传成功: " + response.code() + imageId
                    );
                    String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                            .format(new Date());
                    executor.execute(() -> {
                        db.AudioDao().delete(new AudioEntity(audioPath,imageId,currentTime));
                    });

                }
                response.close();
            }
        });
    }
    private long getLastPhotoId() {
        long id = -1;
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{"MAX(" + MediaStore.Images.Media._ID + ")"},
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            id = cursor.getLong(0);  // MAX(_ID) 的结果在索引 0
            cursor.close();
        }
        return id;
    }


}