Merge branch 'transifex' into setup_app_name
[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 version 2,
7 * as published by the Free Software Foundation.
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.content.pm.PackageInfo;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.os.Bundle;
27 import android.preference.CheckBoxPreference;
28 import android.preference.ListPreference;
29 import android.preference.Preference;
30 import android.preference.Preference.OnPreferenceChangeListener;
31 import android.preference.Preference.OnPreferenceClickListener;
32 import android.preference.PreferenceManager;
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.Log_OC;
39 import com.owncloud.android.OwnCloudSession;
40 import com.owncloud.android.R;
41 import com.owncloud.android.db.DbHandler;
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 OnPreferenceChangeListener {
50
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 ListPreference mTrackingUpdateInterval;
57 private CheckBoxPreference mDeviceTracking;
58 private CheckBoxPreference pCode;
59 private CheckBoxPreference pLogging;
60 private Preference pLoggingHistory;
61 private Preference pAboutApp;
62 private int mSelectedMenuItem;
63
64
65 @SuppressWarnings("deprecation")
66 @Override
67 public void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 mDbHandler = new DbHandler(getBaseContext());
70 mSessions = new Vector<OwnCloudSession>();
71 addPreferencesFromResource(R.xml.preferences);
72 //populateAccountList();
73 ActionBar actionBar = getSherlock().getActionBar();
74 actionBar.setDisplayHomeAsUpEnabled(true);
75 Preference p = findPreference("manage_account");
76 if (p != null)
77 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
78 @Override
79 public boolean onPreferenceClick(Preference preference) {
80 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
81 startActivity(i);
82 return true;
83 }
84 });
85
86 pCode = (CheckBoxPreference) findPreference("set_pincode");
87 if (pCode != null){
88 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
89 @Override
90 public boolean onPreferenceChange(Preference preference, Object newValue) {
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 startActivity(i);
95 return true;
96 }
97 });
98
99 /* About App */
100 pAboutApp = (Preference) findPreference("about_app");
101 if (pAboutApp != null) {
102 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
103 PackageInfo pkg;
104 try {
105 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
106 pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
107 } catch (NameNotFoundException e) {
108 Log_OC.e(TAG, "Error while showing about dialog", e);
109 }
110 }
111
112 /* DISABLED FOR RELEASE UNTIL FIXED
113 pLogging = (CheckBoxPreference) findPreference("log_to_file");
114 if (pLogging != null) {
115 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
116 @Override
117 public boolean onPreferenceChange(Preference preference, Object newValue) {
118
119 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
120
121 if(!pLogging.isChecked()) {
122 Log_OC.d("Debug", "start logging");
123 Log_OC.v("PATH", logpath);
124 Log_OC.startLogging(logpath);
125 }
126 else {
127 Log_OC.d("Debug", "stop logging");
128 Log_OC.stopLogging();
129 }
130 return true;
131 }
132 });
133 }
134
135 pLoggingHistory = (Preference) findPreference("log_history");
136 if (pLoggingHistory != null) {
137 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
138
139 @Override
140 public boolean onPreferenceClick(Preference preference) {
141 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
142 startActivity(intent);
143 return true;
144 }
145 });
146 }
147 */
148
149 }
150 }
151
152 @Override
153 protected void onResume() {
154 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
155 boolean state = appPrefs.getBoolean("set_pincode", false);
156 pCode.setChecked(state);
157 super.onResume();
158 }
159
160 @Override
161 public boolean onCreateOptionsMenu(Menu menu) {
162 super.onCreateOptionsMenu(menu);
163 return true;
164 }
165
166 @Override
167 public boolean onMenuItemSelected(int featureId, MenuItem item) {
168 super.onMenuItemSelected(featureId, item);
169 Intent intent;
170
171 switch (item.getItemId()) {
172 //case R.id.addSessionItem:
173 case 1:
174 intent = new Intent(this, PreferencesNewSession.class);
175 startActivityForResult(intent, mNewSession);
176 break;
177 case R.id.SessionContextEdit:
178 intent = new Intent(this, PreferencesNewSession.class);
179 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
180 .getEntryId());
181 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
182 .getName());
183 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
184 .getUrl());
185 startActivityForResult(intent, mEditSession);
186 break;
187 case android.R.id.home:
188 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
189 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
190 startActivity(intent);
191 break;
192 default:
193 Log_OC.w(TAG, "Unknown menu item triggered");
194 return false;
195 }
196 return true;
197 }
198
199 @Override
200 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
201 super.onActivityResult(requestCode, resultCode, data);
202 }
203
204 @Override
205 protected void onDestroy() {
206 mDbHandler.close();
207 super.onDestroy();
208 }
209
210 @Override
211 /**
212 * Updates various summaries after updates. Also starts and stops
213 * the
214 */
215 public boolean onPreferenceChange(Preference preference, Object newValue) {
216 // Update current account summary
217 /*if (preference.equals(mAccountList)) {
218 mAccountList.setSummary(newValue.toString());
219 }
220
221 // Update tracking interval summary
222 else*/ if (preference.equals(mTrackingUpdateInterval)) {
223 String trackingSummary = getResources().getString(
224 R.string.prefs_trackmydevice_interval_summary);
225 trackingSummary = String.format(trackingSummary,
226 newValue.toString());
227 mTrackingUpdateInterval.setSummary(trackingSummary);
228 }
229
230 // Start or stop tracking service
231 else if (preference.equals(mDeviceTracking)) {
232 Intent locationServiceIntent = new Intent();
233 locationServiceIntent
234 .setAction("com.owncloud.android.location.LocationLauncher");
235 locationServiceIntent.putExtra("TRACKING_SETTING",
236 (Boolean) newValue);
237 sendBroadcast(locationServiceIntent);
238 }
239 return true;
240 }
241 }