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