1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.activity
;
21 import java
.util
.Vector
;
23 import android
.content
.Intent
;
24 import android
.content
.SharedPreferences
;
25 import android
.os
.Bundle
;
26 import android
.preference
.CheckBoxPreference
;
27 import android
.preference
.ListPreference
;
28 import android
.preference
.Preference
;
29 import android
.preference
.PreferenceManager
;
30 import android
.preference
.Preference
.OnPreferenceChangeListener
;
31 import android
.preference
.Preference
.OnPreferenceClickListener
;
32 import android
.util
.Log
;
34 import com
.actionbarsherlock
.app
.ActionBar
;
35 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
36 import com
.actionbarsherlock
.view
.Menu
;
37 import com
.actionbarsherlock
.view
.MenuItem
;
38 import com
.owncloud
.android
.OwnCloudSession
;
39 import com
.owncloud
.android
.db
.DbHandler
;
41 import com
.owncloud
.android
.R
;
44 * An Activity that allows the user to change the application's settings.
46 * @author Bartek Przybylski
49 public class Preferences
extends SherlockPreferenceActivity
implements
50 OnPreferenceChangeListener
{
51 private static final String TAG
= "OwnCloudPreferences";
52 private final int mNewSession
= 47;
53 private final int mEditSession
= 48;
54 private DbHandler mDbHandler
;
55 private Vector
<OwnCloudSession
> mSessions
;
56 //private Account[] mAccounts;
57 //private ListPreference mAccountList;
58 private ListPreference mTrackingUpdateInterval
;
59 private CheckBoxPreference mDeviceTracking
;
60 private CheckBoxPreference pCode
;
61 private int mSelectedMenuItem
;
64 public void onCreate(Bundle savedInstanceState
) {
65 super.onCreate(savedInstanceState
);
66 mDbHandler
= new DbHandler(getBaseContext());
67 mSessions
= new Vector
<OwnCloudSession
>();
68 addPreferencesFromResource(R
.xml
.preferences
);
69 //populateAccountList();
70 ActionBar actionBar
= getSherlock().getActionBar();
71 actionBar
.setDisplayHomeAsUpEnabled(true
);
72 Preference p
= findPreference("manage_account");
74 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
76 public boolean onPreferenceClick(Preference preference
) {
77 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
83 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
88 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
90 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
92 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
93 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
94 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
108 protected void onResume() {
109 // TODO Auto-generated method stub
110 SharedPreferences appPrefs
= PreferenceManager
111 .getDefaultSharedPreferences(getApplicationContext());
113 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
114 pCode
.setChecked(state
);
122 * Populates the account selector
124 private void populateAccountList() {
125 AccountManager accMan = AccountManager.get(this);
126 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
127 mAccountList = (ListPreference) findPreference("select_oc_account");
128 mAccountList.setOnPreferenceChangeListener(this);
130 // Display the name of the current account if there is any
131 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
132 if (defaultAccount != null) {
133 mAccountList.setSummary(defaultAccount.name);
136 // Transform accounts into array of string for preferences to use
137 String[] accNames = new String[mAccounts.length];
138 for (int i = 0; i < mAccounts.length; i++) {
139 Account account = mAccounts[i];
140 accNames[i] = account.name;
143 mAccountList.setEntries(accNames);
144 mAccountList.setEntryValues(accNames);
150 public boolean onCreateOptionsMenu(Menu menu
) {
151 super.onCreateOptionsMenu(menu
);
152 //MenuInflater inflater = getSherlock().getMenuInflater();
153 //inflater.inflate(R.menu.prefs_menu, menu);
158 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
159 super.onMenuItemSelected(featureId
, item
);
162 switch (item
.getItemId()) {
163 //case R.id.addSessionItem:
165 intent
= new Intent(this, PreferencesNewSession
.class);
166 startActivityForResult(intent
, mNewSession
);
168 case R
.id
.SessionContextEdit
:
169 intent
= new Intent(this, PreferencesNewSession
.class);
170 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
172 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
174 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
176 startActivityForResult(intent
, mEditSession
);
178 case android
.R
.id
.home
:
179 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
180 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
181 startActivity(intent
);
184 Log
.w(TAG
, "Unknown menu item triggered");
191 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
192 super.onActivityResult(requestCode
, resultCode
, data
);
196 protected void onDestroy() {
205 * Updates various summaries after updates. Also starts and stops
208 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
209 // Update current account summary
210 /*if (preference.equals(mAccountList)) {
211 mAccountList.setSummary(newValue.toString());
214 // Update tracking interval summary
215 else*/ if (preference
.equals(mTrackingUpdateInterval
)) {
216 String trackingSummary
= getResources().getString(
217 R
.string
.prefs_trackmydevice_interval_summary
);
218 trackingSummary
= String
.format(trackingSummary
,
219 newValue
.toString());
220 mTrackingUpdateInterval
.setSummary(trackingSummary
);
223 // Start or stop tracking service
224 else if (preference
.equals(mDeviceTracking
)) {
225 Intent locationServiceIntent
= new Intent();
226 locationServiceIntent
227 .setAction("com.owncloud.android.location.LocationLauncher");
228 locationServiceIntent
.putExtra("TRACKING_SETTING",
230 sendBroadcast(locationServiceIntent
);