32bd11d2c7ce5dc07218d69107343f2b8cb46e28
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Preferences.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
20 import android.accounts.Account;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.preference.CheckBoxPreference;
28 import android.preference.Preference;
29 import android.preference.Preference.OnPreferenceChangeListener;
30 import android.preference.Preference.OnPreferenceClickListener;
31 import android.preference.PreferenceCategory;
32 import android.preference.PreferenceManager;
33
34 import com.actionbarsherlock.app.ActionBar;
35 import com.actionbarsherlock.app.SherlockPreferenceActivity;
36 import com.actionbarsherlock.view.Menu;
37 import com.actionbarsherlock.view.MenuItem;
38 import com.owncloud.android.R;
39 import com.owncloud.android.authentication.AccountUtils;
40 import com.owncloud.android.db.DbHandler;
41 import com.owncloud.android.ui.CheckBoxPreferenceWithLongTitle;
42 import com.owncloud.android.utils.DisplayUtils;
43 import com.owncloud.android.utils.Log_OC;
44
45
46 /**
47 * An Activity that allows the user to change the application's settings.
48 *
49 * @author Bartek Przybylski
50 * @author David A. Velasco
51 */
52 public class Preferences extends SherlockPreferenceActivity {
53
54 private static final String TAG = "OwnCloudPreferences";
55 private DbHandler mDbHandler;
56 private CheckBoxPreference pCode;
57 private CheckBoxPreference pSaveLocation;
58 //private CheckBoxPreference pLogging;
59 //private Preference pLoggingHistory;
60 private Preference pAboutApp;
61
62
63 @SuppressWarnings("deprecation")
64 @Override
65 public void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67 mDbHandler = new DbHandler(getBaseContext());
68 addPreferencesFromResource(R.xml.preferences);
69 //populateAccountList();
70 ActionBar actionBar = getSherlock().getActionBar();
71 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
72 actionBar.setDisplayHomeAsUpEnabled(true);
73
74 Preference p = findPreference("manage_account");
75 if (p != null)
76 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
77 @Override
78 public boolean onPreferenceClick(Preference preference) {
79 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
80 startActivity(i);
81 return true;
82 }
83 });
84
85 pCode = (CheckBoxPreference) findPreference("set_pincode");
86 if (pCode != null){
87 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
88 @Override
89 public boolean onPreferenceChange(Preference preference, Object newValue) {
90 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
91 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
92 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
93 startActivity(i);
94
95 return true;
96 }
97 });
98
99 }
100
101 pSaveLocation = (CheckBoxPreferenceWithLongTitle) findPreference("save_last_upload_location");
102 if(pSaveLocation != null){
103 pSaveLocation.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
104 @Override
105 public boolean onPreferenceChange(Preference preference, Object newValue) {
106 if( newValue instanceof Boolean)
107 {
108 if(!(Boolean) newValue)
109 {
110 SharedPreferences.Editor appPrefs = PreferenceManager
111 .getDefaultSharedPreferences(getApplicationContext()).edit();
112 appPrefs.remove("last_upload_path");
113 appPrefs.commit();
114 }
115 }
116 return true;
117 }
118 });
119 }
120
121 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
122
123 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
124 Preference pHelp = findPreference("help");
125 if (pHelp != null ){
126 if (helpEnabled) {
127 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
128 @Override
129 public boolean onPreferenceClick(Preference preference) {
130 String helpWeb =(String) getText(R.string.url_help);
131 if (helpWeb != null && helpWeb.length() > 0) {
132 Uri uriUrl = Uri.parse(helpWeb);
133 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
134 startActivity(intent);
135 }
136 return true;
137 }
138 });
139 } else {
140 preferenceCategory.removePreference(pHelp);
141 }
142
143 }
144
145
146 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
147 Preference pRecommend = findPreference("recommend");
148 if (pRecommend != null){
149 if (recommendEnabled) {
150 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
151 @Override
152 public boolean onPreferenceClick(Preference preference) {
153
154 Intent intent = new Intent(Intent.ACTION_SENDTO);
155 intent.setType("text/plain");
156 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
157 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
158
159 String appName = getString(R.string.app_name);
160 String downloadUrl = getString(R.string.url_app_download);
161 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
162 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
163
164 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
165 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
166
167 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
168 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
169 startActivity(intent);
170
171
172 return(true);
173
174 }
175 });
176 } else {
177 preferenceCategory.removePreference(pRecommend);
178 }
179
180 }
181
182 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
183 Preference pFeedback = findPreference("feedback");
184 if (pFeedback != null){
185 if (feedbackEnabled) {
186 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
187 @Override
188 public boolean onPreferenceClick(Preference preference) {
189 String feedbackMail =(String) getText(R.string.mail_feedback);
190 String feedback =(String) getText(R.string.prefs_feedback);
191 Intent intent = new Intent(Intent.ACTION_SENDTO);
192 intent.setType("text/plain");
193 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
194
195 intent.setData(Uri.parse(feedbackMail));
196 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
197 startActivity(intent);
198
199 return true;
200 }
201 });
202 } else {
203 preferenceCategory.removePreference(pFeedback);
204 }
205
206 }
207
208 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
209 Preference pImprint = findPreference("imprint");
210 if (pImprint != null) {
211 if (imprintEnabled) {
212 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
213 @Override
214 public boolean onPreferenceClick(Preference preference) {
215 String imprintWeb = (String) getText(R.string.url_imprint);
216 if (imprintWeb != null && imprintWeb.length() > 0) {
217 Uri uriUrl = Uri.parse(imprintWeb);
218 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
219 startActivity(intent);
220 }
221 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
222 return true;
223 }
224 });
225 } else {
226 preferenceCategory.removePreference(pImprint);
227 }
228 }
229
230 /* About App */
231 pAboutApp = (Preference) findPreference("about_app");
232 if (pAboutApp != null) {
233 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
234 PackageInfo pkg;
235 try {
236 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
237 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
238 } catch (NameNotFoundException e) {
239 Log_OC.e(TAG, "Error while showing about dialog", e);
240 }
241 }
242
243 /* DISABLED FOR RELEASE UNTIL FIXED
244 pLogging = (CheckBoxPreference) findPreference("log_to_file");
245 if (pLogging != null) {
246 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
247 @Override
248 public boolean onPreferenceChange(Preference preference, Object newValue) {
249
250 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
251
252 if(!pLogging.isChecked()) {
253 Log_OC.d("Debug", "start logging");
254 Log_OC.v("PATH", logpath);
255 Log_OC.startLogging(logpath);
256 }
257 else {
258 Log_OC.d("Debug", "stop logging");
259 Log_OC.stopLogging();
260 }
261 return true;
262 }
263 });
264 }
265
266 pLoggingHistory = (Preference) findPreference("log_history");
267 if (pLoggingHistory != null) {
268 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
269
270 @Override
271 public boolean onPreferenceClick(Preference preference) {
272 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
273 startActivity(intent);
274 return true;
275 }
276 });
277 }
278 */
279
280 }
281
282 @Override
283 protected void onResume() {
284 super.onResume();
285 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
286 boolean state = appPrefs.getBoolean("set_pincode", false);
287 pCode.setChecked(state);
288 }
289
290 @Override
291 public boolean onCreateOptionsMenu(Menu menu) {
292 super.onCreateOptionsMenu(menu);
293 return true;
294 }
295
296 @Override
297 public boolean onMenuItemSelected(int featureId, MenuItem item) {
298 super.onMenuItemSelected(featureId, item);
299 Intent intent;
300
301 switch (item.getItemId()) {
302 case android.R.id.home:
303 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
304 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
305 startActivity(intent);
306 break;
307 default:
308 Log_OC.w(TAG, "Unknown menu item triggered");
309 return false;
310 }
311 return true;
312 }
313
314 @Override
315 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
316 super.onActivityResult(requestCode, resultCode, data);
317 }
318
319 @Override
320 protected void onDestroy() {
321 mDbHandler.close();
322 super.onDestroy();
323 }
324
325 }