Preferences.java 24 KB

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