26b1b6c6f63e4ec3518e9f7edec145fafdf87213
[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 Uri uriUrl = Uri.parse(helpWeb);
110 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
111 startActivity(intent);
112 return true;
113 }
114 });
115 }
116
117 Preference pRecommend = findPreference("recommend");
118 if (pRecommend != null){
119 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
120 @Override
121 public boolean onPreferenceClick(Preference preference) {
122
123 Intent intent = new Intent(Intent.ACTION_SENDTO);
124 intent.setType("text/plain");
125 //Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
126 String appName = getString(R.string.app_name);
127 //String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
128 //String recommendSubject = String.format(getString(R.string.recommend_subject), username, appName);
129 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
130 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
131 //String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), username);
132 String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), getString(R.string.url_app_download));
133 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
134
135 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
136 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
137 startActivity(intent);
138
139
140 return(true);
141
142 }
143 });
144 }
145 Preference pFeedback = findPreference("feedback");
146 if (pFeedback != null){
147 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
148 @Override
149 public boolean onPreferenceClick(Preference preference) {
150 String feedbackMail =(String) getText(R.string.mail_feedback);
151 String feedback =(String) getText(R.string.prefs_feedback);
152 Intent intent = new Intent(Intent.ACTION_SENDTO);
153 intent.setType("text/plain");
154 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
155
156 intent.setData(Uri.parse(feedbackMail));
157 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
158 startActivity(intent);
159
160 return true;
161 }
162 });
163 }
164
165 /*
166 Preference pImprint = findPreference("imprint");
167 if (pImprint != null){
168 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
169 @Override
170 public boolean onPreferenceClick(Preference preference) {
171 /*String imprintWeb = (String) getText(R.string.url_imprint);
172 Uri uriUrl = Uri.parse(imprintWeb);
173 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
174 startActivity(intent);*-/
175 ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
176 return true;
177 }
178 });
179
180 /* About App */
181 pAboutApp = (Preference) findPreference("about_app");
182 if (pAboutApp != null) {
183 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
184 PackageInfo pkg;
185 try {
186 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
187 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
188 } catch (NameNotFoundException e) {
189 Log_OC.e(TAG, "Error while showing about dialog", e);
190 }
191 }
192
193 /* DISABLED FOR RELEASE UNTIL FIXED
194 pLogging = (CheckBoxPreference) findPreference("log_to_file");
195 if (pLogging != null) {
196 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
197 @Override
198 public boolean onPreferenceChange(Preference preference, Object newValue) {
199
200 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
201
202 if(!pLogging.isChecked()) {
203 Log_OC.d("Debug", "start logging");
204 Log_OC.v("PATH", logpath);
205 Log_OC.startLogging(logpath);
206 }
207 else {
208 Log_OC.d("Debug", "stop logging");
209 Log_OC.stopLogging();
210 }
211 return true;
212 }
213 });
214 }
215
216 pLoggingHistory = (Preference) findPreference("log_history");
217 if (pLoggingHistory != null) {
218 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
219
220 @Override
221 public boolean onPreferenceClick(Preference preference) {
222 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
223 startActivity(intent);
224 return true;
225 }
226 });
227 }
228 */
229
230 }
231
232 @Override
233 protected void onResume() {
234 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
235 boolean state = appPrefs.getBoolean("set_pincode", false);
236 pCode.setChecked(state);
237 super.onResume();
238 }
239
240 @Override
241 public boolean onCreateOptionsMenu(Menu menu) {
242 super.onCreateOptionsMenu(menu);
243 return true;
244 }
245
246 @Override
247 public boolean onMenuItemSelected(int featureId, MenuItem item) {
248 super.onMenuItemSelected(featureId, item);
249 Intent intent;
250
251 switch (item.getItemId()) {
252 //case R.id.addSessionItem:
253 case 1:
254 intent = new Intent(this, PreferencesNewSession.class);
255 startActivityForResult(intent, mNewSession);
256 break;
257 case R.id.SessionContextEdit:
258 intent = new Intent(this, PreferencesNewSession.class);
259 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
260 .getEntryId());
261 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
262 .getName());
263 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
264 .getUrl());
265 startActivityForResult(intent, mEditSession);
266 break;
267 case android.R.id.home:
268 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
269 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
270 startActivity(intent);
271 break;
272 default:
273 Log_OC.w(TAG, "Unknown menu item triggered");
274 return false;
275 }
276 return true;
277 }
278
279 @Override
280 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
281 super.onActivityResult(requestCode, resultCode, data);
282 }
283
284 @Override
285 protected void onDestroy() {
286 mDbHandler.close();
287 super.onDestroy();
288 }
289
290 @Override
291 /**
292 * Updates various summaries after updates. Also starts and stops
293 * the
294 */
295 public boolean onPreferenceChange(Preference preference, Object newValue) {
296 // Update current account summary
297 /*if (preference.equals(mAccountList)) {
298 mAccountList.setSummary(newValue.toString());
299 }
300
301 // Update tracking interval summary
302 else*/ if (preference.equals(mTrackingUpdateInterval)) {
303 String trackingSummary = getResources().getString(
304 R.string.prefs_trackmydevice_interval_summary);
305 trackingSummary = String.format(trackingSummary,
306 newValue.toString());
307 mTrackingUpdateInterval.setSummary(trackingSummary);
308 }
309
310 // Start or stop tracking service
311 else if (preference.equals(mDeviceTracking)) {
312 Intent locationServiceIntent = new Intent();
313 locationServiceIntent
314 .setAction("com.owncloud.android.location.LocationLauncher");
315 locationServiceIntent.putExtra("TRACKING_SETTING",
316 (Boolean) newValue);
317 sendBroadcast(locationServiceIntent);
318 }
319 return true;
320 }
321 }