1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui.sidebar;
18
19import android.content.ContentProviderClient;
20import android.content.ContentResolver;
21import android.net.Uri;
22import android.os.AsyncTask;
23import android.provider.DocumentsContract;
24import android.util.Log;
25
26import com.android.documentsui.DocumentsApplication;
27import com.android.documentsui.base.BooleanConsumer;
28import com.android.documentsui.base.Shared;
29
30public final class EjectRootTask extends AsyncTask<Void, Void, Boolean> {
31
32    private final String TAG = "EjectRootTask";
33
34    private final ContentResolver mResolver;
35    private final String mAuthority;
36    private final String mRootId;
37    private final BooleanConsumer mCallback;
38
39    /**
40     * @param finishCallback The end callback necessary when the eject task finishes
41     */
42    public EjectRootTask(
43            ContentResolver resolver,
44            String authority,
45            String rootId,
46            BooleanConsumer finishCallback) {
47        mResolver = resolver;
48        mAuthority = authority;
49        mRootId = rootId;
50        mCallback = finishCallback;
51    }
52
53    @Override
54    protected Boolean doInBackground(Void... args) {
55        Uri rootUri = DocumentsContract.buildRootUri(mAuthority, mRootId);
56        ContentProviderClient client = null;
57        try {
58            client = DocumentsApplication.acquireUnstableProviderOrThrow(
59                    mResolver, mAuthority);
60            DocumentsContract.ejectRoot(client, rootUri);
61            return true;
62        } catch (IllegalStateException e) {
63            Log.w(TAG, "Failed to eject root.", e);
64        } catch (Exception e) {
65            Log.w(TAG, "Binder call failed.", e);
66        } finally {
67            ContentProviderClient.releaseQuietly(client);
68        }
69
70        return false;
71    }
72
73    @Override
74    protected void onPostExecute(Boolean ejected) {
75        mCallback.accept(ejected);
76    }
77}