35638c5e7554e4d060452becff2f65bf20e7a1b6
[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.content.pm.PackageInfo;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.os.Bundle;
28 import android.preference.CheckBoxPreference;
29 import android.preference.ListPreference;
30 import android.preference.Preference;
31 import android.preference.Preference.OnPreferenceChangeListener;
32 import android.preference.Preference.OnPreferenceClickListener;
33 import android.preference.PreferenceManager;
34 import android.util.Log;
35
36 import com.actionbarsherlock.app.ActionBar;
37 import com.actionbarsherlock.app.SherlockPreferenceActivity;
38 import com.actionbarsherlock.view.Menu;
39 import com.actionbarsherlock.view.MenuItem;
40 import com.owncloud.android.OwnCloudSession;
41 import com.owncloud.android.R;
42 import com.owncloud.android.db.DbHandler;
43
44 /**
45 * An Activity that allows the user to change the application's settings.
46 *
47 * @author Bartek Przybylski
48 *
49 */
50 public class Preferences extends SherlockPreferenceActivity implements OnPreferenceChangeListener {
51
52 private static final String TAG = "OwnCloudPreferences";
53 private final int mNewSession = 47;
54 private final int mEditSession = 48;
55 private DbHandler mDbHandler;
56 private Vector<OwnCloudSession> mSessions;
57 private ListPreference mTrackingUpdateInterval;
58 private CheckBoxPreference mDeviceTracking;
59 private CheckBoxPreference pCode;
60 private Preference pAboutApp;
61 private int mSelectedMenuItem;
62
63 @SuppressWarnings("deprecation")
64 @Override
65 public void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67 mDbHandler = new DbHandler(getBaseContext());
68 mSessions = new Vector<OwnCloudSession>();
69 addPreferencesFromResource(R.xml.preferences);
70 //populateAccountList();
71 ActionBar actionBar = getSherlock().getActionBar();
72 actionBar.setDisplayHomeAsUpEnabled(true);
73 Preference p = findPreference("manage_account");
74 if (p != null)
75 p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
76 @Override
77 public boolean onPreferenceClick(Preference preference) {
78 Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
79 startActivity(i);
80 return true;
81 }
82 });
83
84 pCode = (CheckBoxPreference) findPreference("set_pincode");
85 if (pCode != null){
86 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
87 @Override
88 public boolean onPreferenceChange(Preference preference, Object newValue) {
89 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
90 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
91 i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
92 startActivity(i);
93 return true;
94 }
95 });
96
97 /* About App */
98 pAboutApp = (Preference) findPreference("about_app");
99 if (pAboutApp != null) {
100 pAboutApp.setTitle(getString(R.string.app_name)+" "+getString(R.string.about_android));
101 PackageInfo pkg;
102 try {
103 pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
104 pAboutApp.setSummary(getString(R.string.about_version)+" "+pkg.versionName);
105 } catch (NameNotFoundException e) {
106 Log.e(TAG, "Error while showing about dialog", e);
107 }
108 }
109 }
110 }
111
112 @Override
113 protected void onResume() {
114 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
115 boolean state = appPrefs.getBoolean("set_pincode", false);
116 pCode.setChecked(state);
117 super.onResume();
118 }
119
120 @Override
121 public boolean onCreateOptionsMenu(Menu menu) {
122 super.onCreateOptionsMenu(menu);
123 return true;
124 }
125
126 @Override
127 public boolean onMenuItemSelected(int featureId, MenuItem item) {
128 super.onMenuItemSelected(featureId, item);
129 Intent intent;
130
131 switch (item.getItemId()) {
132 //case R.id.addSessionItem:
133 case 1:
134 intent = new Intent(this, PreferencesNewSession.class);
135 startActivityForResult(intent, mNewSession);
136 break;
137 case R.id.SessionContextEdit:
138 intent = new Intent(this, PreferencesNewSession.class);
139 intent.putExtra("sessionId", mSessions.get(mSelectedMenuItem)
140 .getEntryId());
141 intent.putExtra("sessionName", mSessions.get(mSelectedMenuItem)
142 .getName());
143 intent.putExtra("sessionURL", mSessions.get(mSelectedMenuItem)
144 .getUrl());
145 startActivityForResult(intent, mEditSession);
146 break;
147 case android.R.id.home:
148 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
149 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
150 startActivity(intent);
151 break;
152 default:
153 Log.w(TAG, "Unknown menu item triggered");
154 return false;
155 }
156 return true;
157 }
158
159 @Override
160 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
161 super.onActivityResult(requestCode, resultCode, data);
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("com.owncloud.android.location.LocationLauncher");
195 locationServiceIntent.putExtra("TRACKING_SETTING",
196 (Boolean) newValue);
197 sendBroadcast(locationServiceIntent);
198 }
199 return true;
200 }
201 }