4b2aed504c314dc3d8543a718cbba2abee0f2bc6
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorAsyncTask.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio on 09/02/2015.
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20 package com.owncloud.android.authentication;
21
22 import android.app.Activity;
23 import android.content.Context;
24 import android.net.Uri;
25 import android.os.AsyncTask;
26
27 import com.owncloud.android.MainApp;
28 import com.owncloud.android.lib.common.OwnCloudClient;
29 import com.owncloud.android.lib.common.OwnCloudClientFactory;
30 import com.owncloud.android.lib.common.OwnCloudCredentials;
31 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
32 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
33
34 import java.lang.ref.WeakReference;
35
36
37 /**
38 * Async Task to verify the credentials of a user
39 */
40 public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
41
42 private static String REMOTE_PATH = "/";
43 private static boolean SUCCESS_IF_ABSENT = false;
44
45 private Context mContext;
46 private final WeakReference<OnAuthenticatorTaskListener> mListener;
47 protected Activity mActivity;
48
49 public AuthenticatorAsyncTask(Activity activity) {
50 mContext = activity.getApplicationContext();
51 mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
52 }
53
54 @Override
55 protected RemoteOperationResult doInBackground(Object... params) {
56
57 RemoteOperationResult result;
58 if (params!= null && params.length==2) {
59 String url = (String)params[0];
60 OwnCloudCredentials credentials = (OwnCloudCredentials)params[1];
61
62 // Client
63 Uri uri = Uri.parse(url);
64 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
65
66 client.setCredentials(credentials);
67
68 // Operation
69 ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(
70 REMOTE_PATH,
71 mContext,
72 SUCCESS_IF_ABSENT
73 );
74 result = operation.execute(client);
75
76 } else {
77 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
78 }
79
80 return result;
81 }
82
83 @Override
84 protected void onPostExecute(RemoteOperationResult result) {
85
86 if (result!= null)
87 {
88 OnAuthenticatorTaskListener listener = mListener.get();
89 if (listener!= null)
90 {
91 listener.onAuthenticatorTaskCallback(result);
92 }
93 }
94 }
95 /*
96 * Interface to retrieve data from recognition task
97 */
98 public interface OnAuthenticatorTaskListener{
99
100 void onAuthenticatorTaskCallback(RemoteOperationResult result);
101 }
102 }