ffc1397660c5c3c1634522479ea7f2df5b3c1e97
[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.os.Bundle;
26 import android.preference.CheckBoxPreference;
27 import android.preference.ListPreference;
28 import android.preference.Preference;
29 import android.preference.Preference.OnPreferenceChangeListener;
30 import android.preference.Preference.OnPreferenceClickListener;
31 import android.util.Log;
32 import android.view.ContextMenu;
33 import android.view.ContextMenu.ContextMenuInfo;
34 import android.view.View;
35 import android.widget.AdapterView.AdapterContextMenuInfo;
36
37 import com.actionbarsherlock.app.ActionBar;
38 import com.actionbarsherlock.app.SherlockPreferenceActivity;
39 import com.actionbarsherlock.view.Menu;
40 import com.actionbarsherlock.view.MenuInflater;
41 import com.actionbarsherlock.view.MenuItem;
42
43 import eu.alefzero.owncloud.AccountUtils;
44 import eu.alefzero.owncloud.OwnCloudSession;
45 import eu.alefzero.owncloud.R;
46 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
47 import eu.alefzero.owncloud.db.DbHandler;
48 import eu.alefzero.owncloud.syncadapter.FileSyncAdapter;
49
50 /**
51 * An Activity that allows the user to change the application's settings.
52 *
53 * @author Bartek Przybylski
54 *
55 */
56 public class Preferences extends SherlockPreferenceActivity implements
57 OnPreferenceChangeListener {
58 private static final String TAG = "OwnCloudPreferences";
59 private final int mNewSession = 47;
60 private final int mEditSession = 48;
61 private DbHandler mDbHandler;
62 private Vector<OwnCloudSession> mSessions;
63 private Account[] mAccounts;
64 private ListPreference mAccountList;
65 private ListPreference mTrackingUpdateInterval;
66 private CheckBoxPreference mDeviceTracking;
67 private int mSelectedMenuItem;
68
69 @Override
70 public void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 mDbHandler = new DbHandler(getBaseContext());
73 mSessions = new Vector<OwnCloudSession>();
74 addPreferencesFromResource(R.xml.preferences);
75 populateAccountList();
76 ActionBar actionBar = getSherlock().getActionBar();
77 actionBar.setDisplayHomeAsUpEnabled(true);
78 Preference p = findPreference("manage_account");
79 if (p != null)
80 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
81 @Override
82 public boolean onPreferenceClick(Preference preference) {
83 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
84 startActivity(i);
85 return true;
86 }
87 });
88
89 CheckBoxPreference pCode = (CheckBoxPreference) findPreference("set_pincode");
90 if (pCode != null){
91
92 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
93 @Override
94 public boolean onPreferenceChange(Preference preference, Object newValue) {
95
96
97 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
98 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
99 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
100 startActivity(i);
101
102 return true;
103 }
104 });
105
106 }
107
108 }
109
110 /**
111 * Populates the account selector
112 */
113 private void populateAccountList() {
114 AccountManager accMan = AccountManager.get(this);
115 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
116 mAccountList = (ListPreference) findPreference("select_oc_account");
117 mAccountList.setOnPreferenceChangeListener(this);
118
119 // Display the name of the current account if there is any
120 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
121 if (defaultAccount != null) {
122 mAccountList.setSummary(defaultAccount.name);
123 }
124
125 // Transform accounts into array of string for preferences to use
126 String[] accNames = new String[mAccounts.length];
127 for (int i = 0; i < mAccounts.length; i++) {
128 Account account = mAccounts[i];
129 accNames[i] = account.name;
130 }
131
132 mAccountList.setEntries(accNames);
133 mAccountList.setEntryValues(accNames);
134 }
135
136 @Override
137 public boolean onCreateOptionsMenu(Menu menu) {
138 super.onCreateOptionsMenu(menu);
139 //MenuInflater inflater = getSherlock().getMenuInflater();
140 //inflater.inflate(R.menu.prefs_menu, menu);
141 return true;
142 }
143
144 @Override
145 public boolean onMenuItemSelected(int featureId, MenuItem item) {
146 super.onMenuItemSelected(featureId, item);
147 Intent intent;
148
149 switch (item.getItemId()) {
150 //case R.id.addSessionItem:
151 case 1:
152 intent = new Intent(this, PreferencesNewSession.class);
153 startActivityForResult(intent, mNewSession);
154 break;
155 case R.id.SessionContextEdit:
156 intent = new Intent(this, PreferencesNewSession.class);
157 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
158 .getEntryId());
159 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
160 .getName());
161 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
162 .getUrl());
163 startActivityForResult(intent, mEditSession);
164 break;
165 case android.R.id.home:
166 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
167 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
168 startActivity(intent);
169 break;
170 default:
171 Log.w(TAG, "Unknown menu item triggered");
172 return false;
173 }
174 return true;
175 }
176
177 @Override
178 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
179 super.onActivityResult(requestCode, resultCode, data);
180 }
181
182 @Override
183 protected void onDestroy() {
184 mDbHandler.close();
185 super.onDestroy();
186 }
187
188 @Override
189 /**
190 * Updates various summaries after updates. Also starts and stops
191 * the
192 */
193 public boolean onPreferenceChange(Preference preference, Object newValue) {
194 // Update current account summary
195 if (preference.equals(mAccountList)) {
196 mAccountList.setSummary(newValue.toString());
197 }
198
199 // Update tracking interval summary
200 else if (preference.equals(mTrackingUpdateInterval)) {
201 String trackingSummary = getResources().getString(
202 R.string.prefs_trackmydevice_interval_summary);
203 trackingSummary = String.format(trackingSummary,
204 newValue.toString());
205 mTrackingUpdateInterval.setSummary(trackingSummary);
206 }
207
208 // Start or stop tracking service
209 else if (preference.equals(mDeviceTracking)) {
210 Intent locationServiceIntent = new Intent();
211 locationServiceIntent
212 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
213 locationServiceIntent.putExtra("TRACKING_SETTING",
214 (Boolean) newValue);
215 sendBroadcast(locationServiceIntent);
216 }
217 return true;
218 }
219
220 }