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