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