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 android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.app
.Activity
;
27 import android
.content
.Intent
;
28 import android
.os
.Bundle
;
29 import android
.preference
.ListPreference
;
30 import android
.preference
.Preference
;
31 import android
.preference
.PreferenceScreen
;
32 import android
.util
.Log
;
33 import android
.view
.ContextMenu
;
34 import android
.view
.ContextMenu
.ContextMenuInfo
;
35 import android
.view
.View
;
36 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
38 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
39 import com
.actionbarsherlock
.view
.Menu
;
40 import com
.actionbarsherlock
.view
.MenuInflater
;
41 import com
.actionbarsherlock
.view
.MenuItem
;
43 import eu
.alefzero
.owncloud
.OwnCloudSession
;
44 import eu
.alefzero
.owncloud
.R
;
45 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
46 import eu
.alefzero
.owncloud
.authenticator
.AuthUtils
;
47 import eu
.alefzero
.owncloud
.db
.DbHandler
;
50 * An Activity that allows the user to change the application's settings.
51 * @author Bartek Przybylski
54 public class Preferences
extends SherlockPreferenceActivity
{
55 private static final String TAG
= "OwnCloudPreferences";
56 private final int mNewSession
= 47;
57 private final int mEditSession
= 48;
58 private DbHandler mDbHandler
;
59 private Vector
<OwnCloudSession
> mSessions
;
60 private Account
[] mAccounts
;
61 private int mSelectedMenuItem
;
64 public void onCreate(Bundle savedInstanceState
){
65 super.onCreate(savedInstanceState
);
66 mDbHandler
= new DbHandler(getBaseContext());
67 mSessions
= new Vector
<OwnCloudSession
>();
68 addPreferencesFromResource(R
.xml
.preferences
);
69 registerForContextMenu(getListView());
70 populateAccountList();
71 //populateSessionList();
74 private void populateSessionList() {
76 mSessions
= mDbHandler
.getSessionList();
77 PreferenceScreen ps
= getPreferenceScreen();
79 addPreferencesFromResource(R
.xml
.preferences
);
80 for (int i
= 0; i
< mSessions
.size(); i
++) {
81 Preference preference
= new Preference(getBaseContext());
82 preference
.setTitle(mSessions
.get(i
).getName());
85 uri
= new URI(mSessions
.get(i
).getUrl());
86 } catch (URISyntaxException e
) {
87 e
.printStackTrace(); // should never happen
90 preference
.setSummary(uri
.getScheme() + "://" + uri
.getHost()+uri
.getPath());
91 ps
.addPreference(preference
);
96 * Populates the account selector
98 private void populateAccountList(){
99 AccountManager accMan
= AccountManager
.get(this);
100 mAccounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
101 ListPreference accountList
= (ListPreference
) findPreference("select_oc_account");
103 // Display the name of the current account if there is any
104 Account defaultAccount
= AuthUtils
.getCurrentOwnCloudAccount(this);
105 if(defaultAccount
!= null
){
106 accountList
.setSummary(defaultAccount
.name
);
109 // Transform accounts into array of string for preferences to use
110 String
[] accNames
= new String
[mAccounts
.length
];
111 for(int i
= 0; i
< mAccounts
.length
; i
++){
112 Account account
= mAccounts
[i
];
113 accNames
[i
] = account
.name
;
116 accountList
.setEntries(accNames
);
117 accountList
.setEntryValues(accNames
);
121 public boolean onCreateOptionsMenu(Menu menu
) {
122 super.onCreateOptionsMenu(menu
);
123 MenuInflater inflater
= getSherlock().getMenuInflater();
124 inflater
.inflate(R
.menu
.prefs_menu
, menu
);
129 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
130 super.onMenuItemSelected(featureId
, item
);
133 switch (item
.getItemId()) {
134 case R
.id
.addSessionItem
:
135 intent
= new Intent(this, PreferencesNewSession
.class);
136 startActivityForResult(intent
, mNewSession
);
138 case R
.id
.SessionContextEdit
:
139 intent
= new Intent(this, PreferencesNewSession
.class);
140 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
).getEntryId());
141 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
).getName());
142 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
).getUrl());
143 startActivityForResult(intent
, mEditSession
);
145 case R
.id
.SessionContextRemove
:
146 OwnCloudSession ocs
= mSessions
.get(mSelectedMenuItem
);
147 mDbHandler
.removeSessionWithId(ocs
.getEntryId());
148 mSessions
.remove(ocs
);
149 getPreferenceScreen().removePreference(getPreferenceScreen().getPreference(mSelectedMenuItem
+1));
152 Log
.w(TAG
, "Unknown menu item triggered");
159 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
160 super.onActivityResult(requestCode
, resultCode
, data
);
161 if (resultCode
== Activity
.RESULT_OK
) {
162 switch (requestCode
) {
164 mDbHandler
.addSession(data
.getStringExtra("sessionName"),
165 data
.getStringExtra("sessionURL"));
166 getPreferenceScreen().removeAll();
167 addPreferencesFromResource(R
.xml
.preferences
);
168 populateSessionList();
171 mDbHandler
.changeSessionFields(data
.getIntExtra("sessionId", -1),
172 data
.getStringExtra("sessionName"),
173 data
.getStringExtra("sessionURL"));
174 populateSessionList();
181 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) {
182 super.onCreateContextMenu(menu
, v
, menuInfo
);
183 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
184 mSelectedMenuItem
= info
.position
-1;
185 menu
.setHeaderTitle(mSessions
.get(mSelectedMenuItem
).getName());
187 MenuInflater inflater
= getSherlock().getMenuInflater();
188 inflater
.inflate(R
.menu
.session_context_menu
, (Menu
) menu
);
193 protected void onDestroy() {