Merge master into oauth_login
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UpdateOCVersionOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
21 import org.apache.commons.httpclient.HttpStatus;
22 import org.apache.commons.httpclient.methods.GetMethod;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25
26 import android.accounts.Account;
27 import android.accounts.AccountManager;
28 import android.content.Context;
29 import android.util.Log;
30
31 import com.owncloud.android.AccountUtils;
32 import com.owncloud.android.authenticator.AccountAuthenticator;
33 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
34 import com.owncloud.android.utils.OwnCloudVersion;
35
36 import eu.alefzero.webdav.WebdavClient;
37
38 /**
39 * Remote operation that checks the version of an ownCloud server and stores it locally
40 *
41 * @author David A. Velasco
42 */
43 public class UpdateOCVersionOperation extends RemoteOperation {
44
45 private static final String TAG = UpdateOCVersionOperation.class.getSimpleName();
46
47 private Account mAccount;
48 private Context mContext;
49
50
51 public UpdateOCVersionOperation(Account account, Context context) {
52 mAccount = account;
53 mContext = context;
54 }
55
56
57 @Override
58 protected RemoteOperationResult run(WebdavClient client) {
59 AccountManager accountMngr = AccountManager.get(mContext);
60 String statUrl = accountMngr.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
61 statUrl += AccountUtils.STATUS_PATH;
62 RemoteOperationResult result = null;
63 GetMethod get = null;
64 try {
65 get = new GetMethod(statUrl);
66 int status = client.executeMethod(get);
67 if (status != HttpStatus.SC_OK) {
68 client.exhaustResponse(get.getResponseBodyAsStream());
69 result = new RemoteOperationResult(false, status);
70
71 } else {
72 String response = get.getResponseBodyAsString();
73 if (response != null) {
74 JSONObject json = new JSONObject(response);
75 if (json != null && json.getString("version") != null) {
76 OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version"));
77 if (ocver.isVersionValid()) {
78 accountMngr.setUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION, ocver.toString());
79 Log.d(TAG, "Got new OC version " + ocver.toString());
80 result = new RemoteOperationResult(ResultCode.OK);
81
82 } else {
83 Log.w(TAG, "Invalid version number received from server: " + json.getString("version"));
84 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
85 }
86 }
87 }
88 if (result == null) {
89 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
90 }
91 }
92 Log.i(TAG, "Check for update of ownCloud server version at " + client.getBaseUri() + ": " + result.getLogMessage());
93
94 } catch (JSONException e) {
95 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
96 Log.e(TAG, "Check for update of ownCloud server version at " + client.getBaseUri() + ": " + result.getLogMessage(), e);
97
98 } catch (Exception e) {
99 result = new RemoteOperationResult(e);
100 Log.e(TAG, "Check for update of ownCloud server version at " + client.getBaseUri() + ": " + result.getLogMessage(), e);
101
102 } finally {
103 if (get != null)
104 get.releaseConnection();
105 }
106 return result;
107 }
108
109 }