Skip to content

Commit 130b457

Browse files
committed
ActionProvider API update
* Add ActionProvider#overridesItemVisibility and isVisible. These methods allow an ActionProvider to override the visibility of a MenuItem that it is bound to. If a MenuItem has been explicitly hidden by the application, it will not be visible. * Change MediaRouteActionProvider to not require a MediaRouter callback, to avoid extra lifecycle management headaches. Change-Id: I606fa98b3a6a3e60a953dd024274f9bf9c67acdd
1 parent d8bbf96 commit 130b457

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

api/current.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22814,10 +22814,12 @@ package android.view {
2281422814
public abstract class ActionProvider {
2281522815
ctor public ActionProvider(android.content.Context);
2281622816
method public boolean hasSubMenu();
22817+
method public boolean isVisible();
2281722818
method public abstract deprecated android.view.View onCreateActionView();
2281822819
method public android.view.View onCreateActionView(android.view.MenuItem);
2281922820
method public boolean onPerformDefaultAction();
2282022821
method public void onPrepareSubMenu(android.view.SubMenu);
22822+
method public boolean overridesItemVisibility();
2282122823
}
2282222824

2282322825
public final class Choreographer {

core/java/android/app/MediaRouteActionProvider.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.content.Context;
2222
import android.content.ContextWrapper;
2323
import android.media.MediaRouter;
24-
import android.media.MediaRouter.RouteInfo;
2524
import android.util.Log;
2625
import android.view.ActionProvider;
2726
import android.view.MenuItem;
@@ -35,7 +34,6 @@ public class MediaRouteActionProvider extends ActionProvider {
3534
private MenuItem mMenuItem;
3635
private MediaRouteButton mView;
3736
private int mRouteTypes;
38-
private final RouterCallback mRouterCallback = new RouterCallback();
3937
private View.OnClickListener mExtendedSettingsListener;
4038

4139
public MediaRouteActionProvider(Context context) {
@@ -50,18 +48,10 @@ public MediaRouteActionProvider(Context context) {
5048
}
5149

5250
public void setRouteTypes(int types) {
53-
if (types == mRouteTypes) {
54-
// Already registered; nothing to do.
55-
return;
56-
}
57-
if (mRouteTypes != 0) {
58-
mRouter.removeCallback(mRouterCallback);
59-
}
6051
mRouteTypes = types;
6152
if (mView != null) {
6253
mView.setRouteTypes(mRouteTypes);
6354
}
64-
mRouter.addCallback(types, mRouterCallback);
6555
}
6656

6757
@Override
@@ -124,15 +114,13 @@ public void setExtendedSettingsClickListener(View.OnClickListener listener) {
124114
}
125115
}
126116

127-
private class RouterCallback extends MediaRouter.SimpleCallback {
128-
@Override
129-
public void onRouteAdded(MediaRouter router, RouteInfo info) {
130-
mMenuItem.setVisible(mRouter.getRouteCount() > 1);
131-
}
117+
@Override
118+
public boolean overridesItemVisibility() {
119+
return true;
120+
}
132121

133-
@Override
134-
public void onRouteRemoved(MediaRouter router, RouteInfo info) {
135-
mMenuItem.setVisible(mRouter.getRouteCount() > 1);
136-
}
122+
@Override
123+
public boolean isVisible() {
124+
return mRouter.getRouteCount() > 1;
137125
}
138126
}

core/java/android/view/ActionProvider.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ public View onCreateActionView(MenuItem forItem) {
9595
return onCreateActionView();
9696
}
9797

98+
/**
99+
* The result of this method determines whether or not {@link #isVisible()} will be used
100+
* by the {@link MenuItem} this ActionProvider is bound to help determine its visibility.
101+
*
102+
* @return true if this ActionProvider overrides the visibility of the MenuItem
103+
* it is bound to, false otherwise. The default implementation returns false.
104+
* @see #isVisible()
105+
*/
106+
public boolean overridesItemVisibility() {
107+
return false;
108+
}
109+
110+
/**
111+
* If {@link #overridesItemVisibility()} returns true, the return value of this method
112+
* will help determine the visibility of the {@link MenuItem} this ActionProvider is bound to.
113+
*
114+
* <p>If the MenuItem's visibility is explicitly set to false by the application,
115+
* the MenuItem will not be shown, even if this method returns true.</p>
116+
*
117+
* @return true if the MenuItem this ActionProvider is bound to is visible, false if
118+
* it is invisible. The default implementation returns true.
119+
*/
120+
public boolean isVisible() {
121+
return true;
122+
}
123+
98124
/**
99125
* Performs an optional default action.
100126
* <p>

core/java/com/android/internal/view/menu/MenuItemImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ void setCheckedInt(boolean checked) {
456456
}
457457

458458
public boolean isVisible() {
459+
if (mActionProvider != null && mActionProvider.overridesItemVisibility()) {
460+
return (mFlags & HIDDEN) == 0 && mActionProvider.isVisible();
461+
}
459462
return (mFlags & HIDDEN) == 0;
460463
}
461464

media/java/android/media/MediaRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ public Drawable getIconDrawable() {
662662
*/
663663
public void setTag(Object tag) {
664664
mTag = tag;
665+
routeUpdated();
665666
}
666667

667668
/**
@@ -675,7 +676,6 @@ public Object getTag() {
675676
void setStatusInt(CharSequence status) {
676677
if (!status.equals(mStatus)) {
677678
mStatus = status;
678-
routeUpdated();
679679
if (mGroup != null) {
680680
mGroup.memberStatusChanged(this, status);
681681
}

0 commit comments

Comments
 (0)