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