package de.thedevstack.conversationsplus; import android.app.Application; import android.content.Context; import android.content.pm.PackageManager; import android.preference.PreferenceManager; import java.io.File; import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.SerialSingleThreadExecutor; /** * This class is used to provide static access to the applicationcontext. */ public class ConversationsPlusApplication extends Application { /** * Application instance for static access */ private static ConversationsPlusApplication instance; private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor(); private final SerialSingleThreadExecutor mDatabaseExecutor = new SerialSingleThreadExecutor(); /** * Initializes the application and saves its instance. */ public void onCreate(){ super.onCreate(); ConversationsPlusApplication.instance = this; ConversationsPlusPreferences.init(PreferenceManager.getDefaultSharedPreferences(getAppContext())); ImageUtil.initBitmapCache(); FileBackend.init(); } /** * Returns the instance of the application * @return this application instance */ public static ConversationsPlusApplication getInstance() { return ConversationsPlusApplication.instance; } public static void executeFileAdding(Runnable r) { getInstance().mFileAddingExecutor.execute(r); } public static void executeDatabaseOperation(Runnable r) { getInstance().mDatabaseExecutor.execute(r); } /** * Returns the application's context. * @return Context the application's context */ public static Context getAppContext() { return ConversationsPlusApplication.instance.getApplicationContext(); } /** * Returns the application's private data directory. * @return File the application's private data dir */ public static File getPrivateFilesDir() { return ConversationsPlusApplication.instance.getFilesDir(); } /** * Returns the version of the application. * @see android.content.pm.PackageInfo#versionName * @return a string representation of the version stored in packageInfo */ public static String getVersion() { final String packageName = ConversationsPlusApplication.getAppContext().getPackageName(); if (packageName != null) { try { return ConversationsPlusApplication.getAppContext().getPackageManager().getPackageInfo(packageName, 0).versionName; } catch (final PackageManager.NameNotFoundException e) { return "unknown"; } } else { return "unknown"; } } /** * Returns the application's name. * @return the name as it is defined in R.string.app_name */ public static String getName() { return ConversationsPlusApplication.getAppContext().getString(R.string.app_name); } /** * Returns the name and the version of this application. * @see #getName() and #getVersion * @return a concatination of name and version with a whitespace in between */ public static String getNameAndVersion() { return getName() + " " + getVersion(); } }