Added notification for failures in manual synchronizations; some improvements in...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Preferences.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.accounts.Account;
23 import android.accounts.AccountManager;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Bundle;
27 import android.preference.CheckBoxPreference;
28 import android.preference.ListPreference;
29 import android.preference.Preference;
30 import android.preference.PreferenceManager;
31 import android.preference.Preference.OnPreferenceChangeListener;
32 import android.preference.Preference.OnPreferenceClickListener;
33 import android.util.Log;
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.AccountUtils;
40 import com.owncloud.android.OwnCloudSession;
41 import com.owncloud.android.authenticator.AccountAuthenticator;
42 import com.owncloud.android.db.DbHandler;
43
44 import com.owncloud.android.R;
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
53 OnPreferenceChangeListener{
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 Account[] mAccounts;
60 //private ListPreference mAccountList;
61 private ListPreference mTrackingUpdateInterval;
62 private CheckBoxPreference mDeviceTracking;
63 private CheckBoxPreference pCode;
64 private int mSelectedMenuItem;
65
66 @Override
67 public void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 mDbHandler = new DbHandler(getBaseContext());
70 mSessions = new Vector<OwnCloudSession>();
71 addPreferencesFromResource(R.xml.preferences);
72 //populateAccountList();
73 ActionBar actionBar = getSherlock().getActionBar();
74 actionBar.setDisplayHomeAsUpEnabled(true);
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
88
89 if (pCode != null){
90
91 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
92 @Override
93 public boolean onPreferenceChange(Preference preference, Object newValue) {
94
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
99 startActivity(i);
100
101 return true;
102 }
103 });
104
105 }
106
107 }
108
109
110 @Override
111 protected void onResume() {
112 // TODO Auto-generated method stub
113 SharedPreferences appPrefs = PreferenceManager
114 .getDefaultSharedPreferences(getApplicationContext());
115
116 boolean state = appPrefs.getBoolean("set_pincode", false);
117 pCode.setChecked(state);
118
119 super.onResume();
120 }
121
122
123
124 /**
125 * Populates the account selector
126 *-/
127 private void populateAccountList() {
128 AccountManager accMan = AccountManager.get(this);
129 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
130 mAccountList = (ListPreference) findPreference("select_oc_account");
131 mAccountList.setOnPreferenceChangeListener(this);
132
133 // Display the name of the current account if there is any
134 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
135 if (defaultAccount != null) {
136 mAccountList.setSummary(defaultAccount.name);
137 }
138
139 // Transform accounts into array of string for preferences to use
140 String[] accNames = new String[mAccounts.length];
141 for (int i = 0; i < mAccounts.length; i++) {
142 Account account = mAccounts[i];
143 accNames[i] = account.name;
144 }
145
146 mAccountList.setEntries(accNames);
147 mAccountList.setEntryValues(accNames);
148 }*/
149
150
151
152 @Override
153 public boolean onCreateOptionsMenu(Menu menu) {
154 super.onCreateOptionsMenu(menu);
155 //MenuInflater inflater = getSherlock().getMenuInflater();
156 //inflater.inflate(R.menu.prefs_menu, menu);
157 return true;
158 }
159
160 @Override
161 public boolean onMenuItemSelected(int featureId, MenuItem item) {
162 super.onMenuItemSelected(featureId, item);
163 Intent intent;
164
165 switch (item.getItemId()) {
166 //case R.id.addSessionItem:
167 case 1:
168 intent = new Intent(this, PreferencesNewSession.class);
169 startActivityForResult(intent, mNewSession);
170 break;
171 case R.id.SessionContextEdit:
172 intent = new Intent(this, PreferencesNewSession.class);
173 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
174 .getEntryId());
175 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
176 .getName());
177 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
178 .getUrl());
179 startActivityForResult(intent, mEditSession);
180 break;
181 case android.R.id.home:
182 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
183 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
184 startActivity(intent);
185 break;
186 default:
187 Log.w(TAG, "Unknown menu item triggered");
188 return false;
189 }
190 return true;
191 }
192
193 @Override
194 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
195 super.onActivityResult(requestCode, resultCode, data);
196 }
197
198 @Override
199 protected void onDestroy() {
200 mDbHandler.close();
201 super.onDestroy();
202 }
203
204
205
206 @Override
207 /**
208 * Updates various summaries after updates. Also starts and stops
209 * the
210 */
211 public boolean onPreferenceChange(Preference preference, Object newValue) {
212 // Update current account summary
213 /*if (preference.equals(mAccountList)) {
214 mAccountList.setSummary(newValue.toString());
215 }
216
217 // Update tracking interval summary
218 else*/ if (preference.equals(mTrackingUpdateInterval)) {
219 String trackingSummary = getResources().getString(
220 R.string.prefs_trackmydevice_interval_summary);
221 trackingSummary = String.format(trackingSummary,
222 newValue.toString());
223 mTrackingUpdateInterval.setSummary(trackingSummary);
224 }
225
226 // Start or stop tracking service
227 else if (preference.equals(mDeviceTracking)) {
228 Intent locationServiceIntent = new Intent();
229 locationServiceIntent
230 .setAction("com.owncloud.android.location.LocationLauncher");
231 locationServiceIntent.putExtra("TRACKING_SETTING",
232 (Boolean) newValue);
233 sendBroadcast(locationServiceIntent);
234 }
235 return true;
236 }
237
238
239
240 }