Add new preference category on settings and list in it the accounts
[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.utils.DisplayUtils;
44 import com.owncloud.android.utils.Log_OC;
45
46
47 /**
48 * An Activity that allows the user to change the application's settings.
49 *
50 * @author Bartek Przybylski
51 * @author David A. Velasco
52 */
53 public class Preferences extends SherlockPreferenceActivity {
54
55 private static final String TAG = "OwnCloudPreferences";
56 private DbHandler mDbHandler;
57 private CheckBoxPreference pCode;
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 // Load the accounts category for adding the list of accounts
75 PreferenceCategory accountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
76
77 // Populate the accounts category with the list of accounts
78 createAccountsCheckboxPreferences(accountsPrefCategory);
79
80
81 Preference p = findPreference("manage_account");
82 if (p != null)
83 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
84 @Override
85 public boolean onPreferenceClick(Preference preference) {
86 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
87 startActivity(i);
88 return true;
89 }
90 });
91
92 pCode = (CheckBoxPreference) findPreference("set_pincode");
93 if (pCode != null){
94 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
95 @Override
96 public boolean onPreferenceChange(Preference preference, Object newValue) {
97 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
98 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
99 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
100 startActivity(i);
101
102 return true;
103 }
104 });
105
106 }
107
108
109
110 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
111
112 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
113 Preference pHelp = findPreference("help");
114 if (pHelp != null ){
115 if (helpEnabled) {
116 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
117 @Override
118 public boolean onPreferenceClick(Preference preference) {
119 String helpWeb =(String) getText(R.string.url_help);
120 if (helpWeb != null && helpWeb.length() > 0) {
121 Uri uriUrl = Uri.parse(helpWeb);
122 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
123 startActivity(intent);
124 }
125 return true;
126 }
127 });
128 } else {
129 preferenceCategory.removePreference(pHelp);
130 }
131
132 }
133
134
135 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
136 Preference pRecommend = findPreference("recommend");
137 if (pRecommend != null){
138 if (recommendEnabled) {
139 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
140 @Override
141 public boolean onPreferenceClick(Preference preference) {
142
143 Intent intent = new Intent(Intent.ACTION_SENDTO);
144 intent.setType("text/plain");
145 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
146 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
147
148 String appName = getString(R.string.app_name);
149 String downloadUrl = getString(R.string.url_app_download);
150 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
151 String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
152
153 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
154 String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
155
156 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
157 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
158 startActivity(intent);
159
160
161 return(true);
162
163 }
164 });
165 } else {
166 preferenceCategory.removePreference(pRecommend);
167 }
168
169 }
170
171 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
172 Preference pFeedback = findPreference("feedback");
173 if (pFeedback != null){
174 if (feedbackEnabled) {
175 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
176 @Override
177 public boolean onPreferenceClick(Preference preference) {
178 String feedbackMail =(String) getText(R.string.mail_feedback);
179 String feedback =(String) getText(R.string.prefs_feedback);
180 Intent intent = new Intent(Intent.ACTION_SENDTO);
181 intent.setType("text/plain");
182 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
183
184 intent.setData(Uri.parse(feedbackMail));
185 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
186 startActivity(intent);
187
188 return true;
189 }
190 });
191 } else {
192 preferenceCategory.removePreference(pFeedback);
193 }
194
195 }
196
197 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
198 Preference pImprint = findPreference("imprint");
199 if (pImprint != null) {
200 if (imprintEnabled) {
201 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
202 @Override
203 public boolean onPreferenceClick(Preference preference) {
204 String imprintWeb = (String) getText(R.string.url_imprint);
205 if (imprintWeb != null && imprintWeb.length() > 0) {
206 Uri uriUrl = Uri.parse(imprintWeb);
207 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
208 startActivity(intent);
209 }
210 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
211 return true;
212 }
213 });
214 } else {
215 preferenceCategory.removePreference(pImprint);
216 }
217 }
218
219 /* About App */
220 pAboutApp = (Preference) findPreference("about_app");
221 if (pAboutApp != null) {
222 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
223 PackageInfo pkg;
224 try {
225 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
226 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
227 } catch (NameNotFoundException e) {
228 Log_OC.e(TAG, "Error while showing about dialog", e);
229 }
230 }
231
232 /* DISABLED FOR RELEASE UNTIL FIXED
233 pLogging = (CheckBoxPreference) findPreference("log_to_file");
234 if (pLogging != null) {
235 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
236 @Override
237 public boolean onPreferenceChange(Preference preference, Object newValue) {
238
239 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
240
241 if(!pLogging.isChecked()) {
242 Log_OC.d("Debug", "start logging");
243 Log_OC.v("PATH", logpath);
244 Log_OC.startLogging(logpath);
245 }
246 else {
247 Log_OC.d("Debug", "stop logging");
248 Log_OC.stopLogging();
249 }
250 return true;
251 }
252 });
253 }
254
255 pLoggingHistory = (Preference) findPreference("log_history");
256 if (pLoggingHistory != null) {
257 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
258
259 @Override
260 public boolean onPreferenceClick(Preference preference) {
261 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
262 startActivity(intent);
263 return true;
264 }
265 });
266 }
267 */
268
269 }
270
271 @Override
272 protected void onResume() {
273 super.onResume();
274 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
275 boolean state = appPrefs.getBoolean("set_pincode", false);
276 pCode.setChecked(state);
277 }
278
279 @Override
280 public boolean onCreateOptionsMenu(Menu menu) {
281 super.onCreateOptionsMenu(menu);
282 return true;
283 }
284
285 @Override
286 public boolean onMenuItemSelected(int featureId, MenuItem item) {
287 super.onMenuItemSelected(featureId, item);
288 Intent intent;
289
290 switch (item.getItemId()) {
291 case android.R.id.home:
292 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
293 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
294 startActivity(intent);
295 break;
296 default:
297 Log_OC.w(TAG, "Unknown menu item triggered");
298 return false;
299 }
300 return true;
301 }
302
303 @Override
304 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
305 super.onActivityResult(requestCode, resultCode, data);
306 }
307
308 @Override
309 protected void onDestroy() {
310 mDbHandler.close();
311 super.onDestroy();
312 }
313
314 /**
315 * Create the list of accounts that have been added into the app
316 *
317 * @param accountsPrefCategory
318 */
319 private void createAccountsCheckboxPreferences(PreferenceCategory accountsPrefCategory) {
320 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
321 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
322 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
323 for (Account a : accounts) {
324 CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
325 checkBoxPreference.setKey(a.name);
326 checkBoxPreference.setTitle(a.name);
327
328 // Check the current account that is being used
329 if (a.name.equals(currentAccount.name)) {
330 checkBoxPreference.setChecked(true);
331 }
332
333 accountsPrefCategory.addPreference(checkBoxPreference);
334 }
335 }
336 }