UpdateOCVersionOperation.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 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 android.content.Context;
  24. import com.owncloud.android.lib.common.OwnCloudClient;
  25. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  26. import com.owncloud.android.lib.common.operations.RemoteOperation;
  27. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  28. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  29. import com.owncloud.android.lib.common.utils.Log_OC;
  30. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  31. import org.apache.commons.httpclient.HttpStatus;
  32. import org.apache.commons.httpclient.methods.GetMethod;
  33. import org.json.JSONException;
  34. import org.json.JSONObject;
  35. /**
  36. * Remote operation that checks the version of an ownCloud server and stores it locally
  37. */
  38. public class UpdateOCVersionOperation extends RemoteOperation {
  39. private static final String TAG = UpdateOCVersionOperation.class.getSimpleName();
  40. private static final String STATUS_PATH = "/status.php";
  41. private Account mAccount;
  42. private Context mContext;
  43. private OwnCloudVersion mOwnCloudVersion;
  44. public UpdateOCVersionOperation(Account account, Context context) {
  45. mAccount = account;
  46. mContext = context;
  47. mOwnCloudVersion = null;
  48. }
  49. @Override
  50. protected RemoteOperationResult run(OwnCloudClient client) {
  51. AccountManager accountMngr = AccountManager.get(mContext);
  52. String statUrl = accountMngr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);
  53. statUrl += STATUS_PATH;
  54. RemoteOperationResult result = null;
  55. GetMethod getMethod = null;
  56. String webDav = client.getWebdavUri().toString();
  57. try {
  58. getMethod = new GetMethod(statUrl);
  59. int status = client.executeMethod(getMethod);
  60. if (status != HttpStatus.SC_OK) {
  61. result = new RemoteOperationResult(false, getMethod);
  62. client.exhaustResponse(getMethod.getResponseBodyAsStream());
  63. } else {
  64. String response = getMethod.getResponseBodyAsString();
  65. if (response != null) {
  66. JSONObject json = new JSONObject(response);
  67. if (json.getString("version") != null) {
  68. String version = json.getString("version");
  69. mOwnCloudVersion = new OwnCloudVersion(version);
  70. if (mOwnCloudVersion.isVersionValid()) {
  71. accountMngr.setUserData(mAccount, Constants.KEY_OC_VERSION, mOwnCloudVersion.getVersion());
  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 Nextcloud server version at " + webDav + ": " + result.getLogMessage());
  85. } catch (JSONException e) {
  86. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  87. Log_OC.e(TAG, "Check for update of Nextcloud server version at " + webDav + ": " + result.getLogMessage(), e);
  88. } catch (Exception e) {
  89. result = new RemoteOperationResult(e);
  90. Log_OC.e(TAG, "Check for update of Nextcloud server version at " + webDav + ": " + result.getLogMessage(), e);
  91. } finally {
  92. if (getMethod != null) {
  93. getMethod.releaseConnection();
  94. }
  95. }
  96. return result;
  97. }
  98. public OwnCloudVersion getOCVersion() {
  99. return mOwnCloudVersion;
  100. }
  101. }