OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.widget.bottomsheet; | 5 package org.chromium.chrome.browser.widget.bottomsheet; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
| 9 import android.support.annotation.IntDef; |
9 import android.support.design.internal.BottomNavigationItemView; | 10 import android.support.design.internal.BottomNavigationItemView; |
10 import android.support.design.internal.BottomNavigationMenuView; | 11 import android.support.design.internal.BottomNavigationMenuView; |
11 import android.support.design.widget.BottomNavigationView; | 12 import android.support.design.widget.BottomNavigationView; |
12 import android.support.design.widget.BottomNavigationView.OnNavigationItemSelect
edListener; | 13 import android.support.design.widget.BottomNavigationView.OnNavigationItemSelect
edListener; |
13 import android.util.AttributeSet; | 14 import android.util.AttributeSet; |
14 import android.view.MenuItem; | 15 import android.view.MenuItem; |
15 import android.view.View; | 16 import android.view.View; |
16 | 17 |
17 import org.chromium.chrome.R; | 18 import org.chromium.chrome.R; |
18 import org.chromium.chrome.browser.bookmarks.BookmarkSheetContent; | 19 import org.chromium.chrome.browser.bookmarks.BookmarkSheetContent; |
19 import org.chromium.chrome.browser.download.DownloadSheetContent; | 20 import org.chromium.chrome.browser.download.DownloadSheetContent; |
20 import org.chromium.chrome.browser.history.HistorySheetContent; | 21 import org.chromium.chrome.browser.history.HistorySheetContent; |
21 import org.chromium.chrome.browser.suggestions.SuggestionsBottomSheetContent; | 22 import org.chromium.chrome.browser.suggestions.SuggestionsBottomSheetContent; |
22 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 23 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
23 import org.chromium.chrome.browser.util.MathUtils; | 24 import org.chromium.chrome.browser.util.MathUtils; |
24 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetCon
tent; | 25 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetCon
tent; |
25 | 26 |
| 27 import java.lang.annotation.Retention; |
| 28 import java.lang.annotation.RetentionPolicy; |
26 import java.lang.reflect.Field; | 29 import java.lang.reflect.Field; |
27 import java.util.HashMap; | 30 import java.util.HashMap; |
28 import java.util.Iterator; | 31 import java.util.Iterator; |
29 import java.util.Map; | 32 import java.util.Map; |
30 import java.util.Map.Entry; | 33 import java.util.Map.Entry; |
31 | 34 |
32 /** | 35 /** |
33 * Displays and controls a {@link BottomNavigationView} fixed to the bottom of t
he | 36 * Displays and controls a {@link BottomNavigationView} fixed to the bottom of t
he |
34 * {@link BottomSheet}. Also manages {@link BottomSheetContent} displayed in the
BottomSheet. | 37 * {@link BottomSheet}. Also manages {@link BottomSheetContent} displayed in the
BottomSheet. |
35 */ | 38 */ |
36 public class BottomSheetContentController extends BottomNavigationView | 39 public class BottomSheetContentController extends BottomNavigationView |
37 implements BottomSheetObserver, OnNavigationItemSelectedListener { | 40 implements OnNavigationItemSelectedListener { |
| 41 /** The different types of content that may be displayed in the bottom sheet
. */ |
| 42 @IntDef({TYPE_SUGGESTIONS, TYPE_DOWNLOADS, TYPE_BOOKMARKS, TYPE_HISTORY}) |
| 43 @Retention(RetentionPolicy.SOURCE) |
| 44 public @interface ContentType {} |
| 45 public static final int TYPE_SUGGESTIONS = 0; |
| 46 public static final int TYPE_DOWNLOADS = 1; |
| 47 public static final int TYPE_BOOKMARKS = 2; |
| 48 public static final int TYPE_HISTORY = 3; |
| 49 |
| 50 private final Map<Integer, BottomSheetContent> mBottomSheetContents = new Ha
shMap<>(); |
| 51 |
| 52 private final BottomSheetObserver mBottomSheetObserver = new EmptyBottomShee
tObserver() { |
| 53 @Override |
| 54 public void onTransitionPeekToHalf(float transitionFraction) { |
| 55 float offsetY = (mBottomSheet.getMinOffset() - mBottomSheet.getSheet
OffsetFromBottom()) |
| 56 + mDistanceBelowToolbarPx; |
| 57 setTranslationY((int) Math.max(offsetY, 0f)); |
| 58 setVisibility( |
| 59 MathUtils.areFloatsEqual(transitionFraction, 0f) ? View.GONE
: View.VISIBLE); |
| 60 } |
| 61 |
| 62 @Override |
| 63 public void onSheetOpened() { |
| 64 if (!mDefaultContentInitialized && mTabModelSelector.getCurrentTab()
!= null) { |
| 65 initializeDefaultContent(); |
| 66 } |
| 67 } |
| 68 |
| 69 @Override |
| 70 public void onSheetClosed() { |
| 71 Iterator<Entry<Integer, BottomSheetContent>> contentIterator = |
| 72 mBottomSheetContents.entrySet().iterator(); |
| 73 while (contentIterator.hasNext()) { |
| 74 Entry<Integer, BottomSheetContent> entry = contentIterator.next(
); |
| 75 if (entry.getKey() == R.id.action_home) continue; |
| 76 |
| 77 entry.getValue().destroy(); |
| 78 contentIterator.remove(); |
| 79 } |
| 80 // TODO(twellington): determine a policy for destroying the |
| 81 // SuggestionsBottomSheetContent. |
| 82 |
| 83 if (mSelectedItemId == 0 || mSelectedItemId == R.id.action_home) ret
urn; |
| 84 |
| 85 showBottomSheetContent(R.id.action_home); |
| 86 } |
| 87 }; |
| 88 |
38 private BottomSheet mBottomSheet; | 89 private BottomSheet mBottomSheet; |
39 private TabModelSelector mTabModelSelector; | 90 private TabModelSelector mTabModelSelector; |
40 private float mDistanceBelowToolbarPx; | 91 private float mDistanceBelowToolbarPx; |
41 private int mSelectedItemId; | 92 private int mSelectedItemId; |
42 private boolean mDefaultContentInitialized; | 93 private boolean mDefaultContentInitialized; |
43 | 94 |
44 private final Map<Integer, BottomSheetContent> mBottomSheetContents = new Ha
shMap<>(); | |
45 | |
46 public BottomSheetContentController(Context context, AttributeSet atts) { | 95 public BottomSheetContentController(Context context, AttributeSet atts) { |
47 super(context, atts); | 96 super(context, atts); |
48 } | 97 } |
49 | 98 |
50 /** | 99 /** |
51 * Initializes the {@link BottomSheetContentController}. | 100 * Initializes the {@link BottomSheetContentController}. |
52 * @param bottomSheet The {@link BottomSheet} associated with this bottom na
v. | 101 * @param bottomSheet The {@link BottomSheet} associated with this bottom na
v. |
53 * @param controlContainerHeight The height of the control container in px. | 102 * @param controlContainerHeight The height of the control container in px. |
54 * @param tabModelSelector The {@link TabModelSelector} for the application. | 103 * @param tabModelSelector The {@link TabModelSelector} for the application. |
55 */ | 104 */ |
56 public void init(BottomSheet bottomSheet, int controlContainerHeight, | 105 public void init(BottomSheet bottomSheet, int controlContainerHeight, |
57 TabModelSelector tabModelSelector) { | 106 TabModelSelector tabModelSelector) { |
58 mBottomSheet = bottomSheet; | 107 mBottomSheet = bottomSheet; |
59 mBottomSheet.addObserver(this); | 108 mBottomSheet.addObserver(mBottomSheetObserver); |
60 mTabModelSelector = tabModelSelector; | 109 mTabModelSelector = tabModelSelector; |
61 | 110 |
62 Resources res = getContext().getResources(); | 111 Resources res = getContext().getResources(); |
63 mDistanceBelowToolbarPx = controlContainerHeight | 112 mDistanceBelowToolbarPx = controlContainerHeight |
64 + res.getDimensionPixelOffset(R.dimen.bottom_nav_space_from_tool
bar); | 113 + res.getDimensionPixelOffset(R.dimen.bottom_nav_space_from_tool
bar); |
65 | 114 |
66 setOnNavigationItemSelectedListener(this); | 115 setOnNavigationItemSelectedListener(this); |
67 disableShiftingMode(); | 116 disableShiftingMode(); |
68 } | 117 } |
69 | 118 |
70 /** | 119 /** |
71 * Initialize the default {@link BottomSheetContent}. | 120 * Initialize the default {@link BottomSheetContent}. |
72 */ | 121 */ |
73 public void initializeDefaultContent() { | 122 public void initializeDefaultContent() { |
74 if (mDefaultContentInitialized) return; | 123 if (mDefaultContentInitialized) return; |
75 showBottomSheetContent(R.id.action_home); | 124 showBottomSheetContent(R.id.action_home); |
76 mDefaultContentInitialized = true; | 125 mDefaultContentInitialized = true; |
77 } | 126 } |
78 | 127 |
79 @Override | 128 @Override |
80 public boolean onNavigationItemSelected(MenuItem item) { | 129 public boolean onNavigationItemSelected(MenuItem item) { |
81 if (mSelectedItemId == item.getItemId()) return false; | 130 if (mSelectedItemId == item.getItemId()) return false; |
82 | 131 |
83 showBottomSheetContent(item.getItemId()); | 132 showBottomSheetContent(item.getItemId()); |
84 return true; | 133 return true; |
85 } | 134 } |
86 | 135 |
87 @Override | |
88 public void onTransitionPeekToHalf(float transitionFraction) { | |
89 float offsetY = (mBottomSheet.getMinOffset() - mBottomSheet.getSheetOffs
etFromBottom()) | |
90 + mDistanceBelowToolbarPx; | |
91 setTranslationY((int) Math.max(offsetY, 0f)); | |
92 setVisibility(MathUtils.areFloatsEqual(transitionFraction, 0f) ? View.GO
NE : View.VISIBLE); | |
93 } | |
94 | |
95 @Override | |
96 public void onSheetOpened() { | |
97 if (!mDefaultContentInitialized && mTabModelSelector.getCurrentTab() !=
null) { | |
98 initializeDefaultContent(); | |
99 } | |
100 } | |
101 | |
102 @Override | |
103 public void onSheetClosed() { | |
104 Iterator<Entry<Integer, BottomSheetContent>> contentIterator = | |
105 mBottomSheetContents.entrySet().iterator(); | |
106 while (contentIterator.hasNext()) { | |
107 Entry<Integer, BottomSheetContent> entry = contentIterator.next(); | |
108 if (entry.getKey() == R.id.action_home) continue; | |
109 | |
110 entry.getValue().destroy(); | |
111 contentIterator.remove(); | |
112 } | |
113 // TODO(twellington): determine a policy for destroying the SuggestionsB
ottomSheetContent. | |
114 | |
115 if (mSelectedItemId == 0 || mSelectedItemId == R.id.action_home) return; | |
116 | |
117 showBottomSheetContent(R.id.action_home); | |
118 } | |
119 | |
120 @Override | |
121 public void onLoadUrl(String url) {} | |
122 | |
123 @Override | |
124 public void onSheetOffsetChanged(float heightFraction) {} | |
125 | |
126 // TODO(twellington): remove this once the support library is updated to all
ow disabling | 136 // TODO(twellington): remove this once the support library is updated to all
ow disabling |
127 // shifting mode or determines shifting mode based on the
width of the | 137 // shifting mode or determines shifting mode based on the
width of the |
128 // child views. | 138 // child views. |
129 private void disableShiftingMode() { | 139 private void disableShiftingMode() { |
130 BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildA
t(0); | 140 BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildA
t(0); |
131 try { | 141 try { |
132 Field shiftingMode = menuView.getClass().getDeclaredField("mShifting
Mode"); | 142 Field shiftingMode = menuView.getClass().getDeclaredField("mShifting
Mode"); |
133 shiftingMode.setAccessible(true); | 143 shiftingMode.setAccessible(true); |
134 shiftingMode.setBoolean(menuView, false); | 144 shiftingMode.setBoolean(menuView, false); |
135 shiftingMode.setAccessible(false); | 145 shiftingMode.setAccessible(false); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 // There are some bugs related to programatically selecting menu items t
hat are fixed in | 178 // There are some bugs related to programatically selecting menu items t
hat are fixed in |
169 // newer support library versions. | 179 // newer support library versions. |
170 // TODO(twellington): remove this after the support library is rolled. | 180 // TODO(twellington): remove this after the support library is rolled. |
171 if (mSelectedItemId != 0) getMenu().findItem(mSelectedItemId).setChecked
(false); | 181 if (mSelectedItemId != 0) getMenu().findItem(mSelectedItemId).setChecked
(false); |
172 mSelectedItemId = navItemId; | 182 mSelectedItemId = navItemId; |
173 getMenu().findItem(mSelectedItemId).setChecked(true); | 183 getMenu().findItem(mSelectedItemId).setChecked(true); |
174 | 184 |
175 mBottomSheet.showContent(getSheetContentForId(mSelectedItemId)); | 185 mBottomSheet.showContent(getSheetContentForId(mSelectedItemId)); |
176 } | 186 } |
177 } | 187 } |
OLD | NEW |