Android Open Source - BluetoothSppPro C Shared Preferences






From Project

Back to project page BluetoothSppPro.

License

The source code is released under:

Apache License

If you think the Android project BluetoothSppPro 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 mobi.dzs.android.storage;
//  w ww. j  a  va  2s .com
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

final public class CSharedPreferences extends CKVStorage{
  private static final String msDELIMITER = "|_|"; 
  /** Android Activity???? */
  private Context mc = null;
  /** ???? */
  private String msPkgName;
  /** SharedPreferences?Editor?????? */
  private Editor meSaveData = null;
  /** android????????????:/data/data/<package name>/shared_prefs */
  private SharedPreferences mSP = null;
  /**
   * ????
   * @param C Context Activity?????
   */
  public CSharedPreferences(Context C){
    this.mc = C;
    
    PackageManager manager = mc.getPackageManager();
    PackageInfo info;
    try{
      info = manager.getPackageInfo(mc.getPackageName(), 0);
      this.msPkgName = info.packageName;
      this.mSP = mc.getSharedPreferences(this.msPkgName, Context.MODE_PRIVATE);
      this._bSrorageIsReady = true; //????????
    }catch (NameNotFoundException e){
      e.printStackTrace();
      this.msPkgName = "";
      this._bSrorageIsReady = false;
    }
  }
  
  /**
   * ???????????<br/>
   * (??????????????)
   * */
  private void newStorage(){
    if (null == this.meSaveData)
      this.meSaveData = 
        mc.getSharedPreferences(this.msPkgName, Context.MODE_PRIVATE).edit(); //?????
  }
  
  /**
   * ???????????
   * @param sKey String ????
   * @param sSubKey String ?????
   * @return String ????????????????
   * */
  private String getIdxKey(String sKey, String sSubKey){
    return sKey + msDELIMITER + sSubKey;
  }
  
  @Override
  public boolean saveStorage() {
    if (null != this.meSaveData){
      this.meSaveData.commit();//???????????
      this.meSaveData = null; //???????????
      return true;
    }
    else
      return false;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, String sVal) {
    this.newStorage();
    this.meSaveData.putString(this.getIdxKey(sKey, sSubKey), sVal);
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, int iVal) {
    this.newStorage();
    this.meSaveData.putInt(this.getIdxKey(sKey, sSubKey), iVal);
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, double dbVal) {
    this.newStorage();
    this.meSaveData.putFloat(this.getIdxKey(sKey, sSubKey), (float)dbVal);
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, long lVal) {
    this.newStorage();
    this.meSaveData.putLong(this.getIdxKey(sKey, sSubKey), lVal);
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, boolean bVal) {
    this.newStorage();
    this.meSaveData.putBoolean(this.getIdxKey(sKey, sSubKey), bVal);
    return this;
  }

  @Override
  public String getStringVal(String sKey, String sSubKey) {
    return this.mSP.getString(this.getIdxKey(sKey, sSubKey), "");
  }

  @Override
  public double getDoubleVal(String sKey, String sSubKey) {
    return (double)this.mSP.getFloat(this.getIdxKey(sKey, sSubKey), 0.0f);
  }

  @Override
  public int getIntVal(String sKey, String sSubKey) {
    return this.mSP.getInt(this.getIdxKey(sKey, sSubKey), 0);
  }

  @Override
  public long getLongVal(String sKey, String sSubKey) {
    return this.mSP.getLong(this.getIdxKey(sKey, sSubKey), 0);
  }

  @Override
  public boolean getBooleanVal(String sKey, String sSubKey) {
    return this.mSP.getBoolean(this.getIdxKey(sKey, sSubKey), false);
  }

  @Override
  public CKVStorage removeVal(String sKey, String sSubKey) {
    this.newStorage();
    this.meSaveData.remove(this.getIdxKey(sKey, sSubKey));
    return this;
  }

}




Java Source Code List

mobi.dzs.android.BLE_SPP_PRO.BaseActivity.java
mobi.dzs.android.BLE_SPP_PRO.BaseCommActivity.java
mobi.dzs.android.BLE_SPP_PRO.actAbout.java
mobi.dzs.android.BLE_SPP_PRO.actByteStream.java
mobi.dzs.android.BLE_SPP_PRO.actCmdLine.java
mobi.dzs.android.BLE_SPP_PRO.actDiscovery.java
mobi.dzs.android.BLE_SPP_PRO.actKeyBoard.java
mobi.dzs.android.BLE_SPP_PRO.actMain.java
mobi.dzs.android.BLE_SPP_PRO.globalPool.java
mobi.dzs.android.bluetooth.BTSerialComm.java
mobi.dzs.android.bluetooth.BluetoothCtrl.java
mobi.dzs.android.bluetooth.BluetoothSppClient.java
mobi.dzs.android.bluetooth.CResourcePV.java
mobi.dzs.android.control.button.ButtonPassListener.java
mobi.dzs.android.control.button.RepeatingButton.java
mobi.dzs.android.storage.CJsonStorage.java
mobi.dzs.android.storage.CKVStorage.java
mobi.dzs.android.storage.CSharedPreferences.java
mobi.dzs.android.util.CHexConver.java
mobi.dzs.android.util.LocalIOTools.java