ViewMode.java revision 8ffe4320a17c3f4234f473e8f0ce083732064aa9
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.google.common.collect.Lists; 21 22import android.content.Context; 23import android.os.Bundle; 24 25import com.android.mail.utils.Utils; 26 27import java.util.ArrayList; 28 29/** 30 * Represents the view mode for the tablet Gmail activity. 31 * Transitions between modes should be done through this central object, and UI components that are 32 * dependent on the mode should listen to changes on this object. 33 */ 34public class ViewMode { 35 /** 36 * A listener for changes on a ViewMode. 37 */ 38 public interface ModeChangeListener { 39 /** 40 * Handles a mode change. 41 */ 42 void onViewModeChanged(ViewMode mode); 43 } 44 45 /** 46 * Mode when showing a single conversation. 47 */ 48 public static final int CONVERSATION = 1; 49 /** 50 * Mode when showing a list of conversations 51 */ 52 public static final int CONVERSATION_LIST = 2; 53 /** 54 * Mode when showing a list of folders. 55 */ 56 public static final int FOLDER_LIST = 3; 57 /** 58 * Mode when showing results from user search. 59 */ 60 public static final int SEARCH_RESULTS = 4; 61 /** 62 * Uncertain mode. The mode has not been initialized. 63 */ 64 public static final int UNKNOWN = 0; 65 66 // Key used to save this {@link ViewMode}. 67 private static final String VIEW_MODE_KEY = "view-mode"; 68 private final ArrayList<ModeChangeListener> mListeners = Lists.newArrayList(); 69 private int mMode = UNKNOWN; 70 private boolean mTwoPane; 71 72 public ViewMode(Context context) { 73 mTwoPane = Utils.useTabletUI(context); 74 } 75 76 /** 77 * Adds a listener from this view mode. 78 * Must happen in the UI thread. 79 */ 80 public void addListener(ModeChangeListener listener) { 81 mListeners.add(listener); 82 } 83 84 /** 85 * Dispatches a change event for the mode. 86 * Always happens in the UI thread. 87 */ 88 private void dispatchModeChange() { 89 ArrayList<ModeChangeListener> list = new ArrayList<ModeChangeListener>(mListeners); 90 for (ModeChangeListener listener : list) { 91 listener.onViewModeChanged(this); 92 } 93 } 94 95 /** 96 * @return The current mode. 97 */ 98 public int getMode() { 99 return mMode; 100 } 101 102 public void handleRestore(Bundle inState) { 103 mMode = inState.getInt(VIEW_MODE_KEY); 104 } 105 106 public void handleSaveInstanceState(Bundle outState) { 107 outState.putInt(VIEW_MODE_KEY, mMode); 108 } 109 110 public boolean isConversationListMode() { 111 return mMode == CONVERSATION_LIST; 112 } 113 114 public boolean isConversationMode() { 115 return mMode == CONVERSATION; 116 } 117 118 public boolean isFolderListMode() { 119 return mMode == FOLDER_LIST; 120 } 121 122 /** 123 * @return Whether or not to display 2 pane. 124 */ 125 public boolean isTwoPane() { 126 return mTwoPane; 127 } 128 129 /** 130 * Removes a listener from this view mode. 131 * Must happen in the UI thread. 132 */ 133 public void removeListener(ModeChangeListener listener) { 134 mListeners.remove(listener); 135 } 136 137 /** 138 * Sets the internal mode. 139 * @return Whether or not a change occured. 140 */ 141 private boolean setModeInternal(int mode) { 142 if (mMode == mode) { 143 return false; 144 } 145 mMode = mode; 146 dispatchModeChange(); 147 return true; 148 } 149 150 /** 151 * Requests a transition of the mode to show the conversation list as the prominent view. 152 * @return Whether or not a change occured. 153 */ 154 public boolean transitionToConversationListMode() { 155 return setModeInternal(CONVERSATION_LIST); 156 } 157 158 /** 159 * Requests a transition of the mode to show a conversation as the prominent view. 160 * @return Whether or not a change occured. 161 */ 162 public boolean transitionToConversationMode() { 163 return setModeInternal(CONVERSATION); 164 } 165 166 /** 167 * Requests a transition of the mode to show the folder list as the prominent view. 168 * @return Whether or not a change occured. 169 */ 170 public boolean transitionToFolderListMode() { 171 return setModeInternal(FOLDER_LIST); 172 } 173} 174