MessageCommandButtonView.java revision 931ca67a1f1baa0c1fd115124e35183c7786fc63
1/*
2 * Copyright (C) 2010 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.email.activity;
18
19import com.android.email.R;
20
21import android.content.Context;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.RelativeLayout;
25import android.widget.TextView;
26
27/**
28 * A View that is shown at the bottom of {@link MessageViewFragment} and contains buttons such
29 * as "(move to) newer".
30 *
31 * This class is meant to hide layout differences between portrait and landscape, if any.
32 * e.g. We might combine some of the buttons when we have small real estate.
33 */
34public class MessageCommandButtonView extends RelativeLayout implements View.OnClickListener {
35    /**
36     * If false, we don't want to show anything, in which case all fields holding a view
37     * (e.g. {@link #mMoveToNewerButton}) are null.
38     */
39    private boolean mShowPanel;
40
41    private View mMoveToNewerButton;
42    private View mMoveToOlderButton;
43    private TextView mMessagePosition;
44
45    private Callback mCallback = EmptyCallback.INSTANCE;
46
47    public interface Callback {
48        public void onMoveToNewer();
49        public void onMoveToOlder();
50    }
51
52    private static class EmptyCallback implements Callback {
53        public static final Callback INSTANCE = new EmptyCallback();
54        @Override public void onMoveToNewer() {}
55        @Override public void onMoveToOlder() {}
56    }
57
58    public MessageCommandButtonView(Context context, AttributeSet attrs, int defStyle) {
59        super(context, attrs, defStyle);
60    }
61
62    public MessageCommandButtonView(Context context, AttributeSet attrs) {
63        super(context, attrs);
64    }
65
66    public MessageCommandButtonView(Context context) {
67        super(context);
68    }
69
70    @Override
71    protected void onFinishInflate() {
72        super.onFinishInflate();
73
74        mMoveToNewerButton = findViewById(R.id.move_to_newer_button);
75        if (mMoveToNewerButton == null) {
76            mShowPanel = false;
77            return;
78        }
79        mShowPanel = true;
80        mMoveToOlderButton = findViewById(R.id.move_to_older_button);
81        mMessagePosition = (TextView) findViewById(R.id.message_position);
82
83        mMoveToNewerButton.setOnClickListener(this);
84        mMoveToOlderButton.setOnClickListener(this);
85    }
86
87    public void setCallback(Callback callback) {
88        mCallback = (callback == null) ? EmptyCallback.INSTANCE : callback;
89    }
90
91    public void enableNavigationButtons(boolean enableMoveToNewer, boolean enableMoveToOlder,
92            int currentPosition, int countMessages) {
93        if (!mShowPanel) {
94            return;
95        }
96        mMoveToNewerButton.setEnabled(enableMoveToNewer);
97        mMoveToOlderButton.setEnabled(enableMoveToOlder);
98
99        // Show "POSITION of TOTAL"
100        final String positionOfCount;
101        if (countMessages == 0) {
102            positionOfCount = "";
103        } else {
104            positionOfCount = getContext().getResources().getString(R.string.position_of_count,
105                (currentPosition + 1), countMessages);
106        }
107        mMessagePosition.setText(positionOfCount);
108    }
109
110    @Override
111    public void onClick(View v) {
112        switch (v.getId()) {
113            case R.id.move_to_newer_button:
114                mCallback.onMoveToNewer();
115                break;
116            case R.id.move_to_older_button:
117                mCallback.onMoveToOlder();
118                break;
119        }
120    }
121}
122