1/*******************************************************************************
2 *      Copyright (C) 2013 Google Inc.
3 *      Licensed to The Android Open Source Project.
4 *
5 *      Licensed under the Apache License, Version 2.0 (the "License");
6 *      you may not use this file except in compliance with the License.
7 *      You may obtain a copy of the License at
8 *
9 *           http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *      Unless required by applicable law or agreed to in writing, software
12 *      distributed under the License is distributed on an "AS IS" BASIS,
13 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *      See the License for the specific language governing permissions and
15 *      limitations under the License.
16 *******************************************************************************/
17package com.android.mail.browse;
18
19import android.database.Cursor;
20import android.database.CursorWrapper;
21
22public interface ConversationCursorOperationListener {
23    /**
24     * Marks all contents of this cursor as seen.
25     */
26    void markContentsSeen();
27
28    /**
29     * Empties the folder of all messages, if possible.
30     */
31    void emptyFolder();
32
33    public class OperationHelper {
34        /**
35         * Invokes {@link ConversationCursorOperationListener#markContentsSeen(Cursor)} on the
36         * specified {@link Cursor}, recursively calls {@link #markContentsSeen(Cursor)} on a
37         * wrapped cursor, or returns.
38         */
39        public static void markContentsSeen(final Cursor cursor) {
40            if (cursor == null) {
41                return;
42            }
43
44            if (cursor instanceof ConversationCursorOperationListener) {
45                ((ConversationCursorOperationListener) cursor).markContentsSeen();
46            } else if (cursor instanceof CursorWrapper) {
47                markContentsSeen(((CursorWrapper) cursor).getWrappedCursor());
48            }
49        }
50
51        /**
52         * Invokes {@link ConversationCursorOperationListener#emptyFolder(Cursor)} on the
53         * specified {@link Cursor}, recursively calls {@link #emptyFolder(Cursor)} on a
54         * wrapped cursor, or returns.
55         */
56        public static void emptyFolder(final Cursor cursor) {
57            if (cursor == null) {
58                return;
59            }
60
61            if (cursor instanceof ConversationCursorOperationListener) {
62                ((ConversationCursorOperationListener) cursor).emptyFolder();
63            } else if (cursor instanceof CursorWrapper) {
64                emptyFolder(((CursorWrapper) cursor).getWrappedCursor());
65            }
66        }
67    }
68}
69