de.incoherent.osaac.tasks.LoadHolidaysTask.java Source code

Java tutorial

Introduction

Here is the source code for de.incoherent.osaac.tasks.LoadHolidaysTask.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Matt Barringer <matt@incoherent.de>
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 ******************************************************************************/

package de.incoherent.osaac.tasks;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import de.incoherent.osaac.R;
import de.incoherent.osaac.activities.MainActivity;
import de.incoherent.osaac.database.Database;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;

public class LoadHolidaysTask extends AsyncTask<Void, Void, Boolean> {
    private Database mDb;
    private ProgressDialog mDialog = null;
    private WeakReference<MainActivity> mActivity = null;

    public LoadHolidaysTask(MainActivity activity) {
        mActivity = new WeakReference<MainActivity>(activity);
        this.mDb = new Database(mActivity.get());
        mDb.open();
        mDialog = ProgressDialog.show(mActivity.get(), "", "Importing holidays, please wait...", true);
    }

    protected void onPostExecute(Boolean result) {
        mDialog.dismiss();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        Boolean ret = false;
        if (mActivity.get() != null) {
            InputStream input = mActivity.get().getResources().openRawResource(R.raw.holidays);
            InputStreamReader inputReader = new InputStreamReader(input);
            BufferedReader reader = new BufferedReader(inputReader);
            StringBuilder builder = new StringBuilder();

            String inputStr;
            try {
                while ((inputStr = reader.readLine()) != null)
                    builder.append(inputStr);
                JSONObject top = new JSONObject(builder.toString());
                int version = top.getInt("version");
                JSONArray regions = top.getJSONArray("regions");
                int len = regions.length();
                for (int i = 0; i < len; i++) {
                    JSONObject region = regions.getJSONObject(i);
                    insertRegion(region);
                }
                mDb.close();
                SharedPreferences prefs = mActivity.get().getSharedPreferences("osaac", 0);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putInt("holidayVersion", version);
                editor.commit();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    private void insertRegion(JSONObject region) throws JSONException {
        String countryName = region.getString("name");
        String code = region.getString("code");
        JSONArray holidays = region.getJSONArray("holidays");
        int len = holidays.length();
        for (int i = 0; i < len; i++) {
            JSONObject holiday = holidays.getJSONObject(i);
            String holidayName = holiday.getString("name");
            String dates = holiday.getString("dates");
            String guid = code + "_" + holidayName;
            mDb.insertHoliday(guid, countryName, holidayName, dates);
        }
    }
}