Android Open Source - SurveySDK_android Serialization Helper






From Project

Back to project page SurveySDK_android.

License

The source code is released under:

Apache License

If you think the Android project SurveySDK_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 com.survey.android.db;
/*from  w w  w.  j a  v  a 2 s . c o  m*/
import com.survey.android.util.Log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;

public class SerializationHelper {
  public static final String DB_NAME = "paused_surveys.db";
  public static final int DB_VERSION = 43;
  // ********************* names of used tables *******************************************
  public static final String TABLE_SURVEYS = "surveys";
  public static final String C_ID = BaseColumns._ID;  
  public static final String C_ARRAY = "array"; 
  
  private Context context;
  private SQLiteDatabase db;

  public SQLiteDatabase getSQLiteDatabase() {
    return db;
  }

  // insert statement
  @SuppressWarnings("unused")
  private SQLiteStatement insertStmtSurveys;
  
  
  // sql string for insert into appropriate tables
  private static final String INSERT_SURVEYS = "INSERT INTO "
      + TABLE_SURVEYS
      + "(array) VALUES (?);";

  public SerializationHelper(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
    this.insertStmtSurveys = this.db.compileStatement(INSERT_SURVEYS);
  }

  public boolean pauseSurvey(byte[] array) {

    boolean result = true;
    this.db.beginTransaction();
    try {
      this.db.delete(TABLE_SURVEYS, null, null);
      ContentValues dataToInsert = new ContentValues();                          
      dataToInsert.put(C_ARRAY,array);
      db.insert(TABLE_SURVEYS, null, dataToInsert);
//      this.insertStmtSurveys.bindBlob(0, array);
      db.setTransactionSuccessful();
    } catch (Exception e) {
      e.printStackTrace();
      Log.e("DB", e.getMessage());
      result = false;
    } finally {
      db.endTransaction();
      if(db.isOpen()){
        db.close();
      }
    }
    return result;
  }

  /**
   * Delete all records from tables
   */
  public void deleteAll() {
    this.db.delete(TABLE_SURVEYS, null, null);
  }
  
  /**
   * 
   * @return
   */
  public byte[] reloadPauseSurvey(){
    byte[] result=null;
    try{
      Cursor cursor = this.db.query(TABLE_SURVEYS, new String[] {
          "array"}, null, null, null, null, null);
      if (cursor.moveToFirst()) {
        // do {
        result = cursor.getBlob(0);        
      }
      if (cursor != null && !cursor.isClosed()) {
        cursor.close();
      }

      if (cursor != null && !cursor.isClosed()) {
        cursor.close();
      }
      
    }
    catch(Exception e){
      e.printStackTrace();
      result=null;
    }
    return result;
  }


  /**
   * Checks if there is paused survey 
   * @return true - there is paused survey false - there is no paused survey
   */
  public boolean isPaused(){
    try{
        String count = "SELECT count(*) FROM "+TABLE_SURVEYS;
        Cursor mcursor = db.rawQuery(count, null);
        mcursor.moveToFirst();
        int icount = mcursor.getInt(0);
        
        if (mcursor != null && !mcursor.isClosed()) {
          mcursor.close();
      }
        return icount==1;
    }
    catch(Exception e){
      e.printStackTrace();
    }
    return false;
  }
  
  
  // public List<String> selectAll() {
  // List<String> list = new ArrayList<String>();
  // Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
  // null, null, null, null, "name desc");
  // if (cursor.moveToFirst()) {
  // do {
  // list.add(cursor.getString(0));
  // } while (cursor.moveToNext());
  // }
  // if (cursor != null && !cursor.isClosed()) {
  // cursor.close();
  // }
  // return list;
  // }

  private static class OpenHelper extends SQLiteOpenHelper {
    OpenHelper(Context context) {
      super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      String sqlSurvey = String.format("CREATE TABLE %s ( %s long,%s BLOB);",TABLE_SURVEYS, C_ID, C_ARRAY);
      db.execSQL(sqlSurvey);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.d("Example",
          "Upgrading database, this will drop tables and recreate.");
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_SURVEYS);
      onCreate(db);
    }
  }
}




