01a21ec61a281512c6e3d84491858a7b41d88cd4
[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
83 private void populateSessionList() {
84 mSessions.clear();
85 mSessions = mDbHandler.getSessionList();
86 PreferenceScreen ps = getPreferenceScreen();
87 ps.removeAll();
88 addPreferencesFromResource(R.xml.preferences);
89 for (int i = 0; i < mSessions.size(); i++) {
90 Preference preference = new Preference(getBaseContext());
91 preference.setTitle(mSessions.get(i).getName());
92 URI uri;
93 try {
94 uri = new URI(mSessions.get(i).getUrl());
95 } catch (URISyntaxException e) {
96 e.printStackTrace(); // should never happen
97 continue;
98 }
99 preference.setSummary(uri.getScheme() + "://" + uri.getHost()
100 + uri.getPath());
101 ps.addPreference(preference);
102 }
103 }
104
105 /**
106 * Populates the account selector
107 */
108 private void populateAccountList() {
109 AccountManager accMan = AccountManager.get(this);
110 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
111 mAccountList = (ListPreference) findPreference("select_oc_account");
112 mAccountList.setOnPreferenceChangeListener(this);
113
114 // Display the name of the current account if there is any
115 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
116 if (defaultAccount != null) {
117 mAccountList.setSummary(defaultAccount.name);
118 }
119
120 // Transform accounts into array of string for preferences to use
121 String[] accNames = new String[mAccounts.length];
122 for (int i = 0; i < mAccounts.length; i++) {
123 Account account = mAccounts[i];
124 accNames[i] = account.name;
125 }
126
127 mAccountList.setEntries(accNames);
128 mAccountList.setEntryValues(accNames);
129 }
130
131 @Override
132 public boolean onCreateOptionsMenu(Menu menu) {
133 super.onCreateOptionsMenu(menu);
134 //MenuInflater inflater = getSherlock().getMenuInflater();
135 //inflater.inflate(R.menu.prefs_menu, menu);
136 return true;
137 }
138
139 @Override
140 public boolean onMenuItemSelected(int featureId, MenuItem item) {
141 super.onMenuItemSelected(featureId, item);
142 Intent intent;
143
144 switch (item.getItemId()) {
145 //case R.id.addSessionItem:
146 case 1:
147 intent = new Intent(this, PreferencesNewSession.class);
148 startActivityForResult(intent, mNewSession);
149 break;
150 case R.id.SessionContextEdit:
151 intent = new Intent(this, PreferencesNewSession.class);
152 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
153 .getEntryId());
154 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
155 .getName());
156 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
157 .getUrl());
158 startActivityForResult(intent, mEditSession);
159 break;
160 case R.id.SessionContextRemove:
161 OwnCloudSession ocs = mSessions.get(mSelectedMenuItem);
162 mDbHandler.removeSessionWithId(ocs.getEntryId());
163 mSessions.remove(ocs);
164 getPreferenceScreen().removePreference(
165 getPreferenceScreen().getPreference(mSelectedMenuItem + 1));
166 break;
167 case android.R.id.home:
168 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
169 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
170 startActivity(intent);
171 break;
172 default:
173 Log.w(TAG, "Unknown menu item triggered");
174 return false;
175 }
176 return true;
177 }
178
179 @Override
180 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
181 super.onActivityResult(requestCode, resultCode, data);
182 if (resultCode == Activity.RESULT_OK) {
183 switch (requestCode) {
184 case mNewSession:
185 mDbHandler.addSession(data.getStringExtra("sessionName"),
186 data.getStringExtra("sessionURL"));
187 getPreferenceScreen().removeAll();
188 addPreferencesFromResource(R.xml.preferences);
189 populateSessionList();
190 break;
191 case mEditSession:
192 mDbHandler.changeSessionFields(
193 data.getIntExtra("sessionId", -1),
194 data.getStringExtra("sessionName"),
195 data.getStringExtra("sessionURL"));
196 populateSessionList();
197 break;
198 }
199 }
200 }
201
202 @Override
203 public void onCreateContextMenu(ContextMenu menu, View v,
204 ContextMenuInfo menuInfo) {
205 super.onCreateContextMenu(menu, v, menuInfo);
206 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
207 mSelectedMenuItem = info.position - 1;
208 menu.setHeaderTitle(mSessions.get(mSelectedMenuItem).getName());
209
210 MenuInflater inflater = getSherlock().getMenuInflater();
211 inflater.inflate(R.menu.session_context_menu, (Menu) menu);
212
213 }
214
215 @Override
216 protected void onDestroy() {
217 mDbHandler.close();
218 super.onDestroy();
219 }
220
221 @Override
222 /**
223 * Updates various summaries after updates. Also starts and stops
224 * the
225 */
226 public boolean onPreferenceChange(Preference preference, Object newValue) {
227 // Update current account summary
228 if (preference.equals(mAccountList)) {
229 mAccountList.setSummary(newValue.toString());
230 }
231
232 // Update tracking interval summary
233 else if (preference.equals(mTrackingUpdateInterval)) {
234 String trackingSummary = getResources().getString(
235 R.string.prefs_trackmydevice_interval_summary);
236 trackingSummary = String.format(trackingSummary,
237 newValue.toString());
238 mTrackingUpdateInterval.setSummary(trackingSummary);
239 }
240
241 // Start or stop tracking service
242 else if (preference.equals(mDeviceTracking)) {
243 Intent locationServiceIntent = new Intent();
244 locationServiceIntent
245 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
246 locationServiceIntent.putExtra("TRACKING_SETTING",
247 (Boolean) newValue);
248 sendBroadcast(locationServiceIntent);
249 }
250 return true;
251 }
252
253 }