UpdateOCVersionOperation.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 android.accounts.Account;
  23. import android.accounts.AccountManager;
  24. import android.content.Context;
  25. import com.owncloud.android.authentication.AccountAuthenticator;
  26. import com.owncloud.android.authentication.AccountUtils;
  27. import com.owncloud.android.Log_OC;
  28. import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
  29. import com.owncloud.android.utils.OwnCloudVersion;
  30. import eu.alefzero.webdav.WebdavClient;
  31. /**
  32. * Remote operation that checks the version of an ownCloud server and stores it locally
  33. *
  34. * @author David A. Velasco
  35. */
  36. public class UpdateOCVersionOperation extends RemoteOperation {
  37. private static final String TAG = UpdateOCVersionOperation.class.getSimpleName();
  38. private Account mAccount;
  39. private Context mContext;
  40. public UpdateOCVersionOperation(Account account, Context context) {
  41. mAccount = account;
  42. mContext = context;
  43. }
  44. @Override
  45. protected RemoteOperationResult run(WebdavClient client) {
  46. AccountManager accountMngr = AccountManager.get(mContext);
  47. String statUrl = accountMngr.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
  48. statUrl += AccountUtils.STATUS_PATH;
  49. RemoteOperationResult result = null;
  50. GetMethod get = null;
  51. try {
  52. get = new GetMethod(statUrl);
  53. int status = client.executeMethod(get);
  54. if (status != HttpStatus.SC_OK) {
  55. client.exhaustResponse(get.getResponseBodyAsStream());
  56. result = new RemoteOperationResult(false, status);
  57. } else {
  58. String response = get.getResponseBodyAsString();
  59. if (response != null) {
  60. JSONObject json = new JSONObject(response);
  61. if (json != null && json.getString("version") != null) {
  62. OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version"));
  63. if (ocver.isVersionValid()) {
  64. accountMngr.setUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION, ocver.toString());
  65. Log_OC.d(TAG, "Got new OC version " + ocver.toString());
  66. result = new RemoteOperationResult(ResultCode.OK);
  67. } else {
  68. Log_OC.w(TAG, "Invalid version number received from server: " + json.getString("version"));
  69. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
  70. }
  71. }
  72. }
  73. if (result == null) {
  74. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  75. }
  76. }
  77. Log_OC.i(TAG, "Check for update of ownCloud server version at " + client.getBaseUri() + ": " + result.getLogMessage());
  78. } catch (JSONException e) {
  79. result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
  80. Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getBaseUri() + ": " + result.getLogMessage(), e);
  81. } catch (Exception e) {
  82. result = new RemoteOperationResult(e);
  83. Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getBaseUri() + ": " + result.getLogMessage(), e);
  84. } finally {
  85. if (get != null)
  86. get.releaseConnection();
  87. }
  88. return result;
  89. }
  90. }