/* This file is part of RateBeer For Android. RateBeer for Android 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. RateBeer for Android 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 RateBeer for Android. If not, see <http://www.gnu.org/licenses/>. */ package com.ratebeer.android.gui.components.helpers; import android.app.IntentService; import android.content.Context; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.support.ConnectionSource; import com.ratebeer.android.app.persistance.DatabaseHelper; public abstract class DatabaseConsumerService extends IntentService { public DatabaseConsumerService(String name) { super(name); } private volatile DatabaseHelper helper; private volatile boolean created = false; private volatile boolean destroyed = false; /** * Get a helper for this action. */ public DatabaseHelper getHelper() { if (helper == null) { if (!created) { throw new IllegalStateException("A call has not been made to onCreate() yet so the helper is null"); } else if (destroyed) { throw new IllegalStateException( "A call to onDestroy has already been made and the helper cannot be used after that point"); } else { throw new IllegalStateException("Helper is null for some unknown reason"); } } else { return helper; } } /** * Get a connection source for this action. */ public ConnectionSource getConnectionSource() { return getHelper().getConnectionSource(); } @Override public void onCreate() { if (helper == null) { helper = getHelperInternal(this); created = true; } super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); releaseHelper(helper); destroyed = true; } /** * This is called internally by the class to populate the helper object instance. This should not be called directly * by client code unless you know what you are doing. Use {@link #getHelper()} to get a helper instance. If you are * managing your own helper creation, override this method to supply this activity with a helper instance. * * <p> * <b> NOTE: </b> If you override this method, you most likely will need to override the * {@link #releaseHelper(OrmLiteSqliteOpenHelper)} method as well. * </p> */ protected DatabaseHelper getHelperInternal(Context context) { DatabaseHelper newHelper = (DatabaseHelper) OpenHelperManager.getHelper(context, DatabaseHelper.class); return newHelper; } /** * Release the helper instance created in {@link #getHelperInternal(Context)}. You most likely will not need to call * this directly since {@link #onDestroy()} does it for you. * * <p> * <b> NOTE: </b> If you override this method, you most likely will need to override the * {@link #getHelperInternal(Context)} method as well. * </p> */ protected void releaseHelper(DatabaseHelper helper) { OpenHelperManager.releaseHelper(); helper = null; } }