summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/ConversationListContext.java
blob: f5885aa2f73dd546ed1fecb9a070abf9bd80917d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2012 Google Inc.
 * Licensed to The Android Open Source Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mail;

import android.content.UriMatcher;
import android.os.Bundle;
import android.text.TextUtils;

import com.android.mail.providers.Account;
import com.android.mail.providers.Folder;
import com.android.mail.providers.UIProvider;
import com.android.mail.utils.Utils;
import com.google.common.base.Preconditions;

/**
 * This class is supposed to have the same thing that the Gmail ConversationListContext
 * contained. For now, it has no implementation at all. The goal is to bring over functionality
 * as required.
 *
 * Original purpose:
 * An encapsulation over a request to a list of conversations and the various states around it.
 * This includes the folder the user selected to view the list, or the search query for the
 * list, etc.
 */
public class ConversationListContext {
    public static final String EXTRA_SEARCH_QUERY = "query";

    /**
     * A matcher for data URI's that specify conversation list info.
     */
    private static final UriMatcher sUrlMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    /**
     * The account for whom we are showing a list
     */
    public final Account account;
    /**
     * The folder whose conversations we are displaying, if any.
     */
    public final Folder folder;
    /**
     * The search query whose results we are displaying, if any.
     */
    public final String searchQuery;

    static {
        sUrlMatcher.addURI(UIProvider.AUTHORITY, "account/*/folder/*", 0);
    }

    /**
     * De-serializes a context from a bundle.
     */
    public static ConversationListContext forBundle(Bundle bundle) {
        // The account is created here as a new object. This is probably not the best thing to do.
        // We should probably be reading an account instance from our controller.
        Account account = bundle.getParcelable(Utils.EXTRA_ACCOUNT);
        Folder folder = bundle.getParcelable(Utils.EXTRA_FOLDER);
        return new ConversationListContext(account, bundle.getString(EXTRA_SEARCH_QUERY), folder);
    }

    /**
     * Builds a context for a view to a Gmail folder.
     * @param account
     * @param folder
     * @return
     */
    public static ConversationListContext forFolder(Account account, Folder folder) {
        return new ConversationListContext(account, null, folder);
    }

    /**
     * Builds a context object for viewing a conversation list for a search query.
     */
    public static ConversationListContext forSearchQuery(Account account, Folder folder,
            String query) {
        return new ConversationListContext(account, Preconditions.checkNotNull(query), folder);
    }

    /**
     * Internal constructor
     *
     * To create a class, use the static {@link #forFolder} or {@link #forBundle(Bundle)} method.
     * @param a
     * @param query
     * @param f
     */
    private ConversationListContext(Account a, String query, Folder f) {
        account = a;
        searchQuery = query;
        folder = f;
    }

    /**
     * Returns true if the provided context represents search results.
     * @param in
     * @return true the context represents search results. False otherwise
     */
    public static final boolean isSearchResult(ConversationListContext in) {
        return in != null && !TextUtils.isEmpty(in.searchQuery);
    }

    /**
     * Serializes the context to a bundle.
     */
    public Bundle toBundle() {
        Bundle result = new Bundle();
        result.putParcelable(Utils.EXTRA_ACCOUNT, account);
        result.putString(EXTRA_SEARCH_QUERY, searchQuery);
        result.putParcelable(Utils.EXTRA_FOLDER, folder);
        return result;
    }
}