1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
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
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.content
.Context
;
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
.oc_framework
.accounts
.OwnCloudAccount
;
33 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
34 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
35 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
36 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
.ResultCode
;
37 import com
.owncloud
.android
.oc_framework
.utils
.OwnCloudVersion
;
41 * Remote operation that checks the version of an ownCloud server and stores it locally
43 * @author David A. Velasco
45 public class UpdateOCVersionOperation
extends RemoteOperation
{
47 private static final String TAG
= UpdateOCVersionOperation
.class.getSimpleName();
49 private Account mAccount
;
50 private Context mContext
;
53 public UpdateOCVersionOperation(Account account
, Context context
) {
60 protected RemoteOperationResult
run(WebdavClient client
) {
61 AccountManager accountMngr
= AccountManager
.get(mContext
);
62 String statUrl
= accountMngr
.getUserData(mAccount
, OwnCloudAccount
.Constants
.KEY_OC_BASE_URL
);
63 statUrl
+= AccountUtils
.STATUS_PATH
;
64 RemoteOperationResult result
= null
;
67 get
= new GetMethod(statUrl
);
68 int status
= client
.executeMethod(get
);
69 if (status
!= HttpStatus
.SC_OK
) {
70 client
.exhaustResponse(get
.getResponseBodyAsStream());
71 result
= new RemoteOperationResult(false
, status
, get
.getResponseHeaders());
74 String response
= get
.getResponseBodyAsString();
75 if (response
!= null
) {
76 JSONObject json
= new JSONObject(response
);
77 if (json
!= null
&& json
.getString("version") != null
) {
78 OwnCloudVersion ocver
= new OwnCloudVersion(json
.getString("version"));
79 if (ocver
.isVersionValid()) {
80 accountMngr
.setUserData(mAccount
, OwnCloudAccount
.Constants
.KEY_OC_VERSION
, ocver
.toString());
81 Log_OC
.d(TAG
, "Got new OC version " + ocver
.toString());
82 result
= new RemoteOperationResult(ResultCode
.OK
);
85 Log_OC
.w(TAG
, "Invalid version number received from server: " + json
.getString("version"));
86 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
91 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
94 Log_OC
.i(TAG
, "Check for update of ownCloud server version at " + client
.getBaseUri() + ": " + result
.getLogMessage());
96 } catch (JSONException e
) {
97 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
98 Log_OC
.e(TAG
, "Check for update of ownCloud server version at " + client
.getBaseUri() + ": " + result
.getLogMessage(), e
);
100 } catch (Exception e
) {
101 result
= new RemoteOperationResult(e
);
102 Log_OC
.e(TAG
, "Check for update of ownCloud server version at " + client
.getBaseUri() + ": " + result
.getLogMessage(), e
);
106 get
.releaseConnection();