Java Source Code List

android.UnusedStub.java
com.google.android.gms.BuildConfig.java
com.google.android.gms.BuildConfig.java
com.survey.android.UnusedStub.java
com.survey.android.c2dm.C2DMRegistrationReceiver.java
com.survey.android.c2dm.C2DMTokenRefresher.java
com.survey.android.c2dm.GcmBroadcastReceiver.java
com.survey.android.c2dm.GcmNotificationReceiver.java
com.survey.android.c2dm.GcmRegistrationService.java
com.survey.android.c2dm.RegService.java
com.survey.android.common.PlacesAutoCompleteAdapter.java
com.survey.android.common.Themes.java
com.survey.android.containers.AppContainer.java
com.survey.android.containers.PollContainer.java
com.survey.android.custom_widgets.FontTextView.java
com.survey.android.custom_widgets.PollRatingsScaleLabeled.java
com.survey.android.custom_widgets.PollRatings.java
com.survey.android.custom_widgets.PollSelectionTable.java
com.survey.android.db.SerializationHelper.java
com.survey.android.db.SerializationManager.java
com.survey.android.fragment.AudioRecorderFragment.java
com.survey.android.geofence.GeofenceRemover.java
com.survey.android.geofence.GeofenceRequester.java
com.survey.android.geofence.GeofenceUtils.java
com.survey.android.geofence.LocationServiceErrorMessages.java
com.survey.android.geofence.ReceiveTransitionsIntentService.java
com.survey.android.geofence.SimpleGeofenceStore.java
com.survey.android.geofence.SimpleGeofence.java
com.survey.android.model.AnswerModel.java
com.survey.android.model.CategoryModel.java
com.survey.android.model.CurrentSectionModel.java
com.survey.android.model.Prefs.java
com.survey.android.model.QuestionModel.java
com.survey.android.model.ResponseModel.java
com.survey.android.model.SurveyModel.java
com.survey.android.model.UserModel.java
com.survey.android.services.BackgroundUploader.java
com.survey.android.services.DataBroadcastReceiver.java
com.survey.android.services.DeviceStartUpReceiver.java
com.survey.android.services.GeoSurveyPollService.java
com.survey.android.services.LocationTesterService.java
com.survey.android.services.ReferrerCatcher.java
com.survey.android.session.Configuration.java
com.survey.android.session.Session.java
com.survey.android.util.Base64.java
com.survey.android.util.ConstantData.java
com.survey.android.util.GeoPush.java
com.survey.android.util.GeoTriggerBroadcastReceiver.java
com.survey.android.util.GeoTrigger.java
com.survey.android.util.LocationLog.java
com.survey.android.util.Log.java
com.survey.android.util.StrToIntMap.java
com.survey.android.util.StrToStrMap.java
com.survey.android.util.Toiler.java
com.survey.android.util.WhiteLabel.java
com.survey.android.view.Dashboard.java
com.survey.android.view.Gallery.java
com.survey.android.view.LocalizedFragmentActivity.java
com.survey.android.view.Main.java
com.survey.android.view.Notification.java
com.survey.android.view.Question.java
com.survey.android.view.Survey.java
com.survey.android.view.ThemeCustomizer.java
com.survey.android.view.themed.DashboardThemed.java
com.survey.android.view.themed.MainThemed.java
com.survey.android.view.themed.NotificationThemed.java
com.survey.android.view.themed.QuestionThemed.java
com.survey.android.view.themed.SurveyThemed.java
com.survey.android.webclient.HttpRequest.java
com.survey.android.webclient.HttpsClient.java
com.survey.android.webclient.RestClient.java
com.survey.android.webclient.SurveyHttpClient.java
com.survey.android.webclient.SurveyRequest.java
com.survey.android.webclient.SurveySSLSocketFactory.java
com.survey.android.webclient.SurveyX509TrustManager.java
com.survey.android.widget.Widget.java
com.survey.androiddemo.AppContainer.java
com.survey.androiddemo.LoginActivity.java
com.survey.androiddemo.MainActivity.java
com.survey.androiddemo.SDKConfigSettings.java