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