Android Open Source - ExampleApp Preference Utils






From Project

Back to project page ExampleApp.

License

The source code is released under:

Copyright (c) 2014, Altinn All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redis...

If you think the Android project ExampleApp 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.altinn.apps.fisher.utils;
// w  ww.  ja  v a 2 s .  co  m
/**
 * This class is used to save, key value pairs in Preferences.
 */

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Base64;

import com.altinn.apps.fisher.AppContext;
import com.altinn.apps.fisher.models.CaughtInfoData;
import com.altinn.apps.fisher.models.InfoData;
import com.altinn.apps.fisher.models.ReportInfoData;
import com.altinn.apps.fisher.models.UserProfile;
import com.altinn.apps.fisher.ui.screen.HomeActivity;


public class PreferenceUtils {
  private static PreferenceUtils mInstance;
  private static final String PREFERENC_STORAGE = "com.altinn.apps.fisher.PSTORE";
  private static final String PREF_KEY_USER_REGISTERED = "PREF_KEY_USER_REGISTERED";
  private static final String PREF_KEY_USER_ID = "PREF_KEY_USER_ID";
  private static final String PREF_KEY_RECEIVED_DATA = "PREF_KEY_RECEIVED_DATA";
  private static final String PREF_KEY_CAUGHT_DATA = "PREF_KEY_CAUGHT_DATA";
  public static final String PREF_KEY_REGS_NUMBERS = "PREF_KEY_REGS_NUMBERS";
  public static final String PREF_KEY_VESSEL_NAMES = "PREF_KEY_VESSEL_NAMES";
  public static final String PREF_KEY_USER_PROFILE = "PREF_KEY_USER_PROFILE";
  public static final String  PREF_KEY_COOKIE_ID = "PREF_KEY_COOKIE_ID";
  public static final String  PREF_KEY_COOKIE_UPDATE_TIME= "PREF_KEY_COOKIE_UPDATE_TIME";
  private SharedPreferences mSharedPreferences;
  private Editor mPreferenceEditor;
  private static final char DELIMETER = '\u0AA0';//This delimiter is used to store/retrieval of arrays.
  
  private PreferenceUtils(){
    mSharedPreferences = AppContext.getInstance().getSharedPreferences(PREFERENC_STORAGE, AppContext.MODE_PRIVATE);
    mPreferenceEditor = mSharedPreferences.edit();    
  }
  public static PreferenceUtils getInstance(){
    if(mInstance == null){
      mInstance = new PreferenceUtils();
          }
    return mInstance;
  }
  
  public boolean isUserRegistered(){
    return getBoolean(PREF_KEY_USER_REGISTERED,false);
    
  }
  
  public void setUserRegistered(boolean status){
    updateBoolean(PREF_KEY_USER_REGISTERED,status );
  }
  
  public String getUserId(){
    return getString(PREF_KEY_USER_ID,"");
    
  }
  
  public void setUserId(String id){
    updateString(PREF_KEY_USER_ID,id );
  }
  
  public void setCookieUpdateTime(long currentTimeMillis) {    
    updateLong(PREF_KEY_COOKIE_UPDATE_TIME, currentTimeMillis);
  }  
  
  public long getCookieUpdateTime() {    
    return getLong(PREF_KEY_COOKIE_UPDATE_TIME, 0);
  }
  
  public void setInfoFormData(InfoData data){
    String value = Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
    String key = null;
    if(data.mFormType == HomeActivity.MENU_REPORT_RECEIVED){
      key = PREF_KEY_RECEIVED_DATA;
    }else if(data.mFormType == HomeActivity.MENU_REPORT_CAUGHT){
      key = PREF_KEY_CAUGHT_DATA;
    }
    updateString(key,value );
  }
  
  public InfoData getInfoFormData(int formType){
    String key = null;
    if(formType == HomeActivity.MENU_REPORT_RECEIVED){
      key = PREF_KEY_RECEIVED_DATA;
    }else if(formType == HomeActivity.MENU_REPORT_CAUGHT){
      key = PREF_KEY_CAUGHT_DATA;
    }
    String value = getString(key, null);
    InfoData infodata = null;
    if(value != null){
      byte[] dataBytes =  Base64.decode(value, Base64.DEFAULT);
      if(dataBytes != null){
        if(formType == HomeActivity.MENU_REPORT_RECEIVED){
          infodata = new  ReportInfoData(dataBytes);
        }else if(formType == HomeActivity.MENU_REPORT_CAUGHT){
          infodata = new  CaughtInfoData(dataBytes);
        }        
      }
    }
    return infodata;
  }
  
