UpdateOCVersionOperation.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. import org.apache.commons.httpclient.HttpStatus;
  19. import org.apache.commons.httpclient.methods.GetMethod;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22. import com.owncloud.android.authentication.AccountUtils;
  23. import com.owncloud.android.lib.common.OwnCloudClient;
  24. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  25. import com.owncloud.android.lib.common.operations.RemoteOperation;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  27. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  28. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  29. import com.owncloud.android.utils.Log_OC;
  30. import android.accounts.Account;
  31. import android.accounts.AccountManager;
  32. import android.content.Context;
  33. /**
  34. * Remote operation that checks the version of an ownCloud server and stores it locally
  35. *
  36. * @author David A. Velasco
  37. */
  38. public class UpdateOCVersionOperation extends RemoteOperation {
  39. private static final String TAG = UpdateOCVersionOperation.class.getSimpleName();
  40. private Account mAccount;
  41. private Context mContext;
  42. public UpdateOCVersionOperation(Account account, Context context) {
  43. mAccount = account;
  44. mContext = context;
  45. }
  46. @Override
  47. protected RemoteOperationResult run(OwnCloudClient client) {
  48. AccountManager accountMngr = AccountManager.get(mContext);
  49. String statUrl = accountMngr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);
  50. statUrl += AccountUtils.STATUS_PATH;
  51. RemoteOperationResult result = null;
  52. GetMethod get = null;
  53. try {
  54. get = new GetMethod(statUrl);
  55. int status = client.executeMethod(get);
  56. if (status != HttpStatus.SC_OK) {
  57. client.exhaustResponse(get.getResponseBodyAsStream());
  58. result = new RemoteOperationResult(false, status, get.getResponseHeaders());
  59. } else {
  60. String response = get.getResponseBodyAsString();
  61. if (response != null) {
  62. JSONObject json = new JSONObject(response);
  63. if (json != null && json.getString("version") != null) {
  64. OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version"));
  65. if (ocver.isVersionValid()) {
  66. accountMngr.setUserData(mAccount, Constants.KEY_OC_VERSION, ocver.toString());
  67. Log_OC.d(TAG, "Got new OC version " + ocver.toString());
  68. result = new RemoteOperationResult(ResultCode.OK);
  69. } else {
  70. Log_OC.w(TAG, "Invalid version number received from server: " + json.getString("version"));
  71. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
  72. }
  73. }
  74. }
  75. if (result == null) {
  76. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  77. }
  78. }
  79. Log_OC.i(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage());
  80. } catch (JSONException e) {
  81. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  82. Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
  83. } catch (Exception e) {
  84. result = new RemoteOperationResult(e);
  85. Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
  86. } finally {
  87. if (get != null)
  88. get.releaseConnection();
  89. }
  90. return result;
  91. }
  92. }