]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
Avoid inheriting from Application.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Tue, 27 Jun 2017 20:25:17 +0000 (22:25 +0200)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Tue, 27 Jun 2017 20:26:36 +0000 (22:26 +0200)
Supposedly this is best practice (a bit doubtful about
that) but it is a brute-force approach to try fixing
the ClassCastException issues.

AndroidManifest.xml
src/com/hughes/android/dictionary/AboutActivity.java
src/com/hughes/android/dictionary/DictionaryActivity.java
src/com/hughes/android/dictionary/DictionaryApplication.java
src/com/hughes/android/dictionary/DictionaryManagerActivity.java
src/com/hughes/android/dictionary/HtmlDisplayActivity.java
src/com/hughes/android/dictionary/PreferenceActivity.java

index e901fb607c7d4b306445eaddb04e382ecebe8661..c3d8d9f034998e72f4dbd1c84b6a8b8aa2e61f18 100644 (file)
@@ -2,8 +2,8 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.hughes.android.dictionary"
     android:installLocation="auto"
-    android:versionCode="89"
-    android:versionName="5.3.6" >
+    android:versionCode="90"
+    android:versionName="5.3.7" >
 
     <uses-sdk
         android:minSdkVersion="10"
@@ -21,7 +21,6 @@
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
     <application
-        android:name=".DictionaryApplication"
         android:allowBackup="true"
         android:resizeableActivity="true"
         android:icon="@drawable/icon"
index 7afcc0cf6bf67e66a9261c33e6f1e9e813815eee..d6a5c1d06300c1fe13ea6d726ca3082da0c3968c 100644 (file)
@@ -27,7 +27,8 @@ public final class AboutActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(final Bundle savedInstanceState) {
-        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        setTheme(DictionaryApplication.INSTANCE.getSelectedTheme().themeId);
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.about_activity);
index ac77c8558c8d6be0e62a06ac36522f261436a0bd..008b57de6c1ef6869612267e22177b1e79829a63 100644 (file)
@@ -226,10 +226,12 @@ public class DictionaryActivity extends ActionBarActivity {
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        application = DictionaryApplication.INSTANCE;
         // This needs to be before super.onCreate, otherwise ActionbarSherlock
         // doesn't makes the background of the actionbar white when you're
         // in the dark theme.
-        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+        setTheme(application.getSelectedTheme().themeId);
 
         Log.d(LOG, "onCreate:" + this);
         super.onCreate(savedInstanceState);
@@ -241,7 +243,6 @@ public class DictionaryActivity extends ActionBarActivity {
 
         setContentView(R.layout.dictionary_activity);
 
-        application = (DictionaryApplication) getApplication();
         theme = application.getSelectedTheme();
         textColorFg = getResources().getColor(theme.tokenRowFgColor);
 
index 3f2463d68e8684b8d30ba4245e250807c55169ef..407d097c427c85e57d31138bde9e2af99bd615fe 100644 (file)
@@ -58,7 +58,10 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
-public class DictionaryApplication extends Application {
+public enum DictionaryApplication {
+    INSTANCE;
+
+    private Context appContext;
 
     static final String LOG = "QuickDicApp";
 
@@ -168,18 +171,21 @@ public class DictionaryApplication extends Application {
 
     private File dictDir;
 
-    @Override
-    public void onCreate() {
-        super.onCreate();
+    public void init(Context c) {
+        if (appContext != null) {
+            assert c == appContext;
+            return;
+        }
+        appContext = c;
         Log.d("QuickDic", "Application: onCreate");
         TransliteratorManager.init(null, threadBackground);
-        staticInit(getApplicationContext());
+        staticInit(appContext);
 
         languageButtonPixels = (int) TypedValue.applyDimension(
-                                   TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
+                                   TypedValue.COMPLEX_UNIT_DIP, 60, appContext.getResources().getDisplayMetrics());
 
         // Load the dictionaries we know about.
-        dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(
+        dictionaryConfig = PersistentObjectCache.init(appContext).read(
                                C.DICTIONARY_CONFIGS, DictionaryConfig.class);
         if (dictionaryConfig == null) {
             dictionaryConfig = new DictionaryConfig();
@@ -189,53 +195,54 @@ public class DictionaryApplication extends Application {
         }
 
         // Theme stuff.
-        setTheme(getSelectedTheme().themeId);
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+        appContext.setTheme(getSelectedTheme().themeId);
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
         prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
             @Override
             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                                   String key) {
                 Log.d("QuickDic", "prefs changed: " + key);
-                if (key.equals(getString(R.string.themeKey))) {
-                    setTheme(getSelectedTheme().themeId);
+                if (key.equals(appContext.getString(R.string.themeKey))) {
+                    appContext.setTheme(getSelectedTheme().themeId);
                 }
             }
         });
     }
 
-    public void onCreateGlobalOptionsMenu(
+    public static void onCreateGlobalOptionsMenu(
         final Context context, final Menu menu) {
-        final MenuItem about = menu.add(getString(R.string.about));
+        final Context c = context.getApplicationContext();
+        final MenuItem about = menu.add(c.getString(R.string.about));
         MenuItemCompat.setShowAsAction(about, MenuItem.SHOW_AS_ACTION_NEVER);
         about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
             public boolean onMenuItemClick(final MenuItem menuItem) {
-                final Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
+                final Intent intent = new Intent(c, AboutActivity.class);
                 context.startActivity(intent);
                 return false;
             }
         });
 
-        final MenuItem help = menu.add(getString(R.string.help));
+        final MenuItem help = menu.add(c.getString(R.string.help));
         MenuItemCompat.setShowAsAction(help, MenuItem.SHOW_AS_ACTION_NEVER);
         help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
             public boolean onMenuItemClick(final MenuItem menuItem) {
-                context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent(getApplicationContext()));
+                context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent(c));
                 return false;
             }
         });
 
-        final MenuItem preferences = menu.add(getString(R.string.settings));
+        final MenuItem preferences = menu.add(c.getString(R.string.settings));
         MenuItemCompat.setShowAsAction(preferences, MenuItem.SHOW_AS_ACTION_NEVER);
         preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
             public boolean onMenuItemClick(final MenuItem menuItem) {
                 PreferenceActivity.prefsMightHaveChanged = true;
-                final Intent intent = new Intent(getApplicationContext(), PreferenceActivity.class);
+                final Intent intent = new Intent(c, PreferenceActivity.class);
                 context.startActivity(intent);
                 return false;
             }
         });
 
