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