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 17 package com.android.documentsui.sidebar; 18 19 import static android.content.ContentResolver.wrap; 20 21 import android.content.ContentProviderClient; 22 import android.content.ContentResolver; 23 import android.net.Uri; 24 import android.os.AsyncTask; 25 import android.os.FileUtils; 26 import android.provider.DocumentsContract; 27 import android.util.Log; 28 29 import com.android.documentsui.DocumentsApplication; 30 import com.android.documentsui.base.BooleanConsumer; 31 import com.android.documentsui.base.Shared; 32 33 public final class EjectRootTask extends AsyncTask<Void, Void, Boolean> { 34 35 private final String TAG = "EjectRootTask"; 36 37 private final ContentResolver mResolver; 38 private final String mAuthority; 39 private final String mRootId; 40 private final BooleanConsumer mCallback; 41 42 /** 43 * @param finishCallback The end callback necessary when the eject task finishes 44 */ EjectRootTask( ContentResolver resolver, String authority, String rootId, BooleanConsumer finishCallback)45 public EjectRootTask( 46 ContentResolver resolver, 47 String authority, 48 String rootId, 49 BooleanConsumer finishCallback) { 50 mResolver = resolver; 51 mAuthority = authority; 52 mRootId = rootId; 53 mCallback = finishCallback; 54 } 55 56 @Override doInBackground(Void... args)57 protected Boolean doInBackground(Void... args) { 58 Uri rootUri = DocumentsContract.buildRootUri(mAuthority, mRootId); 59 ContentProviderClient client = null; 60 try { 61 client = DocumentsApplication.acquireUnstableProviderOrThrow( 62 mResolver, mAuthority); 63 DocumentsContract.ejectRoot(wrap(client), rootUri); 64 return true; 65 } catch (IllegalStateException e) { 66 Log.w(TAG, "Failed to eject root.", e); 67 } catch (Exception e) { 68 Log.w(TAG, "Binder call failed.", e); 69 } finally { 70 FileUtils.closeQuietly(client); 71 } 72 73 return false; 74 } 75 76 @Override onPostExecute(Boolean ejected)77 protected void onPostExecute(Boolean ejected) { 78 mCallback.accept(ejected); 79 } 80 }