Update preference description after the user makes a new selection
[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.ListPreference;
30 import android.preference.Preference;
31 import android.preference.Preference.OnPreferenceChangeListener;
32 import android.preference.PreferenceScreen;
33 import android.util.Log;
34 import android.view.ContextMenu;
35 import android.view.ContextMenu.ContextMenuInfo;
36 import android.view.View;
37 import android.widget.AdapterView.AdapterContextMenuInfo;
38
39 import com.actionbarsherlock.app.SherlockPreferenceActivity;
40 import com.actionbarsherlock.view.Menu;
41 import com.actionbarsherlock.view.MenuInflater;
42 import com.actionbarsherlock.view.MenuItem;
43
44 import eu.alefzero.owncloud.OwnCloudSession;
45 import eu.alefzero.owncloud.R;
46 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
47 import eu.alefzero.owncloud.authenticator.AuthUtils;
48 import eu.alefzero.owncloud.db.DbHandler;
49
50 /**
51 * An Activity that allows the user to change the application's settings.
52 * @author Bartek Przybylski
53 *
54 */
55 public class Preferences extends SherlockPreferenceActivity implements OnPreferenceChangeListener {
56 private static final String TAG = "OwnCloudPreferences";
57 private final int mNewSession = 47;
58 private final int mEditSession = 48;
59 private DbHandler mDbHandler;
60 private Vector<OwnCloudSession> mSessions;
61 private Account[] mAccounts;
62 private ListPreference mAccountList;
63 private int mSelectedMenuItem;
64
65 @Override
66 public void onCreate(Bundle savedInstanceState){
67 super.onCreate(savedInstanceState);
68 mDbHandler = new DbHandler(getBaseContext());
69 mSessions = new Vector<OwnCloudSession>();
70 addPreferencesFromResource(R.xml.preferences);
71 registerForContextMenu(getListView());
72 populateAccountList();
73 //populateSessionList();
74 }
75
76 private void populateSessionList() {
77 mSessions.clear();
78 mSessions = mDbHandler.getSessionList();
79 PreferenceScreen ps = getPreferenceScreen();
80 ps.removeAll();
81 addPreferencesFromResource(R.xml.preferences);
82 for (int i = 0; i < mSessions.size(); i++) {
83 Preference preference = new Preference(getBaseContext());
84 preference.setTitle(mSessions.get(i).getName());
85 URI uri;
86 try {
87 uri = new URI(mSessions.get(i).getUrl());
88 } catch (URISyntaxException e) {
89 e.printStackTrace(); // should never happen
90 continue;
91 }
92 preference.setSummary(uri.getScheme() + "://" + uri.getHost()+uri.getPath());
93 ps.addPreference(preference);
94 }
95 }
96
97 /**
98 * Populates the account selector
99 */
100 private void populateAccountList(){
101 AccountManager accMan = AccountManager.get(this);
102 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
103 mAccountList = (ListPreference) findPreference("select_oc_account");
104 mAccountList.setOnPreferenceChangeListener(this);
105
106 // Display the name of the current account if there is any
107 Account defaultAccount = AuthUtils.getCurrentOwnCloudAccount(this);
108 if(defaultAccount != null){
109 mAccountList.setSummary(defaultAccount.name);
110 }
111
112 // Transform accounts into array of string for preferences to use
113 String[] accNames = new String[mAccounts.length];
114 for(int i = 0; i < mAccounts.length; i++){
115 Account account = mAccounts[i];
116 accNames[i] = account.name;
117 }
118
119 mAccountList.setEntries(accNames);
120 mAccountList.setEntryValues(accNames);
121 }
122
123 @Override
124 public boolean onCreateOptionsMenu(Menu menu) {
125 super.onCreateOptionsMenu(menu);
126 MenuInflater inflater = getSherlock().getMenuInflater();
127 inflater.inflate(R.menu.prefs_menu, menu);
128 return true;
129 }
130
131 @Override
132 public boolean onMenuItemSelected(int featureId, MenuItem item) {
133 super.onMenuItemSelected(featureId, item);
134 Intent intent;
135
136 switch (item.getItemId()) {
137 case R.id.addSessionItem:
138 intent = new Intent(this, PreferencesNewSession.class);
139 startActivityForResult(intent, mNewSession);
140 break;
141 case R.id.SessionContextEdit:
142 intent = new Intent(this, PreferencesNewSession.class);
143 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem).getEntryId());
144 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem).getName());
145 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem).getUrl());
146 startActivityForResult(intent, mEditSession);
147 break;
148 case R.id.SessionContextRemove:
149 OwnCloudSession ocs = mSessions.get(mSelectedMenuItem);
150 mDbHandler.removeSessionWithId(ocs.getEntryId());
151 mSessions.remove(ocs);
152 getPreferenceScreen().removePreference(getPreferenceScreen().getPreference(mSelectedMenuItem+1));
153 break;
154 default:
155 Log.w(TAG, "Unknown menu item triggered");
156 return false;
157 }
158 return true;
159 }
160
161 @Override
162 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
163 super.onActivityResult(requestCode, resultCode, data);
164 if (resultCode == Activity.RESULT_OK) {
165 switch (requestCode) {
166 case mNewSession:
167 mDbHandler.addSession(data.getStringExtra("sessionName"),
168 data.getStringExtra("sessionURL"));
169 getPreferenceScreen().removeAll();
170 addPreferencesFromResource(R.xml.preferences);
171 populateSessionList();
172 break;
173 case mEditSession:
174 mDbHandler.changeSessionFields(data.getIntExtra("sessionId", -1),
175 data.getStringExtra("sessionName"),
176 data.getStringExtra("sessionURL"));
177 populateSessionList();
178 break;
179 }
180 }
181 }
182
183 @Override
184 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
185 super.onCreateContextMenu(menu, v, menuInfo);
186 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
187 mSelectedMenuItem = info.position-1;
188 menu.setHeaderTitle(mSessions.get(mSelectedMenuItem).getName());
189
190 MenuInflater inflater = getSherlock().getMenuInflater();
191 inflater.inflate(R.menu.session_context_menu, (Menu) menu);
192
193 }
194
195 @Override
196 protected void onDestroy() {
197 mDbHandler.close();
198 super.onDestroy();
199 }
200
201 @Override
202 /**
203 * Updates the summary of the account selector after a new account has
204 * been selected
205 */
206 public boolean onPreferenceChange(Preference preference, Object newValue) {
207 if(preference.equals(mAccountList)) {
208 mAccountList.setSummary(newValue.toString());
209 }
210 return true;
211 }
212
213 }