]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/PreferenceActivity.java
Remove unnecessary cast and imports.
[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
25 import java.io.File;
26
27 public class PreferenceActivity extends AppCompatActivity
28     implements SharedPreferences.OnSharedPreferenceChangeListener {
29
30     static boolean prefsMightHaveChanged = false;
31
32     @SuppressWarnings("deprecation")
33     @Override
34     public void onCreate(Bundle savedInstanceState) {
35         DictionaryApplication.INSTANCE.init(getApplicationContext());
36         final DictionaryApplication application = DictionaryApplication.INSTANCE;
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         setContentView(R.layout.preference_activity);
54     }
55
56     @Override
57     protected void onPause() {
58         super.onPause();
59         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
60         prefs.unregisterOnSharedPreferenceChangeListener(this);
61     }
62
63     @Override
64     protected void onResume() {
65         super.onResume();
66         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
67         prefs.registerOnSharedPreferenceChangeListener(this);
68     }
69
70     private String suggestedPaths(String suffix) {
71         String dirs = "";
72         String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
73         if (new File(externalDir).canWrite())
74             dirs += "\n" + externalDir + "/quickDic" + suffix;
75         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
76             File[] files = getApplicationContext().getExternalFilesDirs(null);
77             for (File f : files) {
78                 if (f.canWrite())
79                     dirs += "\n" + f.getAbsolutePath() + suffix;
80             }
81         } else {
82             File efd = null;
83             try {
84                 efd = getApplicationContext().getExternalFilesDir(null);
85             } catch (Exception ignored) {
86             }
87             if (efd != null) {
88                 String externalFilesDir = efd.getAbsolutePath();
89                 if (new File(externalFilesDir).canWrite())
90                     dirs += "\n" + externalFilesDir + suffix;
91             }
92         }
93         File fd = getApplicationContext().getFilesDir();
94         if (fd.canWrite())
95             dirs += "\n" + fd.getAbsolutePath() + suffix;
96         return dirs;
97     }
98
99     @Override
100     public void onSharedPreferenceChanged(SharedPreferences p, String v) {
101         DictionaryApplication.INSTANCE.init(getApplicationContext());
102         final DictionaryApplication application = DictionaryApplication.INSTANCE;
103         File dictDir = application.getDictDir();
104         if (!dictDir.isDirectory() || !dictDir.canWrite() ||
105                 !DictionaryApplication.checkFileCreate(dictDir)) {
106             String dirs = suggestedPaths("");
107             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
108             .setMessage(getString(R.string.chosenNotWritable) + dirs)
109             .setNeutralButton("Close", null).show();
110         }
111         File wordlist = application.getWordListFile();
112         boolean ok = false;
113         try {
114             ok = wordlist.canWrite() || wordlist.createNewFile();
115         } catch (Exception ignored) {
116         }
117         if (!ok) {
118             String dirs = suggestedPaths("/wordList.txt");
119             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
120             .setMessage(getString(R.string.chosenNotWritable) + dirs)
121             .setNeutralButton("Close", null).show();
122         }
123     }
124 }