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