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 com
.owncloud
.android
.authentication
.AccountUtils
;
26 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
27 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
28 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
31 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
32 import com
.owncloud
.android
.utils
.Log_OC
;
34 import android
.accounts
.Account
;
35 import android
.accounts
.AccountManager
;
36 import android
.content
.Context
;
40 * Remote operation that checks the version of an ownCloud server and stores it locally
42 * @author David A. Velasco
44 public class UpdateOCVersionOperation
extends RemoteOperation
{
46 private static final String TAG
= UpdateOCVersionOperation
.class.getSimpleName();
48 private Account mAccount
;
49 private Context mContext
;
50 private OwnCloudVersion mOwnCloudVersion
;
53 public UpdateOCVersionOperation(Account account
, Context context
) {
56 mOwnCloudVersion
= null
;
61 protected RemoteOperationResult
run(OwnCloudClient client
) {
62 AccountManager accountMngr
= AccountManager
.get(mContext
);
63 String statUrl
= accountMngr
.getUserData(mAccount
, Constants
.KEY_OC_BASE_URL
);
64 statUrl
+= AccountUtils
.STATUS_PATH
;
65 RemoteOperationResult result
= null
;
68 get
= new GetMethod(statUrl
);
69 int status
= client
.executeMethod(get
);
70 if (status
!= HttpStatus
.SC_OK
) {
71 client
.exhaustResponse(get
.getResponseBodyAsStream());
72 result
= new RemoteOperationResult(false
, status
, get
.getResponseHeaders());
75 String response
= get
.getResponseBodyAsString();
76 if (response
!= null
) {
77 JSONObject json
= new JSONObject(response
);
78 if (json
!= null
&& json
.getString("version") != null
) {
80 String version
= json
.getString("version");
81 String versionstring
= json
.getString("versionstring");
82 mOwnCloudVersion
= new OwnCloudVersion(version
, versionstring
);
83 if (mOwnCloudVersion
.isVersionValid()) {
84 accountMngr
.setUserData(mAccount
, Constants
.KEY_OC_VERSION
, mOwnCloudVersion
.getVersion());
85 accountMngr
.setUserData(mAccount
, Constants
.KEY_OC_VERSION_STRING
, mOwnCloudVersion
.getVersionString());
86 Log_OC
.d(TAG
, "Got new OC version " + mOwnCloudVersion
.toString());
88 result
= new RemoteOperationResult(ResultCode
.OK
);
91 Log_OC
.w(TAG
, "Invalid version number received from server: " + json
.getString("version"));
92 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
97 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
100 Log_OC
.i(TAG
, "Check for update of ownCloud server version at " + client
.getWebdavUri() + ": " + result
.getLogMessage());
102 } catch (JSONException e
) {
103 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
104 Log_OC
.e(TAG
, "Check for update of ownCloud server version at " + client
.getWebdavUri() + ": " + result
.getLogMessage(), e
);
106 } catch (Exception e
) {
107 result
= new RemoteOperationResult(e
);
108 Log_OC
.e(TAG
, "Check for update of ownCloud server version at " + client
.getWebdavUri() + ": " + result
.getLogMessage(), e
);
112 get
.releaseConnection();
118 public OwnCloudVersion
getOCVersion() {
119 return mOwnCloudVersion
;