28
28
import android .net .ConnectivityManager ;
29
29
import android .net .Uri ;
30
30
31
- import com .owncloud . android . lib . common .OwnCloudClient ;
32
- import com .owncloud . android . lib . common . OwnCloudClientManagerFactory ;
31
+ import com .nextcloud . common .NextcloudClient ;
32
+ import com .nextcloud . operations . GetMethod ;
33
33
import com .owncloud .android .lib .common .accounts .AccountUtils ;
34
34
import com .owncloud .android .lib .common .operations .RemoteOperation ;
35
35
import com .owncloud .android .lib .common .operations .RemoteOperationResult ;
36
36
import com .owncloud .android .lib .common .utils .Log_OC ;
37
37
38
38
import org .apache .commons .httpclient .HttpStatus ;
39
- import org .apache .commons .httpclient .methods .GetMethod ;
40
- import org .apache .commons .httpclient .params .HttpMethodParams ;
41
- import org .apache .commons .httpclient .params .HttpParams ;
42
39
import org .json .JSONException ;
43
40
import org .json .JSONObject ;
44
41
45
- import java . util . ArrayList ;
42
+ import kotlin . Pair ;
46
43
47
44
/**
48
45
* Checks if the server is valid and if the server supports the Share API
49
46
*
50
47
* @author David A. Velasco
51
48
* @author masensio
52
49
*/
53
- public class GetStatusRemoteOperation extends RemoteOperation {
54
-
55
- /**
56
- * Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs.
57
- */
58
- private static final int TRY_CONNECTION_TIMEOUT = 50000 ;
59
-
50
+ public class GetStatusRemoteOperation extends RemoteOperation <Pair <OwnCloudVersion , Boolean >> {
60
51
private static final String TAG = GetStatusRemoteOperation .class .getSimpleName ();
61
52
62
53
private static final String NODE_INSTALLED = "installed" ;
@@ -66,28 +57,24 @@ public class GetStatusRemoteOperation extends RemoteOperation {
66
57
private static final String PROTOCOL_HTTP = "http://" ;
67
58
private static final int UNTRUSTED_DOMAIN_ERROR_CODE = 15 ;
68
59
69
- private RemoteOperationResult mLatestResult ;
70
- private Context mContext ;
60
+ private RemoteOperationResult < Pair < OwnCloudVersion , Boolean >> mLatestResult ;
61
+ private final Context mContext ;
71
62
72
63
public GetStatusRemoteOperation (Context context ) {
73
64
mContext = context ;
74
65
}
75
66
76
- private boolean tryConnection (OwnCloudClient client ) {
67
+ private boolean tryConnection (NextcloudClient client ) {
77
68
boolean retval = false ;
78
- GetMethod get = null ;
79
- String baseUrlSt = client .getBaseUri (). toString ( );
69
+ com . nextcloud . operations . GetMethod get = null ;
70
+ String baseUrlSt = String . valueOf ( client .getBaseUri ());
80
71
try {
81
- get = new GetMethod (baseUrlSt + AccountUtils .STATUS_PATH );
82
-
83
- HttpParams params = HttpMethodParams .getDefaultParams ();
84
- params .setParameter (HttpMethodParams .USER_AGENT , OwnCloudClientManagerFactory .getUserAgent ());
85
- get .getParams ().setDefaults (params );
72
+ get = new com .nextcloud .operations .GetMethod (baseUrlSt + AccountUtils .STATUS_PATH , false );
86
73
87
74
client .setFollowRedirects (false );
88
75
boolean isRedirectToNonSecureConnection = false ;
89
- int status = client .executeMethod (get , TRY_CONNECTION_TIMEOUT , TRY_CONNECTION_TIMEOUT );
90
- mLatestResult = new RemoteOperationResult ((status == HttpStatus .SC_OK ), get );
76
+ int status = client .execute (get );
77
+ mLatestResult = new RemoteOperationResult <> ((status == HttpStatus .SC_OK ), get );
91
78
92
79
String redirectedLocation = mLatestResult .getRedirectedLocation ();
93
80
while (redirectedLocation != null && redirectedLocation .length () > 0
@@ -98,9 +85,9 @@ private boolean tryConnection(OwnCloudClient client) {
98
85
redirectedLocation .startsWith (PROTOCOL_HTTP )
99
86
);
100
87
get .releaseConnection ();
101
- get = new GetMethod (redirectedLocation );
102
- status = client .executeMethod (get , TRY_CONNECTION_TIMEOUT , TRY_CONNECTION_TIMEOUT );
103
- mLatestResult = new RemoteOperationResult ((status == HttpStatus .SC_OK ), get );
88
+ get = new GetMethod (redirectedLocation , false );
89
+ status = client .execute (get );
90
+ mLatestResult = new RemoteOperationResult <> ((status == HttpStatus .SC_OK ), get );
104
91
redirectedLocation = mLatestResult .getRedirectedLocation ();
105
92
}
106
93
@@ -109,7 +96,7 @@ private boolean tryConnection(OwnCloudClient client) {
109
96
if (status == HttpStatus .SC_OK ) {
110
97
JSONObject json = new JSONObject (response );
111
98
if (!json .getBoolean (NODE_INSTALLED )) {
112
- mLatestResult = new RemoteOperationResult (
99
+ mLatestResult = new RemoteOperationResult <> (
113
100
RemoteOperationResult .ResultCode .INSTANCE_NOT_CONFIGURED );
114
101
} else {
115
102
boolean extendedSupport = false ;
@@ -121,24 +108,22 @@ private boolean tryConnection(OwnCloudClient client) {
121
108
OwnCloudVersion ocVersion = new OwnCloudVersion (version );
122
109
123
110
if (!ocVersion .isVersionValid ()) {
124
- mLatestResult = new RemoteOperationResult (RemoteOperationResult .ResultCode .BAD_OC_VERSION );
111
+ mLatestResult = new RemoteOperationResult <> (RemoteOperationResult .ResultCode .BAD_OC_VERSION );
125
112
} else {
126
113
// success
127
114
if (isRedirectToNonSecureConnection ) {
128
- mLatestResult = new RemoteOperationResult (
115
+ mLatestResult = new RemoteOperationResult <> (
129
116
RemoteOperationResult .ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION );
130
117
} else {
131
- mLatestResult = new RemoteOperationResult (
118
+ mLatestResult = new RemoteOperationResult <> (
132
119
baseUrlSt .startsWith (PROTOCOL_HTTPS ) ?
133
120
RemoteOperationResult .ResultCode .OK_SSL :
134
121
RemoteOperationResult .ResultCode .OK_NO_SSL
135
122
);
136
123
}
137
124
138
- ArrayList <Object > data = new ArrayList <>();
139
- data .add (ocVersion );
140
- data .add (extendedSupport );
141
- mLatestResult .setData (data );
125
+ Pair <OwnCloudVersion , Boolean > data = new Pair <>(ocVersion , extendedSupport );
126
+ mLatestResult .setResultData (data );
142
127
retval = true ;
143
128
}
144
129
}
@@ -147,22 +132,22 @@ private boolean tryConnection(OwnCloudClient client) {
147
132
JSONObject json = new JSONObject (response );
148
133
149
134
if (json .getInt ("code" ) == UNTRUSTED_DOMAIN_ERROR_CODE ) {
150
- mLatestResult = new RemoteOperationResult (RemoteOperationResult .ResultCode .UNTRUSTED_DOMAIN );
135
+ mLatestResult = new RemoteOperationResult <> (RemoteOperationResult .ResultCode .UNTRUSTED_DOMAIN );
151
136
} else {
152
- mLatestResult = new RemoteOperationResult (false , status , get . getResponseHeaders () );
137
+ mLatestResult = new RemoteOperationResult <> (false , get );
153
138
}
154
139
} catch (JSONException e ) {
155
- mLatestResult = new RemoteOperationResult (false , status , get . getResponseHeaders () );
140
+ mLatestResult = new RemoteOperationResult <> (false , get );
156
141
}
157
142
} else {
158
- mLatestResult = new RemoteOperationResult (false , status , get . getResponseHeaders () );
143
+ mLatestResult = new RemoteOperationResult <> (false , get );
159
144
}
160
145
161
146
} catch (JSONException e ) {
162
- mLatestResult = new RemoteOperationResult (RemoteOperationResult .ResultCode .INSTANCE_NOT_CONFIGURED );
147
+ mLatestResult = new RemoteOperationResult <> (RemoteOperationResult .ResultCode .INSTANCE_NOT_CONFIGURED );
163
148
164
149
} catch (Exception e ) {
165
- mLatestResult = new RemoteOperationResult (e );
150
+ mLatestResult = new RemoteOperationResult <> (e );
166
151
167
152
} finally {
168
153
if (get != null )
@@ -184,18 +169,16 @@ private boolean tryConnection(OwnCloudClient client) {
184
169
}
185
170
186
171
private boolean isOnline () {
187
- ConnectivityManager cm = (ConnectivityManager ) mContext
188
- .getSystemService (Context .CONNECTIVITY_SERVICE );
189
- return cm != null && cm .getActiveNetworkInfo () != null
190
- && cm .getActiveNetworkInfo ().isConnectedOrConnecting ();
172
+ ConnectivityManager cm = (ConnectivityManager ) mContext .getSystemService (Context .CONNECTIVITY_SERVICE );
173
+ return cm != null && cm .getActiveNetworkInfo () != null && cm .getActiveNetworkInfo ().isConnectedOrConnecting ();
191
174
}
192
175
193
176
@ Override
194
- protected RemoteOperationResult run (OwnCloudClient client ) {
177
+ public RemoteOperationResult < Pair < OwnCloudVersion , Boolean >> run (NextcloudClient client ) {
195
178
if (!isOnline ()) {
196
- return new RemoteOperationResult (RemoteOperationResult .ResultCode .NO_NETWORK_CONNECTION );
179
+ return new RemoteOperationResult <> (RemoteOperationResult .ResultCode .NO_NETWORK_CONNECTION );
197
180
}
198
- String baseUriStr = client .getBaseUri (). toString ( );
181
+ String baseUriStr = String . valueOf ( client .getBaseUri ());
199
182
if (baseUriStr .startsWith (PROTOCOL_HTTP ) || baseUriStr .startsWith (PROTOCOL_HTTPS )) {
200
183
tryConnection (client );
201
184
0 commit comments