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
.content
.SharedPreferences
;
26 import android
.os
.Bundle
;
27 import android
.preference
.CheckBoxPreference
;
28 import android
.preference
.ListPreference
;
29 import android
.preference
.Preference
;
30 import android
.preference
.PreferenceManager
;
31 import android
.preference
.Preference
.OnPreferenceChangeListener
;
32 import android
.preference
.Preference
.OnPreferenceClickListener
;
33 import android
.util
.Log
;
34 import android
.view
.ContextMenu
;
35 import android
.view
.ContextMenu
.ContextMenuInfo
;
36 import android
.view
.View
;
37 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
39 import com
.actionbarsherlock
.app
.ActionBar
;
40 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
41 import com
.actionbarsherlock
.view
.Menu
;
42 import com
.actionbarsherlock
.view
.MenuInflater
;
43 import com
.actionbarsherlock
.view
.MenuItem
;
45 import eu
.alefzero
.owncloud
.AccountUtils
;
46 import eu
.alefzero
.owncloud
.OwnCloudSession
;
47 import eu
.alefzero
.owncloud
.R
;
48 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
49 import eu
.alefzero
.owncloud
.db
.DbHandler
;
50 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncAdapter
;
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 CheckBoxPreference pCode
;
70 private int mSelectedMenuItem
;
73 public void onCreate(Bundle savedInstanceState
) {
74 super.onCreate(savedInstanceState
);
75 mDbHandler
= new DbHandler(getBaseContext());
76 mSessions
= new Vector
<OwnCloudSession
>();
77 addPreferencesFromResource(R
.xml
.preferences
);
78 populateAccountList();
79 ActionBar actionBar
= getSherlock().getActionBar();
80 actionBar
.setDisplayHomeAsUpEnabled(true
);
81 Preference p
= findPreference("manage_account");
83 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
85 public boolean onPreferenceClick(Preference preference
) {
86 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
92 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
97 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
99 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
101 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
102 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
103 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
117 protected void onResume() {
118 // TODO Auto-generated method stub
119 SharedPreferences appPrefs
= PreferenceManager
120 .getDefaultSharedPreferences(getApplicationContext());
122 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
123 pCode
.setChecked(state
);
131 * Populates the account selector
133 private void populateAccountList() {
134 AccountManager accMan
= AccountManager
.get(this);
135 mAccounts
= accMan
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
136 mAccountList
= (ListPreference
) findPreference("select_oc_account");
137 mAccountList
.setOnPreferenceChangeListener(this);
139 // Display the name of the current account if there is any
140 Account defaultAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
141 if (defaultAccount
!= null
) {
142 mAccountList
.setSummary(defaultAccount
.name
);
145 // Transform accounts into array of string for preferences to use
146 String
[] accNames
= new String
[mAccounts
.length
];
147 for (int i
= 0; i
< mAccounts
.length
; i
++) {
148 Account account
= mAccounts
[i
];
149 accNames
[i
] = account
.name
;
152 mAccountList
.setEntries(accNames
);
153 mAccountList
.setEntryValues(accNames
);
159 public boolean onCreateOptionsMenu(Menu menu
) {
160 super.onCreateOptionsMenu(menu
);
161 //MenuInflater inflater = getSherlock().getMenuInflater();
162 //inflater.inflate(R.menu.prefs_menu, menu);
167 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
168 super.onMenuItemSelected(featureId
, item
);
171 switch (item
.getItemId()) {
172 //case R.id.addSessionItem:
174 intent
= new Intent(this, PreferencesNewSession
.class);
175 startActivityForResult(intent
, mNewSession
);
177 case R
.id
.SessionContextEdit
:
178 intent
= new Intent(this, PreferencesNewSession
.class);
179 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
181 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
183 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
185 startActivityForResult(intent
, mEditSession
);
187 case android
.R
.id
.home
:
188 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
189 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
190 startActivity(intent
);
193 Log
.w(TAG
, "Unknown menu item triggered");
200 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
201 super.onActivityResult(requestCode
, resultCode
, data
);
205 protected void onDestroy() {
214 * Updates various summaries after updates. Also starts and stops
217 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
218 // Update current account summary
219 if (preference
.equals(mAccountList
)) {
220 mAccountList
.setSummary(newValue
.toString());
223 // Update tracking interval summary
224 else if (preference
.equals(mTrackingUpdateInterval
)) {
225 String trackingSummary
= getResources().getString(
226 R
.string
.prefs_trackmydevice_interval_summary
);
227 trackingSummary
= String
.format(trackingSummary
,
228 newValue
.toString());
229 mTrackingUpdateInterval
.setSummary(trackingSummary
);
232 // Start or stop tracking service
233 else if (preference
.equals(mDeviceTracking
)) {
234 Intent locationServiceIntent
= new Intent();
235 locationServiceIntent
236 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
237 locationServiceIntent
.putExtra("TRACKING_SETTING",
239 sendBroadcast(locationServiceIntent
);