package monakhv.android.samlib.service; import android.app.IntentService; import android.content.Context; import com.j256.ormlite.android.apptools.OpenHelperManager; import monakhv.android.samlib.SamlibApplication; import monakhv.android.samlib.data.DataExportImport; import monakhv.android.samlib.data.SettingsHelper; import monakhv.android.samlib.sql.DatabaseHelper; import monakhv.samlib.db.AuthorController; /* * Copyright 2015 Dmitry Monakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * 02.07.15. */ public abstract class MyServiceIntent extends IntentService { private volatile DatabaseHelper helper; private volatile boolean created = false; private volatile boolean destroyed = false; private SamlibApplication mSamlibApplication; /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public MyServiceIntent(String name) { super(name); } 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; } } @Override public void onCreate() { if (helper == null) { helper = getHelperInternal(this); created = true; } super.onCreate(); mSamlibApplication= (SamlibApplication) getApplication(); } public SettingsHelper getSettingsHelper(){ return mSamlibApplication.getSettingsHelper(); } public DataExportImport getDataExportImport(){ return mSamlibApplication.getDataExportImport(); } public AuthorController getAuthorController(){ return mSamlibApplication.getDatabaseComponent(getHelper()).getAuthorController(); } @Override public void onDestroy() { super.onDestroy(); releaseHelper(helper); destroyed = true; mSamlibApplication.releaseDatabaseComponent(); mSamlibApplication.releaseServiceComponent(); } /** * 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> I */ protected DatabaseHelper getHelperInternal(Context context) { @SuppressWarnings({ "unchecked", "deprecation" }) 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(); this.helper = null; } }