1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary;
17 import android.app.AlertDialog;
18 import android.content.SharedPreferences;
19 import android.os.Bundle;
20 import android.os.Build;
21 import android.os.Environment;
22 import android.preference.ListPreference;
23 import android.preference.PreferenceManager;
26 import java.util.List;
28 public class PreferenceActivity extends android.preference.PreferenceActivity
29 implements SharedPreferences.OnSharedPreferenceChangeListener {
31 static boolean prefsMightHaveChanged = false;
33 @SuppressWarnings("deprecation")
35 public void onCreate(Bundle savedInstanceState) {
36 final DictionaryApplication application = (DictionaryApplication) getApplication();
37 setTheme(application.getSelectedTheme().themeId);
39 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
40 if (prefs.getString(getString(R.string.quickdicDirectoryKey), "").equals("")) {
41 prefs.edit().putString(getString(R.string.quickdicDirectoryKey), application.getDictDir().getAbsolutePath()).commit();
43 if (prefs.getString(getString(R.string.wordListFileKey), "").equals("")) {
44 prefs.edit().putString(getString(R.string.wordListFileKey), application.getWordListFile().getAbsolutePath()).commit();
48 * @author Dominik Köppl Preference: select default dictionary As this
49 * list is dynamically generated, we have to do it in this
52 super.onCreate(savedInstanceState);
53 addPreferencesFromResource(R.xml.preferences);
54 ListPreference defaultDic = (ListPreference) findPreference(getResources().getString(
55 R.string.defaultDicKey));
56 List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
58 final CharSequence[] entries = new CharSequence[dicts.size()];
59 final CharSequence[] entryvalues = new CharSequence[dicts.size()];
61 for (int i = 0; i < entries.length; ++i)
63 entries[i] = dicts.get(i).dictInfo;
64 entryvalues[i] = dicts.get(i).uncompressedFilename;
67 defaultDic.setEntries(entries);
68 defaultDic.setEntryValues(entryvalues);
72 protected void onPause() {
74 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
75 prefs.unregisterOnSharedPreferenceChangeListener(this);
79 protected void onResume() {
81 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
82 prefs.registerOnSharedPreferenceChangeListener(this);
86 public void onContentChanged() {
87 super.onContentChanged();
91 public void onSharedPreferenceChanged(SharedPreferences p, String v) {
92 final DictionaryApplication application = (DictionaryApplication)getApplication();
93 File dictDir = application.getDictDir();
94 if (!dictDir.isDirectory() || !dictDir.canWrite() ||
95 !application.checkFileCreate(dictDir)) {
97 String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
98 if (new File(externalDir).canWrite())
99 dirs += "\n" + externalDir + "/quickDic";
100 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
101 File[] files = getApplicationContext().getExternalFilesDirs(null);
102 for (File f : files) {
104 dirs += "\n" + f.getAbsolutePath();
109 efd = getApplicationContext().getExternalFilesDir(null);
110 } catch (Exception e) {
113 String externalFilesDir = efd.getAbsolutePath();
114 if (new File(externalFilesDir).canWrite())
115 dirs += "\n" + externalFilesDir;
118 File fd = getApplicationContext().getFilesDir();
120 dirs += "\n" + fd.getAbsolutePath();
121 new AlertDialog.Builder(this).setTitle(getString(R.string.error))
122 .setMessage(getString(R.string.chosenNotWritable) + dirs)
123 .setNeutralButton("Close", null).show();