From cfcbcbe64802d0df9b4cb64500283959e2ec12de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Tue, 27 Jun 2017 22:25:17 +0200 Subject: [PATCH] Avoid inheriting from Application. 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 | 5 +- .../android/dictionary/AboutActivity.java | 3 +- .../dictionary/DictionaryActivity.java | 5 +- .../dictionary/DictionaryApplication.java | 69 ++++++++++--------- .../dictionary/DictionaryManagerActivity.java | 15 ++-- .../dictionary/HtmlDisplayActivity.java | 3 +- .../dictionary/PreferenceActivity.java | 6 +- 7 files changed, 55 insertions(+), 51 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e901fb6..c3d8d9f 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,8 +2,8 @@ + android:versionCode="90" + android:versionName="5.3.7" > = 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 { diff --git a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java index 9dc901d..891c042 100644 --- a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java @@ -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; diff --git a/src/com/hughes/android/dictionary/HtmlDisplayActivity.java b/src/com/hughes/android/dictionary/HtmlDisplayActivity.java index e96b351..2dabd80 100644 --- a/src/com/hughes/android/dictionary/HtmlDisplayActivity.java +++ b/src/com/hughes/android/dictionary/HtmlDisplayActivity.java @@ -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); diff --git a/src/com/hughes/android/dictionary/PreferenceActivity.java b/src/com/hughes/android/dictionary/PreferenceActivity.java index 7822384..966f21c 100644 --- a/src/com/hughes/android/dictionary/PreferenceActivity.java +++ b/src/com/hughes/android/dictionary/PreferenceActivity.java @@ -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)) { -- 2.43.0