Preferences.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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.accounts.AccountManagerCallback;
  22. import android.accounts.AccountManagerFuture;
  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.os.Handler;
  30. import android.preference.CheckBoxPreference;
  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;
  36. import android.view.ContextMenu;
  37. import android.view.ContextMenu.ContextMenuInfo;
  38. import android.view.View;
  39. import android.widget.AdapterView;
  40. import android.widget.AdapterView.OnItemLongClickListener;
  41. import android.widget.ListAdapter;
  42. import android.widget.ListView;
  43. import com.actionbarsherlock.app.ActionBar;
  44. import com.actionbarsherlock.app.SherlockPreferenceActivity;
  45. import com.actionbarsherlock.view.Menu;
  46. import com.actionbarsherlock.view.MenuItem;
  47. import com.owncloud.android.MainApp;
  48. import com.owncloud.android.R;
  49. import com.owncloud.android.authentication.AccountUtils;
  50. import com.owncloud.android.authentication.AuthenticatorActivity;
  51. import com.owncloud.android.db.DbHandler;
  52. import com.owncloud.android.lib.common.utils.Log_OC;
  53. import com.owncloud.android.ui.RadioButtonPreference;
  54. import com.owncloud.android.utils.DisplayUtils;
  55. /**
  56. * An Activity that allows the user to change the application's settings.
  57. *
  58. * @author Bartek Przybylski
  59. * @author David A. Velasco
  60. */
  61. public class Preferences extends SherlockPreferenceActivity implements AccountManagerCallback<Boolean> {
  62. private static final String TAG = "OwnCloudPreferences";
  63. private DbHandler mDbHandler;
  64. private CheckBoxPreference pCode;
  65. private Preference pAboutApp;
  66. private PreferenceCategory mAccountsPrefCategory = null;
  67. private final Handler mHandler = new Handler();
  68. private String mAccountName;
  69. private boolean mShowContextMenu = false;
  70. private String mUploadPath;
  71. @SuppressWarnings("deprecation")
  72. @Override
  73. public void onCreate(Bundle savedInstanceState) {
  74. super.onCreate(savedInstanceState);
  75. mDbHandler = new DbHandler(getBaseContext());
  76. addPreferencesFromResource(R.xml.preferences);
  77. ActionBar actionBar = getSherlock().getActionBar();
  78. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  79. actionBar.setDisplayHomeAsUpEnabled(true);
  80. actionBar.setTitle(R.string.actionbar_settings);
  81. loadInstantUploadPath();
  82. // Load the accounts category for adding the list of accounts
  83. mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
  84. ListView listView = getListView();
  85. listView.setOnItemLongClickListener(new OnItemLongClickListener() {
  86. @Override
  87. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  88. ListView listView = (ListView) parent;
  89. ListAdapter listAdapter = listView.getAdapter();
  90. Object obj = listAdapter.getItem(position);
  91. if (obj != null && obj instanceof RadioButtonPreference) {
  92. mShowContextMenu = true;
  93. mAccountName = ((RadioButtonPreference) obj).getKey();
  94. Preferences.this.openContextMenu(listView);
  95. View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
  96. return longListener.onLongClick(view);
  97. }
  98. return false;
  99. }
  100. });
  101. // Register context menu for list of preferences.
  102. registerForContextMenu(getListView());
  103. pCode = (CheckBoxPreference) findPreference("set_pincode");
  104. if (pCode != null){
  105. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  106. @Override
  107. public boolean onPreferenceChange(Preference preference, Object newValue) {
  108. Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
  109. i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
  110. i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
  111. startActivity(i);
  112. return true;
  113. }
  114. });
  115. }
  116. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  117. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  118. Preference pHelp = findPreference("help");
  119. if (pHelp != null ){
  120. if (helpEnabled) {
  121. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  122. @Override
  123. public boolean onPreferenceClick(Preference preference) {
  124. String helpWeb =(String) getText(R.string.url_help);
  125. if (helpWeb != null && helpWeb.length() > 0) {
  126. Uri uriUrl = Uri.parse(helpWeb);
  127. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  128. startActivity(intent);
  129. }
  130. return true;
  131. }
  132. });
  133. } else {
  134. preferenceCategory.removePreference(pHelp);
  135. }
  136. }
  137. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  138. Preference pRecommend = findPreference("recommend");
  139. if (pRecommend != null){
  140. if (recommendEnabled) {
  141. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  142. @Override
  143. public boolean onPreferenceClick(Preference preference) {
  144. Intent intent = new Intent(Intent.ACTION_SENDTO);
  145. intent.setType("text/plain");
  146. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  147. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  148. String appName = getString(R.string.app_name);
  149. String downloadUrl = getString(R.string.url_app_download);
  150. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
  151. String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
  152. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  153. String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
  154. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  155. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  156. startActivity(intent);
  157. return(true);
  158. }
  159. });
  160. } else {
  161. preferenceCategory.removePreference(pRecommend);
  162. }
  163. }
  164. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  165. Preference pFeedback = findPreference("feedback");
  166. if (pFeedback != null){
  167. if (feedbackEnabled) {
  168. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  169. @Override
  170. public boolean onPreferenceClick(Preference preference) {
  171. String feedbackMail =(String) getText(R.string.mail_feedback);
  172. String feedback =(String) getText(R.string.prefs_feedback);
  173. Intent intent = new Intent(Intent.ACTION_SENDTO);
  174. intent.setType("text/plain");
  175. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  176. intent.setData(Uri.parse(feedbackMail));
  177. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  178. startActivity(intent);
  179. return true;
  180. }
  181. });
  182. } else {
  183. preferenceCategory.removePreference(pFeedback);
  184. }
  185. }
  186. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  187. Preference pImprint = findPreference("imprint");
  188. if (pImprint != null) {
  189. if (imprintEnabled) {
  190. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  191. @Override
  192. public boolean onPreferenceClick(Preference preference) {
  193. String imprintWeb = (String) getText(R.string.url_imprint);
  194. if (imprintWeb != null && imprintWeb.length() > 0) {
  195. Uri uriUrl = Uri.parse(imprintWeb);
  196. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  197. startActivity(intent);
  198. }
  199. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  200. return true;
  201. }
  202. });
  203. } else {
  204. preferenceCategory.removePreference(pImprint);
  205. }
  206. }
  207. Preference pInstantUploadPathApp = (Preference) findPreference("instant_upload_path");
  208. pInstantUploadPathApp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  209. @Override
  210. public boolean onPreferenceChange(Preference preference, Object newValue) {
  211. mUploadPath = updateInstantUploadPath(newValue.toString());
  212. return true;
  213. }
  214. });
  215. /* About App */
  216. pAboutApp = (Preference) findPreference("about_app");
  217. if (pAboutApp != null) {
  218. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  219. PackageInfo pkg;
  220. try {
  221. pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  222. pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
  223. } catch (NameNotFoundException e) {
  224. Log_OC.e(TAG, "Error while showing about dialog", e);
  225. }
  226. }
  227. }
  228. @Override
  229. protected void onPause() {
  230. saveInstantUploadPathOnPreferences();
  231. super.onPause();
  232. }
  233. @Override
  234. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  235. // Filter for only showing contextual menu when long press on the
  236. // accounts
  237. if (mShowContextMenu) {
  238. getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
  239. mShowContextMenu = false;
  240. }
  241. super.onCreateContextMenu(menu, v, menuInfo);
  242. }
  243. /**
  244. * Called when the user clicked on an item into the context menu created at
  245. * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
  246. * every ownCloud {@link Account} , containing 'secondary actions' for them.
  247. *
  248. * {@inheritDoc}
  249. */
  250. @Override
  251. public boolean onContextItemSelected(android.view.MenuItem item) {
  252. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  253. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  254. for (Account a : accounts) {
  255. if (a.name.equals(mAccountName)) {
  256. if (item.getItemId() == R.id.change_password) {
  257. // Change account password
  258. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  259. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
  260. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  261. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  262. startActivity(updateAccountCredentials);
  263. } else if (item.getItemId() == R.id.delete_account) {
  264. // Remove account
  265. am.removeAccount(a, this, mHandler);
  266. }
  267. }
  268. }
  269. return true;
  270. }
  271. @Override
  272. public void run(AccountManagerFuture<Boolean> future) {
  273. if (future.isDone()) {
  274. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  275. String accountName = "";
  276. if (a == null) {
  277. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  278. if (accounts.length != 0)
  279. accountName = accounts[0].name;
  280. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  281. }
  282. addAccountsCheckboxPreferences();
  283. }
  284. }
  285. @Override
  286. protected void onResume() {
  287. super.onResume();
  288. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  289. boolean state = appPrefs.getBoolean("set_pincode", false);
  290. pCode.setChecked(state);
  291. // Populate the accounts category with the list of accounts
  292. addAccountsCheckboxPreferences();
  293. }
  294. @Override
  295. public boolean onCreateOptionsMenu(Menu menu) {
  296. super.onCreateOptionsMenu(menu);
  297. return true;
  298. }
  299. @Override
  300. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  301. super.onMenuItemSelected(featureId, item);
  302. Intent intent;
  303. switch (item.getItemId()) {
  304. case android.R.id.home:
  305. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  306. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  307. startActivity(intent);
  308. break;
  309. default:
  310. Log_OC.w(TAG, "Unknown menu item triggered");
  311. return false;
  312. }
  313. return true;
  314. }
  315. @Override
  316. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  317. super.onActivityResult(requestCode, resultCode, data);
  318. }
  319. @Override
  320. protected void onDestroy() {
  321. mDbHandler.close();
  322. super.onDestroy();
  323. }
  324. /**
  325. * Create the list of accounts that has been added into the app
  326. */
  327. @SuppressWarnings("deprecation")
  328. private void addAccountsCheckboxPreferences() {
  329. // Remove accounts in case list is refreshing for avoiding to have
  330. // duplicate items
  331. if (mAccountsPrefCategory.getPreferenceCount() > 0) {
  332. mAccountsPrefCategory.removeAll();
  333. }
  334. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  335. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  336. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  337. if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
  338. // Show create account screen if there isn't any account
  339. am.addAccount(MainApp.getAccountType(), null, null, null, this,
  340. null,
  341. null);
  342. }
  343. else {
  344. for (Account a : accounts) {
  345. RadioButtonPreference accountPreference = new RadioButtonPreference(this);
  346. accountPreference.setKey(a.name);
  347. // Handle internationalized domain names
  348. accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
  349. mAccountsPrefCategory.addPreference(accountPreference);
  350. // Check the current account that is being used
  351. if (a.name.equals(currentAccount.name)) {
  352. accountPreference.setChecked(true);
  353. } else {
  354. accountPreference.setChecked(false);
  355. }
  356. accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  357. @Override
  358. public boolean onPreferenceChange(Preference preference, Object newValue) {
  359. String key = preference.getKey();
  360. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  361. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  362. for (Account a : accounts) {
  363. RadioButtonPreference p = (RadioButtonPreference) findPreference(a.name);
  364. if (key.equals(a.name)) {
  365. boolean accountChanged = !p.isChecked();
  366. p.setChecked(true);
  367. AccountUtils.setCurrentOwnCloudAccount(
  368. getApplicationContext(),
  369. a.name
  370. );
  371. if (accountChanged) {
  372. // restart the main activity
  373. Intent i = new Intent(
  374. Preferences.this,
  375. FileDisplayActivity.class
  376. );
  377. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  378. startActivity(i);
  379. } else {
  380. finish();
  381. }
  382. } else {
  383. p.setChecked(false);
  384. }
  385. }
  386. return (Boolean) newValue;
  387. }
  388. });
  389. }
  390. // Add Create Account preference at the end of account list if
  391. // Multiaccount is enabled
  392. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  393. createAddAccountPreference();
  394. }
  395. }
  396. }
  397. /**
  398. * Create the preference for allow adding new accounts
  399. */
  400. private void createAddAccountPreference() {
  401. Preference addAccountPref = new Preference(this);
  402. addAccountPref.setKey("add_account");
  403. addAccountPref.setTitle(getString(R.string.prefs_add_account));
  404. mAccountsPrefCategory.addPreference(addAccountPref);
  405. addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  406. @Override
  407. public boolean onPreferenceClick(Preference preference) {
  408. AccountManager am = AccountManager.get(getApplicationContext());
  409. am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
  410. return true;
  411. }
  412. });
  413. }
  414. /**
  415. * Update the upload path checking that it is a correct path
  416. * @param uploadPath: path write by user
  417. * @return String: uploadPath
  418. */
  419. private String updateInstantUploadPath(String uploadPath) {
  420. String slashString = "/";
  421. // If slashes are duplicated, replace them for only one slash
  422. uploadPath = uploadPath.replaceAll("/+", slashString);
  423. // Remove last slash from path
  424. if (uploadPath.length() > 0 && uploadPath.charAt(uploadPath.length()-1) == slashString.charAt(0)) {
  425. uploadPath = uploadPath.substring(0, uploadPath.length()-1);
  426. }
  427. if (uploadPath.isEmpty()) { // Set default instant upload path
  428. uploadPath = getString(R.string.instant_upload_path);
  429. }else {
  430. if (!uploadPath.startsWith(slashString)) { // Add initial slash on path if necessary
  431. uploadPath = slashString.concat(uploadPath);
  432. }
  433. }
  434. return uploadPath;
  435. }
  436. /**
  437. * Load upload path set on preferences
  438. */
  439. private void loadInstantUploadPath() {
  440. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  441. mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
  442. }
  443. /**
  444. * Save the "Instant Upload Path" on preferences
  445. */
  446. private void saveInstantUploadPathOnPreferences() {
  447. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  448. SharedPreferences.Editor editor = appPrefs.edit();
  449. editor.putString("instant_upload_path", mUploadPath);
  450. editor.commit();
  451. }
  452. }