Add author's line in license
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorAsyncTask.java
1 /* ownCloud Android client application
2 *
3 * @author masensio on 09/02/2015.
4 * Copyright (C) 2012-2015 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.authentication;
20
21 import android.app.Activity;
22 import android.content.Context;
23 import android.net.Uri;
24 import android.os.AsyncTask;
25
26 import com.owncloud.android.lib.common.OwnCloudClient;
27 import com.owncloud.android.lib.common.OwnCloudClientFactory;
28 import com.owncloud.android.lib.common.OwnCloudCredentials;
29 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
30 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
31
32 import java.lang.ref.WeakReference;
33
34
35 /**
36 * Async Task to verify the credentials of a user
37 */
38 public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
39
40 private static String REMOTE_PATH = "/";
41 private static boolean SUCCESS_IF_ABSENT = false;
42
43 private Context mContext;
44 private final WeakReference<OnAuthenticatorTaskListener> mListener;
45 protected Activity mActivity;
46
47 public AuthenticatorAsyncTask(Activity activity) {
48 mContext = activity.getApplicationContext();
49 mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
50 }
51
52 @Override
53 protected RemoteOperationResult doInBackground(Object... params) {
54
55 RemoteOperationResult result;
56 if (params!= null && params.length==2) {
57 String url = (String)params[0];
58 OwnCloudCredentials credentials = (OwnCloudCredentials)params[1];
59
60 // Client
61 Uri uri = Uri.parse(url);
62 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
63
64 client.setCredentials(credentials);
65
66 // Operation
67 ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(
68 REMOTE_PATH,
69 mContext,
70 SUCCESS_IF_ABSENT
71 );
72 result = operation.execute(client);
73
74 } else {
75 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
76 }
77
78 return result;
79 }
80
81 @Override
82 protected void onPostExecute(RemoteOperationResult result) {
83
84 if (result!= null)
85 {
86 OnAuthenticatorTaskListener listener = mListener.get();
87 if (listener!= null)
88 {
89 listener.onAuthenticatorTaskCallback(result);
90 }
91 }
92 }
93 /*
94 * Interface to retrieve data from recognition task
95 */
96 public interface OnAuthenticatorTaskListener{
97
98 void onAuthenticatorTaskCallback(RemoteOperationResult result);
99 }
100 }