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