Android Open Source - OpenQOTD Quote Provider






From Project

Back to project page OpenQOTD.

License

The source code is released under:

GNU General Public License

If you think the Android project OpenQOTD 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

/*
 *  Copyright 2011 Thibault Jouannic <thibault@jouannic.fr>. All Rights Reserved.
 *  This file is part of OpenQOTD.//from ww w  .j  av a2 s  .c o  m
 *
 *  OpenQOTD 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.
 *
 *  OpenQOTD 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 OpenQOTD. If not, see <http://www.gnu.org/licenses/>.
 */

package fr.miximum.qotd;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.preference.PreferenceManager;
import fr.miximum.qotd.R;

/**
 * Single entry point to get some quotes
 *
 * We don't extend ContentProvider because we don't need to provide quotes to other apps
 */
public class QuoteProvider {

    private Context mContext;
    private QuoteDatabase mQuoteDatabase;

    private static final int COLUMN_QUOTE_ID_INDEX = 0;
    private static final int COLUMN_QUOTE_INDEX = 1;

    public QuoteProvider(Context context) {
        mContext = context;
        mQuoteDatabase = new QuoteDatabase(mContext);
    }

    /**
     * Fetch a previously generated quote
     */
    public String getCurrentQuote() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        int currentQuoteId = sp.getInt("qotd.current_quote_id", -1);

        if (currentQuoteId < 0) {
            return resetQuote();
        }
        else
        {
            Cursor c = mQuoteDatabase.getQuote(currentQuoteId);
            if (c.moveToFirst()) {
              return getQuoteFromCursor(c);
            }
            else {
              return getQuoteNotFound();
            }
        }
    }

    /**
     * Fetch a randow quote, and save its id for later access
     */
    public String resetQuote() {

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        String lang = sp.getString("quote_lang", "en");

        Cursor c = mQuoteDatabase.getRandomQuote(lang);

        if (c.moveToFirst()) {
            int currentQuoteId = c.getInt(COLUMN_QUOTE_ID_INDEX);
            Editor editor = sp.edit();
            editor.putInt("qotd.current_quote_id", currentQuoteId);
            editor.commit();
            return getQuoteFromCursor(c);
        }
        else {
            return getQuoteNotFound();
        }
    }

    /**
     * Get a quote not found error message
     */
    private String getQuoteNotFound() {
        return mContext.getString(R.string.no_quote_found);
    }


    /**
     * Take a cursor, and return a formatted quote
     */
    private String getQuoteFromCursor(Cursor c) {
        String res = "";
        if (c.moveToFirst()) {
            res = c.getString(COLUMN_QUOTE_INDEX);
        }
        c.close();
        return res;
    }
}




Java Source Code List

fr.miximum.qotd.QOTDPreferences.java
fr.miximum.qotd.QOTDWidget.java
fr.miximum.qotd.QOTD.java
fr.miximum.qotd.QuoteDatabase.java
fr.miximum.qotd.QuoteProvider.java
fr.miximum.qotd.Quote.java