]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/PreferenceActivity.java
Switch to app compat preferences.
[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.support.v7.app.AppCompatActivity;
23 import android.support.v7.preference.PreferenceManager;
24 import android.support.v7.preference.ListPreference;
25
26 import java.io.File;
27 import java.util.List;
28
29 public class PreferenceActivity extends AppCompatActivity
30     implements SharedPreferences.OnSharedPreferenceChangeListener {
31
32     static boolean prefsMightHaveChanged = false;
33
34     @SuppressWarnings("deprecation")
35     @Override
36     public void onCreate(Bundle savedInstanceState) {
37         DictionaryApplication.INSTANCE.init(getApplicationContext());
38         final DictionaryApplication application = DictionaryApplication.INSTANCE;
39         setTheme(application.getSelectedTheme().themeId);
40
41         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
42         if (prefs.getString(getString(R.string.quickdicDirectoryKey), "").equals("")) {
43             prefs.edit().putString(getString(R.string.quickdicDirectoryKey), application.getDictDir().getAbsolutePath()).commit();
44         }
45         if (prefs.getString(getString(R.string.wordListFileKey), "").equals("")) {
46             prefs.edit().putString(getString(R.string.wordListFileKey), application.getWordListFile().getAbsolutePath()).commit();
47         }
48
49         /*
50           @author Dominik Köppl Preference: select default dictionary As this
51          *         list is dynamically generated, we have to do it in this
52          *         fashion
53          */
54         super.onCreate(savedInstanceState);
55         setContentView(R.layout.preference_activity);
56     }
57
58     @Override
59     protected void onPause() {
60         super.onPause();
61         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
62         prefs.unregisterOnSharedPreferenceChangeListener(this);
63     }
64
65     @Override
66     protected void onResume() {
67         super.onResume();
68         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
69         prefs.registerOnSharedPreferenceChangeListener(this);
70     }
71
72     private String suggestedPaths(String suffix) {
73         String dirs = "";
74         String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
75         if (new File(externalDir).canWrite())
76             dirs += "\n" + externalDir + "/quickDic" + suffix;
77         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
78             File[] files = getApplicationContext().getExternalFilesDirs(null);
79             for (File f : files) {
80                 if (f.canWrite())
81                     dirs += "\n" + f.getAbsolutePath() + suffix;
82             }
83         } else {
84             File efd = null;
85             try {
86                 efd = getApplicationContext().getExternalFilesDir(null);
87             } catch (Exception ignored) {
88             }
89             if (efd != null) {
90                 String externalFilesDir = efd.getAbsolutePath();
91                 if (new File(externalFilesDir).canWrite())
92                     dirs += "\n" + externalFilesDir + suffix;
93             }
94         }
95         File fd = getApplicationContext().getFilesDir();
96         if (fd.canWrite())
97             dirs += "\n" + fd.getAbsolutePath() + suffix;
98         return dirs;
99     }
100
101     @Override
102     public void onSharedPreferenceChanged(SharedPreferences p, String v) {
103         DictionaryApplication.INSTANCE.init(getApplicationContext());
104         final DictionaryApplication application = DictionaryApplication.INSTANCE;
105         File dictDir = application.getDictDir();
106         if (!dictDir.isDirectory() || !dictDir.canWrite() ||
107                 !DictionaryApplication.checkFileCreate(dictDir)) {
108             String dirs = suggestedPaths("");
109             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
110             .setMessage(getString(R.string.chosenNotWritable) + dirs)
111             .setNeutralButton("Close", null).show();
112         }
113         File wordlist = application.getWordListFile();
114         boolean ok = false;
115         try {
116             ok = wordlist.canWrite() || wordlist.createNewFile();
117         } catch (Exception ignored) {
118         }
119         if (!ok) {
120             String dirs = suggestedPaths("/wordList.txt");
121             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
122             .setMessage(getString(R.string.chosenNotWritable) + dirs)
123             .setNeutralButton("Close", null).show();
124         }
125     }
126 }