Project

General

Profile

« Previous | Next » 

Revision 431285f4

Added by Leszek Koltunski 5 months ago

  • ID 431285f43b01f48b237957e292bc7220dc9ab94d
  • Parent 4aa66b05

simplify Preferences interface of OSInterface

View differences:

build.gradle
25 25

  
26 26
dependencies {
27 27
    api project(path: ':distorted-objectlib')
28
    implementation ('androidx.preference:preference:1.2.1')
28 29
}
29 30

  
30 31

  
src/main/java/org/distorted/os/OSInterface.java
15 15
import android.util.DisplayMetrics;
16 16
import android.view.MotionEvent;
17 17

  
18
import androidx.preference.PreferenceManager;
19

  
18 20
import java.io.File;
19 21
import java.io.FileInputStream;
20 22
import java.io.FileNotFoundException;
......
225 227

  
226 228
///////////////////////////////////////////////////////////////////////////////////////////////////
227 229
// PREFERENCES
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

  
230
  public void setPreferences(SharedPreferences prefs)
231
    {
232
    mPreferences = prefs;
233
    }
234

  
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

  
237
  public void setEditor(SharedPreferences.Editor editor)
238
    {
239
    mEditor = editor;
240
    }
241

  
242 230
///////////////////////////////////////////////////////////////////////////////////////////////////
243 231

  
244 232
  public void remove(String key)
245 233
    {
234
    initializePreferences();
246 235
    mEditor.remove(key);
247 236
    }
248 237

  
......
250 239

  
251 240
  public void putInt(String key, int value)
252 241
    {
242
    initializePreferences();
253 243
    mEditor.putInt(key,value);
254 244
    }
255 245

  
......
257 247

  
258 248
  public int getInt(String key, int def)
259 249
    {
250
    initializePreferences();
260 251
    return mPreferences.getInt(key,def);
261 252
    }
262 253

  
......
264 255

  
265 256
  public void putFloat(String key, float value)
266 257
    {
258
    initializePreferences();
267 259
    mEditor.putFloat(key,value);
268 260
    }
269 261

  
......
271 263

  
272 264
  public float getFloat(String key, float def)
273 265
    {
266
    initializePreferences();
274 267
    return mPreferences.getFloat(key,def);
275 268
    }
276 269

  
......
278 271

  
279 272
  public void putString(String key, String value)
280 273
    {
274
    initializePreferences();
281 275
    mEditor.putString(key,value);
282 276
    }
283 277

  
......
285 279

  
286 280
  public String getString(String key, String def)
287 281
    {
282
    initializePreferences();
288 283
    return mPreferences.getString(key,def);
289 284
    }
290 285

  
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

  
288
  public void savePreferences()
289
    {
290
    initializePreferences();
291
    mEditor.apply();
292
    }
293

  
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

  
296
  private void initializePreferences()
297
    {
298
    if( mPreferences==null || mEditor==null )
299
      {
300
      Activity act = mAct.get();
301
      mPreferences = PreferenceManager.getDefaultSharedPreferences(act);
302
      mEditor = mPreferences.edit();
303
      }
304
    }
305

  
291 306
///////////////////////////////////////////////////////////////////////////////////////////////////
292 307
// STRINGS
293 308
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff