Fixed configuration loss bug

This commit is contained in:
Daniele Verducci (Slimpenguin) 2022-02-02 08:31:28 +01:00
parent ab7f844447
commit a750dbbb00
5 changed files with 21 additions and 21 deletions

View File

@ -19,20 +19,21 @@ import it.danieleverducci.ojo.entities.Camera;
/** /**
* Manages the settings persistence * Manages the settings persistence
*/ */
public class CamerasSettings implements Serializable { public class Settings implements Serializable {
private static final long serialVersionUID = 1081285022445419696L;
private static final String FILENAME = "settings.bin"; private static final String FILENAME = "settings.bin";
private static final String TAG = "Settings"; private static final String TAG = "Settings";
private volatile String settingsFilePath; private volatile String settingsFilePath;
private List<Camera> cameras = new ArrayList<>(); private List<Camera> cameras = new ArrayList<>();
public static CamerasSettings fromDisk(Context context) { public static Settings fromDisk(Context context) {
String filePath = context.getFilesDir() + File.separator + FILENAME; String filePath = context.getFilesDir() + File.separator + FILENAME;
CamerasSettings s = new CamerasSettings(); Settings s = new Settings();
try { try {
FileInputStream fin = new FileInputStream(filePath); FileInputStream fin = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fin); ObjectInputStream ois = new ObjectInputStream(fin);
s = (CamerasSettings) ois.readObject(); s = (Settings) ois.readObject();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.d(TAG, "No saved settings found, will create a new one"); Log.d(TAG, "No saved settings found, will create a new one");
} catch (IOException e) { } catch (IOException e) {

View File

@ -3,6 +3,7 @@ package it.danieleverducci.ojo.entities;
import java.io.Serializable; import java.io.Serializable;
public class Camera implements Serializable { public class Camera implements Serializable {
private static final long serialVersionUID = -3837361587400158910L;
private String name; private String name;
private String rtspUrl; private String rtspUrl;

View File

@ -1,7 +1,5 @@
package it.danieleverducci.ojo.ui; package it.danieleverducci.ojo.ui;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
@ -21,7 +19,7 @@ import android.view.ViewGroup;
import java.util.List; import java.util.List;
import it.danieleverducci.ojo.R; import it.danieleverducci.ojo.R;
import it.danieleverducci.ojo.CamerasSettings; import it.danieleverducci.ojo.Settings;
import it.danieleverducci.ojo.SharedPreferencesManager; import it.danieleverducci.ojo.SharedPreferencesManager;
import it.danieleverducci.ojo.databinding.FragmentSettingsItemListBinding; import it.danieleverducci.ojo.databinding.FragmentSettingsItemListBinding;
import it.danieleverducci.ojo.entities.Camera; import it.danieleverducci.ojo.entities.Camera;
@ -34,7 +32,7 @@ import it.danieleverducci.ojo.utils.ItemMoveCallback;
public class SettingsFragment extends Fragment { public class SettingsFragment extends Fragment {
private FragmentSettingsItemListBinding binding; private FragmentSettingsItemListBinding binding;
private CamerasSettings camerasSettings; private Settings settings;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -79,8 +77,8 @@ public class SettingsFragment extends Fragment {
super.onResume(); super.onResume();
// Load cameras // Load cameras
camerasSettings = CamerasSettings.fromDisk(getContext()); settings = Settings.fromDisk(getContext());
List<Camera> cams = camerasSettings.getCameras(); List<Camera> cams = settings.getCameras();
// Set the adapter // Set the adapter
RecyclerView recyclerView = binding.list; RecyclerView recyclerView = binding.list;
@ -109,7 +107,7 @@ public class SettingsFragment extends Fragment {
// Save cameras // Save cameras
List<Camera> cams = ((SettingsRecyclerViewAdapter)binding.list.getAdapter()).getItems(); List<Camera> cams = ((SettingsRecyclerViewAdapter)binding.list.getAdapter()).getItems();
this.camerasSettings.setCameras(cams); this.settings.setCameras(cams);
this.camerasSettings.save(); this.settings.save();
} }
} }

View File

@ -13,7 +13,7 @@ import androidx.navigation.fragment.NavHostFragment;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import it.danieleverducci.ojo.R; import it.danieleverducci.ojo.R;
import it.danieleverducci.ojo.CamerasSettings; import it.danieleverducci.ojo.Settings;
import it.danieleverducci.ojo.databinding.FragmentAddStreamBinding; import it.danieleverducci.ojo.databinding.FragmentAddStreamBinding;
import it.danieleverducci.ojo.entities.Camera; import it.danieleverducci.ojo.entities.Camera;
@ -21,7 +21,7 @@ public class StreamUrlFragment extends Fragment {
public static final String ARG_CAMERA = "arg_camera"; public static final String ARG_CAMERA = "arg_camera";
private FragmentAddStreamBinding binding; private FragmentAddStreamBinding binding;
private CamerasSettings camerasSettings; private Settings settings;
private Integer selectedCamera = null; private Integer selectedCamera = null;
@Override @Override
@ -29,7 +29,7 @@ public class StreamUrlFragment extends Fragment {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Load existing settings (if any) // Load existing settings (if any)
camerasSettings = CamerasSettings.fromDisk(getContext()); settings = Settings.fromDisk(getContext());
} }
@Override @Override
@ -45,7 +45,7 @@ public class StreamUrlFragment extends Fragment {
if (args != null && args.containsKey(ARG_CAMERA)) { if (args != null && args.containsKey(ARG_CAMERA)) {
this.selectedCamera = args.getInt(ARG_CAMERA); this.selectedCamera = args.getInt(ARG_CAMERA);
Camera c = camerasSettings.getCameras().get(this.selectedCamera); Camera c = settings.getCameras().get(this.selectedCamera);
binding.streamName.setText(c.getName()); binding.streamName.setText(c.getName());
binding.streamName.setHint(getContext().getString(R.string.stream_list_default_camera_name).replace("{camNo}", (this.selectedCamera+1)+"")); binding.streamName.setHint(getContext().getString(R.string.stream_list_default_camera_name).replace("{camNo}", (this.selectedCamera+1)+""));
binding.streamUrl.setText(c.getRtspUrl()); binding.streamUrl.setText(c.getRtspUrl());
@ -74,16 +74,16 @@ public class StreamUrlFragment extends Fragment {
if (StreamUrlFragment.this.selectedCamera != null) { if (StreamUrlFragment.this.selectedCamera != null) {
// Update camera // Update camera
Camera c = camerasSettings.getCameras().get(StreamUrlFragment.this.selectedCamera); Camera c = settings.getCameras().get(StreamUrlFragment.this.selectedCamera);
c.setName(name); c.setName(name);
c.setRtspUrl(url); c.setRtspUrl(url);
} else { } else {
// Add stream to list // Add stream to list
camerasSettings.addCamera(new Camera(name, url)); settings.addCamera(new Camera(name, url));
} }
// Save // Save
if (!camerasSettings.save()) { if (!settings.save()) {
Snackbar.make(view, R.string.add_stream_error_saving, Snackbar.LENGTH_LONG).show(); Snackbar.make(view, R.string.add_stream_error_saving, Snackbar.LENGTH_LONG).show();
return; return;
} }

View File

@ -28,7 +28,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import it.danieleverducci.ojo.R; import it.danieleverducci.ojo.R;
import it.danieleverducci.ojo.CamerasSettings; import it.danieleverducci.ojo.Settings;
import it.danieleverducci.ojo.databinding.FragmentSurveillanceBinding; import it.danieleverducci.ojo.databinding.FragmentSurveillanceBinding;
import it.danieleverducci.ojo.entities.Camera; import it.danieleverducci.ojo.entities.Camera;
import it.danieleverducci.ojo.utils.DpiUtils; import it.danieleverducci.ojo.utils.DpiUtils;
@ -137,7 +137,7 @@ public class SurveillanceFragment extends Fragment {
private void addAllCameras() { private void addAllCameras() {
CamerasSettings settings = CamerasSettings.fromDisk(getContext()); Settings settings = Settings.fromDisk(getContext());
List<Camera> cc = settings.getCameras(); List<Camera> cc = settings.getCameras();
int elemsPerSide = calcGridSideElements(cc.size()); int elemsPerSide = calcGridSideElements(cc.size());