de.uni_koblenz_landau.apow.tasks.SyncTask.java Source code

Java tutorial

Introduction

Here is the source code for de.uni_koblenz_landau.apow.tasks.SyncTask.java

Source

/**
 * Apow - a mobile EHR Management System for low-resource environments
 * in developing countries, exemplified by rural Ghana
 * Copyright (C) 2014 Martin Landua
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package de.uni_koblenz_landau.apow.tasks;

import java.io.IOException;
import java.util.Date;

import org.json.JSONException;

import de.uni_koblenz_landau.apow.LoginActivity;
import de.uni_koblenz_landau.apow.PatientListFragment;
import de.uni_koblenz_landau.apow.helper.Constants;
import de.uni_koblenz_landau.apow.helper.Helper;
import de.uni_koblenz_landau.apow.helper.SyncHelper;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

/**
 * Task for synchronization.
 * @author Martin Landua
 *
 */
public class SyncTask extends AsyncTask<String, Integer, Integer> implements TaskInterface {

    public static final String LASTUPDATE = "lastupdate";

    public static final String TASK_ID = "synctask";

    private SyncInterface mParent;

    @Override
    protected Integer doInBackground(String... params) {
        if (mParent != null && mParent.getActivity() != null) {
            try {
                publishProgress(PatientListFragment.STATUS_SYNC_FETCH_FACTS);
                // Get last update date.
                String lastupdate = mParent.getActivity()
                        .getSharedPreferences(Constants.PREFERENCE, Context.MODE_PRIVATE).getString(LASTUPDATE, "");

                // Fetch fact tables.
                SyncHelper.fetchFactTables(lastupdate, mParent.getActivity());

                // Fetch user tables.
                publishProgress(PatientListFragment.STATUS_SYNC_FETCH_USERDATA);
                SyncHelper.fetchUserTables(mParent.getActivity());

                // Upload user tables.
                publishProgress(PatientListFragment.STATUS_SYNC_UPLOAD_USERDATA);
                SyncHelper.postUserTables(mParent.getActivity());

                // Save new date for last update.
                mParent.getActivity().getSharedPreferences(Constants.PREFERENCE, Context.MODE_PRIVATE).edit()
                        .putString(LASTUPDATE, Helper.dateToDateTimeString(new Date())).commit();

            } catch (IllegalStateException | IOException | JSONException e) {
                return PatientListFragment.STATUS_CONNECTION_ERROR;
            } catch (Exception e) {
                return PatientListFragment.STATUS_ERROR;
            }
        }
        return LoginActivity.STATUS_OK;
    }

    protected void onProgressUpdate(Integer... progress) {
        if (mParent != null) {
            int status = progress[0];
            mParent.updateStatus(status);
        }
    }

    @Override
    protected void onPostExecute(Integer status) {
        super.onPostExecute(status);

        if (mParent != null) {
            // reference UI updates using activity
            mParent.onSyncFinished(status);
            // remove the reference
            TaskActivityReference.getInstance().removeTask(TASK_ID);
        }
    }

    /**
     * Attaches an activity to the task
     * 
     * @param activity
     *            The activity to attach
     */
    public void attach(TaskActivity activity) {
        try {
            this.mParent = (SyncInterface) activity;
        } catch (Exception e) {
            Log.e("Tag", "Error on casting " + TASK_ID, e);
        }
    }

    /**
     * Removes the activity from the task
     */
    public void detach() {
        this.mParent = null;
    }
}