-        final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
+        final MenuItem reportIssue = menu.add(c.getString(R.string.reportIssue));
         MenuItemCompat.setShowAsAction(reportIssue, MenuItem.SHOW_AS_ACTION_NEVER);
         reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
             public boolean onMenuItemClick(final MenuItem menuItem) {
@@ -258,20 +265,20 @@ public class DictionaryApplication extends Application {
         }
         File efd = null;
         try {
-            efd = getApplicationContext().getExternalFilesDir(null);
+            efd = appContext.getExternalFilesDir(null);
         } catch (Exception e) {
         }
         if (efd != null) {
             efd.mkdirs();
             if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-                getApplicationContext().getExternalFilesDirs(null);
+                appContext.getExternalFilesDirs(null);
             }
             if (efd.isDirectory() && efd.canWrite() && checkFileCreate(efd)) {
                 return efd.getAbsolutePath();
             }
         }
         if (!dictDir.isDirectory() && !dictDir.mkdirs()) {
-            return getApplicationContext().getFilesDir().getAbsolutePath();
+            return appContext.getFilesDir().getAbsolutePath();
         }
         return dir;
     }
@@ -279,15 +286,15 @@ public class DictionaryApplication extends Application {
     public synchronized File getDictDir() {
         // This metaphor doesn't work, because we've already reset
         // prefsMightHaveChanged.
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-        String dir = prefs.getString(getString(R.string.quickdicDirectoryKey), "");
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+        String dir = prefs.getString(appContext.getString(R.string.quickdicDirectoryKey), "");
         if (dir.isEmpty()) {
             dir = selectDefaultDir();
         }
         dictDir = new File(dir);
         dictDir.mkdirs();
         if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-            getApplicationContext().getExternalFilesDirs(null);
+            appContext.getExternalFilesDirs(null);
         }
         return dictDir;
     }
