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