2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.operations
;
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
;
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
;
37 import android
.accounts
.Account
;
38 import android
.accounts
.AccountManager
;
39 import android
.content
.Context
;
43 * Remote operation that checks the version of an ownCloud server and stores it locally
45 public class UpdateOCVersionOperation
extends RemoteOperation
{
47 private static final String TAG
= UpdateOCVersionOperation
.class.getSimpleName();
49 private Account mAccount
;
50 private Context mContext
;
51 private OwnCloudVersion mOwnCloudVersion
;
54 public UpdateOCVersionOperation(Account account
, Context context
) {
57 mOwnCloudVersion
= null
;
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
;
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());
76 String response
= get
.getResponseBodyAsString();
77 if (response
!= null
) {
78 JSONObject json
= new JSONObject(response
);
79 if (json
!= null
&& json
.getString("version") != null
) {
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());
87 result
= new RemoteOperationResult(ResultCode
.OK
);
90 Log_OC
.w(TAG
, "Invalid version number received from server: " + json
.getString("version"));
91 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
96 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
99 Log_OC
.i(TAG
, "Check for update of ownCloud server version at " + client
.getWebdavUri() + ": " + result
.getLogMessage());
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
);
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
);
111 get
.releaseConnection();
117 public OwnCloudVersion
getOCVersion() {
118 return mOwnCloudVersion
;