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
.CheckBoxPreference
;
30 import android
.preference
.ListPreference
;
31 import android
.preference
.Preference
;
32 import android
.preference
.Preference
.OnPreferenceChangeListener
;
33 import android
.preference
.PreferenceScreen
;
34 import android
.util
.Log
;
35 import android
.view
.ContextMenu
;
36 import android
.view
.ContextMenu
.ContextMenuInfo
;
37 import android
.view
.View
;
38 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
40 import com
.actionbarsherlock
.app
.ActionBar
;
41 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
42 import com
.actionbarsherlock
.view
.Menu
;
43 import com
.actionbarsherlock
.view
.MenuInflater
;
44 import com
.actionbarsherlock
.view
.MenuItem
;
46 import eu
.alefzero
.owncloud
.AccountUtils
;
47 import eu
.alefzero
.owncloud
.OwnCloudSession
;
48 import eu
.alefzero
.owncloud
.R
;
49 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
50 import eu
.alefzero
.owncloud
.db
.DbHandler
;
53 * An Activity that allows the user to change the application's settings.
55 * @author Bartek Przybylski
58 public class Preferences
extends SherlockPreferenceActivity
implements
59 OnPreferenceChangeListener
{
60 private static final String TAG
= "OwnCloudPreferences";
61 private final int mNewSession
= 47;
62 private final int mEditSession
= 48;
63 private DbHandler mDbHandler
;
64 private Vector
<OwnCloudSession
> mSessions
;
65 private Account
[] mAccounts
;
66 private ListPreference mAccountList
;
67 private ListPreference mTrackingUpdateInterval
;
68 private CheckBoxPreference mDeviceTracking
;
69 private int mSelectedMenuItem
;
72 public void onCreate(Bundle savedInstanceState
) {
73 super.onCreate(savedInstanceState
);
74 mDbHandler
= new DbHandler(getBaseContext());
75 mSessions
= new Vector
<OwnCloudSession
>();
76 addPreferencesFromResource(R
.xml
.preferences
);
77 registerForContextMenu(getListView());
78 populateAccountList();
79 ActionBar actionBar
= getSherlock().getActionBar();
80 actionBar
.setDisplayHomeAsUpEnabled(true
);
83 private void populateSessionList() {
85 mSessions
= mDbHandler
.getSessionList();
86 PreferenceScreen ps
= getPreferenceScreen();
88 addPreferencesFromResource(R
.xml
.preferences
);
89 for (int i
= 0; i
< mSessions
.size(); i
++) {
90 Preference preference
= new Preference(getBaseContext());
91 preference
.setTitle(mSessions
.get(i
).getName());
94 uri
= new URI(mSessions
.get(i
).getUrl());
95 } catch (URISyntaxException e
) {
96 e
.printStackTrace(); // should never happen
99 preference
.setSummary(uri
.getScheme() + "://" + uri
.getHost()
101 ps
.addPreference(preference
);
106 * Populates the account selector
108 private void populateAccountList() {
109 AccountManager accMan
= AccountManager
.get(this);
110 mAccounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
111 mAccountList
= (ListPreference
) findPreference("select_oc_account");
112 mAccountList
.setOnPreferenceChangeListener(this);
114 // Display the name of the current account if there is any
115 Account defaultAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
116 if (defaultAccount
!= null
) {
117 mAccountList
.setSummary(defaultAccount
.name
);
120 // Transform accounts into array of string for preferences to use
121 String
[] accNames
= new String
[mAccounts
.length
];
122 for (int i
= 0; i
< mAccounts
.length
; i
++) {
123 Account account
= mAccounts
[i
];
124 accNames
[i
] = account
.name
;
127 mAccountList
.setEntries(accNames
);
128 mAccountList
.setEntryValues(accNames
);
132 public boolean onCreateOptionsMenu(Menu menu
) {
133 super.onCreateOptionsMenu(menu
);
134 //MenuInflater inflater = getSherlock().getMenuInflater();
135 //inflater.inflate(R.menu.prefs_menu, menu);
140 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
141 super.onMenuItemSelected(featureId
, item
);
144 switch (item
.getItemId()) {
145 //case R.id.addSessionItem:
147 intent
= new Intent(this, PreferencesNewSession
.class);
148 startActivityForResult(intent
, mNewSession
);
150 case R
.id
.SessionContextEdit
:
151 intent
= new Intent(this, PreferencesNewSession
.class);
152 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
154 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
156 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
158 startActivityForResult(intent
, mEditSession
);
160 case R
.id
.SessionContextRemove
:
161 OwnCloudSession ocs
= mSessions
.get(mSelectedMenuItem
);
162 mDbHandler
.removeSessionWithId(ocs
.getEntryId());
163 mSessions
.remove(ocs
);
164 getPreferenceScreen().removePreference(
165 getPreferenceScreen().getPreference(mSelectedMenuItem
+ 1));
167 case android
.R
.id
.home
:
168 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
169 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
170 startActivity(intent
);
173 Log
.w(TAG
, "Unknown menu item triggered");
180 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
181 super.onActivityResult(requestCode
, resultCode
, data
);
182 if (resultCode
== Activity
.RESULT_OK
) {
183 switch (requestCode
) {
185 mDbHandler
.addSession(data
.getStringExtra("sessionName"),
186 data
.getStringExtra("sessionURL"));
187 getPreferenceScreen().removeAll();
188 addPreferencesFromResource(R
.xml
.preferences
);
189 populateSessionList();
192 mDbHandler
.changeSessionFields(
193 data
.getIntExtra("sessionId", -1),
194 data
.getStringExtra("sessionName"),
195 data
.getStringExtra("sessionURL"));
196 populateSessionList();
203 public void onCreateContextMenu(ContextMenu menu
, View v
,
204 ContextMenuInfo menuInfo
) {
205 super.onCreateContextMenu(menu
, v
, menuInfo
);
206 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
207 mSelectedMenuItem
= info
.position
- 1;
208 menu
.setHeaderTitle(mSessions
.get(mSelectedMenuItem
).getName());
210 MenuInflater inflater
= getSherlock().getMenuInflater();
211 inflater
.inflate(R
.menu
.session_context_menu
, (Menu
) menu
);
216 protected void onDestroy() {
223 * Updates various summaries after updates. Also starts and stops
226 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
227 // Update current account summary
228 if (preference
.equals(mAccountList
)) {
229 mAccountList
.setSummary(newValue
.toString());
232 // Update tracking interval summary
233 else if (preference
.equals(mTrackingUpdateInterval
)) {
234 String trackingSummary
= getResources().getString(
235 R
.string
.prefs_trackmydevice_interval_summary
);
236 trackingSummary
= String
.format(trackingSummary
,
237 newValue
.toString());
238 mTrackingUpdateInterval
.setSummary(trackingSummary
);
241 // Start or stop tracking service
242 else if (preference
.equals(mDeviceTracking
)) {
243 Intent locationServiceIntent
= new Intent();
244 locationServiceIntent
245 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
246 locationServiceIntent
.putExtra("TRACKING_SETTING",
248 sendBroadcast(locationServiceIntent
);