Change the title of preference for adding new accounts and remove unused commented...
[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.accounts.AccountManager;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.content.pm.PackageInfo;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.preference.CheckBoxPreference;
29 import android.preference.Preference;
30 import android.preference.Preference.OnPreferenceChangeListener;
31 import android.preference.Preference.OnPreferenceClickListener;
32 import android.preference.PreferenceCategory;
33 import android.preference.PreferenceManager;
34
35 import com.actionbarsherlock.app.ActionBar;
36 import com.actionbarsherlock.app.SherlockPreferenceActivity;
37 import com.actionbarsherlock.view.Menu;
38 import com.actionbarsherlock.view.MenuItem;
39 import com.owncloud.android.MainApp;
40 import com.owncloud.android.R;
41 import com.owncloud.android.authentication.AccountUtils;
42 import com.owncloud.android.db.DbHandler;
43 import com.owncloud.android.ui.PreferenceMultiline;
44 import com.owncloud.android.utils.DisplayUtils;
45 import com.owncloud.android.utils.Log_OC;
46
47
48 /**
49 * An Activity that allows the user to change the application's settings.
50 *
51 * @author Bartek Przybylski
52 * @author David A. Velasco
53 */
54 public class Preferences extends SherlockPreferenceActivity {
55
56 private static final String TAG = "OwnCloudPreferences";
57
58 private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
59
60 private DbHandler mDbHandler;
61 private CheckBoxPreference pCode;
62 //private CheckBoxPreference pLogging;
63 //private Preference pLoggingHistory;
64 private Preference pAboutApp;
65
66 private Account mPreviousAccount = null;
67
68
69 @SuppressWarnings("deprecation")
70 @Override
71 public void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73 mDbHandler = new DbHandler(getBaseContext());
74 addPreferencesFromResource(R.xml.preferences);
75 //populateAccountList();
76 ActionBar actionBar = getSherlock().getActionBar();
77 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
78 actionBar.setDisplayHomeAsUpEnabled(true);
79
80 if (savedInstanceState != null) {
81 mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
82 } else {
83 mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
84 }
85
86 // Load the accounts category for adding the list of accounts
87 PreferenceCategory accountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
88
89 // Populate the accounts category with the list of accounts
90 createAccountsCheckboxPreferences(accountsPrefCategory);
91
92 // Show Create Account if Multiaccount is enabled
93 if (!getResources().getBoolean(R.bool.multiaccount_support)) {
94 PreferenceMultiline addAccountPreference = (PreferenceMultiline) findPreference("add_account");
95 accountsPrefCategory.removePreference(addAccountPreference);
96 }
97
98 Preference pAddAccount = findPreference("add_account");
99 if (pAddAccount != null)
100 pAddAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
101 @Override
102 public boolean onPreferenceClick(Preference preference) {
103 AccountManager am = AccountManager.get(getApplicationContext());
104 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
105 return true;
106 }
107 });
108
109
110 pCode = (CheckBoxPreference) findPreference("set_pincode");
111 if (pCode != null){
112 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
113 @Override
114 public boolean onPreferenceChange(Preference preference, Object newValue) {
115 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
116 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
117 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
118 startActivity(i);
119
120 return true;
121 }
122 });
123
124 }
125
126 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
127
128 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
129 Preference pHelp = findPreference("help");
130 if (pHelp != null ){
131 if (helpEnabled) {
132 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
133 @Override
134 public boolean onPreferenceClick(Preference preference) {
135 String helpWeb =(String) getText(R.string.url_help);
136 if (helpWeb != null && helpWeb.length() > 0) {
137 Uri uriUrl = Uri.parse(helpWeb);
138 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
139 startActivity(intent);
140 }
141 return true;
142 }
143 });
144 } else {
145 preferenceCategory.removePreference(pHelp);
146 }
147
148 }
149
150
151 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
152 Preference pRecommend = findPreference("recommend");
153 if (pRecommend != null){
154 if (recommendEnabled) {
155 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
156 @Override
157 public boolean onPreferenceClick(Preference preference) {
158
159 Intent intent = new Intent(Intent.ACTION_SENDTO);
160 intent.setType("text/plain");
161 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
162 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
163
164 String appName = getString(R.string.app_name);
165 String downloadUrl = getString(R.string.url_app_download);
166 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
167 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
168
169 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
170 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
171
172 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
173 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
174 startActivity(intent);
175
176
177 return(true);
178
179 }
180 });
181 } else {
182 preferenceCategory.removePreference(pRecommend);
183 }
184
185 }
186
187 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
188 Preference pFeedback = findPreference("feedback");
189 if (pFeedback != null){
190 if (feedbackEnabled) {
191 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
192 @Override
193 public boolean onPreferenceClick(Preference preference) {
194 String feedbackMail =(String) getText(R.string.mail_feedback);
195 String feedback =(String) getText(R.string.prefs_feedback);
196 Intent intent = new Intent(Intent.ACTION_SENDTO);
197 intent.setType("text/plain");
198 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
199
200 intent.setData(Uri.parse(feedbackMail));
201 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
202 startActivity(intent);
203
204 return true;
205 }
206 });
207 } else {
208 preferenceCategory.removePreference(pFeedback);
209 }
210
211 }
212
213 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
214 Preference pImprint = findPreference("imprint");
215 if (pImprint != null) {
216 if (imprintEnabled) {
217 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
218 @Override
219 public boolean onPreferenceClick(Preference preference) {
220 String imprintWeb = (String) getText(R.string.url_imprint);
221 if (imprintWeb != null && imprintWeb.length() > 0) {
222 Uri uriUrl = Uri.parse(imprintWeb);
223 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
224 startActivity(intent);
225 }
226 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
227 return true;
228 }
229 });
230 } else {
231 preferenceCategory.removePreference(pImprint);
232 }
233 }
234
235 /* About App */
236 pAboutApp = (Preference) findPreference("about_app");
237 if (pAboutApp != null) {
238 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
239 PackageInfo pkg;
240 try {
241 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
242 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
243 } catch (NameNotFoundException e) {
244 Log_OC.e(TAG, "Error while showing about dialog", e);
245 }
246 }
247
248 /* DISABLED FOR RELEASE UNTIL FIXED
249 pLogging = (CheckBoxPreference) findPreference("log_to_file");
250 if (pLogging != null) {
251 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
252 @Override
253 public boolean onPreferenceChange(Preference preference, Object newValue) {
254
255 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
256
257 if(!pLogging.isChecked()) {
258 Log_OC.d("Debug", "start logging");
259 Log_OC.v("PATH", logpath);
260 Log_OC.startLogging(logpath);
261 }
262 else {
263 Log_OC.d("Debug", "stop logging");
264 Log_OC.stopLogging();
265 }
266 return true;
267 }
268 });
269 }
270
271 pLoggingHistory = (Preference) findPreference("log_history");
272 if (pLoggingHistory != null) {
273 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
274
275 @Override
276 public boolean onPreferenceClick(Preference preference) {
277 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
278 startActivity(intent);
279 return true;
280 }
281 });
282 }
283 */
284
285 }
286
287 @Override
288 protected void onResume() {
289 super.onResume();
290 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
291 boolean state = appPrefs.getBoolean("set_pincode", false);
292 pCode.setChecked(state);
293 }
294
295 @Override
296 public boolean onCreateOptionsMenu(Menu menu) {
297 super.onCreateOptionsMenu(menu);
298 return true;
299 }
300
301 @Override
302 public boolean onMenuItemSelected(int featureId, MenuItem item) {
303 super.onMenuItemSelected(featureId, item);
304 Intent intent;
305
306 switch (item.getItemId()) {
307 case android.R.id.home:
308 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
309 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
310 startActivity(intent);
311 break;
312 default:
313 Log_OC.w(TAG, "Unknown menu item triggered");
314 return false;
315 }
316 return true;
317 }
318
319 @Override
320 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
321 super.onActivityResult(requestCode, resultCode, data);
322 }
323
324 @Override
325 protected void onDestroy() {
326 mDbHandler.close();
327 super.onDestroy();
328 }
329
330 /**
331 * Create the list of accounts that have been added into the app
332 *
333 * @param accountsPrefCategory
334 */
335 private void createAccountsCheckboxPreferences(PreferenceCategory accountsPrefCategory) {
336 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
337 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
338 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
339 for (Account a : accounts) {
340 CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
341 checkBoxPreference.setKey(a.name);
342 checkBoxPreference.setTitle(a.name);
343
344 // Check the current account that is being used
345 if (a.name.equals(currentAccount.name)) {
346 checkBoxPreference.setChecked(true);
347 }
348
349 checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
350 @Override
351 public boolean onPreferenceChange(Preference preference, Object newValue) {
352 String key = preference.getKey();
353 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
354 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
355 for (Account a : accounts) {
356 @SuppressWarnings("deprecation")
357 CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
358 if (key.equals(a.name)) {
359 p.setChecked(true);
360 AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
361 } else {
362 p.setChecked(false);
363 }
364 }
365 return (Boolean) newValue;
366 }
367 });
368
369 accountsPrefCategory.addPreference(checkBoxPreference);
370 }
371 }
372
373 @Override
374 protected void onPause() {
375 if (this.isFinishing()) {
376 Account current = AccountUtils.getCurrentOwnCloudAccount(this);
377 if ((mPreviousAccount == null && current != null)
378 || (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
379 // the account set as default changed since this activity was
380 // created
381
382 // restart the main activity
383 Intent i = new Intent(this, FileDisplayActivity.class);
384 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
385 startActivity(i);
386 }
387 }
388 super.onPause();
389 }
390
391 }