Android Open Source - sdl_tester_android Application Preferences






From Project

Back to project page sdl_tester_android.

License

The source code is released under:

Copyright (c) 2014, Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project sdl_tester_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.livio.sdl.utils;
/*  w ww. j  a  va  2  s .  co  m*/
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Contains static methods for saving and restoring data from Android SharedPreferences.
 *
 * @author Mike Burke
 *
 */
public final class ApplicationPreferences {
  
  private ApplicationPreferences(){} // don't allow instantiation of static classes
  
  /**
   * Determines if the input key exists as part of the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to look up
   * @return True if the key exists, false otherwise
   */
  public static boolean exists(Context context, String fileName, String key){
    SharedPreferences prefs = getSharedPreferences(context, fileName);
    if(prefs == null){
      return false;
    }
    
    Map<String, ?> mapping = prefs.getAll();
    if(mapping == null || mapping.size() == 0){
      return false;
    }
    
    return (mapping.get(key) != null);
  }
  
  /**
   * Retrieves the string with the input key from the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to look up
   * @return The string if it was found, null otherwise
   */
  public static String getString(Context context, String fileName, String key){
    SharedPreferences prefs = getSharedPreferences(context, fileName);
    String result = prefs.getString(key, null);
    return result;
  }
  
  /**
   * Retrieves the boolean with the input key from the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to look up
   * @return The boolean if it was found, null otherwise
   */
  public static boolean getBoolean(Context context, String fileName, String key){
    SharedPreferences prefs = getSharedPreferences(context, fileName);
    boolean result = prefs.getBoolean(key, false);
    return result;
  }

  /**
   * Retrieves the integer with the input key from the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to look up
   * @return The integer if it was found, null otherwise
   */
  public static int getInt(Context context, String fileName, String key){
    SharedPreferences prefs = getSharedPreferences(context, fileName);
    int result = prefs.getInt(key, -1);
    return result;
  }

  /**
   * Retrieves the float with the input key from the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to look up
   * @return The float if it was found, null otherwise
   */
  public static float getFloat(Context context, String fileName, String key){
    SharedPreferences prefs = getSharedPreferences(context, fileName);
    float result = prefs.getFloat(key, -1f);
    return result;
  }

  /**
   * Retrieves the long with the input key from the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to look up
   * @return The long if it was found, null otherwise
   */
  public static long getLong(Context context, String fileName, String key){
    SharedPreferences prefs = getSharedPreferences(context, fileName);
    long result = prefs.getLong(key, -1);
    return result;
  }

  /**
   * Saves the input value at the input key in the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to save
   * @param value Value of the object to save
   */
  public static void putString(Context context, String fileName, String key, String value){
    SharedPreferences.Editor editor = getEditor(context, fileName);
    editor.putString(key, value);
    editor.apply();
  }

  /**
   * Saves the input value at the input key in the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to save
   * @param value Value of the object to save
   */
  public static void putBoolean(Context context, String fileName, String key, boolean value){
    SharedPreferences.Editor editor = getEditor(context, fileName);
    editor.putBoolean(key, value);
    editor.apply();
  }

  /**
   * Saves the input value at the input key in the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to save
   * @param value Value of the object to save
   */
  public static void putInt(Context context, String fileName, String key, int value){
    SharedPreferences.Editor editor = getEditor(context, fileName);
    editor.putInt(key, value);
    editor.apply();
  }

  /**
   * Saves the input value at the input key in the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to save
   * @param value Value of the object to save
   */
  public static void putFloat(Context context, String fileName, String key, float value){
    SharedPreferences.Editor editor = getEditor(context, fileName);
    editor.putFloat(key, value);
    editor.apply();
  }

  /**
   * Saves the input value at the input key in the input filename.
   * 
   * @param context Context with which to retrieve shared preferences
   * @param fileName File name for shared preferences
   * @param key Key of the object to save
   * @param value Value of the object to save
   */
  public static void putLong(Context context, String fileName, String key, long value){
    SharedPreferences.Editor editor = getEditor(context, fileName);
    editor.putLong(key, value);
    editor.apply();
  }
  
  private static SharedPreferences getSharedPreferences(Context context, String fileName){
    return context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
  }
  
  private static SharedPreferences.Editor getEditor(Context context, String fileName){
    return getSharedPreferences(context, fileName).edit();
  }
  
}




Java Source Code List

