a374b2424c7272dd59cb07f2e6e17cc30064664b
[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.OwnCloudSession;
47 import eu.alefzero.owncloud.R;
48 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
49 import eu.alefzero.owncloud.authenticator.AuthUtils;
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 = AuthUtils.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 intent = new Intent(this, PreferencesNewSession.class);
162 startActivityForResult(intent, mNewSession);
163 break;
164 case R.id.SessionContextEdit:
165 intent = new Intent(this, PreferencesNewSession.class);
166 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
167 .getEntryId());
168 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
169 .getName());
170 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
171 .getUrl());
172 startActivityForResult(intent, mEditSession);
173 break;
174 case R.id.SessionContextRemove:
175 OwnCloudSession ocs = mSessions.get(mSelectedMenuItem);
176 mDbHandler.removeSessionWithId(ocs.getEntryId());
177 mSessions.remove(ocs);
178 getPreferenceScreen().removePreference(
179 getPreferenceScreen().getPreference(mSelectedMenuItem + 1));
180 break;
181 case android.R.id.home:
182 intent = new Intent(getBaseContext(), LandingActivity.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 if (resultCode == Activity.RESULT_OK) {
197 switch (requestCode) {
198 case mNewSession:
199 mDbHandler.addSession(data.getStringExtra("sessionName"),
200 data.getStringExtra("sessionURL"));
201 getPreferenceScreen().removeAll();
202 addPreferencesFromResource(R.xml.preferences);
203 populateSessionList();
204 break;
205 case mEditSession:
206 mDbHandler.changeSessionFields(
207 data.getIntExtra("sessionId", -1),
208 data.getStringExtra("sessionName"),
209 data.getStringExtra("sessionURL"));
210 populateSessionList();
211 break;
212 }
213 }
214 }
215
216 @Override
217 public void onCreateContextMenu(ContextMenu menu, View v,
218 ContextMenuInfo menuInfo) {
219 super.onCreateContextMenu(menu, v, menuInfo);
220 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
221 mSelectedMenuItem = info.position - 1;
222 menu.setHeaderTitle(mSessions.get(mSelectedMenuItem).getName());
223
224 MenuInflater inflater = getSherlock().getMenuInflater();
225 inflater.inflate(R.menu.session_context_menu, (Menu) menu);
226
227 }
228
229 @Override
230 protected void onDestroy() {
231 mDbHandler.close();
232 super.onDestroy();
233 }
234
235 @Override
236 /**
237 * Updates various summaries after updates. Also starts and stops
238 * the
239 */
240 public boolean onPreferenceChange(Preference preference, Object newValue) {
241 // Update current account summary
242 if (preference.equals(mAccountList)) {
243 mAccountList.setSummary(newValue.toString());
244 }
245
246 // Update tracking interval summary
247 else if (preference.equals(mTrackingUpdateInterval)) {
248 String trackingSummary = getResources().getString(
249 R.string.prefs_trackmydevice_interval_summary);
250 trackingSummary = String.format(trackingSummary,
251 newValue.toString());
252 mTrackingUpdateInterval.setSummary(trackingSummary);
253 }
254
255 // Start or stop tracking service
256 else if (preference.equals(mDeviceTracking)) {
257 Intent locationServiceIntent = new Intent();
258 locationServiceIntent.setAction("eu.alefzero.owncloud.location.LocationLauncher");
259 locationServiceIntent.putExtra("TRACKING_SETTING", (Boolean) newValue);
260 sendBroadcast(locationServiceIntent);
261 }
262 return true;
263 }
264
265 }