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