Merge branch 'develop' into share_password_support
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UpdateOCVersionOperation.java
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
21 package com.owncloud.android.operations;
22
23 import org.apache.commons.httpclient.HttpStatus;
24 import org.apache.commons.httpclient.methods.GetMethod;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27
28 import com.owncloud.android.authentication.AccountUtils;
29 import com.owncloud.android.lib.common.OwnCloudClient;
30 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
31 import com.owncloud.android.lib.common.operations.RemoteOperation;
32 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
33 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
34 import com.owncloud.android.lib.common.utils.Log_OC;
35 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
36
37 import android.accounts.Account;
38 import android.accounts.AccountManager;
39 import android.content.Context;
40
41
42 /**
43 * Remote operation that checks the version of an ownCloud server and stores it locally
44 */
45 public class UpdateOCVersionOperation extends RemoteOperation {
46
47 private static final String TAG = UpdateOCVersionOperation.class.getSimpleName();
48
49 private Account mAccount;
50 private Context mContext;
51 private OwnCloudVersion mOwnCloudVersion;
52
53
54 public UpdateOCVersionOperation(Account account, Context context) {
55 mAccount = account;
56 mContext = context;
57 mOwnCloudVersion = null;
58 }
59
60
61 @Override
62 protected RemoteOperationResult run(OwnCloudClient client) {
63 AccountManager accountMngr = AccountManager.get(mContext);
64 String statUrl = accountMngr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);
65 statUrl += AccountUtils.STATUS_PATH;
66 RemoteOperationResult result = null;
67 GetMethod get = null;
68 try {
69 get = new GetMethod(statUrl);
70 int status = client.executeMethod(get);
71 if (status != HttpStatus.SC_OK) {
72 client.exhaustResponse(get.getResponseBodyAsStream());
73 result = new RemoteOperationResult(false, status, get.getResponseHeaders());
74
75 } else {
76 String response = get.getResponseBodyAsString();
77 if (response != null) {
78 JSONObject json = new JSONObject(response);
79 if (json != null && json.getString("version") != null) {
80
81 String version = json.getString("version");
82 mOwnCloudVersion = new OwnCloudVersion(version);
83 if (mOwnCloudVersion.isVersionValid()) {
84 accountMngr.setUserData(mAccount, Constants.KEY_OC_VERSION, mOwnCloudVersion.getVersion());
85 Log_OC.d(TAG, "Got new OC version " + mOwnCloudVersion.toString());
86
87 result = new RemoteOperationResult(ResultCode.OK);
88
89 } else {
90 Log_OC.w(TAG, "Invalid version number received from server: " + json.getString("version"));
91 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
92 }
93 }
94 }
95 if (result == null) {
96 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
97 }
98 }
99 Log_OC.i(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage());
100
101 } catch (JSONException e) {
102 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
103 Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
104
105 } catch (Exception e) {
106 result = new RemoteOperationResult(e);
107 Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
108
109 } finally {
110 if (get != null)
111 get.releaseConnection();
112 }
113 return result;
114 }
115
116
117 public OwnCloudVersion getOCVersion() {
118 return mOwnCloudVersion;
119 }
120
121 }