Fixed configuration loss bug
This commit is contained in:
parent
ab7f844447
commit
a750dbbb00
@ -19,20 +19,21 @@ import it.danieleverducci.ojo.entities.Camera;
|
||||
/**
|
||||
* 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 TAG = "Settings";
|
||||
|
||||
private volatile String settingsFilePath;
|
||||
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;
|
||||
CamerasSettings s = new CamerasSettings();
|
||||
Settings s = new Settings();
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(filePath);
|
||||
ObjectInputStream ois = new ObjectInputStream(fin);
|
||||
s = (CamerasSettings) ois.readObject();
|
||||
s = (Settings) ois.readObject();
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.d(TAG, "No saved settings found, will create a new one");
|
||||
} catch (IOException e) {
|
@ -3,6 +3,7 @@ package it.danieleverducci.ojo.entities;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Camera implements Serializable {
|
||||
private static final long serialVersionUID = -3837361587400158910L;
|
||||
private String name;
|
||||
private String rtspUrl;
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
package it.danieleverducci.ojo.ui;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
|
||||
@ -21,7 +19,7 @@ import android.view.ViewGroup;
|
||||
import java.util.List;
|
||||
|
||||
import it.danieleverducci.ojo.R;
|
||||
import it.danieleverducci.ojo.CamerasSettings;
|
||||
import it.danieleverducci.ojo.Settings;
|
||||
import it.danieleverducci.ojo.SharedPreferencesManager;
|
||||
import it.danieleverducci.ojo.databinding.FragmentSettingsItemListBinding;
|
||||
import it.danieleverducci.ojo.entities.Camera;
|
||||
@ -34,7 +32,7 @@ import it.danieleverducci.ojo.utils.ItemMoveCallback;
|
||||
public class SettingsFragment extends Fragment {
|
||||
|
||||
private FragmentSettingsItemListBinding binding;
|
||||
private CamerasSettings camerasSettings;
|
||||
private Settings settings;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
@ -79,8 +77,8 @@ public class SettingsFragment extends Fragment {
|
||||
super.onResume();
|
||||
|
||||
// Load cameras
|
||||
camerasSettings = CamerasSettings.fromDisk(getContext());
|
||||
List<Camera> cams = camerasSettings.getCameras();
|
||||
settings = Settings.fromDisk(getContext());
|
||||
List<Camera> cams = settings.getCameras();
|
||||
|
||||
// Set the adapter
|
||||
RecyclerView recyclerView = binding.list;
|
||||
@ -109,7 +107,7 @@ public class SettingsFragment extends Fragment {
|
||||
|
||||
// Save cameras
|
||||
List<Camera> cams = ((SettingsRecyclerViewAdapter)binding.list.getAdapter()).getItems();
|
||||
this.camerasSettings.setCameras(cams);
|
||||
this.camerasSettings.save();
|
||||
this.settings.setCameras(cams);
|
||||
this.settings.save();
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ import androidx.navigation.fragment.NavHostFragment;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
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.entities.Camera;
|
||||
|
||||
@ -21,7 +21,7 @@ public class StreamUrlFragment extends Fragment {
|
||||
public static final String ARG_CAMERA = "arg_camera";
|
||||
|
||||
private FragmentAddStreamBinding binding;
|
||||
private CamerasSettings camerasSettings;
|
||||
private Settings settings;
|
||||
private Integer selectedCamera = null;
|
||||
|
||||
@Override
|
||||
@ -29,7 +29,7 @@ public class StreamUrlFragment extends Fragment {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Load existing settings (if any)
|
||||
camerasSettings = CamerasSettings.fromDisk(getContext());
|
||||
settings = Settings.fromDisk(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -45,7 +45,7 @@ public class StreamUrlFragment extends Fragment {
|
||||
if (args != null && args.containsKey(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.setHint(getContext().getString(R.string.stream_list_default_camera_name).replace("{camNo}", (this.selectedCamera+1)+""));
|
||||
binding.streamUrl.setText(c.getRtspUrl());
|
||||
@ -74,16 +74,16 @@ public class StreamUrlFragment extends Fragment {
|
||||
|
||||
if (StreamUrlFragment.this.selectedCamera != null) {
|
||||
// Update camera
|
||||
Camera c = camerasSettings.getCameras().get(StreamUrlFragment.this.selectedCamera);
|
||||
Camera c = settings.getCameras().get(StreamUrlFragment.this.selectedCamera);
|
||||
c.setName(name);
|
||||
c.setRtspUrl(url);
|
||||
} else {
|
||||
// Add stream to list
|
||||
camerasSettings.addCamera(new Camera(name, url));
|
||||
settings.addCamera(new Camera(name, url));
|
||||
}
|
||||
|
||||
// Save
|
||||
if (!camerasSettings.save()) {
|
||||
if (!settings.save()) {
|
||||
Snackbar.make(view, R.string.add_stream_error_saving, Snackbar.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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.entities.Camera;
|
||||
import it.danieleverducci.ojo.utils.DpiUtils;
|
||||
@ -137,7 +137,7 @@ public class SurveillanceFragment extends Fragment {
|
||||
|
||||
|
||||
private void addAllCameras() {
|
||||
CamerasSettings settings = CamerasSettings.fromDisk(getContext());
|
||||
Settings settings = Settings.fromDisk(getContext());
|
||||
List<Camera> cc = settings.getCameras();
|
||||
|
||||
int elemsPerSide = calcGridSideElements(cc.size());
|
||||
|
Loading…
Reference in New Issue
Block a user