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
;
20 import java
.util
.Vector
;
22 import android
.accounts
.Account
;
23 import android
.accounts
.AccountManager
;
24 import android
.content
.Intent
;
25 import android
.os
.Bundle
;
26 import android
.preference
.CheckBoxPreference
;
27 import android
.preference
.ListPreference
;
28 import android
.preference
.Preference
;
29 import android
.preference
.Preference
.OnPreferenceChangeListener
;
30 import android
.preference
.Preference
.OnPreferenceClickListener
;
31 import android
.util
.Log
;
32 import android
.view
.ContextMenu
;
33 import android
.view
.ContextMenu
.ContextMenuInfo
;
34 import android
.view
.View
;
35 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
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
.AccountUtils
;
44 import eu
.alefzero
.owncloud
.OwnCloudSession
;
45 import eu
.alefzero
.owncloud
.R
;
46 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
47 import eu
.alefzero
.owncloud
.db
.DbHandler
;
50 * An Activity that allows the user to change the application's settings.
52 * @author Bartek Przybylski
55 public class Preferences
extends SherlockPreferenceActivity
implements
56 OnPreferenceChangeListener
{
57 private static final String TAG
= "OwnCloudPreferences";
58 private final int mNewSession
= 47;
59 private final int mEditSession
= 48;
60 private DbHandler mDbHandler
;
61 private Vector
<OwnCloudSession
> mSessions
;
62 private Account
[] mAccounts
;
63 private ListPreference mAccountList
;
64 private ListPreference mTrackingUpdateInterval
;
65 private CheckBoxPreference mDeviceTracking
;
66 private int mSelectedMenuItem
;
69 public void onCreate(Bundle savedInstanceState
) {
70 super.onCreate(savedInstanceState
);
71 mDbHandler
= new DbHandler(getBaseContext());
72 mSessions
= new Vector
<OwnCloudSession
>();
73 addPreferencesFromResource(R
.xml
.preferences
);
74 registerForContextMenu(getListView());
75 populateAccountList();
76 ActionBar actionBar
= getSherlock().getActionBar();
77 actionBar
.setDisplayHomeAsUpEnabled(true
);
78 Preference p
= findPreference("manage_account");
80 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
82 public boolean onPreferenceClick(Preference preference
) {
83 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
91 * Populates the account selector
93 private void populateAccountList() {
94 AccountManager accMan
= AccountManager
.get(this);
95 mAccounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
96 mAccountList
= (ListPreference
) findPreference("select_oc_account");
97 mAccountList
.setOnPreferenceChangeListener(this);
99 // Display the name of the current account if there is any
100 Account defaultAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
101 if (defaultAccount
!= null
) {
102 mAccountList
.setSummary(defaultAccount
.name
);
105 // Transform accounts into array of string for preferences to use
106 String
[] accNames
= new String
[mAccounts
.length
];
107 for (int i
= 0; i
< mAccounts
.length
; i
++) {
108 Account account
= mAccounts
[i
];
109 accNames
[i
] = account
.name
;
112 mAccountList
.setEntries(accNames
);
113 mAccountList
.setEntryValues(accNames
);
117 public boolean onCreateOptionsMenu(Menu menu
) {
118 super.onCreateOptionsMenu(menu
);
119 //MenuInflater inflater = getSherlock().getMenuInflater();
120 //inflater.inflate(R.menu.prefs_menu, menu);
125 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
126 super.onMenuItemSelected(featureId
, item
);
129 switch (item
.getItemId()) {
130 //case R.id.addSessionItem:
132 intent
= new Intent(this, PreferencesNewSession
.class);
133 startActivityForResult(intent
, mNewSession
);
135 case R
.id
.SessionContextEdit
:
136 intent
= new Intent(this, PreferencesNewSession
.class);
137 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
139 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
141 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
143 startActivityForResult(intent
, mEditSession
);
145 case android
.R
.id
.home
:
146 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
147 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
148 startActivity(intent
);
151 Log
.w(TAG
, "Unknown menu item triggered");
158 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
159 super.onActivityResult(requestCode
, resultCode
, data
);
163 public void onCreateContextMenu(ContextMenu menu
, View v
,
164 ContextMenuInfo menuInfo
) {
165 super.onCreateContextMenu(menu
, v
, menuInfo
);
166 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
167 mSelectedMenuItem
= info
.position
- 1;
168 menu
.setHeaderTitle(mSessions
.get(mSelectedMenuItem
).getName());
170 MenuInflater inflater
= getSherlock().getMenuInflater();
171 inflater
.inflate(R
.menu
.session_context_menu
, (Menu
) menu
);
176 protected void onDestroy() {
183 * Updates various summaries after updates. Also starts and stops
186 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
187 // Update current account summary
188 if (preference
.equals(mAccountList
)) {
189 mAccountList
.setSummary(newValue
.toString());
192 // Update tracking interval summary
193 else if (preference
.equals(mTrackingUpdateInterval
)) {
194 String trackingSummary
= getResources().getString(
195 R
.string
.prefs_trackmydevice_interval_summary
);
196 trackingSummary
= String
.format(trackingSummary
,
197 newValue
.toString());
198 mTrackingUpdateInterval
.setSummary(trackingSummary
);
201 // Start or stop tracking service
202 else if (preference
.equals(mDeviceTracking
)) {
203 Intent locationServiceIntent
= new Intent();
204 locationServiceIntent
205 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
206 locationServiceIntent
.putExtra("TRACKING_SETTING",
208 sendBroadcast(locationServiceIntent
);