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
.OwnCloudSession
;
47 import eu
.alefzero
.owncloud
.R
;
48 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
49 import eu
.alefzero
.owncloud
.authenticator
.AuthUtils
;
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
);
82 // Update summary for device tracking preference
83 mTrackingUpdateInterval
= (ListPreference
) findPreference("devicetracking_update_intervall");
84 String trackingSummary
= getResources().getString(
85 R
.string
.prefs_trackmydevice_interval_summary
);
86 trackingSummary
= String
.format(trackingSummary
,
87 mTrackingUpdateInterval
.getValue());
88 mTrackingUpdateInterval
.setSummary(trackingSummary
);
89 mTrackingUpdateInterval
.setOnPreferenceChangeListener(this);
91 // Enable or disable device tracking service. Listen on events
92 mDeviceTracking
= (CheckBoxPreference
) findPreference("enable_devicetracking");
93 mDeviceTracking
.setOnPreferenceChangeListener(this);
95 // populateSessionList();
98 private void populateSessionList() {
100 mSessions
= mDbHandler
.getSessionList();
101 PreferenceScreen ps
= getPreferenceScreen();
103 addPreferencesFromResource(R
.xml
.preferences
);
104 for (int i
= 0; i
< mSessions
.size(); i
++) {
105 Preference preference
= new Preference(getBaseContext());
106 preference
.setTitle(mSessions
.get(i
).getName());
109 uri
= new URI(mSessions
.get(i
).getUrl());
110 } catch (URISyntaxException e
) {
111 e
.printStackTrace(); // should never happen
114 preference
.setSummary(uri
.getScheme() + "://" + uri
.getHost()
116 ps
.addPreference(preference
);
121 * Populates the account selector
123 private void populateAccountList() {
124 AccountManager accMan
= AccountManager
.get(this);
125 mAccounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
126 mAccountList
= (ListPreference
) findPreference("select_oc_account");
127 mAccountList
.setOnPreferenceChangeListener(this);
129 // Display the name of the current account if there is any
130 Account defaultAccount
= AuthUtils
.getCurrentOwnCloudAccount(this);
131 if (defaultAccount
!= null
) {
132 mAccountList
.setSummary(defaultAccount
.name
);
135 // Transform accounts into array of string for preferences to use
136 String
[] accNames
= new String
[mAccounts
.length
];
137 for (int i
= 0; i
< mAccounts
.length
; i
++) {
138 Account account
= mAccounts
[i
];
139 accNames
[i
] = account
.name
;
142 mAccountList
.setEntries(accNames
);
143 mAccountList
.setEntryValues(accNames
);
147 public boolean onCreateOptionsMenu(Menu menu
) {
148 super.onCreateOptionsMenu(menu
);
149 MenuInflater inflater
= getSherlock().getMenuInflater();
150 inflater
.inflate(R
.menu
.prefs_menu
, menu
);
155 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
156 super.onMenuItemSelected(featureId
, item
);
159 switch (item
.getItemId()) {
160 case R
.id
.addSessionItem
:
161 intent
= new Intent(this, PreferencesNewSession
.class);
162 startActivityForResult(intent
, mNewSession
);
164 case R
.id
.SessionContextEdit
:
165 intent
= new Intent(this, PreferencesNewSession
.class);
166 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
168 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
170 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
172 startActivityForResult(intent
, mEditSession
);
174 case R
.id
.SessionContextRemove
:
175 OwnCloudSession ocs
= mSessions
.get(mSelectedMenuItem
);
176 mDbHandler
.removeSessionWithId(ocs
.getEntryId());
177 mSessions
.remove(ocs
);
178 getPreferenceScreen().removePreference(
179 getPreferenceScreen().getPreference(mSelectedMenuItem
+ 1));
181 case android
.R
.id
.home
:
182 intent
= new Intent(getBaseContext(), LandingActivity
.class);
183 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
184 startActivity(intent
);
187 Log
.w(TAG
, "Unknown menu item triggered");
194 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
195 super.onActivityResult(requestCode
, resultCode
, data
);
196 if (resultCode
== Activity
.RESULT_OK
) {
197 switch (requestCode
) {
199 mDbHandler
.addSession(data
.getStringExtra("sessionName"),
200 data
.getStringExtra("sessionURL"));
201 getPreferenceScreen().removeAll();
202 addPreferencesFromResource(R
.xml
.preferences
);
203 populateSessionList();
206 mDbHandler
.changeSessionFields(
207 data
.getIntExtra("sessionId", -1),
208 data
.getStringExtra("sessionName"),
209 data
.getStringExtra("sessionURL"));
210 populateSessionList();
217 public void onCreateContextMenu(ContextMenu menu
, View v
,
218 ContextMenuInfo menuInfo
) {
219 super.onCreateContextMenu(menu
, v
, menuInfo
);
220 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
221 mSelectedMenuItem
= info
.position
- 1;
222 menu
.setHeaderTitle(mSessions
.get(mSelectedMenuItem
).getName());
224 MenuInflater inflater
= getSherlock().getMenuInflater();
225 inflater
.inflate(R
.menu
.session_context_menu
, (Menu
) menu
);
230 protected void onDestroy() {
237 * Updates various summaries after updates. Also starts and stops
240 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
241 // Update current account summary
242 if (preference
.equals(mAccountList
)) {
243 mAccountList
.setSummary(newValue
.toString());
246 // Update tracking interval summary
247 else if (preference
.equals(mTrackingUpdateInterval
)) {
248 String trackingSummary
= getResources().getString(
249 R
.string
.prefs_trackmydevice_interval_summary
);
250 trackingSummary
= String
.format(trackingSummary
,
251 newValue
.toString());
252 mTrackingUpdateInterval
.setSummary(trackingSummary
);
255 // Start or stop tracking service
256 else if (preference
.equals(mDeviceTracking
)) {
257 Intent locationServiceIntent
= new Intent();
258 locationServiceIntent
.setAction("eu.alefzero.owncloud.location.LocationLauncher");
259 locationServiceIntent
.putExtra("TRACKING_SETTING", (Boolean
) newValue
);
260 sendBroadcast(locationServiceIntent
);