1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mail.drawer;
18
19import android.view.View;
20import android.view.ViewGroup;
21import android.widget.ImageView;
22import android.widget.TextView;
23
24import com.android.mail.R;
25import com.android.mail.providers.Account;
26import com.android.mail.ui.ControllableActivity;
27import com.android.mail.ui.DrawerController;
28import com.android.mail.ui.FolderListFragment;
29
30/**
31 * The base class of all footer items. Subclasses must fill in the logic of
32 * {@link #onFooterClicked()} which contains the behavior when the item is selected.
33 */
34public abstract class FooterItem extends DrawerItem implements View.OnClickListener {
35
36    private final FolderListFragment.DrawerStateListener mDrawerListener;
37    private final int mImageResourceId;
38    private final int mTextResourceId;
39
40    FooterItem(ControllableActivity activity, Account account,
41            FolderListFragment.DrawerStateListener drawerListener,
42            final int imageResourceId, final int textResourceId) {
43        super(activity, null, NONFOLDER_ITEM, account);
44        mDrawerListener = drawerListener;
45        mImageResourceId = imageResourceId;
46        mTextResourceId = textResourceId;
47    }
48
49    private int getImageResourceId() {
50        return mImageResourceId;
51    }
52
53    private int getTextResourceId() {
54        return mTextResourceId;
55    }
56
57    /**
58     * Executes the behavior associated with this footer item.<br>
59     * <br>
60     * WARNING: you probably don't want to call this directly; use {@link #onClick(View)} instead.
61     * This method actually performs the action, and its execution may be deferred from when the
62     * 'click' happens so we can smoothly close the drawer beforehand.
63     */
64    public abstract void onFooterClicked();
65
66    @Override
67    public final void onClick(View v) {
68        final DrawerController dc = mActivity.getDrawerController();
69        if (dc.isDrawerEnabled()) {
70            // close the drawer and defer handling the click until onDrawerClosed
71            mActivity.getAccountController().closeDrawer(false /* hasNewFolderOrAccount */,
72                    null /* nextAccount */, null /* nextFolder */);
73            mDrawerListener.setPendingFooterClick(this);
74        } else {
75            onFooterClicked();
76        }
77    }
78
79    /**
80     * For analytics
81     * @return label for analytics event
82     */
83    protected String getEventLabel() {
84        return "drawer_footer/" + mActivity.getViewMode().getModeString();
85    }
86
87    @Override
88    public View getView(View convertView, ViewGroup parent) {
89        final ViewGroup footerItemView;
90        if (convertView != null) {
91            footerItemView = (ViewGroup) convertView;
92        } else {
93            footerItemView =
94                    (ViewGroup) mInflater.inflate(R.layout.drawer_footer_item, parent, false);
95        }
96
97        // adjust the text of the footer item
98        final TextView textView = (TextView) footerItemView.
99                findViewById(R.id.drawer_footer_text);
100        textView.setText(getTextResourceId());
101
102        // adjust the icon of the footer item
103        final ImageView imageView = (ImageView) footerItemView.
104                findViewById(R.id.drawer_footer_image);
105        imageView.setImageResource(getImageResourceId());
106        return footerItemView;
107    }
108}
109