Android Open Source - HockeySDK-Android Prefs Util






From Project

Back to project page HockeySDK-Android.

License

The source code is released under:

Apache License

If you think the Android project HockeySDK-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 net.hockeyapp.android.utils;
/*ww w  .j  ava  2s .c o  m*/
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * <h3>Description</h3>
 * 
 * {@link SharedPreferences} helper class
 * 
 * <h3>License</h3>
 * 
 * <pre>
 * Copyright (c) 2011-2014 Bit Stadium GmbH
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * </pre>
 *
 * @author Bogdan Nistor
 */
public class PrefsUtil {
  private SharedPreferences feedbackTokenPrefs;
  private SharedPreferences.Editor feedbackTokenPrefsEditor;
  private SharedPreferences nameEmailSubjectPrefs;
  private SharedPreferences.Editor nameEmailSubjectPrefsEditor;
  
  /** Private constructor prevents instantiation from other classes */
  private PrefsUtil() { 
  }

  /**
  * PrefsUtilHolder is loaded on the first execution of WbUtil.getInstance() 
  * or the first access to PrefsUtilHolder.INSTANCE, not before.
  */
  private static class PrefsUtilHolder { 
    public static final PrefsUtil INSTANCE = new PrefsUtil();
  }

  /**
   * Return the singleton.
   *
   * @return the singleton
   */
  public static PrefsUtil getInstance() {
    return PrefsUtilHolder.INSTANCE;
  }
    
  /**
   * Save feedback token to {@link SharedPreferences}
   * 
   * @param context the context to use
   * @param token the feedback token
   */
  public void saveFeedbackTokenToPrefs(Context context, String token) {
    if (context != null) {
      feedbackTokenPrefs = context.getSharedPreferences(Util.PREFS_FEEDBACK_TOKEN, 0);
      if (feedbackTokenPrefs != null) {
        feedbackTokenPrefsEditor = feedbackTokenPrefs.edit();
        feedbackTokenPrefsEditor.putString(Util.PREFS_KEY_FEEDBACK_TOKEN, token);
        applyChanges(feedbackTokenPrefsEditor);
      }
    }
  }
    
  /**
   * Retrieves the feedback token from {@link SharedPreferences}
   *
   * @param context the context to use
   * @return the feedback token
   */
  public String getFeedbackTokenFromPrefs(Context context) {
    if (context == null) {
      return null;
    }
    
    feedbackTokenPrefs = context.getSharedPreferences(Util.PREFS_FEEDBACK_TOKEN, 0);
    if (feedbackTokenPrefs == null) {
      return null;
    }
    
    return feedbackTokenPrefs.getString(Util.PREFS_KEY_FEEDBACK_TOKEN, null);
  }

  /**
   * Save name and email to {@link SharedPreferences}
   *
   * @param context the context to use
   * @param name the user's name
   * @param email the user's email
   * @param subject the message subject
   */
  public void saveNameEmailSubjectToPrefs(Context context, String name, String email, String subject) {
    if (context != null) {
      nameEmailSubjectPrefs = context.getSharedPreferences(Util.PREFS_NAME_EMAIL_SUBJECT, 0);
      if (nameEmailSubjectPrefs != null) {
        nameEmailSubjectPrefsEditor = nameEmailSubjectPrefs.edit();
        if (name == null || email == null || subject == null) {
          nameEmailSubjectPrefsEditor.putString(Util.PREFS_KEY_NAME_EMAIL_SUBJECT, null); 
        } else {
          nameEmailSubjectPrefsEditor.putString(Util.PREFS_KEY_NAME_EMAIL_SUBJECT, String.format("%s|%s|%s", 
              name, email, subject));
        }
        
        applyChanges(nameEmailSubjectPrefsEditor);
      }
    }
  }
  
  /**
   * Retrieves the name and email from {@link SharedPreferences}
   *
   * @param context the context to use
   * @return a string with name, email, and subject
   */
  public String getNameEmailFromPrefs(Context context) {
    if (context == null) {
      return null;
    }
    
    nameEmailSubjectPrefs = context.getSharedPreferences(Util.PREFS_NAME_EMAIL_SUBJECT, 0);
    if (nameEmailSubjectPrefs == null) {
      return null;
    }
    
    return nameEmailSubjectPrefs.getString(Util.PREFS_KEY_NAME_EMAIL_SUBJECT, null);
  }
  
