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
.db
.DbHandler
;
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
;
44 * An Activity that allows the user to change the application's settings.
45 * @author Bartek Przybylski
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
;
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();
66 private void populateSessionList() {
68 mSessions
= mDbHandler
.getSessionList();
69 PreferenceScreen ps
= getPreferenceScreen();
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());
77 uri
= new URI(mSessions
.get(i
).getUrl());
78 } catch (URISyntaxException e
) {
79 e
.printStackTrace(); // should never happend
82 preference
.setSummary(uri
.getScheme() + "://" + uri
.getHost()+uri
.getPath());
83 ps
.addPreference(preference
);
88 public boolean onCreateOptionsMenu(Menu menu
) {
89 super.onCreateOptionsMenu(menu
);
90 MenuInflater inflater
= getMenuInflater();
91 inflater
.inflate(R
.menu
.prefs_menu
, menu
);
96 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
97 super.onMenuItemSelected(featureId
, item
);
100 switch (item
.getItemId()) {
101 case R
.id
.addSessionItem
:
102 intent
= new Intent(this, PreferencesNewSession
.class);
103 startActivityForResult(intent
, mNewSession
);
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
);
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));
119 Log
.w(TAG
, "Unknown menu item triggered");
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
) {
131 mDbHandler
.addSession(data
.getStringExtra("sessionName"),
132 data
.getStringExtra("sessionURL"));
133 getPreferenceScreen().removeAll();
134 addPreferencesFromResource(R
.xml
.preferences
);
135 populateSessionList();
138 mDbHandler
.changeSessionFields(data
.getIntExtra("sessionId", -1),
139 data
.getStringExtra("sessionName"),
140 data
.getStringExtra("sessionURL"));
141 populateSessionList();
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());
154 MenuInflater inflater
= getMenuInflater();
155 inflater
.inflate(R
.menu
.session_context_menu
, menu
);
160 protected void onDestroy() {