Android Open Source - piwik_android_sdk Store Data Task






From Project

Back to project page piwik_android_sdk.

License

The source code is released under:

MIT License

If you think the Android project piwik_android_sdk 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.anupcowkur.piwiksdk;
// w ww. jav  a  2 s .c o m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.Calendar;

public class StoreDataTask extends AsyncTask<Void, Void, Void> {

    private Context context;
    private String type, userId;

    public StoreDataTask(Context context, String type) {
        this.context = context;
        this.type = type;
        this.userId = getUserIdFromPreferences();
    }

    private String getUserIdFromPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(SharedPreferenceKeys.PREF_USER_ID, null);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        PiwikDatabaseHelper piwikDatabaseHelper = PiwikDatabaseHelper.getInstance(context);
        SQLiteDatabase sqLiteDatabase = piwikDatabaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(PiwikDatabaseHelper.EVT_TABLE_COL_USER_ID, userId);
        contentValues.put(PiwikDatabaseHelper.EVT_TABLE_COL_INFO, type);
        contentValues.put(PiwikDatabaseHelper.EVT_TABLE_COL_TIMESTAMP, getCurrentTimestamp());
        sqLiteDatabase.insert(PiwikDatabaseHelper.EVT_TABLE, null, contentValues);
        return null;
    }

    private String getCurrentTimestamp() {
        Calendar calendar = Calendar.getInstance();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("h=");
        stringBuilder.append(calendar.get(Calendar.HOUR_OF_DAY));
        stringBuilder.append("&m=");
        stringBuilder.append(calendar.get(Calendar.MINUTE));
        stringBuilder.append("&s=");
        stringBuilder.append(calendar.get(Calendar.SECOND));
        return stringBuilder.toString();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        PiwikDatabaseHelper piwikDatabaseHelper = PiwikDatabaseHelper.getInstance(context);
        SQLiteDatabase sqLiteDatabase = piwikDatabaseHelper.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + PiwikDatabaseHelper.EVT_TABLE, null);
        if (cursor.moveToFirst()) {
            while (cursor.isAfterLast() == false) {
                Log.d(StoreDataTask.class.getName(), " rec: " + cursor.getString(cursor.getColumnIndex(PiwikDatabaseHelper.EVT_TABLE_COL_INFO)));
                cursor.moveToNext();
            }
        }
        super.onPostExecute(aVoid);
    }
}




Java Source Code List

com.anupcowkur.piwiksample.CardFlipActivity.java
com.anupcowkur.piwiksample.LayoutChangesActivity.java
com.anupcowkur.piwiksample.MainActivity.java
com.anupcowkur.piwiksample.ScreenSlideActivity.java
com.anupcowkur.piwiksample.ScreenSlidePageFragment.java
com.anupcowkur.piwiksample.TouchHighlightImageButton.java
com.anupcowkur.piwiksample.ZoomActivity.java
com.anupcowkur.piwiksdk.AuthenticatorService.java
com.anupcowkur.piwiksdk.Authenticator.java
com.anupcowkur.piwiksdk.PiwikCallback.java
com.anupcowkur.piwiksdk.PiwikClient.java
com.anupcowkur.piwiksdk.PiwikContentProvider.java
com.anupcowkur.piwiksdk.PiwikDBItems.java
com.anupcowkur.piwiksdk.PiwikDatabaseHelper.java
com.anupcowkur.piwiksdk.PiwikSyncAdapter.java
com.anupcowkur.piwiksdk.PiwikSyncService.java
com.anupcowkur.piwiksdk.SharedPreferenceKeys.java
com.anupcowkur.piwiksdk.StoreDataTask.java