0c17d7cb83ebe884a6959621a75a853499db356f
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 eu.alefzero.owncloud.ui.activity;
19
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.Vector;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.app.Activity;
27 import android.content.Intent;
28 import android.os.Bundle;
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.PreferenceScreen;
34 import android.util.Log;
35 import android.view.ContextMenu;
36 import android.view.ContextMenu.ContextMenuInfo;
37 import android.view.View;
38 import android.widget.AdapterView.AdapterContextMenuInfo;
39
40 import com.actionbarsherlock.app.ActionBar;
41 import com.actionbarsherlock.app.SherlockPreferenceActivity;
42 import com.actionbarsherlock.view.Menu;
43 import com.actionbarsherlock.view.MenuInflater;
44 import com.actionbarsherlock.view.MenuItem;
45
46 import eu.alefzero.owncloud.AccountUtils;
47 import eu.alefzero.owncloud.OwnCloudSession;
48 import eu.alefzero.owncloud.R;
49 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
50 import eu.alefzero.owncloud.db.DbHandler;
51
52 /**
53 * An Activity that allows the user to change the application's settings.
54 *
55 * @author Bartek Przybylski
56 *
57 */
58 public class Preferences extends SherlockPreferenceActivity implements
59 OnPreferenceChangeListener {
60 private static final String TAG = "OwnCloudPreferences";
61 private final int mNewSession = 47;
62 private final int mEditSession = 48;
63 private DbHandler mDbHandler;
64 private Vector<OwnCloudSession> mSessions;
65 private Account[] mAccounts;
66 private ListPreference mAccountList;
67 private ListPreference mTrackingUpdateInterval;
68 private CheckBoxPreference mDeviceTracking;
69 private int mSelectedMenuItem;
70
71 @Override
72 public void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 mDbHandler = new DbHandler(getBaseContext());
75 mSessions = new Vector<OwnCloudSession>();
76 addPreferencesFromResource(R.xml.preferences);
77 registerForContextMenu(getListView());
78 populateAccountList();
79 ActionBar actionBar = getSherlock().getActionBar();
80 actionBar.setDisplayHomeAsUpEnabled(true);
81
82 // Update summary for device tracking preference
83 mTrackingUpdateInterval = (ListPreference) findPreference("devicetracking_update_intervall");
84 String trackingSummary = getResources().getString(
85 R.string.prefs_trackmydevice_interval_summary);
86 trackingSummary = String.format(trackingSummary,
87 mTrackingUpdateInterval.getValue());
88 mTrackingUpdateInterval.setSummary(trackingSummary);
89 mTrackingUpdateInterval.setOnPreferenceChangeListener(this);
90
91 // Enable or disable device tracking service. Listen on events
92 mDeviceTracking = (CheckBoxPreference) findPreference("enable_devicetracking");
93 mDeviceTracking.setOnPreferenceChangeListener(this);
94
95 // populateSessionList();
96 }
97
98 private void populateSessionList() {
99 mSessions.clear();
100 mSessions = mDbHandler.getSessionList();
101 PreferenceScreen ps = getPreferenceScreen();
102 ps.removeAll();
103 addPreferencesFromResource(R.xml.preferences);
104 for (int i = 0; i < mSessions.size(); i++) {
105 Preference preference = new Preference(getBaseContext());
106 preference.setTitle(mSessions.get(i).getName());
107 URI uri;
108 try {
109 uri = new URI(mSessions.get(i).getUrl());
110 } catch (URISyntaxException e) {
111 e.printStackTrace(); // should never happen
112 continue;
113 }
114 preference.setSummary(uri.getScheme() + "://" + uri.getHost()
115 + uri.getPath());
116 ps.addPreference(preference);
117 }
118 }
119
120 /**
121 * Populates the account selector
122 */
123 private void populateAccountList() {
124 AccountManager accMan = AccountManager.get(this);
125 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
126 mAccountList = (ListPreference) findPreference("select_oc_account");
127 mAccountList.setOnPreferenceChangeListener(this);
128
129 // Display the name of the current account if there is any
130 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
131 if (defaultAccount != null) {
132 mAccountList.setSummary(defaultAccount.name);
133 }
134
135 // Transform accounts into array of string for preferences to use
136 String[] accNames = new String[mAccounts.length];
137 for (int i = 0; i < mAccounts.length; i++) {
138 Account account = mAccounts[i];
139 accNames[i] = account.name;
140 }
141
142 mAccountList.setEntries(accNames);
143 mAccountList.setEntryValues(accNames);
144 }
145
146 @Override
147 public boolean onCreateOptionsMenu(Menu menu) {
148 super.onCreateOptionsMenu(menu);
149 //MenuInflater inflater = getSherlock().getMenuInflater();
150 //inflater.inflate(R.menu.prefs_menu, menu);
151 return true;
152 }
153
154 @Override
155 public boolean onMenuItemSelected(int featureId, MenuItem item) {
156 super.onMenuItemSelected(featureId, item);
157 Intent intent;
158
159 switch (item.getItemId()) {
160 //case R.id.addSessionItem:
161 case 1:
162 intent = new Intent(this, PreferencesNewSession.class);
163 startActivityForResult(intent, mNewSession);
164 break;
165 case R.id.SessionContextEdit:
166 intent = new Intent(this, PreferencesNewSession.class);
167 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
168 .getEntryId());
169 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
170 .getName());
171 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
172 .getUrl());
173 startActivityForResult(intent, mEditSession);
174 break;
175 case R.id.SessionContextRemove:
176 OwnCloudSession ocs = mSessions.get(mSelectedMenuItem);
177 mDbHandler.removeSessionWithId(ocs.getEntryId());
178 mSessions.remove(ocs);
179 getPreferenceScreen().removePreference(
180 getPreferenceScreen().getPreference(mSelectedMenuItem + 1));
181 break;
182 case android.R.id.home:
183 intent = new Intent(getBaseContext(), LandingActivity.class);
184 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
185 startActivity(intent);
186 break;
187 default:
188 Log.w(TAG, "Unknown menu item triggered");
189 return false;
190 }
191 return true;
192 }
193
194 @Override
195 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
196 super.onActivityResult(requestCode, resultCode, data);
197 if (resultCode == Activity.RESULT_OK) {
198 switch (requestCode) {
199 case mNewSession:
200 mDbHandler.addSession(data.getStringExtra("sessionName"),
201 data.getStringExtra("sessionURL"));
202 getPreferenceScreen().removeAll();
203 addPreferencesFromResource(R.xml.preferences);
204 populateSessionList();
205 break;
206 case mEditSession:
207 mDbHandler.changeSessionFields(
208 data.getIntExtra("sessionId", -1),
209 data.getStringExtra("sessionName"),
210 data.getStringExtra("sessionURL"));
211 populateSessionList();
212 break;
213 }
214 }
215 }
216
217 @Override
218 public void onCreateContextMenu(ContextMenu menu, View v,
219 ContextMenuInfo menuInfo) {
220 super.onCreateContextMenu(menu, v, menuInfo);
221 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
222 mSelectedMenuItem = info.position - 1;
223 menu.setHeaderTitle(mSessions.get(mSelectedMenuItem).getName());
224
225 MenuInflater inflater = getSherlock().getMenuInflater();
226 inflater.inflate(R.menu.session_context_menu, (Menu) menu);
227
228 }
229
230 @Override
231 protected void onDestroy() {
232 mDbHandler.close();
233 super.onDestroy();
234 }
235
236 @Override
237 /**
238 * Updates various summaries after updates. Also starts and stops
239 * the
240 */
241 public boolean onPreferenceChange(Preference preference, Object newValue) {
242 // Update current account summary
243 if (preference.equals(mAccountList)) {
244 mAccountList.setSummary(newValue.toString());
245 }
246
247 // Update tracking interval summary
248 else if (preference.equals(mTrackingUpdateInterval)) {
249 String trackingSummary = getResources().getString(
250 R.string.prefs_trackmydevice_interval_summary);
251 trackingSummary = String.format(trackingSummary,
252 newValue.toString());
253 mTrackingUpdateInterval.setSummary(trackingSummary);
254 }
255
256 // Start or stop tracking service
257 else if (preference.equals(mDeviceTracking)) {
258 Intent locationServiceIntent = new Intent();
259 locationServiceIntent
260 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
261 locationServiceIntent.putExtra("TRACKING_SETTING",
262 (Boolean) newValue);
263 sendBroadcast(locationServiceIntent);
264 }
265 return true;
266 }
267
268 }