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
.content
.Intent
;
23 import android
.content
.SharedPreferences
;
24 import android
.os
.Bundle
;
25 import android
.preference
.CheckBoxPreference
;
26 import android
.preference
.ListPreference
;
27 import android
.preference
.Preference
;
28 import android
.preference
.PreferenceManager
;
29 import android
.preference
.Preference
.OnPreferenceChangeListener
;
30 import android
.preference
.Preference
.OnPreferenceClickListener
;
31 import android
.util
.Log
;
33 import com
.actionbarsherlock
.app
.ActionBar
;
34 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
35 import com
.actionbarsherlock
.view
.Menu
;
36 import com
.actionbarsherlock
.view
.MenuItem
;
37 import com
.owncloud
.android
.OwnCloudSession
;
38 import com
.owncloud
.android
.db
.DbHandler
;
40 import com
.owncloud
.android
.R
;
43 * An Activity that allows the user to change the application's settings.
45 * @author Bartek Przybylski
48 public class Preferences
extends SherlockPreferenceActivity
implements
49 OnPreferenceChangeListener
{
50 private static final String TAG
= "OwnCloudPreferences";
51 private final int mNewSession
= 47;
52 private final int mEditSession
= 48;
53 private DbHandler mDbHandler
;
54 private Vector
<OwnCloudSession
> mSessions
;
55 //private Account[] mAccounts;
56 //private ListPreference mAccountList;
57 private ListPreference mTrackingUpdateInterval
;
58 private CheckBoxPreference mDeviceTracking
;
59 private CheckBoxPreference pCode
;
60 private int mSelectedMenuItem
;
63 public void onCreate(Bundle savedInstanceState
) {
64 super.onCreate(savedInstanceState
);
65 mDbHandler
= new DbHandler(getBaseContext());
66 mSessions
= new Vector
<OwnCloudSession
>();
67 addPreferencesFromResource(R
.xml
.preferences
);
68 //populateAccountList();
69 ActionBar actionBar
= getSherlock().getActionBar();
70 actionBar
.setDisplayHomeAsUpEnabled(true
);
71 Preference p
= findPreference("manage_account");
73 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
75 public boolean onPreferenceClick(Preference preference
) {
76 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
82 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
87 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
89 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
91 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
92 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
93 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
107 protected void onResume() {
108 // TODO Auto-generated method stub
109 SharedPreferences appPrefs
= PreferenceManager
110 .getDefaultSharedPreferences(getApplicationContext());
112 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
113 pCode
.setChecked(state
);
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 = AccountUtils.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);
149 public boolean onCreateOptionsMenu(Menu menu
) {
150 super.onCreateOptionsMenu(menu
);
151 //MenuInflater inflater = getSherlock().getMenuInflater();
152 //inflater.inflate(R.menu.prefs_menu, menu);
157 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
158 super.onMenuItemSelected(featureId
, item
);
161 switch (item
.getItemId()) {
162 //case R.id.addSessionItem:
164 intent
= new Intent(this, PreferencesNewSession
.class);
165 startActivityForResult(intent
, mNewSession
);
167 case R
.id
.SessionContextEdit
:
168 intent
= new Intent(this, PreferencesNewSession
.class);
169 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
171 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
173 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
175 startActivityForResult(intent
, mEditSession
);
177 case android
.R
.id
.home
:
178 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
179 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
180 startActivity(intent
);
183 Log
.w(TAG
, "Unknown menu item triggered");
190 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
191 super.onActivityResult(requestCode
, resultCode
, data
);
195 protected void onDestroy() {
204 * Updates various summaries after updates. Also starts and stops
207 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
208 // Update current account summary
209 /*if (preference.equals(mAccountList)) {
210 mAccountList.setSummary(newValue.toString());
213 // Update tracking interval summary
214 else*/ if (preference
.equals(mTrackingUpdateInterval
)) {
215 String trackingSummary
= getResources().getString(
216 R
.string
.prefs_trackmydevice_interval_summary
);
217 trackingSummary
= String
.format(trackingSummary
,
218 newValue
.toString());
219 mTrackingUpdateInterval
.setSummary(trackingSummary
);
222 // Start or stop tracking service
223 else if (preference
.equals(mDeviceTracking
)) {
224 Intent locationServiceIntent
= new Intent();
225 locationServiceIntent
226 .setAction("com.owncloud.android.location.LocationLauncher");
227 locationServiceIntent
.putExtra("TRACKING_SETTING",
229 sendBroadcast(locationServiceIntent
);