22c987673e573853bca110c3bb466e9fec88823e
[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
89 /**
90 * Populates the account selector
91 */
92 private void populateAccountList() {
93 AccountManager accMan = AccountManager.get(this);
94 mAccounts = accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
95 mAccountList = (ListPreference) findPreference("select_oc_account");
96 mAccountList.setOnPreferenceChangeListener(this);
97
98 // Display the name of the current account if there is any
99 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
100 if (defaultAccount != null) {
101 mAccountList.setSummary(defaultAccount.name);
102 }
103
104 // Transform accounts into array of string for preferences to use
105 String[] accNames = new String[mAccounts.length];
106 for (int i = 0; i < mAccounts.length; i++) {
107 Account account = mAccounts[i];
108 accNames[i] = account.name;
109 }
110
111 mAccountList.setEntries(accNames);
112 mAccountList.setEntryValues(accNames);
113 }
114
115 @Override
116 public boolean onCreateOptionsMenu(Menu menu) {
117 super.onCreateOptionsMenu(menu);
118 //MenuInflater inflater = getSherlock().getMenuInflater();
119 //inflater.inflate(R.menu.prefs_menu, menu);
120 return true;
121 }
122
123 @Override
124 public boolean onMenuItemSelected(int featureId, MenuItem item) {
125 super.onMenuItemSelected(featureId, item);
126 Intent intent;
127
128 switch (item.getItemId()) {
129 //case R.id.addSessionItem:
130 case 1:
131 intent = new Intent(this, PreferencesNewSession.class);
132 startActivityForResult(intent, mNewSession);
133 break;
134 case R.id.SessionContextEdit:
135 intent = new Intent(this, PreferencesNewSession.class);
136 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
137 .getEntryId());
138 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
139 .getName());
140 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
141 .getUrl());
142 startActivityForResult(intent, mEditSession);
143 break;
144 case android.R.id.home:
145 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
146 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
147 startActivity(intent);
148 break;
149 default:
150 Log.w(TAG, "Unknown menu item triggered");
151 return false;
152 }
153 return true;
154 }
155
156 @Override
157 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
158 super.onActivityResult(requestCode, resultCode, data);
159 }
160
161 @Override
162 protected void onDestroy() {
163 mDbHandler.close();
164 super.onDestroy();
165 }
166
167 @Override
168 /**
169 * Updates various summaries after updates. Also starts and stops
170 * the
171 */
172 public boolean onPreferenceChange(Preference preference, Object newValue) {
173 // Update current account summary
174 if (preference.equals(mAccountList)) {
175 mAccountList.setSummary(newValue.toString());
176 }
177
178 // Update tracking interval summary
179 else if (preference.equals(mTrackingUpdateInterval)) {
180 String trackingSummary = getResources().getString(
181 R.string.prefs_trackmydevice_interval_summary);
182 trackingSummary = String.format(trackingSummary,
183 newValue.toString());
184 mTrackingUpdateInterval.setSummary(trackingSummary);
185 }
186
187 // Start or stop tracking service
188 else if (preference.equals(mDeviceTracking)) {
189 Intent locationServiceIntent = new Intent();
190 locationServiceIntent
191 .setAction("eu.alefzero.owncloud.location.LocationLauncher");
192 locationServiceIntent.putExtra("TRACKING_SETTING",
193 (Boolean) newValue);
194 sendBroadcast(locationServiceIntent);
195 }
196 return true;
197 }
198
199 }