-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathFileExplorerPreferenceActivity.java
More file actions
147 lines (125 loc) · 5.77 KB
/
FileExplorerPreferenceActivity.java
File metadata and controls
147 lines (125 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* This file is part of FileExplorer.
*
* FileExplorer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FileExplorer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SwiFTP. If not, see <http://www.gnu.org/licenses/>.
*/
package net.micode.fileexplorer;
import java.io.File;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.text.TextUtils;
/**
*
* @author ShunLi
*/
public class FileExplorerPreferenceActivity extends PreferenceActivity
implements OnSharedPreferenceChangeListener {
private static final String PRIMARY_FOLDER = "pref_key_primary_folder";
private static final String READ_ROOT = "pref_key_read_root";
private static final String SHOW_REAL_PATH = "pref_key_show_real_path";
private static final String SYSTEM_SEPARATOR = File.separator;
private EditTextPreference mEditTextPreference;
private CheckBoxPreference mCheckBoxPreferenceShowRealPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
mEditTextPreference = (EditTextPreference) findPreference(PRIMARY_FOLDER);
mCheckBoxPreferenceShowRealPath = (CheckBoxPreference) findPreference(SHOW_REAL_PATH);
mCheckBoxPreferenceShowRealPath.setEnabled(getPreferenceScreen()
.getSharedPreferences().getBoolean(READ_ROOT, false));
}
@Override
protected void onResume() {
super.onResume();
// Setup the initial values
SharedPreferences sharedPreferences = getPreferenceScreen()
.getSharedPreferences();
mEditTextPreference.setSummary(this.getString(
R.string.pref_primary_folder_summary, sharedPreferences
.getString(PRIMARY_FOLDER, GlobalConsts.ROOT_PATH)));
// Set up a listener whenever a key changes
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedpreferences,
String key) {
if (PRIMARY_FOLDER.equals(key)) {
mEditTextPreference
.setSummary(this.getString(
R.string.pref_primary_folder_summary,
sharedpreferences.getString(PRIMARY_FOLDER,
GlobalConsts.ROOT_PATH)));
} else if (READ_ROOT.equals(key)) {
// sharedpreferences.getBoolean(READ_ROOT, false)
mCheckBoxPreferenceShowRealPath.setEnabled(sharedpreferences
.getBoolean(READ_ROOT, false));
}
}
public static String getPrimaryFolder(Context context) {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
String primaryFolder = settings.getString(PRIMARY_FOLDER, context
.getString(R.string.default_primary_folder,
GlobalConsts.ROOT_PATH));
if (TextUtils.isEmpty(primaryFolder)) { // setting primary folder =
// empty("")
primaryFolder = GlobalConsts.ROOT_PATH;
}
// it's remove the end char of the home folder setting when it with the
// '/' at the end.
// if has the backslash at end of the home folder, it's has minor bug at
// "UpLevel" function.
int length = primaryFolder.length();
if (length > 1
&& SYSTEM_SEPARATOR.equals(primaryFolder.substring(length - 1))) { // length
// =
// 1,
// ROOT_PATH
return primaryFolder.substring(0, length - 1);
} else {
return primaryFolder;
}
}
public static boolean isReadRoot(Context context) {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
boolean isReadRootFromSetting = settings.getBoolean(READ_ROOT, false);
boolean isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix = !getPrimaryFolder(
context).startsWith(Util.getSdDirectory());
return isReadRootFromSetting
|| isReadRootWhenSettingPrimaryFolderWithoutSdCardPrefix;
}
public static boolean showRealPath(Context context) {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getBoolean(SHOW_REAL_PATH, false)
&& isReadRoot(context);
}
}