e71dbdf0dc8a108211c15048948bcda1b018b888
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / Preferences.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package eu.alefzero.owncloud.ui.activity;
19
20 import java.util.Vector;
21
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;
38
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;
44
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;
51
52 /**
53 * An Activity that allows the user to change the application's settings.
54 *
55 * @author Bartek Przybylski
56 *
57 */
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;
71
72 @Override
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");
82 if (p != null)
83 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
84 @Override
85 public boolean onPreferenceClick(Preference preference) {
86 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
87 startActivity(i);
88 return true;
89 }
90 });
91
92 pCode = (CheckBoxPreference) findPreference("set_pincode");
93
94
95 if (pCode != null){
96
97 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
98 @Override
99 public boolean onPreferenceChange(Preference preference, Object newValue) {
100
101 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
102 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
103 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
104
105 startActivity(i);
106
107 return true;
108 }
109 });
110
111 }
112
113 }
114
115
116 @Override
117 protected void onResume() {
118 // TODO Auto-generated method stub
119 SharedPreferences appPrefs = PreferenceManager
120 .getDefaultSharedPreferences(getApplicationContext());
121
122 boolean state = appPrefs.getBoolean("set_pincode", false);
123 pCode.setChecked(state);
124
125 super.onResume();
126 }
127
128
129
130 /**
131 * Populates the account selector
132 */
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);
138
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);
143 }
144
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;
150 }
151
152 mAccountList.setEntries(accNames);
153 mAccountList.setEntryValues(accNames);
154 }
155
156
157
158 @Override
159 public boolean onCreateOptionsMenu(Menu menu) {
160 super.onCreateOptionsMenu(menu);
161 //MenuInflater inflater = getSherlock().getMenuInflater();
162 //inflater.inflate(R.menu.prefs_menu, menu);
163 return true;
164 }
165
166 @Override
167 public boolean onMenuItemSelected(int featureId, MenuItem item) {
168 super.onMenuItemSelected(featureId, item);
169 Intent intent;
170
171 switch (item.getItemId()) {
172 //case R.id.addSessionItem:
173 case 1:
174 intent = new Intent(this, PreferencesNewSession.class);
175 startActivityForResult(intent, mNewSession);
176 break;
177 case R.id.SessionContextEdit:
178 intent = new Intent(this, PreferencesNewSession.class);
179 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
180 .getEntryId());
181 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
182 .getName());
183 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
184 .getUrl());
185 startActivityForResult(intent, mEditSession);
186 break;
187 case android.R.id.home:
188 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
189 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
190 startActivity(intent);
191 break;
192 default:
193 Log.w(TAG, "Unknown menu item triggered");
194 return false;
195 }
196 return true;
197 }
198
199 @Override
200 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
201 super.onActivityResult(requestCode, resultCode, data);
202 }
203
204 @Override
205 protected void onDestroy() {
206 mDbHandler.close();
207 super.onDestroy();
208 }
209
210
211
212 @Override
213 /**
214 * Updates various summaries after updates. Also starts and stops
215 * the
216 */
217 public boolean onPreferenceChange(Preference preference, Object newValue) {
218 // Update current account summary
219 if (preference.equals(mAccountList)) {
220 mAccountList.setSummary(newValue.toString());
221 }
222
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);
230 }
231
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",
238 (Boolean) newValue);
239 sendBroadcast(locationServiceIntent);
240 }
241 return true;
242 }
243
244
245
246 }