1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.activity
;
20 import java
.util
.Vector
;
22 import android
.accounts
.Account
;
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
.net
.Uri
;
28 import android
.os
.Bundle
;
29 import android
.preference
.CheckBoxPreference
;
30 import android
.preference
.ListPreference
;
31 import android
.preference
.Preference
;
32 import android
.preference
.Preference
.OnPreferenceChangeListener
;
33 import android
.preference
.Preference
.OnPreferenceClickListener
;
34 import android
.preference
.PreferenceCategory
;
35 import android
.preference
.PreferenceManager
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
38 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
;
39 import com
.actionbarsherlock
.view
.Menu
;
40 import com
.actionbarsherlock
.view
.MenuItem
;
41 import com
.owncloud
.android
.Log_OC
;
42 import com
.owncloud
.android
.OwnCloudSession
;
43 import com
.owncloud
.android
.R
;
44 import com
.owncloud
.android
.authentication
.AccountUtils
;
45 import com
.owncloud
.android
.db
.DbHandler
;
49 * An Activity that allows the user to change the application's settings.
51 * @author Bartek Przybylski
54 public class Preferences
extends SherlockPreferenceActivity
implements OnPreferenceChangeListener
{
56 private static final String TAG
= "OwnCloudPreferences";
57 private final int mNewSession
= 47;
58 private final int mEditSession
= 48;
59 private DbHandler mDbHandler
;
60 private Vector
<OwnCloudSession
> mSessions
;
61 private ListPreference mTrackingUpdateInterval
;
62 private CheckBoxPreference mDeviceTracking
;
63 private CheckBoxPreference pCode
;
64 //private CheckBoxPreference pLogging;
65 //private Preference pLoggingHistory;
66 private Preference pAboutApp
;
67 private int mSelectedMenuItem
;
70 @SuppressWarnings("deprecation")
72 public void onCreate(Bundle savedInstanceState
) {
73 super.onCreate(savedInstanceState
);
74 mDbHandler
= new DbHandler(getBaseContext());
75 mSessions
= new Vector
<OwnCloudSession
>();
76 addPreferencesFromResource(R
.xml
.preferences
);
77 //populateAccountList();
78 ActionBar actionBar
= getSherlock().getActionBar();
79 actionBar
.setDisplayHomeAsUpEnabled(true
);
81 Preference p
= findPreference("manage_account");
83 p
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
85 public boolean onPreferenceClick(Preference preference
) {
86 Intent i
= new Intent(getApplicationContext(), AccountSelectActivity
.class);
92 pCode
= (CheckBoxPreference
) findPreference("set_pincode");
94 pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
96 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
97 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
98 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences");
99 i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString());
110 PreferenceCategory preferenceCategory
= (PreferenceCategory
) findPreference("more");
112 boolean helpEnabled
= getResources().getBoolean(R
.bool
.help_enabled
);
113 Preference pHelp
= findPreference("help");
116 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
118 public boolean onPreferenceClick(Preference preference
) {
119 String helpWeb
=(String
) getText(R
.string
.url_help
);
120 if (helpWeb
!= null
&& helpWeb
.length() > 0) {
121 Uri uriUrl
= Uri
.parse(helpWeb
);
122 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
123 startActivity(intent
);
129 preferenceCategory
.removePreference(pHelp
);
135 boolean recommendEnabled
= getResources().getBoolean(R
.bool
.recommend_enabled
);
136 Preference pRecommend
= findPreference("recommend");
137 if (pRecommend
!= null
){
138 if (recommendEnabled
) {
139 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
141 public boolean onPreferenceClick(Preference preference
) {
143 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
144 intent
.setType("text/plain");
145 //Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
146 String appName
= getString(R
.string
.app_name
);
147 //String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
148 //String recommendSubject = String.format(getString(R.string.recommend_subject), username, appName);
149 String recommendSubject
= String
.format(getString(R
.string
.recommend_subject
), appName
);
150 intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
);
151 //String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), username);
152 String recommendText
= String
.format(getString(R
.string
.recommend_text
), getString(R
.string
.app_name
), getString(R
.string
.url_app_download
));
153 intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
);
155 intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));
156 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
157 startActivity(intent
);
165 preferenceCategory
.removePreference(pRecommend
);
170 boolean feedbackEnabled
= getResources().getBoolean(R
.bool
.feedback_enabled
);
171 Preference pFeedback
= findPreference("feedback");
172 if (pFeedback
!= null
){
173 if (feedbackEnabled
) {
174 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
176 public boolean onPreferenceClick(Preference preference
) {
177 String feedbackMail
=(String
) getText(R
.string
.mail_feedback
);
178 String feedback
=(String
) getText(R
.string
.prefs_feedback
);
179 Intent intent
= new Intent(Intent
.ACTION_SENDTO
);
180 intent
.setType("text/plain");
181 intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
);
183 intent
.setData(Uri
.parse(feedbackMail
));
184 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
185 startActivity(intent
);
191 preferenceCategory
.removePreference(pFeedback
);
196 boolean imprintEnabled
= getResources().getBoolean(R
.bool
.imprint_enabled
);
197 Preference pImprint
= findPreference("imprint");
198 if (pImprint
!= null
) {
199 if (imprintEnabled
) {
200 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
202 public boolean onPreferenceClick(Preference preference
) {
203 String imprintWeb
= (String
) getText(R
.string
.url_imprint
);
204 if (imprintWeb
!= null
&& imprintWeb
.length() > 0) {
205 Uri uriUrl
= Uri
.parse(imprintWeb
);
206 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uriUrl
);
207 startActivity(intent
);
209 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
214 preferenceCategory
.removePreference(pImprint
);
219 pAboutApp
= (Preference
) findPreference("about_app");
220 if (pAboutApp
!= null
) {
221 pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
)));
224 pkg
= getPackageManager().getPackageInfo(getPackageName(), 0);
225 pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
));
226 } catch (NameNotFoundException e
) {
227 Log_OC
.e(TAG
, "Error while showing about dialog", e
);
231 /* DISABLED FOR RELEASE UNTIL FIXED
232 pLogging = (CheckBoxPreference) findPreference("log_to_file");
233 if (pLogging != null) {
234 pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
236 public boolean onPreferenceChange(Preference preference, Object newValue) {
238 String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
240 if(!pLogging.isChecked()) {
241 Log_OC.d("Debug", "start logging");
242 Log_OC.v("PATH", logpath);
243 Log_OC.startLogging(logpath);
246 Log_OC.d("Debug", "stop logging");
247 Log_OC.stopLogging();
254 pLoggingHistory = (Preference) findPreference("log_history");
255 if (pLoggingHistory != null) {
256 pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
259 public boolean onPreferenceClick(Preference preference) {
260 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
261 startActivity(intent);
271 protected void onResume() {
272 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
273 boolean state
= appPrefs
.getBoolean("set_pincode", false
);
274 pCode
.setChecked(state
);
279 public boolean onCreateOptionsMenu(Menu menu
) {
280 super.onCreateOptionsMenu(menu
);
285 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
286 super.onMenuItemSelected(featureId
, item
);
289 switch (item
.getItemId()) {
290 //case R.id.addSessionItem:
292 intent
= new Intent(this, PreferencesNewSession
.class);
293 startActivityForResult(intent
, mNewSession
);
295 case R
.id
.SessionContextEdit
:
296 intent
= new Intent(this, PreferencesNewSession
.class);
297 intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
)
299 intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
)
301 intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
)
303 startActivityForResult(intent
, mEditSession
);
305 case android
.R
.id
.home
:
306 intent
= new Intent(getBaseContext(), FileDisplayActivity
.class);
307 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
308 startActivity(intent
);
311 Log_OC
.w(TAG
, "Unknown menu item triggered");
318 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
319 super.onActivityResult(requestCode
, resultCode
, data
);
323 protected void onDestroy() {
330 * Updates various summaries after updates. Also starts and stops
333 public boolean onPreferenceChange(Preference preference
, Object newValue
) {
334 // Update current account summary
335 /*if (preference.equals(mAccountList)) {
336 mAccountList.setSummary(newValue.toString());
339 // Update tracking interval summary
340 else*/ if (preference
.equals(mTrackingUpdateInterval
)) {
341 String trackingSummary
= getResources().getString(
342 R
.string
.prefs_trackmydevice_interval_summary
);
343 trackingSummary
= String
.format(trackingSummary
,
344 newValue
.toString());
345 mTrackingUpdateInterval
.setSummary(trackingSummary
);
348 // Start or stop tracking service
349 else if (preference
.equals(mDeviceTracking
)) {
350 Intent locationServiceIntent
= new Intent();
351 locationServiceIntent
352 .setAction("com.owncloud.android.location.LocationLauncher");
353 locationServiceIntent
.putExtra("TRACKING_SETTING",
355 sendBroadcast(locationServiceIntent
);