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