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