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.browse;
19 
20 import android.content.Context;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.LinearLayout;
26 
27 import com.android.mail.R;
28 import com.android.mail.providers.Folder;
29 import com.android.mail.providers.UIProvider;
30 
31 public final class ConversationListFooterView extends LinearLayout implements View.OnClickListener {
32 
33     public interface FooterViewClickListener {
onFooterViewLoadMoreClick(Folder folder)34         void onFooterViewLoadMoreClick(Folder folder);
35     }
36 
37     private View mLoading;
38     private View mLoadMore;
39     private Uri mLoadMoreUri;
40     private FooterViewClickListener mClickListener;
41 
ConversationListFooterView(Context context, AttributeSet attrs)42     public ConversationListFooterView(Context context, AttributeSet attrs) {
43         super(context, attrs);
44     }
45 
46     @Override
onFinishInflate()47     protected void onFinishInflate() {
48         super.onFinishInflate();
49 
50         mLoading = findViewById(R.id.loading);
51         mLoadMore = findViewById(R.id.load_more);
52         mLoadMore.setOnClickListener(this);
53     }
54 
setClickListener(FooterViewClickListener listener)55     public void setClickListener(FooterViewClickListener listener) {
56         mClickListener = listener;
57     }
58 
59     @Override
onClick(View v)60     public void onClick(View v) {
61         final int id = v.getId();
62         final Folder f = (Folder) v.getTag();
63         if (id == R.id.load_more) {
64             mClickListener.onFooterViewLoadMoreClick(f);
65         }
66     }
67 
setFolder(Folder folder)68     public void setFolder(Folder folder) {
69         mLoadMore.setTag(folder);
70         mLoadMoreUri = folder.loadMoreUri;
71     }
72 
73     /**
74      * Update the view to reflect the new folder status.
75      */
updateStatus(final ConversationCursor cursor)76     public boolean updateStatus(final ConversationCursor cursor) {
77         if (cursor == null) {
78             mLoading.setVisibility(View.GONE);
79             mLoadMore.setVisibility(View.GONE);
80             return false;
81         }
82         boolean showFooter = true;
83         final Bundle extras = cursor.getExtras();
84         final int cursorStatus = extras.getInt(UIProvider.CursorExtraKeys.EXTRA_STATUS);
85         final int totalCount = extras.getInt(UIProvider.CursorExtraKeys.EXTRA_TOTAL_COUNT);
86 
87         if (UIProvider.CursorStatus.isWaitingForResults(cursorStatus)) {
88             if (cursor.getCount() != 0) {
89                 // When loading more, show the spinner in the footer.
90                 mLoading.setVisibility(View.VISIBLE);
91                 mLoadMore.setVisibility(View.GONE);
92             } else {
93                 // We're currently loading, but we have no messages at all. We don't need to show
94                 // the footer, because we should be displaying the loading state on the
95                 // conversation list itself.
96                 showFooter = false;
97             }
98 
99         } else if (mLoadMoreUri != null && cursor.getCount() < totalCount) {
100             // We know that there are more messages on the server than we have locally, so we
101             // need to show the footer with the "load more" button.
102             mLoading.setVisibility(View.GONE);
103             mLoadMore.setVisibility(View.VISIBLE);
104         } else {
105             showFooter = false;
106         }
107         return showFooter;
108     }
109 }
110