Ejemplo n.º 1
0
 /** Restores content for consoleContent with the key prefKey. */
 private void restoreConsole(List<String> consoleContent, String prefKey) {
   Logger.d("Service restoreConsole for " + prefKey);
   SharedPreferences prefs =
       PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
   String savedConsole = prefs.getString(prefKey, null);
   if (savedConsole != null) {
     Type listType = new TypeToken<ArrayList<String>>() {}.getType();
     ArrayList<String> items =
         MeasurementJsonConvertor.getGsonInstance().fromJson(savedConsole, listType);
     if (items != null) {
       Logger.d("Read " + items.size() + " items from prefkey " + prefKey);
       for (String item : items) {
         insertStringToConsole(consoleContent, item);
       }
       Logger.d("Restored " + consoleContent.size() + " entries to console " + prefKey);
     }
   }
 }
Ejemplo n.º 2
0
  /** Persists the content of the console as a JSON string */
  private void saveConsoleContent(List<String> consoleContent, String prefKey) {
    Logger.d("Service saveConsoleContent for key " + prefKey);
    SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = prefs.edit();

    int length = consoleContent.size();
    Logger.d("Saving " + length + " entries to prefKey " + prefKey);
    ArrayList<String> items = new ArrayList<String>();
    // Since we use insertToConsole later on to restore the content, we have to store them
    // in the reverse order to maintain the same look
    for (int i = length - 1; i >= 0; i--) {
      items.add(consoleContent.get(i));
    }
    Type listType = new TypeToken<ArrayList<String>>() {}.getType();
    editor.putString(prefKey, MeasurementJsonConvertor.getGsonInstance().toJson(items, listType));
    editor.commit();
  }