OC-2332: rewrite getAvailableRemotePath from UpdateFileOperation, using ExistenceCehe...
[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.Preference;
30 import android.preference.Preference.OnPreferenceChangeListener;
31 import android.preference.Preference.OnPreferenceClickListener;
32 import android.preference.PreferenceCategory;
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.R;
40 import com.owncloud.android.db.DbHandler;
41 import com.owncloud.android.utils.Log_OC;
42 import com.owncloud.android.utils.OwnCloudSession;
43
44
45 /**
46 * An Activity that allows the user to change the application's settings.
47 *
48 * @author Bartek Przybylski
49 * @author David A. Velasco
50 */
51 public class Preferences extends SherlockPreferenceActivity {
52
53 private static final String TAG = "OwnCloudPreferences";
54 private final int mNewSession = 47;
55 private final int mEditSession = 48;
56 private DbHandler mDbHandler;
57 private Vector<OwnCloudSession> mSessions;
58 private CheckBoxPreference pCode;
59 //private CheckBoxPreference pLogging;
60 //private Preference pLoggingHistory;
61 private Preference pAboutApp;
62 private int mSelectedMenuItem;
63
64
65 @SuppressWarnings("deprecation")
66 @Override
67 public void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 mDbHandler = new DbHandler(getBaseContext());
70 mSessions = new Vector<OwnCloudSession>();
71 addPreferencesFromResource(R.xml.preferences);
72 //populateAccountList();
73 ActionBar actionBar = getSherlock().getActionBar();
74 actionBar.setDisplayHomeAsUpEnabled(true);
75
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
104
105 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
106
107 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
108 Preference pHelp = findPreference("help");
109 if (pHelp != null ){
110 if (helpEnabled) {
111 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
112 @Override
113 public boolean onPreferenceClick(Preference preference) {
114 String helpWeb =(String) getText(R.string.url_help);
115 if (helpWeb != null && helpWeb.length() > 0) {
116 Uri uriUrl = Uri.parse(helpWeb);
117 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
118 startActivity(intent);
119 }
120 return true;
121 }
122 });
123 } else {
124 preferenceCategory.removePreference(pHelp);
125 }
126
127 }
128
129
130 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
131 Preference pRecommend = findPreference("recommend");
132 if (pRecommend != null){
133 if (recommendEnabled) {
134 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
135 @Override
136 public boolean onPreferenceClick(Preference preference) {
137
138 Intent intent = new Intent(Intent.ACTION_SENDTO);
139 intent.setType("text/plain");
140 //Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
141 String appName = getString(R.string.app_name);
142 //String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
143 //String recommendSubject = String.format(getString(R.string.recommend_subject), username, appName);
144 String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
145 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
146 //String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), username);
147 String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), getString(R.string.url_app_download));
148 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
149
150 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
152 startActivity(intent);
153
154
155 return(true);
156
157 }
158 });
159 } else {
160 preferenceCategory.removePreference(pRecommend);
161 }
162
163 }
164
165 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
166 Preference pFeedback = findPreference("feedback");
167 if (pFeedback != null){
168 if (feedbackEnabled) {
169 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
170 @Override
171 public boolean onPreferenceClick(Preference preference) {
172 String feedbackMail =(String) getText(R.string.mail_feedback);
173 String feedback =(String) getText(R.string.prefs_feedback);
174 Intent intent = new Intent(Intent.ACTION_SENDTO);
175 intent.setType("text/plain");
176 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
177
178 intent.setData(Uri.parse(feedbackMail));
179 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
180 startActivity(intent);
181
182 return true;
183 }
184 });
185 } else {
186 preferenceCategory.removePreference(pFeedback);
187 }
188
189 }
190
191 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
192 Preference pImprint = findPreference("imprint");
193 if (pImprint != null) {
194 if (imprintEnabled) {
195 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
196 @Override
197 public boolean onPreferenceClick(Preference preference) {
198 String imprintWeb = (String) getText(R.string.url_imprint);
199 if (imprintWeb != null && imprintWeb.length() > 0) {
200 Uri uriUrl = Uri.parse(imprintWeb);
201 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
202 startActivity(intent);
203 }
204 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
205 return true;
206 }
207 });
208 } else {
209 preferenceCategory.removePreference(pImprint);
210 }
211 }
212
213 /* About App */
214 pAboutApp = (Preference) findPreference("about_app");
215 if (pAboutApp != null) {
216 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
217 PackageInfo pkg;
218 try {
219 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
220 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
221 } catch (NameNotFoundException e) {
222 Log_OC.e(TAG, "Error while showing about dialog", e);
223 }
224 }
225
226 /* DISABLED FOR RELEASE UNTIL FIXED
227 pLogging = (CheckBoxPreference) findPreference("log_to_file");
228 if (pLogging != null) {
229 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
230 @Override
231 public boolean onPreferenceChange(Preference preference, Object newValue) {
232
233 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
234
235 if(!pLogging.isChecked()) {
236 Log_OC.d("Debug", "start logging");
237 Log_OC.v("PATH", logpath);
238 Log_OC.startLogging(logpath);
239 }
240 else {
241 Log_OC.d("Debug", "stop logging");
242 Log_OC.stopLogging();
243 }
244 return true;
245 }
246 });
247 }
248
249 pLoggingHistory = (Preference) findPreference("log_history");
250 if (pLoggingHistory != null) {
251 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
252
253 @Override
254 public boolean onPreferenceClick(Preference preference) {
255 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
256 startActivity(intent);
257 return true;
258 }
259 });
260 }
261 */
262
263 }
264
265 @Override
266 protected void onResume() {
267 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
268 boolean state = appPrefs.getBoolean("set_pincode", false);
269 pCode.setChecked(state);
270 super.onResume();
271 }
272
273 @Override
274 public boolean onCreateOptionsMenu(Menu menu) {
275 super.onCreateOptionsMenu(menu);
276 return true;
277 }
278
279 @Override
280 public boolean onMenuItemSelected(int featureId, MenuItem item) {
281 super.onMenuItemSelected(featureId, item);
282 Intent intent;
283
284 switch (item.getItemId()) {
285 case android.R.id.home:
286 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
287 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
288 startActivity(intent);
289 break;
290 default:
291 Log_OC.w(TAG, "Unknown menu item triggered");
292 return false;
293 }
294 return true;
295 }
296
297 @Override
298 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
299 super.onActivityResult(requestCode, resultCode, data);
300 }
301
302 @Override
303 protected void onDestroy() {
304 mDbHandler.close();
305 super.onDestroy();
306 }
307
308 }