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