Merge branch 'develop'after SPRINT-002
[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.io.File;
21 import java.util.Vector;
22
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.content.pm.PackageInfo;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.os.Bundle;
28 import android.os.Environment;
29 import android.preference.CheckBoxPreference;
30 import android.preference.ListPreference;
31 import android.preference.Preference;
32 import android.preference.Preference.OnPreferenceChangeListener;
33 import android.preference.Preference.OnPreferenceClickListener;
34 import android.preference.PreferenceManager;
35
36 import com.actionbarsherlock.app.ActionBar;
37 import com.actionbarsherlock.app.SherlockPreferenceActivity;
38 import com.actionbarsherlock.view.Menu;
39 import com.actionbarsherlock.view.MenuItem;
40 import com.owncloud.android.Log_OC;
41 import com.owncloud.android.OwnCloudSession;
42 import com.owncloud.android.R;
43 import com.owncloud.android.db.DbHandler;
44
45 /**
46 * An Activity that allows the user to change the application's settings.
47 *
48 * @author Bartek Przybylski
49 *
50 */
51 public class Preferences extends SherlockPreferenceActivity implements OnPreferenceChangeListener {
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 ListPreference mTrackingUpdateInterval;
59 private CheckBoxPreference mDeviceTracking;
60 private CheckBoxPreference pCode;
61 private CheckBoxPreference pLogging;
62 private Preference pLoggingHistory;
63 private Preference pAboutApp;
64 private int mSelectedMenuItem;
65
66
67 @SuppressWarnings("deprecation")
68 @Override
69 public void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 mDbHandler = new DbHandler(getBaseContext());
72 mSessions = new Vector<OwnCloudSession>();
73 addPreferencesFromResource(R.xml.preferences);
74 //populateAccountList();
75 ActionBar actionBar = getSherlock().getActionBar();
76 actionBar.setDisplayHomeAsUpEnabled(true);
77 Preference p = findPreference("manage_account");
78 if (p != null)
79 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
80 @Override
81 public boolean onPreferenceClick(Preference preference) {
82 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
83 startActivity(i);
84 return true;
85 }
86 });
87
88 pCode = (CheckBoxPreference) findPreference("set_pincode");
89 if (pCode != null){
90 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
91 @Override
92 public boolean onPreferenceChange(Preference preference, Object newValue) {
93 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
94 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
95 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
96 startActivity(i);
97 return true;
98 }
99 });
100
101 /* About App */
102 pAboutApp = (Preference) findPreference("about_app");
103 if (pAboutApp != null) {
104 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
105 PackageInfo pkg;
106 try {
107 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
108 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
109 } catch (NameNotFoundException e) {
110 Log_OC.e(TAG, "Error while showing about dialog", e);
111 }
112 }
113
114 pLogging = (CheckBoxPreference) findPreference("log_to_file");
115 if (pLogging != null) {
116 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
117 @Override
118 public boolean onPreferenceChange(Preference preference, Object newValue) {
119
120 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
121
122 if(!pLogging.isChecked()) {
123 Log_OC.d("Debug", "start logging");
124 Log_OC.v("PATH", logpath);
125 Log_OC.startLogging(logpath);
126 }
127 else {
128 Log_OC.d("Debug", "stop logging");
129 Log_OC.stopLogging();
130 }
131 return true;
132 }
133 });
134 }
135
136 pLoggingHistory = (Preference) findPreference("log_history");
137 if (pLoggingHistory != null) {
138 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
139
140 @Override
141 public boolean onPreferenceClick(Preference preference) {
142 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
143 startActivity(intent);
144 return true;
145 }
146 });
147 }
148 }
149 }
150
151 @Override
152 protected void onResume() {
153 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
154 boolean state = appPrefs.getBoolean("set_pincode", false);
155 pCode.setChecked(state);
156 super.onResume();
157 }
158
159 @Override
160 public boolean onCreateOptionsMenu(Menu menu) {
161 super.onCreateOptionsMenu(menu);
162 return true;
163 }
164
165 @Override
166 public boolean onMenuItemSelected(int featureId, MenuItem item) {
167 super.onMenuItemSelected(featureId, item);
168 Intent intent;
169
170 switch (item.getItemId()) {
171 //case R.id.addSessionItem:
172 case 1:
173 intent = new Intent(this, PreferencesNewSession.class);
174 startActivityForResult(intent, mNewSession);
175 break;
176 case R.id.SessionContextEdit:
177 intent = new Intent(this, PreferencesNewSession.class);
178 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
179 .getEntryId());
180 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
181 .getName());
182 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
183 .getUrl());
184 startActivityForResult(intent, mEditSession);
185 break;
186 case android.R.id.home:
187 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
188 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
189 startActivity(intent);
190 break;
191 default:
192 Log_OC.w(TAG, "Unknown menu item triggered");
193 return false;
194 }
195 return true;
196 }
197
198 @Override
199 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
200 super.onActivityResult(requestCode, resultCode, data);
201 }
202
203 @Override
204 protected void onDestroy() {
205 mDbHandler.close();
206 super.onDestroy();
207 }
208
209 @Override
210 /**
211 * Updates various summaries after updates. Also starts and stops
212 * the
213 */
214 public boolean onPreferenceChange(Preference preference, Object newValue) {
215 // Update current account summary
216 /*if (preference.equals(mAccountList)) {
217 mAccountList.setSummary(newValue.toString());
218 }
219
220 // Update tracking interval summary
221 else*/ if (preference.equals(mTrackingUpdateInterval)) {
222 String trackingSummary = getResources().getString(
223 R.string.prefs_trackmydevice_interval_summary);
224 trackingSummary = String.format(trackingSummary,
225 newValue.toString());
226 mTrackingUpdateInterval.setSummary(trackingSummary);
227 }
228
229 // Start or stop tracking service
230 else if (preference.equals(mDeviceTracking)) {
231 Intent locationServiceIntent = new Intent();
232 locationServiceIntent
233 .setAction("com.owncloud.android.location.LocationLauncher");
234 locationServiceIntent.putExtra("TRACKING_SETTING",
235 (Boolean) newValue);
236 sendBroadcast(locationServiceIntent);
237 }
238 return true;
239 }
240 }