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