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