]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/PreferenceActivity.java
Apply astyle code formattting.
[Dictionary.git] / src / com / hughes / android / dictionary / PreferenceActivity.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary;
16
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;
24
25 import java.io.File;
26 import java.util.List;
27
28 public class PreferenceActivity extends android.preference.PreferenceActivity
29     implements SharedPreferences.OnSharedPreferenceChangeListener {
30
31     static boolean prefsMightHaveChanged = false;
32
33     @SuppressWarnings("deprecation")
34     @Override
35     public void onCreate(Bundle savedInstanceState) {
36         final DictionaryApplication application = (DictionaryApplication) getApplication();
37         setTheme(application.getSelectedTheme().themeId);
38
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();
42         }
43         if (prefs.getString(getString(R.string.wordListFileKey), "").equals("")) {
44             prefs.edit().putString(getString(R.string.wordListFileKey), application.getWordListFile().getAbsolutePath()).commit();
45         }
46
47         /**
48          * @author Dominik Köppl Preference: select default dictionary As this
49          *         list is dynamically generated, we have to do it in this
50          *         fashion
51          */
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);
57
58         final CharSequence[] entries = new CharSequence[dicts.size()];
59         final CharSequence[] entryvalues = new CharSequence[dicts.size()];
60
61         for (int i = 0; i < entries.length; ++i) {
62             entries[i] = dicts.get(i).dictInfo;
63             entryvalues[i] = dicts.get(i).uncompressedFilename;
64         }
65
66         defaultDic.setEntries(entries);
67         defaultDic.setEntryValues(entryvalues);
68     }
69
70     @Override
71     protected void onPause() {
72         super.onPause();
73         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
74         prefs.unregisterOnSharedPreferenceChangeListener(this);
75     }
76
77     @Override
78     protected void onResume() {
79         super.onResume();
80         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
81         prefs.registerOnSharedPreferenceChangeListener(this);
82     }
83
84     @Override
85     public void onContentChanged() {
86         super.onContentChanged();
87     }
88
89     @Override
90     public void onSharedPreferenceChanged(SharedPreferences p, String v) {
91         final DictionaryApplication application = (DictionaryApplication)getApplication();
92         File dictDir = application.getDictDir();
93         if (!dictDir.isDirectory() || !dictDir.canWrite() ||
94                 !application.checkFileCreate(dictDir)) {
95             String dirs = "";
96             String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
97             if (new File(externalDir).canWrite())
98                 dirs += "\n" + externalDir + "/quickDic";
99             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
100                 File[] files = getApplicationContext().getExternalFilesDirs(null);
101                 for (File f : files) {
102                     if (f.canWrite())
103                         dirs += "\n" + f.getAbsolutePath();
104                 }
105             } else {
106                 File efd = null;
107                 try {
108                     efd = getApplicationContext().getExternalFilesDir(null);
109                 } catch (Exception e) {
110                 }
111                 if (efd != null) {
112                     String externalFilesDir = efd.getAbsolutePath();
113                     if (new File(externalFilesDir).canWrite())
114                         dirs += "\n" + externalFilesDir;
115                 }
116             }
117             File fd = getApplicationContext().getFilesDir();
118             if (fd.canWrite())
119                 dirs += "\n" + fd.getAbsolutePath();
120             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
121             .setMessage(getString(R.string.chosenNotWritable) + dirs)
122             .setNeutralButton("Close", null).show();
123         }
124     }
125 }