com.livio.sdl.IdGenerator.java
com.livio.sdl.IpAddress.java
com.livio.sdl.SdlConstants.java
com.livio.sdl.SdlImageItem.java
com.livio.sdl.SdlLogMessage.java
com.livio.sdl.SdlRequestFactory.java
com.livio.sdl.SdlResponseFactory.java
com.livio.sdl.SdlResponseTracker.java
com.livio.sdl.SdlService.java
com.livio.sdl.adapters.SdlImageAdapter.java
com.livio.sdl.adapters.SdlMessageAdapter.java
com.livio.sdl.dialogs.BaseAlertDialog.java
com.livio.sdl.dialogs.BaseImageListDialog.java
com.livio.sdl.dialogs.BaseMultipleListViewDialog.java
com.livio.sdl.dialogs.BaseOkCancelDialog.java
com.livio.sdl.dialogs.BaseSingleListViewDialog.java
com.livio.sdl.dialogs.ImageListDialog.java
com.livio.sdl.dialogs.IndeterminateProgressDialog.java
com.livio.sdl.dialogs.JsonFlipperDialog.java
com.livio.sdl.dialogs.ListViewDialog.java
com.livio.sdl.dialogs.MultipleListViewDialog.java
com.livio.sdl.dialogs.SingleJsonDialog.java
com.livio.sdl.dialogs.TextViewAlertDialog.java
com.livio.sdl.dialogs.TextViewOkCancelDialog.java
com.livio.sdl.enums.EnumClickListener.java
com.livio.sdl.enums.EnumComparator.java
com.livio.sdl.enums.SdlButton.java
com.livio.sdl.enums.SdlCommand.java
com.livio.sdl.enums.SdlImageType.java
com.livio.sdl.enums.SdlInteractionMode.java
com.livio.sdl.enums.SdlLanguage.java
com.livio.sdl.enums.SdlSpeechCapability.java
com.livio.sdl.enums.SdlSystemAction.java
com.livio.sdl.enums.SdlTextAlignment.java
com.livio.sdl.enums.SdlTransportType.java
com.livio.sdl.enums.SdlUpdateMode.java
com.livio.sdl.enums.SdlVehicleData.java
com.livio.sdl.menu.CommandButton.java
com.livio.sdl.menu.MenuItem.java
com.livio.sdl.menu.MenuManager.java
com.livio.sdl.menu.SubmenuButton.java
com.livio.sdl.utils.AndroidUtils.java
com.livio.sdl.utils.ApplicationPreferences.java
com.livio.sdl.utils.Counter.java
com.livio.sdl.utils.DownCounter.java
com.livio.sdl.utils.MathUtils.java
com.livio.sdl.utils.SdlUtils.java
com.livio.sdl.utils.StringUtils.java
com.livio.sdl.utils.Timeout.java
com.livio.sdl.utils.UpCounter.java
com.livio.sdl.utils.WifiUtils.java
com.livio.sdl.viewhelpers.MinMaxInputFilter.java
com.livio.sdl.viewhelpers.SeekBarCalculator.java
com.livio.sdltester.HelpActivity.java
com.livio.sdltester.LivioSdlTesterPreferences.java
com.livio.sdltester.MainActivity.java
com.livio.sdltester.SdlTesterImageResource.java
com.livio.sdltester.dialogs.AddCommandDialog.java
com.livio.sdltester.dialogs.AddSubMenuDialog.java
com.livio.sdltester.dialogs.ButtonSubscriptionDialog.java
com.livio.sdltester.dialogs.ButtonUnsubscriptionDialog.java
com.livio.sdltester.dialogs.ChangeRegistrationDialog.java
com.livio.sdltester.dialogs.ChoiceItemDialog.java
com.livio.sdltester.dialogs.CreateInteractionChoiceSetDialog.java
com.livio.sdltester.dialogs.DeleteCommandDialog.java
com.livio.sdltester.dialogs.DeleteFileDialog.java
com.livio.sdltester.dialogs.DeleteInteractionDialog.java
com.livio.sdltester.dialogs.DeleteSubmenuDialog.java
com.livio.sdltester.dialogs.GetDtcsDialog.java
com.livio.sdltester.dialogs.PerformInteractionDialog.java
com.livio.sdltester.dialogs.PutFileDialog.java
com.livio.sdltester.dialogs.ReadDidsDialog.java
com.livio.sdltester.dialogs.ScrollableMessageDialog.java
com.livio.sdltester.dialogs.SdlAlertDialog.java
com.livio.sdltester.dialogs.SdlConnectionDialog.java
com.livio.sdltester.dialogs.SetAppIconDialog.java
com.livio.sdltester.dialogs.SetMediaClockTimerDialog.java
com.livio.sdltester.dialogs.ShowDialog.java
com.livio.sdltester.dialogs.SliderDialog.java
com.livio.sdltester.dialogs.SoftButtonItemDialog.java
com.livio.sdltester.dialogs.SoftButtonListDialog.java
com.livio.sdltester.dialogs.SpeakDialog.java