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