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