  public void setUserProfileData(UserProfile data){
    String value = Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
    updateString(PREF_KEY_USER_PROFILE,value );
  }
  
  public UserProfile getUserProfileData(){
    String value = getString(PREF_KEY_USER_PROFILE, null);
    UserProfile userProfile = null;
    if(value != null){
      byte[] dataBytes =  Base64.decode(value, Base64.DEFAULT);
      if(dataBytes != null){        
        userProfile = new UserProfile(dataBytes);
      }
    }
    return userProfile;
  }
  
  public void updateHistoryWord(String key, String value){
    if(value == null || value.trim().length()==0){
      return;
    }
    value = value.trim();
    String[] strArr = getStringArray(key);
    boolean matchFound = false;
    if(strArr != null){
      for(String temp:strArr){
        if(temp.equalsIgnoreCase(value)){
          matchFound = true;
          break;
        }
      }
    }
    if(!matchFound){
      if(strArr == null || strArr.length == 0){
        strArr = new String[]{value};
      }else{
        String[] tempArray = new String[strArr.length+1];
        System.arraycopy(strArr, 0, tempArray, 0, strArr.length);
        tempArray[strArr.length] = value;  
        strArr = tempArray;
      }
      updateStringArray(key,strArr);
    }
  }
  
  public String[] getHistoryWords(String key){
    String[] result = getStringArray(key);
    if(result == null){
      result = new String[]{};
    }
    return result;
  }
  
  
  private boolean getBoolean(String key, boolean defValue){
    defValue = mSharedPreferences.getBoolean(key, defValue);
    return defValue;
  }
    
  private void updateStringArray(String key, String[] value){
    if(value != null && value.length >0){
      StringBuffer sb = new StringBuffer("");
      for(String strVal : value){
        sb.append(strVal).append(DELIMETER);
      }
      sb.deleteCharAt(sb.length()-1);
      updateString(key,sb.toString());
    }  
  }
  
  private String[] getStringArray(String key){
    String[] result = null;
    String value = getString(key, null);
    if(value != null && value.length() >0){
      result = TextUtils.split(value, ""+DELIMETER);
    }  
    return result;
  }
  
  public String getCookies(){
    return getString(PREF_KEY_COOKIE_ID,"");    
  }
  public void setCookies(String cookieStr){
    updateString(PREF_KEY_COOKIE_ID,cookieStr);    
  }
  
  private void updateBoolean(String key, boolean value){
    //Editor editor = mSharedPreferences.edit();
    mPreferenceEditor.putBoolean(key, value);
    mPreferenceEditor.commit();
  }
  
  private int getInt(String key, int defValue){
    defValue = mSharedPreferences.getInt(key, defValue);
    return defValue;
  }
  
  private void updateInt(String key, int value){
    //Editor editor = mSharedPreferences.edit();
    mPreferenceEditor.putInt(key, value);
    mPreferenceEditor.commit();
  }
  
  private long getLong(String key, long defValue){
    defValue = mSharedPreferences.getLong(key, defValue);
    return defValue;
  }
  
  private void updateLong(String key, long value){
    //Editor editor = mSharedPreferences.edit();
    mPreferenceEditor.putLong(key, value);
    mPreferenceEditor.commit();
    
    
  }
  
  
  
  private String getString(String key, String defValue){
    defValue = mSharedPreferences.getString(key, defValue);
    return defValue;
  }
  
  private void updateString(String key, String value){
    //Editor editor = mSharedPreferences.edit();
    mPreferenceEditor.putString(key, value);
    mPreferenceEditor.commit();
  }
  
  

}




Java Source Code List

