GetUserProfileOperation.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.operations;
  21. import android.accounts.Account;
  22. import android.accounts.AccountManager;
  23. import com.owncloud.android.MainApp;
  24. import com.owncloud.android.lib.common.OwnCloudClient;
  25. import com.owncloud.android.lib.common.UserInfo;
  26. import com.owncloud.android.lib.common.accounts.AccountUtils;
  27. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  28. import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
  29. import com.owncloud.android.operations.common.SyncOperation;
  30. /**
  31. * Get and save user's profile from the server.
  32. *
  33. * Currently only retrieves the display name.
  34. */
  35. public class GetUserProfileOperation extends SyncOperation {
  36. /**
  37. * Performs the operation.
  38. *
  39. * Target user account is implicit in 'client'.
  40. *
  41. * Stored account is implicit in {@link #getStorageManager()}.
  42. *
  43. * @return Result of the operation. If successful, includes an instance of
  44. * {@link String} with the display name retrieved from the server.
  45. * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
  46. */
  47. @Override
  48. protected RemoteOperationResult run(OwnCloudClient client) {
  49. // get display name
  50. GetRemoteUserInfoOperation getDisplayName = new GetRemoteUserInfoOperation();
  51. RemoteOperationResult result = getDisplayName.execute(client);
  52. if (result.isSuccess()) {
  53. // store display name with account data
  54. AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
  55. UserInfo userInfo = (UserInfo) result.getData().get(0);
  56. Account storedAccount = getStorageManager().getAccount();
  57. accountManager.setUserData(
  58. storedAccount,
  59. AccountUtils.Constants.KEY_DISPLAY_NAME,
  60. userInfo.getDisplayName()
  61. );
  62. }
  63. return result;
  64. }
  65. }