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 com
.owncloud
.android
.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
;
35 import com
.actionbarsherlock
.app
.ActionBar
;
36 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
37 import com
.actionbarsherlock
.view
.Menu
;
38 import com
.actionbarsherlock
.view
.MenuItem
;
39 import com
.owncloud
.android
.AccountUtils
;
40 import com
.owncloud
.android
.OwnCloudSession
;
41 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
42 import com
.owncloud
.android
.db
.DbHandler
;
44 import com
.owncloud
.android
.R
;
47 * An Activity that allows the user to change the application's settings.
49 * @author Bartek Przybylski
52 public class Preferences
extends SherlockPreferenceActivity
implements
53 OnPreferenceChangeListener
{
54 private static final String TAG
= "OwnCloudPreferences";
55 private final int mNewSession
= 47;
56 private final int mEditSession
= 48;
57 private DbHandler mDbHandler
;
58 private Vector
<OwnCloudSession
> mSessions
;
59 //private Account[] mAccounts;
60 //private ListPreference mAccountList;
61 private ListPreference mTrackingUpdateInterval
;
62 private CheckBoxPreference mDeviceTracking
;
63 private CheckBoxPreference pCode
;
64 private int mSelectedMenuItem
;
67 public void onCreate(Bundle savedInstanceState
) {
68 super.onCreate(savedInstanceState
);
69 mDbHandler
= new DbHandler(getBaseContext());
70 mSessions
= new Vector
<OwnCloudSession
>();
71 addPreferencesFromResource(R
.xml
.preferences
);
72 //populateAccountList();
73 ActionBar actionBar
= getSherlock().getActionBar();
74 actionBar
.setDisplayHomeAsUpEnabled(true
);
75 Preference p
= findPreference("manage_account");
77 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
79 public boolean onPreferenceClick(Preference preference
) {
80 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
86 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
91 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
93 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
95 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
96 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
97 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
111 protected void onResume() {
112 // TODO Auto-generated method stub
113 SharedPreferences appPrefs
= PreferenceManager
114 .getDefaultSharedPreferences(getApplicationContext());
116 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
117 pCode
.setChecked(state
);
125 * Populates the account selector
127 private void populateAccountList() {
128 AccountManager accMan = AccountManager.get(this);
129 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
130 mAccountList = (ListPreference) findPreference("select_oc_account");
131 mAccountList.setOnPreferenceChangeListener(this);
133 // Display the name of the current account if there is any
134 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
135 if (defaultAccount != null) {
136 mAccountList.setSummary(defaultAccount.name);
139 // Transform accounts into array of string for preferences to use
140 String[] accNames = new String[mAccounts.length];
141 for (int i = 0; i < mAccounts.length; i++) {
142 Account account = mAccounts[i];
143 accNames[i] = account.name;
146 mAccountList.setEntries(accNames);
147 mAccountList.setEntryValues(accNames);
153 public boolean onCreateOptionsMenu(Menu menu
) {
154 super.onCreateOptionsMenu(menu
);
155 //MenuInflater inflater = getSherlock().getMenuInflater();
156 //inflater.inflate(R.menu.prefs_menu, menu);
161 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
162 super.onMenuItemSelected(featureId
, item
);
165 switch (item
.getItemId()) {
166 //case R.id.addSessionItem:
168 intent
= new Intent(this, PreferencesNewSession
.class);
169 startActivityForResult(intent
, mNewSession
);
171 case R
.id
.SessionContextEdit
:
172 intent
= new Intent(this, PreferencesNewSession
.class);
173 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
175 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
177 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
179 startActivityForResult(intent
, mEditSession
);
181 case android
.R
.id
.home
:
182 intent
= new Intent(getBaseContext(), FileDisplayActivity
.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
);
199 protected void onDestroy() {
208 * Updates various summaries after updates. Also starts and stops
211 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
212 // Update current account summary
213 /*if (preference.equals(mAccountList)) {
214 mAccountList.setSummary(newValue.toString());
217 // Update tracking interval summary
218 else*/ if (preference
.equals(mTrackingUpdateInterval
)) {
219 String trackingSummary
= getResources().getString(
220 R
.string
.prefs_trackmydevice_interval_summary
);
221 trackingSummary
= String
.format(trackingSummary
,
222 newValue
.toString());
223 mTrackingUpdateInterval
.setSummary(trackingSummary
);
226 // Start or stop tracking service
227 else if (preference
.equals(mDeviceTracking
)) {
228 Intent locationServiceIntent
= new Intent();
229 locationServiceIntent
230 .setAction("com.owncloud.android.location.LocationLauncher");
231 locationServiceIntent
.putExtra("TRACKING_SETTING",
233 sendBroadcast(locationServiceIntent
);