TwoPaneController.java revision 04ff99c8ac7742e952afb8a25b17f7997fc907de
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
18package com.android.mail.ui;
19
20import com.android.mail.ConversationListContext;
21import com.android.mail.R;
22import com.android.mail.providers.Conversation;
23import com.android.mail.utils.LogUtils;
24
25import android.app.Fragment;
26import android.app.FragmentTransaction;
27import android.os.Bundle;
28import android.view.Window;
29
30/**
31 * Controller for one-pane Mail activity. One Pane is used for phones, where screen real estate is
32 * limited.
33 */
34
35// Called OnePaneActivityController in Gmail.
36public final class TwoPaneController extends AbstractActivityController {
37    private boolean mJumpToFirstConversation;
38    private TwoPaneLayout mLayout;
39
40    /**
41     * @param activity
42     * @param viewMode
43     */
44    public TwoPaneController(MailActivity activity, ViewMode viewMode) {
45        super(activity, viewMode);
46    }
47
48    /**
49     * Display the conversation list fragment.
50     * @param show
51     */
52    private void initializeConversationListFragment(boolean show) {
53        if (show) {
54            mViewMode.enterConversationListMode();
55        }
56        renderConversationList();
57    }
58
59    /**
60     * Render the conversation list in the correct pane.
61     */
62    private void renderConversationList() {
63        FragmentTransaction fragmentTransaction = mActivity.getFragmentManager().beginTransaction();
64        // Use cross fading animation.
65        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
66        Fragment conversationListFragment = ConversationListFragment
67                .newInstance(mConvListContext);
68        fragmentTransaction.replace(R.id.conversation_list_pane, conversationListFragment);
69        fragmentTransaction.commitAllowingStateLoss();
70    }
71
72    /**
73     * Render the folder list in the correct pane.
74     */
75    private void renderFolderList() {
76        Fragment folderListFragment = FolderListFragment.newInstance(this, mAccount.folderListUri);
77        FragmentTransaction fragmentTransaction = mActivity.getFragmentManager().beginTransaction();
78        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
79        fragmentTransaction.replace(R.id.folders_pane, folderListFragment);
80        fragmentTransaction.commitAllowingStateLoss();
81        // Since we are showing the folder list, we are at the start of the view stack.
82        resetActionBarIcon();
83    }
84
85    @Override
86    protected boolean isConversationListVisible() {
87        // TODO(viki): Auto-generated method stub
88        return false;
89    }
90
91    @Override
92    public void showConversationList(ConversationListContext context) {
93        initializeConversationListFragment(true);
94        renderFolderList();
95    }
96
97    @Override
98    public void showFolderList() {
99        // On two-pane layouts, showing the folder list takes you to the top level of the
100        // application, which is the same as pressing the Up button
101        onUpPressed();
102    }
103
104    @Override
105    public boolean onCreate(Bundle savedState) {
106        mActivity.setContentView(R.layout.two_pane_activity);
107        mLayout = (TwoPaneLayout) mActivity.findViewById(R.id.two_pane_activity);
108        if (mLayout == null) {
109            LogUtils.d(LOG_TAG, "mLayout is null!");
110        }
111        mLayout.initializeLayout(mActivity.getApplicationContext());
112
113        // The tablet layout needs to refer to mode changes.
114        mViewMode.addListener(mLayout);
115        // The activity controller needs to listen to layout changes.
116        mLayout.setListener(this);
117
118        final boolean isParentInitialized = super.onCreate(savedState);
119        return isParentInitialized;
120    }
121
122    @Override
123    public void onViewModeChanged(int newMode) {
124        super.onViewModeChanged(newMode);
125        if (newMode != ViewMode.CONVERSATION) {
126            // Clear this flag if the user jumps out of conversation mode
127            // before a load completes.
128            mJumpToFirstConversation = false;
129        }
130    }
131
132    @Override
133    public void resetActionBarIcon() {
134        if (mViewMode.getMode() == ViewMode.CONVERSATION_LIST) {
135            mActionBarView.removeBackButton();
136        } else {
137            mActionBarView.setBackButton();
138        }
139    }
140
141    @Override
142    public void showConversation(Conversation conversation) {
143        mViewMode.enterConversationMode();
144        Fragment convFragment = ConversationViewFragment.newInstance(mAccount, conversation);
145        FragmentTransaction fragmentTransaction = mActivity.getFragmentManager().beginTransaction();
146        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
147        fragmentTransaction.replace(R.id.conversation_pane, convFragment);
148        fragmentTransaction.commitAllowingStateLoss();
149    }
150
151    /**
152     * Show the conversation list if it can be shown in the current orientation.
153     * @return true if the conversation list was shown
154     */
155    private boolean unhideConversationList(){
156        // Find if the conversation list can be shown
157        final boolean isConversationListShowable =
158                (mViewMode.getMode() == ViewMode.CONVERSATION &&
159                mLayout.isConversationListCollapsible());
160        if (isConversationListShowable) {
161            return mLayout.uncollapseList();
162        }
163        return false;
164    }
165
166    @Override
167    public boolean onUpPressed() {
168        return unhideConversationList();
169    }
170
171    @Override
172    public boolean onBackPressed() {
173        if (mViewMode.getMode() == ViewMode.CONVERSATION) {
174            return mViewMode.enterConversationListMode();
175        }
176        return false;
177    }
178}
179