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.support.v7.app.AppCompatActivity;
23 import android.support.v7.preference.PreferenceManager;
24 import android.support.v7.preference.ListPreference;
27 import java.util.List;
29 public class PreferenceActivity extends AppCompatActivity
30 implements SharedPreferences.OnSharedPreferenceChangeListener {
32 static boolean prefsMightHaveChanged = false;
34 @SuppressWarnings("deprecation")
36 public void onCreate(Bundle savedInstanceState) {
37 DictionaryApplication.INSTANCE.init(getApplicationContext());
38 final DictionaryApplication application = DictionaryApplication.INSTANCE;
39 setTheme(application.getSelectedTheme().themeId);
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();
45 if (prefs.getString(getString(R.string.wordListFileKey), "").equals("")) {
46 prefs.edit().putString(getString(R.string.wordListFileKey), application.getWordListFile().getAbsolutePath()).commit();
50 @author Dominik Köppl Preference: select default dictionary As this
51 * list is dynamically generated, we have to do it in this
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.preference_activity);
59 protected void onPause() {
61 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
62 prefs.unregisterOnSharedPreferenceChangeListener(this);
66 protected void onResume() {
68 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
69 prefs.registerOnSharedPreferenceChangeListener(this);
72 private String suggestedPaths(String suffix) {
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) {
81 dirs += "\n" + f.getAbsolutePath() + suffix;
86 efd = getApplicationContext().getExternalFilesDir(null);
87 } catch (Exception ignored) {
90 String externalFilesDir = efd.getAbsolutePath();
91 if (new File(externalFilesDir).canWrite())
92 dirs += "\n" + externalFilesDir + suffix;
95 File fd = getApplicationContext().getFilesDir();
97 dirs += "\n" + fd.getAbsolutePath() + suffix;
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();
113 File wordlist = application.getWordListFile();
116 ok = wordlist.canWrite() || wordlist.createNewFile();
117 } catch (Exception ignored) {
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();