Java tutorial
/** * 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.File; 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.db.DBHelper; import de.uni_koblenz_landau.apow.db.UserDAO; import de.uni_koblenz_landau.apow.helper.Constants; import de.uni_koblenz_landau.apow.helper.CustomApplication; 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 setup. * @author Martin Landua * */ public class SetupTask extends AsyncTask<Void, Integer, Integer> implements TaskInterface { public static final String TASK_ID = "setuptask"; private SetupInterface mParent; private final String mDatabasePassword; private final String mServerUsername; private final String mServerPassword; private final int mLocationID; public SetupTask(String databasePassword, String serverUsername, String serverPassword, int locationID) { super(); mDatabasePassword = databasePassword; mServerUsername = serverUsername; mServerPassword = serverPassword; mLocationID = locationID; } @Override protected Integer doInBackground(Void... params) { if (mParent != null && mParent.getActivity() != null) { try { // Check credentials. publishProgress(LoginActivity.STATUS_CHECK_LOGIN); int userID = SyncHelper.fetchUser(mServerUsername, mServerPassword); if (userID == -1) { return LoginActivity.STATUS_LOGIN_WRONG; } // Delete old database file. publishProgress(LoginActivity.STATUS_DATABASE); File databaseFile = mParent.getActivity().getDatabasePath(DBHelper.DATABASE_NAME); if (!databaseFile.mkdirs() || !databaseFile.delete()) { throw new Exception(); } // Setup new encrypted database with password. DBHelper.initializeInstance(mParent.getActivity()); DBHelper.getInstance().openDatabase(mDatabasePassword); // Cache password as global variable. ((CustomApplication) mParent.getActivity().getApplication()).setPassword(mDatabasePassword); // Save settings. UserDAO dao = new UserDAO(mParent.getActivity()); dao.open(mParent.getActivity()); dao.createSettings(userID, mServerUsername, mServerPassword, mLocationID); dao.close(); DBHelper.getInstance().closeDatabase(); // Fetch fact tables. publishProgress(LoginActivity.STATUS_SYNC_FETCH_FACTS); SyncHelper.fetchFactTables("", mParent.getActivity()); // Fetch user tables. publishProgress(LoginActivity.STATUS_SYNC_FETCH_USERDATA); SyncHelper.fetchUserTables(mParent.getActivity()); // Save new date for last update. mParent.getActivity().getSharedPreferences(Constants.PREFERENCE, Context.MODE_PRIVATE).edit() .putString(SyncTask.LASTUPDATE, Helper.dateToDateTimeString(new Date())).commit(); } catch (IllegalStateException | IOException | JSONException e) { return LoginActivity.STATUS_CONNECTION_ERROR; } catch (Exception e) { return LoginActivity.STATUS_DATABASE_ERROR; } finally { DBHelper.initializeInstance(mParent.getActivity()); DBHelper.getInstance().clear(); } } 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.onSetupFinished(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 = (SetupInterface) 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; } }