-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathSharedPreferencesManager.java
218 lines (175 loc) · 6.91 KB
/
SharedPreferencesManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package org.openbot.env;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.openbot.projects.ProjectsDataInObject;
import org.openbot.tflite.Network;
import org.openbot.utils.Enums;
public class SharedPreferencesManager {
private static final String PREFERENCES_NAME = "openbot_settings";
private static final int DEFAULT_BAUD_RATE = 115200;
private static final String BAUD_RATE = "BAUD_RATE";
private static final int DEFAULT_LOG_MODE = Enums.LogMode.CROP_IMG.ordinal();
private static final String LOG_MODE = "LOG_MODE";
private static final int DEFAULT_CONTROL_MODE = Enums.ControlMode.GAMEPAD.getValue();
private static final String CONTROL_MODE = "CONTROL_MODE";
private static final int DEFAULT_SPEED_MODE = Enums.SpeedMode.NORMAL.getValue();
private static final String SPEED_MODE = "SPEED_MODE";
private static final int DEFAULT_DRIVE_MODE = Enums.DriveMode.GAME.getValue();
private static final String DRIVE_MODE = "DRIVE_MODE";
private static final String DEFAULT_MODEL = "DEFAULT_MODEL_NAME";
private static final String OBJECT_NAV_MODEL = "OBJECT_NAV_MODEL_NAME";
private static final String AUTOPILOT_MODEL = "AUTOPILOT_MODEL_NAME";
private static final String SERVER_NAME = "SERVER_NAME";
private static final String OBJECT_TYPE = "OBJECT_TYPE";
private static final String DEFAULT_OBJECT_TYPE = "person";
// object tracker switch for speed adjusted by estimated object distance
private static final String OBJECT_NAV_DYNAMIC_SPEED = "OBJECT_NAV_DYNAMICSPEED";
private static final int DEFAULT_DEVICE = Network.Device.CPU.ordinal();
private static final String DEVICE = "DEVICE";
private static final int DEFAULT_NUM_THREAD = 4;
private static final String NUM_THREAD = "NUM_THREAD";
private static final String CAMERA_SWITCH = "CAMERA_SWITCH";
private static final String SHEET_EXPANDED = "SHEET_EXPANDED";
private static final String DELAY = "DELAY";
private static final String PROJECTS_LIST = "PROJECTS_LIST";
private final SharedPreferences preferences;
public SharedPreferencesManager(Context context) {
preferences =
context
.getApplicationContext()
.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public int getBaudrate() {
return preferences.getInt(BAUD_RATE, DEFAULT_BAUD_RATE);
}
public int getDevice() {
return preferences.getInt(DEVICE, DEFAULT_DEVICE);
}
public int getDriveMode() {
return preferences.getInt(DRIVE_MODE, DEFAULT_DRIVE_MODE);
}
public int getLogMode() {
return preferences.getInt(LOG_MODE, DEFAULT_LOG_MODE);
}
public int getControlMode() {
return preferences.getInt(CONTROL_MODE, DEFAULT_CONTROL_MODE);
}
public int getSpeedMode() {
return preferences.getInt(SPEED_MODE, DEFAULT_SPEED_MODE);
}
public int getNumThreads() {
return preferences.getInt(NUM_THREAD, DEFAULT_NUM_THREAD);
}
/**
* Get selected camera lens facing
*
* @return true for LENS_FACING_FRONT, false for LENS_FACING_BACK
*/
public boolean getCameraSwitch() {
return preferences.getBoolean(CAMERA_SWITCH, false);
}
public boolean getSheetExpanded() {
return preferences.getBoolean(SHEET_EXPANDED, false);
}
public void setBaudrate(int baudRate) {
preferences.edit().putInt(BAUD_RATE, baudRate).apply();
}
public void setDefaultModel(String model) {
preferences.edit().putString(DEFAULT_MODEL, model).apply();
}
public String getDefaultModel() {
return preferences.getString(DEFAULT_MODEL, "");
}
public void setObjectNavModel(String model) {
preferences.edit().putString(OBJECT_NAV_MODEL, model).apply();
}
public String getObjectNavModel() {
return preferences.getString(OBJECT_NAV_MODEL, "");
}
public void setAutopilotModel(String model) {
preferences.edit().putString(AUTOPILOT_MODEL, model).apply();
}
public String getAutopilotModel() {
return preferences.getString(AUTOPILOT_MODEL, "");
}
public void setServer(String server) {
preferences.edit().putString(SERVER_NAME, server).apply();
}
public String getServer() {
return preferences.getString(SERVER_NAME, "");
}
public void setObjectType(String model) {
preferences.edit().putString(OBJECT_TYPE, model).apply();
}
public String getObjectType() {
return preferences.getString(OBJECT_TYPE, DEFAULT_OBJECT_TYPE);
}
public void setDevice(int device) {
preferences.edit().putInt(DEVICE, device).apply();
}
public void setDriveMode(int mode) {
preferences.edit().putInt(DRIVE_MODE, mode).apply();
}
public void setDynamicSpeed(boolean isEnabled) {
preferences.edit().putBoolean(OBJECT_NAV_DYNAMIC_SPEED, isEnabled).apply();
}
public boolean getDynamicSpeed() {
return preferences.getBoolean(OBJECT_NAV_DYNAMIC_SPEED, false);
}
public void setLogMode(int mode) {
preferences.edit().putInt(LOG_MODE, mode).apply();
}
public void setControlMode(int mode) {
preferences.edit().putInt(CONTROL_MODE, mode).apply();
}
public void setSpeedMode(int mode) {
preferences.edit().putInt(SPEED_MODE, mode).apply();
}
public void setNumThreads(int numThreads) {
preferences.edit().putInt(NUM_THREAD, numThreads).apply();
}
public void setCameraSwitch(boolean isChecked) {
preferences.edit().putBoolean(CAMERA_SWITCH, isChecked).apply();
}
public void setSheetExpanded(boolean expanded) {
preferences.edit().putBoolean(SHEET_EXPANDED, expanded).apply();
}
public void setSensorStatus(boolean status, String sensor) {
preferences.edit().putBoolean(sensor, status).apply();
}
public boolean getSensorStatus(String sensor) {
return preferences.getBoolean(sensor, false);
}
public void setDelay(int delay) {
preferences.edit().putInt(DELAY, delay).apply();
}
public int getDelay() {
return preferences.getInt(DELAY, 200);
}
public void setProjectLIst(List<ProjectsDataInObject> allProjects) {
Gson gson = new Gson();
// Convert the List of ProjectsDataInObject to JSON string
String json = gson.toJson(allProjects);
// Store the JSON string in SharedPreferences
preferences.edit().putString(PROJECTS_LIST, json).apply();
}
public ArrayList<ProjectsDataInObject> getProjectList() {
Gson gson = new Gson();
// Retrieve the JSON string from SharedPreferences
String json = preferences.getString(PROJECTS_LIST, null);
// Define the type of the object to be deserialized
Type type = new TypeToken<ArrayList<ProjectsDataInObject>>() {}.getType();
if (gson.fromJson(json, type) != null) {
// Deserialize the JSON string to an ArrayList<ProjectsDataInObject>
return gson.fromJson(json, type);
} else {
// If the JSON string is null or cannot be deserialized, return an empty ArrayList
return new ArrayList<>();
}
}
}