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