44e442216948ee892686785ffbb7d1a54a1f16c1
[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 java.util.Vector;
21
22 import android.accounts.Account;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.content.pm.PackageInfo;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.preference.CheckBoxPreference;
30 import android.preference.ListPreference;
31 import android.preference.Preference;
32 import android.preference.Preference.OnPreferenceChangeListener;
33 import android.preference.Preference.OnPreferenceClickListener;
34 import android.preference.PreferenceCategory;
35 import android.preference.PreferenceManager;
36
37 import com.actionbarsherlock.app.ActionBar;
38 import com.actionbarsherlock.app.SherlockPreferenceActivity;
39 import com.actionbarsherlock.view.Menu;
40 import com.actionbarsherlock.view.MenuItem;
41 import com.owncloud.android.Log_OC;
42 import com.owncloud.android.OwnCloudSession;
43 import com.owncloud.android.R;
44 import com.owncloud.android.authentication.AccountUtils;
45 import com.owncloud.android.db.DbHandler;
46
47
48 /**
49 * An Activity that allows the user to change the application's settings.
50 *
51 * @author Bartek Przybylski
52 *
53 */
54 public class Preferences extends SherlockPreferenceActivity implements OnPreferenceChangeListener {
55
56 private static final String TAG = "OwnCloudPreferences";
57 private final int mNewSession = 47;
58 private final int mEditSession = 48;
59 private DbHandler mDbHandler;
60 private Vector<OwnCloudSession> mSessions;
61 private ListPreference mTrackingUpdateInterval;
62 private CheckBoxPreference mDeviceTracking;
63 private CheckBoxPreference pCode;
64 //private CheckBoxPreference pLogging;
65 //private Preference pLoggingHistory;
66 private Preference pAboutApp;
67 private int mSelectedMenuItem;
68
69
70 @SuppressWarnings("deprecation")
71 @Override
72 public void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 mDbHandler = new DbHandler(getBaseContext());
75 mSessions = new Vector<OwnCloudSession>();
76 addPreferencesFromResource(R.xml.preferences);
77 //populateAccountList();
78 ActionBar actionBar = getSherlock().getActionBar();
79 actionBar.setDisplayHomeAsUpEnabled(true);
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 //Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
146 String appName = getString(R.string.app_name);
147 //String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
148 //String recommendSubject = String.format(getString(R.string.recommend_subject), username, appName);
149 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
150 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
151 //String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), username);
152 String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), getString(R.string.url_app_download));
153 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
154
155 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
156 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
157 startActivity(intent);
158
159
160 return(true);
161
162 }
163 });
164 } else {
165 preferenceCategory.removePreference(pRecommend);
166 }
167
168 }
169
170 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
171 Preference pFeedback = findPreference("feedback");
172 if (pFeedback != null){
173 if (feedbackEnabled) {
174 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
175 @Override
176 public boolean onPreferenceClick(Preference preference) {
177 String feedbackMail =(String) getText(R.string.mail_feedback);
178 String feedback =(String) getText(R.string.prefs_feedback);
179 Intent intent = new Intent(Intent.ACTION_SENDTO);
180 intent.setType("text/plain");
181 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
182
183 intent.setData(Uri.parse(feedbackMail));
184 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
185 startActivity(intent);
186
187 return true;
188 }
189 });
190 } else {
191 preferenceCategory.removePreference(pFeedback);
192 }
193
194 }
195
196 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
197 Preference pImprint = findPreference("imprint");
198 if (pImprint != null) {
199 if (imprintEnabled) {
200 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
201 @Override
202 public boolean onPreferenceClick(Preference preference) {
203 String imprintWeb = (String) getText(R.string.url_imprint);
204 if (imprintWeb != null && imprintWeb.length() > 0) {
205 Uri uriUrl = Uri.parse(imprintWeb);
206 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
207 startActivity(intent);
208 }
209 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
210 return true;
211 }
212 });
213 } else {
214 preferenceCategory.removePreference(pImprint);
215 }
216 }
217
218 /* About App */
219 pAboutApp = (Preference) findPreference("about_app");
220 if (pAboutApp != null) {
221 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
222 PackageInfo pkg;
223 try {
224 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
225 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
226 } catch (NameNotFoundException e) {
227 Log_OC.e(TAG, "Error while showing about dialog", e);
228 }
229 }
230
231 /* DISABLED FOR RELEASE UNTIL FIXED
232 pLogging = (CheckBoxPreference) findPreference("log_to_file");
233 if (pLogging != null) {
234 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
235 @Override
236 public boolean onPreferenceChange(Preference preference, Object newValue) {
237
238 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
239
240 if(!pLogging.isChecked()) {
241 Log_OC.d("Debug", "start logging");
242 Log_OC.v("PATH", logpath);
243 Log_OC.startLogging(logpath);
244 }
245 else {
246 Log_OC.d("Debug", "stop logging");
247 Log_OC.stopLogging();
248 }
249 return true;
250 }
251 });
252 }
253
254 pLoggingHistory = (Preference) findPreference("log_history");
255 if (pLoggingHistory != null) {
256 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
257
258 @Override
259 public boolean onPreferenceClick(Preference preference) {
260 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
261 startActivity(intent);
262 return true;
263 }
264 });
265 }
266 */
267
268 }
269
270 @Override
271 protected void onResume() {
272 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
273 boolean state = appPrefs.getBoolean("set_pincode", false);
274 pCode.setChecked(state);
275 super.onResume();
276 }
277
278 @Override
279 public boolean onCreateOptionsMenu(Menu menu) {
280 super.onCreateOptionsMenu(menu);
281 return true;
282 }
283
284 @Override
285 public boolean onMenuItemSelected(int featureId, MenuItem item) {
286 super.onMenuItemSelected(featureId, item);
287 Intent intent;
288
289 switch (item.getItemId()) {
290 //case R.id.addSessionItem:
291 case 1:
292 intent = new Intent(this, PreferencesNewSession.class);
293 startActivityForResult(intent, mNewSession);
294 break;
295 case R.id.SessionContextEdit:
296 intent = new Intent(this, PreferencesNewSession.class);
297 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
298 .getEntryId());
299 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
300 .getName());
301 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
302 .getUrl());
303 startActivityForResult(intent, mEditSession);
304 break;
305 case android.R.id.home:
306 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
307 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
308 startActivity(intent);
309 break;
310 default:
311 Log_OC.w(TAG, "Unknown menu item triggered");
312 return false;
313 }
314 return true;
315 }
316
317 @Override
318 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
319 super.onActivityResult(requestCode, resultCode, data);
320 }
321
322 @Override
323 protected void onDestroy() {
324 mDbHandler.close();
325 super.onDestroy();
326 }
327
328 @Override
329 /**
330 * Updates various summaries after updates. Also starts and stops
331 * the
332 */
333 public boolean onPreferenceChange(Preference preference, Object newValue) {
334 // Update current account summary
335 /*if (preference.equals(mAccountList)) {
336 mAccountList.setSummary(newValue.toString());
337 }
338
339 // Update tracking interval summary
340 else*/ if (preference.equals(mTrackingUpdateInterval)) {
341 String trackingSummary = getResources().getString(
342 R.string.prefs_trackmydevice_interval_summary);
343 trackingSummary = String.format(trackingSummary,
344 newValue.toString());
345 mTrackingUpdateInterval.setSummary(trackingSummary);
346 }
347
348 // Start or stop tracking service
349 else if (preference.equals(mDeviceTracking)) {
350 Intent locationServiceIntent = new Intent();
351 locationServiceIntent
352 .setAction("com.owncloud.android.location.LocationLauncher");
353 locationServiceIntent.putExtra("TRACKING_SETTING",
354 (Boolean) newValue);
355 sendBroadcast(locationServiceIntent);
356 }
357 return true;
358 }
359 }