Android Open Source - BluetoothSppPro C Json Storage






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;
/*  ww w .ja  v  a 2s  . com*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.util.Log;
/**
 * ????????<br/>
 * ??????????????SD????package_name/???
 * @author Jerry.Li(hzjerry@gmail.com)
 * @version 0.201404026
 * @see ????????<br/>
 * &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
 * &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
 */
final public class CJsonStorage extends CKVStorage{
  /** Android Activity???? */
  private Context _c = null;
  /** ???? */
  private String _sPkgName;
  /** ????????????? */
  private String _sPROFILES_NAME = "profiles.json";
  /** json ??? */
  private JSONObject _json = null;
  /**
   * ????
   * @param C Context Activity?????
   */
  public CJsonStorage(Context C){
    this._c = C;
    try{
      this._sPkgName =  (_c.getPackageManager().getPackageInfo(_c.getPackageName(), 0)).packageName;
      this._bSrorageIsReady = this.readStorage(); //?????????
    }catch (NameNotFoundException e){
      e.printStackTrace();
    }
  }
  /**
   * ????(??????????)
   * @param C Context Activity?????
   * @param sExtRootPath String ????????
   */
  public CJsonStorage(Context C, String sExtRootPath){
    this._c = C;
    this._sPkgName =  sExtRootPath;
    this._bSrorageIsReady = this.readStorage(); //?????????
  }
  /**
   * ????(??????????)
   * @param C Context Activity?????
   * @param sExtRootPath String ????????
   * @param sConfigFile String ??????????(??????????????????????)
   */
  public CJsonStorage(Context C, String sExtRootPath, String sConfigFile){
    this._c = C;
    this._sPkgName =  sExtRootPath;
    this._sPROFILES_NAME = sConfigFile.concat(".json");
    this._bSrorageIsReady = this.readStorage(); //?????????
  }
  /**
   * ?????????????
   * @return
   */
  private File getFilehd() {
    File f = null;
    String sRoot = null;
    //??SD?????????
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
      sRoot = Environment.getExternalStorageDirectory().getAbsolutePath();//???????
      f = new File(sRoot.concat("/").concat(_sPkgName).concat("/")); // ??????????
      if (!f.exists())//????????????????????
        f.mkdirs();
      //????????
      f = new File(sRoot.concat("/").concat(_sPkgName).concat("/"), _sPROFILES_NAME);
      Log.v(_sPkgName, sRoot.concat("/").concat(_sPkgName).concat("/")+ _sPROFILES_NAME);
    }
    else{//????SD????????????
      f = new File(_c.getFilesDir(), _sPROFILES_NAME);// ??????????
    }
    return f;
  }
  /**
   * ????????
   * @return boolean false:???????? / true:?????
   */
  private boolean readStorage(){
    char[] cBuf = new char[512];
    StringBuilder sb = new StringBuilder();
    int iRet = 0;
    try {
      FileInputStream fis = new FileInputStream(this.getFilehd());
      InputStreamReader reader = new InputStreamReader(fis);
      while((iRet = reader.read(cBuf)) > 0)
        sb.append(cBuf, 0, iRet);
      reader.close();
      fis.close();
      //??json????
      String sTmp = sb.toString();
      if (sTmp.length() > 0)
        _json = new JSONObject(sTmp);
      else
        _json = new JSONObject();
      return true;
    } catch (FileNotFoundException e) { //????????????
      _json = new JSONObject();
      return true;
    } catch (IOException e) { //IO????????????????
      _json = new JSONObject();
      return true;
    } catch (JSONException e) { //json????
      _json = new JSONObject(); //?????????????
      return true;
    }
  }
  
  @Override
  public boolean saveStorage() {
    File f = getFilehd();
    if (f.exists())
      f.delete(); //???????
    try {
//      f.createNewFile(); //?????
      FileOutputStream fso = new FileOutputStream(f);
      fso.write(this._json.toString().getBytes());
      fso.close();
      fso = null;
      return true;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return false;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }

  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, String sVal) {
    if (this.isReady()){
      try {
        JSONObject jTmp = this._json.optJSONObject(sKey);
        if (null == jTmp){
          if (null == sVal)
            sVal = "";
          this._json.put(sKey, new JSONObject().put(sSubKey, sVal));
        }
        else
          jTmp.put(sSubKey, sVal);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, int iVal) {
    if (this.isReady()){
      try {
        JSONObject jTmp = this._json.optJSONObject(sKey);
        if (null == jTmp)
          this._json.put(sKey, new JSONObject().put(sSubKey, iVal));
        else
          jTmp.put(sSubKey, iVal);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, double dbVal) {
    if (this.isReady()){
      try {
        JSONObject jTmp = this._json.optJSONObject(sKey);
        if (null == jTmp)
          this._json.put(sKey, new JSONObject().put(sSubKey, dbVal));
        else
          jTmp.put(sSubKey, dbVal);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, long lVal) {
    if (this.isReady()){
      try {
        JSONObject jTmp = this._json.optJSONObject(sKey);
        if (null == jTmp)
          this._json.put(sKey, new JSONObject().put(sSubKey, lVal));
        else
          jTmp.put(sSubKey, lVal);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return this;
  }

  @Override
  public CKVStorage setVal(String sKey, String sSubKey, boolean bVal) {
    if (this.isReady()){
      try {
        JSONObject jTmp = this._json.optJSONObject(sKey);
        if (null == jTmp)
          this._json.put(sKey, new JSONObject().put(sSubKey, bVal));
        else
          jTmp.put(sSubKey, bVal);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return this;
  }

  @Override
  public String getStringVal(String sKey, String sSubKey) {
    JSONObject jsObj = null;
    if (this.isReady()){
      if (null != (jsObj = this._json.optJSONObject(sKey))){
        try {
          return jsObj.getString(sSubKey);
        } catch (JSONException e) {
          return "";
        }
      }
    }
    return "";
  }

  @Override
  public double getDoubleVal(String sKey, String sSubKey) {
    JSONObject jsObj = null;
    if (this.isReady()){
      if (null != (jsObj = this._json.optJSONObject(sKey))){
        try {
          return jsObj.getDouble(sSubKey);
        } catch (JSONException e) {
          return 0.0d;
        }
      }
    }
    return 0.0d;
  }

  @Override
  public int getIntVal(String sKey, String sSubKey) {
    JSONObject jsObj = null;
    if (this.isReady()){
      if (null != (jsObj = this._json.optJSONObject(sKey))){
        try {
          return jsObj.getInt(sSubKey);
        } catch (JSONException e) {
          return 0;
        }
      }
    }
    return 0;
  }

  @Override
  public long getLongVal(String sKey, String sSubKey) {
    JSONObject jsObj = null;
    if (this.isReady()){
      if (null != (jsObj = this._json.optJSONObject(sKey))){
        try {
          return jsObj.getLong(sSubKey);
        } catch (JSONException e) {
          return 0l;
        }
      }
    }
    return 0l;
  }

  @Override
  public boolean getBooleanVal(String sKey, String sSubKey) {
    JSONObject jsObj = null;
    if (this.isReady()){
      if (null != (jsObj = this._json.optJSONObject(sKey))){
        try {
          return jsObj.getBoolean(sSubKey);
        } catch (JSONException e) {
          return false;
        }
      }
    }
    return false;
  }

  @Override
  public CKVStorage removeVal(String sKey, String sSubKey) {
    JSONObject jsObj = null;
    if (this.isReady()){
      if (null != (jsObj = this._json.optJSONObject(sKey))){
        jsObj.remove(sSubKey);
        if (jsObj.length() == 0) //?????Key??Key
          jsObj.remove(sKey);
      }
    }
    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