1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
18 package eu
.alefzero
.owncloud
.ui
.activity
;
21 import java
.net
.URISyntaxException
;
22 import java
.util
.Vector
;
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
;
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
;
47 * An Activity that allows the user to change the application's settings.
48 * @author Bartek Przybylski
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
;
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();
69 private void populateSessionList() {
71 mSessions
= mDbHandler
.getSessionList();
72 PreferenceScreen ps
= getPreferenceScreen();
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());
80 uri
= new URI(mSessions
.get(i
).getUrl());
81 } catch (URISyntaxException e
) {
82 e
.printStackTrace(); // should never happend
85 preference
.setSummary(uri
.getScheme() + "://" + uri
.getHost()+uri
.getPath());
86 ps
.addPreference(preference
);
91 public boolean onCreateOptionsMenu(Menu menu
) {
92 super.onCreateOptionsMenu(menu
);
93 MenuInflater inflater
= getMenuInflater();
94 inflater
.inflate(R
.menu
.prefs_menu
, menu
);
99 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
100 super.onMenuItemSelected(featureId
, item
);
103 switch (item
.getItemId()) {
104 case R
.id
.addSessionItem
:
105 intent
= new Intent(this, PreferencesNewSession
.class);
106 startActivityForResult(intent
, mNewSession
);
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
);
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));
122 Log
.w(TAG
, "Unknown menu item triggered");
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
) {
134 mDbHandler
.addSession(data
.getStringExtra("sessionName"),
135 data
.getStringExtra("sessionURL"));
136 getPreferenceScreen().removeAll();
137 addPreferencesFromResource(R
.xml
.preferences
);
138 populateSessionList();
141 mDbHandler
.changeSessionFields(data
.getIntExtra("sessionId", -1),
142 data
.getStringExtra("sessionName"),
143 data
.getStringExtra("sessionURL"));
144 populateSessionList();
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());
157 MenuInflater inflater
= getMenuInflater();
158 inflater
.inflate(R
.menu
.session_context_menu
, menu
);
163 protected void onDestroy() {