@@ -304,8 +311,8 @@ public class DictionaryApplication extends Application {
     }
 
     public File getWordListFile() {
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-        String file = prefs.getString(getString(R.string.wordListFileKey), "");
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+        String file = prefs.getString(appContext.getString(R.string.wordListFileKey), "");
         if (file.isEmpty()) {
             return new File(getDictDir(), "wordList.txt");
         }
@@ -313,8 +320,8 @@ public class DictionaryApplication extends Application {
     }
 
     public Theme getSelectedTheme() {
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-        final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+        final String theme = prefs.getString(appContext.getString(R.string.themeKey), "themeLight");
         if (theme.equals("themeLight")) {
             return Theme.LIGHT;
         } else {
@@ -349,7 +356,7 @@ public class DictionaryApplication extends Application {
             defaultLangName = null;
         }
         if (defaultLangName == null) {
-            defaultLangName = IsoUtils.INSTANCE.isoCodeToLocalizedLanguageName(getApplicationContext(), defaultLangISO2);
+            defaultLangName = IsoUtils.INSTANCE.isoCodeToLocalizedLanguageName(appContext, defaultLangISO2);
         }
 
         String name = fileToNameCache.get(uncompressedFilename);
@@ -368,7 +375,7 @@ public class DictionaryApplication extends Application {
                     nameBuilder.append("-");
                 }
                 nameBuilder
-                .append(IsoUtils.INSTANCE.isoCodeToLocalizedLanguageName(getApplicationContext(), sortedIndexInfos.get(i).shortName));
+                .append(IsoUtils.INSTANCE.isoCodeToLocalizedLanguageName(appContext, sortedIndexInfos.get(i).shortName));
             }
             name = nameBuilder.toString();
         } else {
index 9dc901d23abb87945adf648306645fe2a2da150e..891c0422ec3ea2ca679506a1e52decae45f8b58d 100644 (file)
@@ -282,23 +282,16 @@ public class DictionaryManagerActivity extends ActionBarActivity {
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        application = DictionaryApplication.INSTANCE;
         // This must be first, otherwise the action bar doesn't get
         // styled properly.
-        // Unfortunately on some (Samsung?) Android versions this
-        // results in a ClassCastException...
-        boolean themeSet = true;
-        try {
-            setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
-        } catch (ClassCastException e) {
-            themeSet = false;
-        }
+        setTheme(application.getSelectedTheme().themeId);
 
         super.onCreate(savedInstanceState);
         Log.d(LOG, "onCreate:" + this);
 
-        application = (DictionaryApplication) getApplication();
-        if (!themeSet)
-            setTheme(application.getSelectedTheme().themeId);
+        setTheme(application.getSelectedTheme().themeId);
 
         blockAutoLaunch = false;
 
index e96b351ebb7a46560831fd45bbd6890fe4f8862e..2dabd801dc38c5ec6cb7b05c730262822804a0eb 100644 (file)
@@ -71,7 +71,8 @@ public final class HtmlDisplayActivity extends ActionBarActivity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(final Bundle savedInstanceState) {
-        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        setTheme(DictionaryApplication.INSTANCE.getSelectedTheme().themeId);
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.html_display_activity);
index 78223848f297f78a15086dc786748823ecaeed38..966f21c7ad7ffe5548f50b343e470ff1b566059d 100644 (file)
@@ -33,7 +33,8 @@ public class PreferenceActivity extends android.preference.PreferenceActivity
     @SuppressWarnings("deprecation")
     @Override
     public void onCreate(Bundle savedInstanceState) {
-        final DictionaryApplication application = (DictionaryApplication) getApplication();
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        final DictionaryApplication application = DictionaryApplication.INSTANCE;
         setTheme(application.getSelectedTheme().themeId);
 
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -88,7 +89,8 @@ public class PreferenceActivity extends android.preference.PreferenceActivity
 
     @Override
     public void onSharedPreferenceChanged(SharedPreferences p, String v) {
-        final DictionaryApplication application = (DictionaryApplication)getApplication();
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        final DictionaryApplication application = DictionaryApplication.INSTANCE;
         File dictDir = application.getDictDir();
         if (!dictDir.isDirectory() || !dictDir.canWrite() ||
                 !application.checkFileCreate(dictDir)) {