Code example for SharedPreferences

Methods: getAll

0
    // notify user that preferences and credential are not set. 
    if (mPreferences == null || mCredential == null) {
      return; 
    } 
    // check if the values are changed since last update 
    Map<String, ?> values = mPreferences.getAll();
    String localJson = GSON.toJson(values);
    try { 
      if (localJson != null && !localJson.equals(mLastSyncedJson)) {
        updateRemote(localJson);
      } else { 
        updateLocal(); 
      } 
    } catch (IOException e) {
      handleException(e);
    } 
  } 
 
  /** 
   * Gets the context. 
   */ 
  public Context getContext() {
    return mContext;
  } 
 
  /** 
   * Gets the sync manager. 
   */ 
  public AppdataPreferencesSyncManager getSyncManager() { 
    return mSyncManager;
  } 
 
  /** 
   * Gets the credential. 
   */ 
  public GoogleAccountCredential getCredential() { 
    return mCredential;
  } 
 
  /** 
   * Gets the preferences. 
   */ 
  public SharedPreferences getPreferences() {
    return mPreferences;
  } 
 
  /** 
   * Constructs a Drive service in the current context and with the 
   * credentials use to initiate AppdataPreferences instance. 
   * @return Drive service instance. 
   */ 
  public Drive getDriveService() { 
    Drive service = new Drive.Builder(
        AndroidHttp.newCompatibleTransport(), 
        new GsonFactory(), 
        mCredential).build();
    return service;
  } 
 
  /** 
   * Sets the credential and starts a periodic sync for the 
   * selected account. 
   * @param credential User's credential 
   */ 
  public void setCredential(GoogleAccountCredential credential) {
    mCredential = credential;
    mSyncManager =
        new AppdataPreferencesSyncManager(credential.getSelectedAccount());
    mSyncManager.startPeriodicSync();
  } 
 
  /** 
   * Sets the {@code SharedPreferences} instance. 
   * @param preferences {@code SharedPreferences} instance to be synced. 
   */ 
  public void setPreferences(SharedPreferences preferences) {
    mPreferences = preferences;
  } 
 
  /** 
   * Sets a {@code UserRecoverableAuthExceptionListener}. 
   * @param listener 
   */ 
  public void setOnUserRecoverableAuthExceptionListener( 
      OnUserRecoverableAuthExceptionExceptionListener listener) {
    mOnExceptionListener = listener;
  } 
 
  /** 
   * Sets the OnChangeListener. 
   * @param listener 
   */ 
  public void setOnChangeListener(OnChangeListener listener) {
    mOnChangeListener = listener;
  } 
 
  /** 
   * Updates the remote preferences file with the given JSON content. 
   * @param json    New contents of the remote preferences file in JSON. 
   * @throws IOException 
   */ 
  private void updateRemote(String json) throws IOException {
    Log.d(TAG, "Updating the remote preferences file");
    // update the remote 
    new UpdatePreferencesDriveTask(getDriveService()).execute(json);
    mLastSyncedJson = json;
  } 
 
  /** 
   * Updates the local SharedPreferences instance with the remote 
   * changes and calls OnChangeListener. 
   * @throws IOException 
   */ 
  private void updateLocal() throws IOException { 
    Log.d(TAG, "Updating the local preferences file");
    // update the local preferences 
    HashMap<String, Object> remoteObj = null;
    String json =
        new GetOrCreatePreferencesDriveTask(getDriveService()).execute(); 
    Type type = new TypeToken<HashMap<String, Object>>() {}.getType();
    remoteObj = GSON.fromJson(json, type);
    Utils.replaceValues(mPreferences, remoteObj);
    // Notify if there are changes 
    if (json != mLastSyncedJson && mOnChangeListener != null) {
      mOnChangeListener.onChange(mPreferences);
      mLastSyncedJson = json;
    } 
  } 
 
  /**