DirectoryLoader.java revision 6efba22ce510352bb84910d6efc42fecafd31ed7
1c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell/*
2c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Copyright (C) 2013 The Android Open Source Project
3f8ee72d98f2d23e034e90870ff6a760659a462a5Brian *
4c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Licensed under the Apache License, Version 2.0 (the "License");
5f8ee72d98f2d23e034e90870ff6a760659a462a5Brian * you may not use this file except in compliance with the License.
6c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * You may obtain a copy of the License at
7c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
8c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *      http://www.apache.org/licenses/LICENSE-2.0
9c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell *
10c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * Unless required by applicable law or agreed to in writing, software
11c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * distributed under the License is distributed on an "AS IS" BASIS,
12c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * See the License for the specific language governing permissions and
14c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell * limitations under the License.
15c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell */
16c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
17c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellpackage com.android.documentsui;
18c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
19c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.DocumentsActivity.TAG;
20c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
21c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_DISPLAY_NAME;
22c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED;
23c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_SIZE;
24c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
25c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport static com.android.documentsui.model.DocumentInfo.getCursorInt;
26c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
27c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport android.content.AsyncTaskLoader;
28bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.content.ContentProviderClient;
29bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.content.ContentResolver;
30bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.content.Context;
31bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.database.Cursor;
32bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.net.Uri;
33bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.os.CancellationSignal;
34bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paulimport android.os.OperationCanceledException;
35c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport android.provider.DocumentsContract;
36c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport android.provider.DocumentsContract.Document;
37c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport android.util.Log;
38c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
39c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport com.android.documentsui.DocumentsActivity.State;
40c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport com.android.documentsui.RecentsProvider.StateColumns;
41c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport com.android.documentsui.model.DocumentInfo;
42c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport com.android.documentsui.model.RootInfo;
43c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
44188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwellimport libcore.io.IoUtils;
45c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
46c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellimport java.io.FileNotFoundException;
47c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
48c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwellclass DirectoryResult implements AutoCloseable {
49c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    ContentProviderClient client;
50188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell    Cursor cursor;
51c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    Exception exception;
52c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
53c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    int mode = MODE_UNKNOWN;
54c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    int sortOrder = SORT_ORDER_UNKNOWN;
55c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
56c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    @Override
57c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    public void close() {
58c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        IoUtils.closeQuietly(cursor);
59c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        ContentProviderClient.closeQuietly(client);
60c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        cursor = null;
61c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        client = null;
62c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    }
63c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
649827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
659827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwellpublic class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
669827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
679827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
689827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
699827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
709827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
719827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private final int mType;
729827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private final RootInfo mRoot;
739827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private DocumentInfo mDoc;
749827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private final Uri mUri;
759827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private final int mUserSortOrder;
769827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
779827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private CancellationSignal mSignal;
789827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    private DirectoryResult mResult;
799827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
809827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    public DirectoryLoader(Context context, int type, RootInfo root, DocumentInfo doc, Uri uri,
819827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell            int userSortOrder) {
829827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        super(context);
839827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        mType = type;
849827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        mRoot = root;
859827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        mDoc = doc;
869827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        mUri = uri;
879827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        mUserSortOrder = userSortOrder;
889827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    }
899827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
909827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    @Override
910791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul    public final DirectoryResult loadInBackground() {
920791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        synchronized (this) {
930791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            if (isLoadInBackgroundCanceled()) {
940791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul                throw new OperationCanceledException();
950791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            }
960791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            mSignal = new CancellationSignal();
970791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        }
980791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul
990791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        final ContentResolver resolver = getContext().getContentResolver();
1000791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        final String authority = mUri.getAuthority();
1010791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul
1020791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        final DirectoryResult result = new DirectoryResult();
1030791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul
1040791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        int userMode = State.MODE_UNKNOWN;
1050791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul
1060791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        // Use default document when searching
1070791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        if (mType == DirectoryFragment.TYPE_SEARCH) {
1080791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            final Uri docUri = DocumentsContract.buildDocumentUri(
1090791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul                    mRoot.authority, mRoot.documentId);
1100791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            try {
1110791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul                mDoc = DocumentInfo.fromUri(resolver, docUri);
1120791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            } catch (FileNotFoundException e) {
1130791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul                Log.w(TAG, "Failed to query", e);
1149827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell                result.exception = e;
115c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                return result;
116c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            }
117c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
118c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
119c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        // Pick up any custom modes requested by user
120893526b8a823fe1b88f2b46376155afb91c84016Keith Whitwell        Cursor cursor = null;
121c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        try {
122188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell            final Uri stateUri = RecentsProvider.buildState(
123c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                    mRoot.authority, mRoot.rootId, mDoc.documentId);
124c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            cursor = resolver.query(stateUri, null, null, null, null);
125c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            if (cursor.moveToFirst()) {
126c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                userMode = getCursorInt(cursor, StateColumns.MODE);
127c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            }
128c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        } finally {
129c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            IoUtils.closeQuietly(cursor);
130188a4db49c4c22429bfa7ae87d4b1a0c35bf0285Keith Whitwell        }
1317e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell
132c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        if (userMode != State.MODE_UNKNOWN) {
133c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            result.mode = userMode;
134c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        } else {
135c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) {
136c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                result.mode = State.MODE_GRID;
137c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            } else {
1380791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul                result.mode = State.MODE_LIST;
1390791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            }
1400791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        }
1410791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul
1420791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) {
1430791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul            result.sortOrder = mUserSortOrder;
1440791fdff6fe87cf9a29ddf4a716f1881c367c7deBrian Paul        } else {
145c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
146c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
147c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            } else {
148c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
149c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            }
150c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
151c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
152c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        // Search always uses ranking from provider
153c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        if (mType == DirectoryFragment.TYPE_SEARCH) {
154c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            result.sortOrder = State.SORT_ORDER_UNKNOWN;
155c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
156c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
157c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + mUserSortOrder + " --> mode="
158c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                + result.mode + ", sortOrder=" + result.sortOrder);
159c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
160c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        try {
161c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            result.client = resolver.acquireUnstableContentProviderClient(authority);
162c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            cursor = result.client.query(
163c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                    mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
164c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            cursor.registerContentObserver(mObserver);
165c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
1667e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell            cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
167c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
168c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            if (mType == DirectoryFragment.TYPE_SEARCH) {
169c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                // Filter directories out of search results, for now
170c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
171c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            } else {
172c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                // Normal directories should have sorting applied
173c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                cursor = new SortingCursorWrapper(cursor, result.sortOrder);
174c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            }
175c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
176c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            result.cursor = cursor;
177c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        } catch (Exception e) {
178c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            Log.w(TAG, "Failed to query", e);
179c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            result.exception = e;
180c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            ContentProviderClient.closeQuietly(result.client);
181c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        } finally {
182c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            synchronized (this) {
183c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                mSignal = null;
184c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            }
185c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
1869827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1879827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        return result;
1889827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    }
1899827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1909827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    @Override
1919827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    public void cancelLoadInBackground() {
1929827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        super.cancelLoadInBackground();
1939827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
1949827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        synchronized (this) {
1959827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell            if (mSignal != null) {
1969827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell                mSignal.cancel();
1979827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell            }
1989827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        }
1999827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    }
2009827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell
2019827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    @Override
2029827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell    public void deliverResult(DirectoryResult result) {
2039827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        if (isReset()) {
2049827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell            IoUtils.closeQuietly(result);
2059827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell            return;
2069827dc8bea422b940f1efcfbd1c0d76f8bbca844Keith Whitwell        }
207c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        DirectoryResult oldResult = mResult;
208c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        mResult = result;
209c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
210893526b8a823fe1b88f2b46376155afb91c84016Keith Whitwell        if (isStarted()) {
211c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            super.deliverResult(result);
212c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
213c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
214c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        if (oldResult != null && oldResult != result) {
215c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            IoUtils.closeQuietly(oldResult);
216c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
217c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    }
218c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
219c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    @Override
220c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    protected void onStartLoading() {
221c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        if (mResult != null) {
222c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            deliverResult(mResult);
223c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
224c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        if (takeContentChanged() || mResult == null) {
225c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            forceLoad();
2267e9c3684ef45e0df8426317f28c883d16f27c031Keith Whitwell        }
227c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    }
228c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
229c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    @Override
230c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    protected void onStopLoading() {
231c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        cancelLoad();
232c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    }
233c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
234c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    @Override
235c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    public void onCanceled(DirectoryResult result) {
236c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        IoUtils.closeQuietly(result);
237c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    }
238c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
239c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    @Override
240c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    protected void onReset() {
241c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        super.onReset();
242893526b8a823fe1b88f2b46376155afb91c84016Keith Whitwell
243c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        // Ensure the loader is stopped
244c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        onStopLoading();
245c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
246c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        IoUtils.closeQuietly(mResult);
247893526b8a823fe1b88f2b46376155afb91c84016Keith Whitwell        mResult = null;
248c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell
249c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        getContext().getContentResolver().unregisterContentObserver(mObserver);
2505464cd0a60f474753abc6af047fd21b0e29b8ac4Keith Whitwell    }
2515464cd0a60f474753abc6af047fd21b0e29b8ac4Keith Whitwell
252893526b8a823fe1b88f2b46376155afb91c84016Keith Whitwell    public static String getQuerySortOrder(int sortOrder) {
2535464cd0a60f474753abc6af047fd21b0e29b8ac4Keith Whitwell        switch (sortOrder) {
2545464cd0a60f474753abc6af047fd21b0e29b8ac4Keith Whitwell            case SORT_ORDER_DISPLAY_NAME:
255c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                return Document.COLUMN_DISPLAY_NAME + " ASC";
256c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            case SORT_ORDER_LAST_MODIFIED:
257c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                return Document.COLUMN_LAST_MODIFIED + " DESC";
258c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            case SORT_ORDER_SIZE:
259c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                return Document.COLUMN_SIZE + " DESC";
260c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell            default:
261c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell                return null;
262c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell        }
263c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell    }
264c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell}
265c22f8a7787bd5260135a20a0c2ae8b743228497bKeith Whitwell