Preferences.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. import android.accounts.Account;
  20. import android.accounts.AccountManager;
  21. import android.content.Intent;
  22. import android.content.SharedPreferences;
  23. import android.content.pm.PackageInfo;
  24. import android.content.pm.PackageManager.NameNotFoundException;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.preference.CheckBoxPreference;
  28. import android.preference.Preference;
  29. import android.preference.Preference.OnPreferenceChangeListener;
  30. import android.preference.Preference.OnPreferenceClickListener;
  31. import android.preference.PreferenceCategory;
  32. import android.preference.PreferenceManager;
  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.MainApp;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.authentication.AccountUtils;
  40. import com.owncloud.android.db.DbHandler;
  41. import com.owncloud.android.utils.DisplayUtils;
  42. import com.owncloud.android.utils.Log_OC;
  43. /**
  44. * An Activity that allows the user to change the application's settings.
  45. *
  46. * @author Bartek Przybylski
  47. * @author David A. Velasco
  48. */
  49. public class Preferences extends SherlockPreferenceActivity {
  50. private static final String TAG = "OwnCloudPreferences";
  51. private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
  52. private DbHandler mDbHandler;
  53. private CheckBoxPreference pCode;
  54. //private CheckBoxPreference pLogging;
  55. //private Preference pLoggingHistory;
  56. private Preference pAboutApp;
  57. private Account mPreviousAccount = null;
  58. private PreferenceCategory mAccountsPrefCategory = null;
  59. @SuppressWarnings("deprecation")
  60. @Override
  61. public void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. mDbHandler = new DbHandler(getBaseContext());
  64. addPreferencesFromResource(R.xml.preferences);
  65. ActionBar actionBar = getSherlock().getActionBar();
  66. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  67. actionBar.setDisplayHomeAsUpEnabled(true);
  68. if (savedInstanceState != null) {
  69. mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
  70. } else {
  71. mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  72. }
  73. // Load the accounts category for adding the list of accounts
  74. mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
  75. pCode = (CheckBoxPreference) findPreference("set_pincode");
  76. if (pCode != null){
  77. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  78. @Override
  79. public boolean onPreferenceChange(Preference preference, Object newValue) {
  80. Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
  81. i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
  82. i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
  83. startActivity(i);
  84. return true;
  85. }
  86. });
  87. }
  88. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  89. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  90. Preference pHelp = findPreference("help");
  91. if (pHelp != null ){
  92. if (helpEnabled) {
  93. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  94. @Override
  95. public boolean onPreferenceClick(Preference preference) {
  96. String helpWeb =(String) getText(R.string.url_help);
  97. if (helpWeb != null && helpWeb.length() > 0) {
  98. Uri uriUrl = Uri.parse(helpWeb);
  99. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  100. startActivity(intent);
  101. }
  102. return true;
  103. }
  104. });
  105. } else {
  106. preferenceCategory.removePreference(pHelp);
  107. }
  108. }
  109. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  110. Preference pRecommend = findPreference("recommend");
  111. if (pRecommend != null){
  112. if (recommendEnabled) {
  113. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  114. @Override
  115. public boolean onPreferenceClick(Preference preference) {
  116. Intent intent = new Intent(Intent.ACTION_SENDTO);
  117. intent.setType("text/plain");
  118. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  119. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  120. String appName = getString(R.string.app_name);
  121. String downloadUrl = getString(R.string.url_app_download);
  122. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
  123. String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
  124. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  125. String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
  126. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  127. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  128. startActivity(intent);
  129. return(true);
  130. }
  131. });
  132. } else {
  133. preferenceCategory.removePreference(pRecommend);
  134. }
  135. }
  136. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  137. Preference pFeedback = findPreference("feedback");
  138. if (pFeedback != null){
  139. if (feedbackEnabled) {
  140. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  141. @Override
  142. public boolean onPreferenceClick(Preference preference) {
  143. String feedbackMail =(String) getText(R.string.mail_feedback);
  144. String feedback =(String) getText(R.string.prefs_feedback);
  145. Intent intent = new Intent(Intent.ACTION_SENDTO);
  146. intent.setType("text/plain");
  147. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  148. intent.setData(Uri.parse(feedbackMail));
  149. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  150. startActivity(intent);
  151. return true;
  152. }
  153. });
  154. } else {
  155. preferenceCategory.removePreference(pFeedback);
  156. }
  157. }
  158. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  159. Preference pImprint = findPreference("imprint");
  160. if (pImprint != null) {
  161. if (imprintEnabled) {
  162. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  163. @Override
  164. public boolean onPreferenceClick(Preference preference) {
  165. String imprintWeb = (String) getText(R.string.url_imprint);
  166. if (imprintWeb != null && imprintWeb.length() > 0) {
  167. Uri uriUrl = Uri.parse(imprintWeb);
  168. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  169. startActivity(intent);
  170. }
  171. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  172. return true;
  173. }
  174. });
  175. } else {
  176. preferenceCategory.removePreference(pImprint);
  177. }
  178. }
  179. /* About App */
  180. pAboutApp = (Preference) findPreference("about_app");
  181. if (pAboutApp != null) {
  182. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  183. PackageInfo pkg;
  184. try {
  185. pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  186. pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
  187. } catch (NameNotFoundException e) {
  188. Log_OC.e(TAG, "Error while showing about dialog", e);
  189. }
  190. }
  191. /* DISABLED FOR RELEASE UNTIL FIXED
  192. pLogging = (CheckBoxPreference) findPreference("log_to_file");
  193. if (pLogging != null) {
  194. pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  195. @Override
  196. public boolean onPreferenceChange(Preference preference, Object newValue) {
  197. String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
  198. if(!pLogging.isChecked()) {
  199. Log_OC.d("Debug", "start logging");
  200. Log_OC.v("PATH", logpath);
  201. Log_OC.startLogging(logpath);
  202. }
  203. else {
  204. Log_OC.d("Debug", "stop logging");
  205. Log_OC.stopLogging();
  206. }
  207. return true;
  208. }
  209. });
  210. }
  211. pLoggingHistory = (Preference) findPreference("log_history");
  212. if (pLoggingHistory != null) {
  213. pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  214. @Override
  215. public boolean onPreferenceClick(Preference preference) {
  216. Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
  217. startActivity(intent);
  218. return true;
  219. }
  220. });
  221. }
  222. */
  223. }
  224. @Override
  225. protected void onResume() {
  226. super.onResume();
  227. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  228. boolean state = appPrefs.getBoolean("set_pincode", false);
  229. pCode.setChecked(state);
  230. // Populate the accounts category with the list of accounts
  231. createAccountsCheckboxPreferences();
  232. }
  233. @Override
  234. public boolean onCreateOptionsMenu(Menu menu) {
  235. super.onCreateOptionsMenu(menu);
  236. return true;
  237. }
  238. @Override
  239. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  240. super.onMenuItemSelected(featureId, item);
  241. Intent intent;
  242. switch (item.getItemId()) {
  243. case android.R.id.home:
  244. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  245. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  246. startActivity(intent);
  247. break;
  248. default:
  249. Log_OC.w(TAG, "Unknown menu item triggered");
  250. return false;
  251. }
  252. return true;
  253. }
  254. @Override
  255. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  256. super.onActivityResult(requestCode, resultCode, data);
  257. }
  258. @Override
  259. protected void onDestroy() {
  260. mDbHandler.close();
  261. super.onDestroy();
  262. }
  263. /**
  264. * Create the list of accounts that has been added into the app
  265. */
  266. private void createAccountsCheckboxPreferences() {
  267. // Remove accounts in case list is refreshing for avoiding to have
  268. // duplicate items
  269. if (mAccountsPrefCategory.getPreferenceCount() > 0) {
  270. mAccountsPrefCategory.removeAll();
  271. }
  272. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  273. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  274. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  275. for (Account a : accounts) {
  276. CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
  277. checkBoxPreference.setKey(a.name);
  278. checkBoxPreference.setTitle(a.name);
  279. // Check the current account that is being used
  280. if (a.name.equals(currentAccount.name)) {
  281. checkBoxPreference.setChecked(true);
  282. }
  283. checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  284. @Override
  285. public boolean onPreferenceChange(Preference preference, Object newValue) {
  286. String key = preference.getKey();
  287. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  288. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  289. for (Account a : accounts) {
  290. @SuppressWarnings("deprecation")
  291. CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
  292. if (key.equals(a.name)) {
  293. p.setChecked(true);
  294. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
  295. } else {
  296. p.setChecked(false);
  297. }
  298. }
  299. return (Boolean) newValue;
  300. }
  301. });
  302. mAccountsPrefCategory.addPreference(checkBoxPreference);
  303. }
  304. // Add Create Account preference at the end of account list if Multiaccount is enabled
  305. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  306. createAddAccountPreference();
  307. }
  308. }
  309. /**
  310. * Create the preference for allow adding new accounts
  311. */
  312. private void createAddAccountPreference() {
  313. Preference addAccountPref = new Preference(this);
  314. addAccountPref.setKey("add_account");
  315. addAccountPref.setTitle(getString(R.string.prefs_add_account));
  316. mAccountsPrefCategory.addPreference(addAccountPref);
  317. addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  318. @Override
  319. public boolean onPreferenceClick(Preference preference) {
  320. AccountManager am = AccountManager.get(getApplicationContext());
  321. am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
  322. return true;
  323. }
  324. });
  325. }
  326. @Override
  327. protected void onPause() {
  328. if (this.isFinishing()) {
  329. Account current = AccountUtils.getCurrentOwnCloudAccount(this);
  330. if ((mPreviousAccount == null && current != null)
  331. || (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
  332. // the account set as default changed since this activity was
  333. // created
  334. // restart the main activity
  335. Intent i = new Intent(this, FileDisplayActivity.class);
  336. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  337. startActivity(i);
  338. }
  339. }
  340. super.onPause();
  341. }
  342. }