com.altinn.apps.fisher.AppContext.java
com.altinn.apps.fisher.CacheManager.java
com.altinn.apps.fisher.common.AppConstants.java
com.altinn.apps.fisher.common.IStatusMessage.java
com.altinn.apps.fisher.common.MenuItem.java
com.altinn.apps.fisher.common.StatusMessage.java
com.altinn.apps.fisher.db.DataBaseHelper.java
com.altinn.apps.fisher.db.FactoryDBHelper.java
com.altinn.apps.fisher.db.FishCategoryDBHelper.java
com.altinn.apps.fisher.db.FormDBHelper.java
com.altinn.apps.fisher.db.IDBHelper.java
com.altinn.apps.fisher.db.RegsDBHelper.java
com.altinn.apps.fisher.db.VesselDBHelper.java
com.altinn.apps.fisher.gps.CLocationProvider.java
com.altinn.apps.fisher.gps.ILocationUpdateListner.java
com.altinn.apps.fisher.models.CaughtInfoData.java
com.altinn.apps.fisher.models.InfoData.java
com.altinn.apps.fisher.models.ReportInfoData.java
com.altinn.apps.fisher.models.UserProfile.java
com.altinn.apps.fisher.net.AbstractWorkerTask.java
com.altinn.apps.fisher.net.CookieHelper.java
com.altinn.apps.fisher.net.IParser.java
com.altinn.apps.fisher.net.JSParser.java
com.altinn.apps.fisher.net.ParseManager.java
com.altinn.apps.fisher.net.TaskNotifier.java
com.altinn.apps.fisher.net.jsobj.AttachmentObj.java
com.altinn.apps.fisher.net.jsobj.FormObj.java
com.altinn.apps.fisher.net.jsobj.JSConstants.java
com.altinn.apps.fisher.net.jsobj.JsonObj.java
com.altinn.apps.fisher.net.jsobj.LinkItemObj.java
com.altinn.apps.fisher.net.jsobj.LinkObj.java
com.altinn.apps.fisher.net.jsobj.MessageObj.java
com.altinn.apps.fisher.net.jsobj.MessagesEmbedded.java
com.altinn.apps.fisher.net.jsobj.OrganisationObj.java
com.altinn.apps.fisher.net.tasks.LoginTask.java
com.altinn.apps.fisher.net.tasks.RefreshTokenTask.java
com.altinn.apps.fisher.net.tasks.SendReportTask.java
com.altinn.apps.fisher.net.tasks.UserProfileTask.java
com.altinn.apps.fisher.settings.FactoryDetails.java
com.altinn.apps.fisher.settings.FishDetails.java
com.altinn.apps.fisher.settings.SettingItem.java
com.altinn.apps.fisher.settings.VesselsDetails.java
com.altinn.apps.fisher.ui.component.DurationTimePickDialog.java
com.altinn.apps.fisher.ui.component.RAutoCompleteTextView.java
com.altinn.apps.fisher.ui.component.RButton.java
com.altinn.apps.fisher.ui.component.REditText.java
com.altinn.apps.fisher.ui.component.RTextView.java
com.altinn.apps.fisher.ui.screen.BaseActivity.java
com.altinn.apps.fisher.ui.screen.BrowserActivity.java
com.altinn.apps.fisher.ui.screen.FactoryDetailsActivity.java
com.altinn.apps.fisher.ui.screen.HomeActivity.java
com.altinn.apps.fisher.ui.screen.InformationActivity.java
com.altinn.apps.fisher.ui.screen.MenuNavigationActivity.java
com.altinn.apps.fisher.ui.screen.ReportActivity.java
com.altinn.apps.fisher.ui.screen.ReportReceivedFishActivity.java
com.altinn.apps.fisher.ui.screen.ReportSendDetailActivity.java
com.altinn.apps.fisher.ui.screen.SplashActivity.java
com.altinn.apps.fisher.ui.screen.UserProfileActivity.java
com.altinn.apps.fisher.utils.PreferenceUtils.java
com.altinn.apps.fisher.utils.Utils.java
net.simonvt.menudrawer.BuildLayerFrameLayout.java
net.simonvt.menudrawer.ColorDrawable.java
net.simonvt.menudrawer.DraggableDrawer.java
net.simonvt.menudrawer.FloatScroller.java
net.simonvt.menudrawer.MenuDrawer.java
net.simonvt.menudrawer.NoClickThroughFrameLayout.java
net.simonvt.menudrawer.OverlayDrawer.java
net.simonvt.menudrawer.PeekInterpolator.java
net.simonvt.menudrawer.Position.java
net.simonvt.menudrawer.Scroller.java
net.simonvt.menudrawer.SinusoidalInterpolator.java
net.simonvt.menudrawer.SlideDrawable.java
net.simonvt.menudrawer.SlidingDrawer.java
net.simonvt.menudrawer.SmoothInterpolator.java
net.simonvt.menudrawer.StaticDrawer.java
net.simonvt.menudrawer.ViewHelper.java
net.simonvt.menudrawer.compat.ActionBarHelperCompat.java
net.simonvt.menudrawer.compat.ActionBarHelperNative.java
net.simonvt.menudrawer.compat.ActionBarHelper.java