diff options
| author | 2023-12-19 15:38:59 +0000 | |
|---|---|---|
| committer | 2023-12-19 18:28:07 +0000 | |
| commit | c66c3b8e0d3c9ed894c3d69d84d52e0ecff98a37 (patch) | |
| tree | 8d213f4a15cabe44ba2d8c6c1b837300ddac1b06 /java/src | |
| parent | 797b1431e473a74690d5c1fa8cc691a1a5d59667 (diff) | |
Move `setupProfileTabs()` to the pager adapter
This splits out aspects of the "setup" behavior that are generic to
the pager adapter (building the tabs, setting up the event handlers)
from "application-specific" parameterizations (like providing the
strings for the tab labels, or implementing the event handlers in a
way that integrates with other UI elements in our app).
These changes are as prototyped in ag/25335069 and described in
go/chooser-ntab-refactoring, in particular replicating the changes of
"snapshot 31" and "snapshot 32." See below for a "by-snapshot"
breakdown of the incremental changes composed in this CL.
Snapshot 1: separate the `setupProfileTabs()` design within
`ResolverActivity` so it's easier to see the diffs before moving to
the pager adapter. A newly-introduced static method (of the same name)
takes over most of the implementation, while parameterizing the
details we'll consider "application-specific." Notably, this splits
up two existing "callback" designs that had both "generic" and
"application-specific" responsibilities -- our `OnTabChangedListener`
and `MultiProfilePagerAdapter.OnProfileSelectedListener` (which,
TODO: might be consolidated to a single design in the future?).
The "generic" aspects of these listeners move to the newly-extracted
static method, but the listeners also compose-in application-specific
behavior that's provided in the parameterization.
Snapshot 2: move the parameterized method into the pager-adapter
(replace "static" and use the pager-adapter "implicit this" instead
of taking it as the explicit first parameter).
Snapshot 3: integrate ChooserActivity with the "parameterized method"
that was extracted to the pager adapter. This wasn't part of the
original prototype but is needed now that ChooserActivity no longer
inherits a shared implementation from ResolverActivity. Equivalence
is clear from side-by-side reading of the pager-adapter implementation
and the (before-change) ChooserActivity implementation.
Snapshot 4: move `updateActiveTabStyle()` from a caller "parameter"
of `setupProfileTabs()` to an internal implementation detail in the
pager-adapter, and generalize the behavior to support any number of
tabs. (This was "snapshot 32" in the original prototype.)
Bug: 310211468
Test: `IntentResolver-tests-actvity`, `ResolverActivityTest`
Change-Id: Ifc3a0d81d148241966041f7efd74774f22a933d7
Diffstat (limited to 'java/src')
3 files changed, 119 insertions, 113 deletions
diff --git a/java/src/com/android/intentresolver/v2/ChooserActivity.java b/java/src/com/android/intentresolver/v2/ChooserActivity.java index aa2b792a..2e020743 100644 --- a/java/src/com/android/intentresolver/v2/ChooserActivity.java +++ b/java/src/com/android/intentresolver/v2/ChooserActivity.java @@ -88,11 +88,9 @@ import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowInsets; import android.view.WindowManager; -import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TabHost; -import android.widget.TabWidget; import android.widget.TextView; import android.widget.Toast; @@ -1158,63 +1156,24 @@ public class ChooserActivity extends Hilt_ChooserActivity implements private void setupProfileTabs() { TabHost tabHost = findViewById(com.android.internal.R.id.profile_tabhost); - tabHost.setup(); ViewPager viewPager = findViewById(com.android.internal.R.id.profile_pager); - viewPager.setSaveEnabled(false); - - Button personalButton = (Button) getLayoutInflater().inflate( - R.layout.resolver_profile_tab_button, tabHost.getTabWidget(), false); - personalButton.setText(mDevicePolicyResources.getPersonalTabLabel()); - personalButton.setContentDescription( - mDevicePolicyResources.getPersonalTabAccessibilityLabel()); - - TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL) - .setContent(com.android.internal.R.id.profile_pager) - .setIndicator(personalButton); - tabHost.addTab(tabSpec); - - Button workButton = (Button) getLayoutInflater().inflate( - R.layout.resolver_profile_tab_button, tabHost.getTabWidget(), false); - workButton.setText(mDevicePolicyResources.getWorkTabLabel()); - workButton.setContentDescription(mDevicePolicyResources.getWorkTabAccessibilityLabel()); - - tabSpec = tabHost.newTabSpec(TAB_TAG_WORK) - .setContent(com.android.internal.R.id.profile_pager) - .setIndicator(workButton); - tabHost.addTab(tabSpec); - - TabWidget tabWidget = tabHost.getTabWidget(); - tabWidget.setVisibility(View.VISIBLE); - - Runnable updateActiveTabStyle = () -> { - int currentTab = tabHost.getCurrentTab(); - TextView selected = (TextView) tabHost.getTabWidget().getChildAt(currentTab); - TextView unselected = (TextView) tabHost.getTabWidget().getChildAt(1 - currentTab); - selected.setSelected(true); - unselected.setSelected(false); - }; - - updateActiveTabStyle.run(); - - tabHost.setOnTabChangedListener(tabId -> { - updateActiveTabStyle.run(); - if (TAB_TAG_PERSONAL.equals(tabId)) { - viewPager.setCurrentItem(0); - } else { - viewPager.setCurrentItem(1); - } - onProfileTabSelected(viewPager.getCurrentItem()); - }); - viewPager.setVisibility(View.VISIBLE); - tabHost.setCurrentTab(mChooserMultiProfilePagerAdapter.getCurrentPage()); - mChooserMultiProfilePagerAdapter.setOnProfileSelectedListener( + mChooserMultiProfilePagerAdapter.setupProfileTabs( + getLayoutInflater(), + tabHost, + viewPager, + R.layout.resolver_profile_tab_button, + com.android.internal.R.id.profile_pager, + mDevicePolicyResources.getPersonalTabLabel(), + mDevicePolicyResources.getPersonalTabAccessibilityLabel(), + TAB_TAG_PERSONAL, + mDevicePolicyResources.getWorkTabLabel(), + mDevicePolicyResources.getWorkTabAccessibilityLabel(), + TAB_TAG_WORK, + () -> onProfileTabSelected(viewPager.getCurrentItem()), new MultiProfilePagerAdapter.OnProfileSelectedListener() { @Override - public void onProfilePageSelected(@Profile int profileId, int pageNumber) { - tabHost.setCurrentTab(pageNumber); - - } + public void onProfilePageSelected(@Profile int profileId, int pageNumber) {} @Override public void onProfilePageStateChanged(int state) { @@ -1222,7 +1181,9 @@ public class ChooserActivity extends Hilt_ChooserActivity implements } }); mOnSwitchOnWorkSelectedListener = () -> { - final View workTab = tabHost.getTabWidget().getChildAt(1); + final View workTab = + tabHost.getTabWidget().getChildAt( + mChooserMultiProfilePagerAdapter.getPageNumberForProfile(PROFILE_WORK)); workTab.setFocusable(true); workTab.setFocusableInTouchMode(true); workTab.requestFocus(); diff --git a/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java index dc821e88..3387a73f 100644 --- a/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java @@ -19,8 +19,12 @@ import android.annotation.IntDef; import android.annotation.Nullable; import android.os.Trace; import android.os.UserHandle; +import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TabHost; +import android.widget.TextView; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; @@ -170,7 +174,7 @@ class MultiProfilePagerAdapter< return -1; } - private int getPageNumberForProfile(@Profile int profile) { + public int getPageNumberForProfile(@Profile int profile) { for (int i = 0; i < mItems.size(); ++i) { if (profile == mItems.get(i).mProfile) { return i; @@ -179,8 +183,85 @@ class MultiProfilePagerAdapter< return -1; } - public void setOnProfileSelectedListener(OnProfileSelectedListener listener) { - mOnProfileSelectedListener = listener; + private void updateActiveTabStyle(TabHost tabHost) { + int currentTab = tabHost.getCurrentTab(); + + for (int pageNumber = 0; pageNumber < getItemCount(); ++pageNumber) { + // TODO: can we avoid this downcast by pushing our knowledge of the intended view type + // somewhere else? + TextView tabText = (TextView) tabHost.getTabWidget().getChildAt(pageNumber); + tabText.setSelected(currentTab == pageNumber); + } + } + + public void setupProfileTabs( + LayoutInflater layoutInflater, + TabHost tabHost, + ViewPager viewPager, + int tabButtonLayoutResId, + int tabPageContentViewId, + String personalTabLabel, + String personalTabAccessibilityLabel, + String personalTabTag, + String workTabLabel, + String workTabAccessibilityLabel, + String workTabTag, + Runnable onTabChangeListener, + MultiProfilePagerAdapter.OnProfileSelectedListener clientOnProfileSelectedListener) { + tabHost.setup(); + viewPager.setSaveEnabled(false); + + Button personalButton = (Button) layoutInflater.inflate( + tabButtonLayoutResId, tabHost.getTabWidget(), false); + personalButton.setText(personalTabLabel); + personalButton.setContentDescription(personalTabAccessibilityLabel); + + TabHost.TabSpec personalTabSpec = tabHost.newTabSpec(personalTabTag) + .setContent(tabPageContentViewId) + .setIndicator(personalButton); + tabHost.addTab(personalTabSpec); + + Button workButton = (Button) layoutInflater.inflate( + tabButtonLayoutResId, tabHost.getTabWidget(), false); + workButton.setText(workTabLabel); + workButton.setContentDescription(workTabAccessibilityLabel); + + TabHost.TabSpec workTabSpec = tabHost.newTabSpec(workTabTag) + .setContent(tabPageContentViewId) + .setIndicator(workButton); + tabHost.addTab(workTabSpec); + + tabHost.getTabWidget().setVisibility(View.VISIBLE); + + updateActiveTabStyle(tabHost); + + tabHost.setOnTabChangedListener(tabId -> { + updateActiveTabStyle(tabHost); + // TODO: update for 3+ tabs. + if (personalTabTag.equals(tabId)) { + viewPager.setCurrentItem(0); + } else { + viewPager.setCurrentItem(1); + } + onTabChangeListener.run(); + }); + + viewPager.setVisibility(View.VISIBLE); + tabHost.setCurrentTab(getCurrentPage()); + mOnProfileSelectedListener = + new MultiProfilePagerAdapter.OnProfileSelectedListener() { + @Override + public void onProfilePageSelected(@Profile int profileId, int pageNumber) { + tabHost.setCurrentTab(pageNumber); + clientOnProfileSelectedListener.onProfilePageSelected( + profileId, pageNumber); + } + + @Override + public void onProfilePageStateChanged(int state) { + clientOnProfileSelectedListener.onProfilePageStateChanged(state); + } + }; } /** diff --git a/java/src/com/android/intentresolver/v2/ResolverActivity.java b/java/src/com/android/intentresolver/v2/ResolverActivity.java index bc3fb16b..2a6ed9d5 100644 --- a/java/src/com/android/intentresolver/v2/ResolverActivity.java +++ b/java/src/com/android/intentresolver/v2/ResolverActivity.java @@ -81,7 +81,6 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.Space; import android.widget.TabHost; -import android.widget.TabWidget; import android.widget.TextView; import android.widget.Toast; @@ -1895,72 +1894,37 @@ public class ResolverActivity extends Hilt_ResolverActivity implements private void setupProfileTabs() { maybeHideDivider(); + TabHost tabHost = findViewById(com.android.internal.R.id.profile_tabhost); - tabHost.setup(); ViewPager viewPager = findViewById(com.android.internal.R.id.profile_pager); - viewPager.setSaveEnabled(false); - - Button personalButton = (Button) getLayoutInflater().inflate( - R.layout.resolver_profile_tab_button, tabHost.getTabWidget(), false); - personalButton.setText(mDevicePolicyResources.getPersonalTabLabel()); - personalButton.setContentDescription( - mDevicePolicyResources.getPersonalTabAccessibilityLabel()); - - TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL) - .setContent(com.android.internal.R.id.profile_pager) - .setIndicator(personalButton); - tabHost.addTab(tabSpec); - - Button workButton = (Button) getLayoutInflater().inflate( - R.layout.resolver_profile_tab_button, tabHost.getTabWidget(), false); - workButton.setText(mDevicePolicyResources.getWorkTabLabel()); - workButton.setContentDescription(mDevicePolicyResources.getWorkTabAccessibilityLabel()); - - tabSpec = tabHost.newTabSpec(TAB_TAG_WORK) - .setContent(com.android.internal.R.id.profile_pager) - .setIndicator(workButton); - tabHost.addTab(tabSpec); - - TabWidget tabWidget = tabHost.getTabWidget(); - tabWidget.setVisibility(View.VISIBLE); - - Runnable updateActiveTabStyle = () -> { - int currentTab = tabHost.getCurrentTab(); - TextView selected = (TextView) tabHost.getTabWidget().getChildAt(currentTab); - TextView unselected = (TextView) tabHost.getTabWidget().getChildAt(1 - currentTab); - selected.setSelected(true); - unselected.setSelected(false); - }; - - updateActiveTabStyle.run(); - - tabHost.setOnTabChangedListener(tabId -> { - updateActiveTabStyle.run(); - if (TAB_TAG_PERSONAL.equals(tabId)) { - viewPager.setCurrentItem(0); - } else { - viewPager.setCurrentItem(1); - } - onProfileTabSelected(viewPager.getCurrentItem()); - }); - viewPager.setVisibility(View.VISIBLE); - tabHost.setCurrentTab(mMultiProfilePagerAdapter.getCurrentPage()); - mMultiProfilePagerAdapter.setOnProfileSelectedListener( + mMultiProfilePagerAdapter.setupProfileTabs( + getLayoutInflater(), + tabHost, + viewPager, + R.layout.resolver_profile_tab_button, + com.android.internal.R.id.profile_pager, + mDevicePolicyResources.getPersonalTabLabel(), + mDevicePolicyResources.getPersonalTabAccessibilityLabel(), + TAB_TAG_PERSONAL, + mDevicePolicyResources.getWorkTabLabel(), + mDevicePolicyResources.getWorkTabAccessibilityLabel(), + TAB_TAG_WORK, + () -> onProfileTabSelected(viewPager.getCurrentItem()), new MultiProfilePagerAdapter.OnProfileSelectedListener() { @Override public void onProfilePageSelected(@Profile int profileId, int pageNumber) { - tabHost.setCurrentTab(pageNumber); resetButtonBar(); resetCheckedItem(); } @Override - public void onProfilePageStateChanged(int state) { - } + public void onProfilePageStateChanged(int state) {} }); mOnSwitchOnWorkSelectedListener = () -> { - final View workTab = tabHost.getTabWidget().getChildAt(1); + final View workTab = + tabHost.getTabWidget().getChildAt( + mMultiProfilePagerAdapter.getPageNumberForProfile(PROFILE_WORK)); workTab.setFocusable(true); workTab.setFocusableInTouchMode(true); workTab.requestFocus(); |