1 /*
2  * Copyright (C) 2012 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  */
17 
18 package com.android.mail;
19 
20 import android.content.UriMatcher;
21 import android.os.Bundle;
22 import android.text.TextUtils;
23 
24 import com.android.mail.providers.Account;
25 import com.android.mail.providers.Folder;
26 import com.android.mail.providers.UIProvider;
27 import com.android.mail.utils.Utils;
28 import com.google.common.base.Preconditions;
29 
30 /**
31  * This class is supposed to have the same thing that the Gmail ConversationListContext
32  * contained. For now, it has no implementation at all. The goal is to bring over functionality
33  * as required.
34  *
35  * Original purpose:
36  * An encapsulation over a request to a list of conversations and the various states around it.
37  * This includes the folder the user selected to view the list, or the search query for the
38  * list, etc.
39  */
40 public class ConversationListContext {
41     public static final String EXTRA_SEARCH_QUERY = "query";
42 
43     /**
44      * A matcher for data URI's that specify conversation list info.
45      */
46     private static final UriMatcher sUrlMatcher = new UriMatcher(UriMatcher.NO_MATCH);
47     /**
48      * The account for whom we are showing a list
49      */
50     public final Account account;
51     /**
52      * The folder whose conversations we are displaying, if any.
53      */
54     public final Folder folder;
55     /**
56      * The search query whose results we are displaying, if any.
57      */
58     public final String searchQuery;
59 
60     static {
sUrlMatcher.addURI(UIProvider.AUTHORITY, "account/*/folder/*", 0)61         sUrlMatcher.addURI(UIProvider.AUTHORITY, "account/*/folder/*", 0);
62     }
63 
64     /**
65      * De-serializes a context from a bundle.
66      */
forBundle(Bundle bundle)67     public static ConversationListContext forBundle(Bundle bundle) {
68         // The account is created here as a new object. This is probably not the best thing to do.
69         // We should probably be reading an account instance from our controller.
70         Account account = bundle.getParcelable(Utils.EXTRA_ACCOUNT);
71         Folder folder = bundle.getParcelable(Utils.EXTRA_FOLDER);
72         return new ConversationListContext(account, bundle.getString(EXTRA_SEARCH_QUERY), folder);
73     }
74 
75     /**
76      * Builds a context for a view to a Gmail folder.
77      * @param account
78      * @param folder
79      * @return
80      */
forFolder(Account account, Folder folder)81     public static ConversationListContext forFolder(Account account, Folder folder) {
82         return new ConversationListContext(account, null, folder);
83     }
84 
85     /**
86      * Builds a context object for viewing a conversation list for a search query.
87      */
forSearchQuery(Account account, Folder folder, String query)88     public static ConversationListContext forSearchQuery(Account account, Folder folder,
89             String query) {
90         return new ConversationListContext(account, Preconditions.checkNotNull(query), folder);
91     }
92 
93     /**
94      * Internal constructor
95      *
96      * To create a class, use the static {@link #forFolder} or {@link #forBundle(Bundle)} method.
97      * @param a
98      * @param query
99      * @param f
100      */
ConversationListContext(Account a, String query, Folder f)101     private ConversationListContext(Account a, String query, Folder f) {
102         account = a;
103         searchQuery = query;
104         folder = f;
105     }
106 
107     /**
108      * Returns true if the provided context represents search results.
109      * @param in
110      * @return true the context represents search results. False otherwise
111      */
isSearchResult(ConversationListContext in)112     public static final boolean isSearchResult(ConversationListContext in) {
113         return in != null && !TextUtils.isEmpty(in.searchQuery);
114     }
115 
116     /**
117      * Serializes the context to a bundle.
118      */
toBundle()119     public Bundle toBundle() {
120         Bundle result = new Bundle();
121         result.putParcelable(Utils.EXTRA_ACCOUNT, account);
122         result.putString(EXTRA_SEARCH_QUERY, searchQuery);
123         result.putParcelable(Utils.EXTRA_FOLDER, folder);
124         return result;
125     }
126 }
127