UpdateOCVersionOperation.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private OwnCloudVersion mOwnCloudVersion;
  43. public UpdateOCVersionOperation(Account account, Context context) {
  44. mAccount = account;
  45. mContext = context;
  46. mOwnCloudVersion = null;
  47. }
  48. @Override
  49. protected RemoteOperationResult run(OwnCloudClient client) {
  50. AccountManager accountMngr = AccountManager.get(mContext);
  51. String statUrl = accountMngr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);
  52. statUrl += AccountUtils.STATUS_PATH;
  53. RemoteOperationResult result = null;
  54. GetMethod get = null;
  55. try {
  56. get = new GetMethod(statUrl);
  57. int status = client.executeMethod(get);
  58. if (status != HttpStatus.SC_OK) {
  59. client.exhaustResponse(get.getResponseBodyAsStream());
  60. result = new RemoteOperationResult(false, status, get.getResponseHeaders());
  61. } else {
  62. String response = get.getResponseBodyAsString();
  63. if (response != null) {
  64. JSONObject json = new JSONObject(response);
  65. if (json != null && json.getString("version") != null) {
  66. String version = json.getString("version");
  67. String versionstring = json.getString("versionstring");
  68. mOwnCloudVersion = new OwnCloudVersion(version, versionstring);
  69. if (mOwnCloudVersion.isVersionValid()) {
  70. accountMngr.setUserData(mAccount, Constants.KEY_OC_VERSION, mOwnCloudVersion.getVersion());
  71. accountMngr.setUserData(mAccount, Constants.KEY_OC_VERSION_STRING, mOwnCloudVersion.getVersionString());
  72. Log_OC.d(TAG, "Got new OC version " + mOwnCloudVersion.toString());
  73. result = new RemoteOperationResult(ResultCode.OK);
  74. } else {
  75. Log_OC.w(TAG, "Invalid version number received from server: " + json.getString("version"));
  76. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
  77. }
  78. }
  79. }
  80. if (result == null) {
  81. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  82. }
  83. }
  84. Log_OC.i(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage());
  85. } catch (JSONException e) {
  86. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  87. Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
  88. } catch (Exception e) {
  89. result = new RemoteOperationResult(e);
  90. Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
  91. } finally {
  92. if (get != null)
  93. get.releaseConnection();
  94. }
  95. return result;
  96. }
  97. public OwnCloudVersion getOCVersion() {
  98. return mOwnCloudVersion;
  99. }
  100. }