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