  /**
   * Apply SharedPreferences.Editor changes. If the code runs on API level 9 or higher,
   * the asynchronous method apply is used, otherwise commit.
   *
   * @param editor the editor that should be commited
   */
  public static void applyChanges(Editor editor) {
    if (applySupported()) {
      editor.apply();
    }
    else {
      editor.commit();
    }
  }
  
  /**
   * Returns true if SharedPreferences.Editor.apply is supported.
   *
   * @return true if SharedPreferences.Editor.apply is supported
   */
  public static Boolean applySupported() {
    try {
      return (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD);
    }
    catch (NoClassDefFoundError e) {
      return false;
    }
  }
}




Java Source Code List

net.hockeyapp.android.Constants.java
net.hockeyapp.android.CrashManagerListener.java
net.hockeyapp.android.CrashManager.java
net.hockeyapp.android.ExceptionHandler.java
net.hockeyapp.android.ExpiryInfoActivity.java
net.hockeyapp.android.FeedbackActivityInterface.java
net.hockeyapp.android.FeedbackActivity.java
net.hockeyapp.android.FeedbackManagerListener.java
net.hockeyapp.android.FeedbackManager.java
net.hockeyapp.android.LocaleManager.java
net.hockeyapp.android.LoginActivity.java
net.hockeyapp.android.LoginManagerListener.java
net.hockeyapp.android.LoginManager.java
net.hockeyapp.android.PaintActivity.java
net.hockeyapp.android.StringListener.java
net.hockeyapp.android.Strings.java
net.hockeyapp.android.Tracking.java
net.hockeyapp.android.UpdateActivityInterface.java
net.hockeyapp.android.UpdateActivity.java
net.hockeyapp.android.UpdateFragment.java
net.hockeyapp.android.UpdateInfoListener.java
net.hockeyapp.android.UpdateManagerListener.java
net.hockeyapp.android.UpdateManager.java
net.hockeyapp.android.adapters.MessagesAdapter.java
net.hockeyapp.android.listeners.DownloadFileListener.java
net.hockeyapp.android.listeners.SendFeedbackListener.java
net.hockeyapp.android.objects.ErrorObject.java
net.hockeyapp.android.objects.FeedbackAttachment.java
net.hockeyapp.android.objects.FeedbackMessage.java
net.hockeyapp.android.objects.FeedbackResponse.java
net.hockeyapp.android.objects.Feedback.java
net.hockeyapp.android.tasks.AttachmentDownloader.java
net.hockeyapp.android.tasks.CheckUpdateTaskWithUI.java
net.hockeyapp.android.tasks.CheckUpdateTask.java
net.hockeyapp.android.tasks.DownloadFileTask.java
net.hockeyapp.android.tasks.GetFileSizeTask.java
net.hockeyapp.android.tasks.LoginTask.java
net.hockeyapp.android.tasks.ParseFeedbackTask.java
net.hockeyapp.android.tasks.SendFeedbackTask.java
net.hockeyapp.android.utils.AsyncTaskUtils.java
net.hockeyapp.android.utils.Base64.java
net.hockeyapp.android.utils.ConnectionManager.java
net.hockeyapp.android.utils.DeviceUtils.java
net.hockeyapp.android.utils.FeedbackParser.java
net.hockeyapp.android.utils.ImageUtils.java
net.hockeyapp.android.utils.PrefsUtil.java
net.hockeyapp.android.utils.SimpleMultipartEntity.java
net.hockeyapp.android.utils.UiThreadUtil.java
net.hockeyapp.android.utils.Util.java
net.hockeyapp.android.utils.VersionCache.java
net.hockeyapp.android.utils.VersionHelper.java
net.hockeyapp.android.utils.ViewHelper.java
net.hockeyapp.android.views.AttachmentListView.java
net.hockeyapp.android.views.AttachmentView.java
net.hockeyapp.android.views.ExpiryInfoView.java
net.hockeyapp.android.views.FeedbackMessageView.java
net.hockeyapp.android.views.FeedbackView.java
net.hockeyapp.android.views.LoginView.java
net.hockeyapp.android.views.PaintView.java
net.hockeyapp.android.views.UpdateView.java