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