|
|
|
|
@ -15,17 +15,17 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.tasks.ui;
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.support.v4.graphics.drawable.DrawableCompat;
|
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
|
import android.util.TypedValue;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
import android.view.SubMenu;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
|
|
|
|
|
import org.tasks.R;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class to set the color and transparency for menu icons in an ActionBar/Toolbar.</p>
|
|
|
|
|
@ -52,105 +52,42 @@ public class MenuColorizer {
|
|
|
|
|
private MenuColorizer() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets a color filter on all menu icons, including the overflow button (if it exists) */
|
|
|
|
|
public static void colorMenu(final Activity activity, final Menu menu, final int color) {
|
|
|
|
|
colorMenu(activity, menu, color, 0);
|
|
|
|
|
public static void colorToolbar(Context context, Toolbar toolbar) {
|
|
|
|
|
TypedValue typedValue = new TypedValue();
|
|
|
|
|
context.getTheme().resolveAttribute(R.attr.actionBarPrimaryText, typedValue, true);
|
|
|
|
|
colorToolbar(toolbar, typedValue.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void colorToolbar(Toolbar toolbar, int color) {
|
|
|
|
|
Drawable wrapped = DrawableCompat.wrap(toolbar.getNavigationIcon());
|
|
|
|
|
wrapped.setTint(color);
|
|
|
|
|
toolbar.setNavigationIcon(wrapped);
|
|
|
|
|
toolbar.setTitleTextColor(color);
|
|
|
|
|
colorMenu(toolbar.getMenu(), color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets a color filter on all menu icons, including the overflow button (if it exists) */
|
|
|
|
|
public static void colorMenu(final Activity activity, final Menu menu, final int color,
|
|
|
|
|
final int alpha) {
|
|
|
|
|
private static void colorMenu(final Menu menu, final int color) {
|
|
|
|
|
for (int i = 0, size = menu.size(); i < size; i++) {
|
|
|
|
|
final MenuItem menuItem = menu.getItem(i);
|
|
|
|
|
colorMenuItem(menuItem, color, alpha);
|
|
|
|
|
colorMenuItem(menuItem, color);
|
|
|
|
|
if (menuItem.hasSubMenu()) {
|
|
|
|
|
final SubMenu subMenu = menuItem.getSubMenu();
|
|
|
|
|
for (int j = 0; j < subMenu.size(); j++) {
|
|
|
|
|
colorMenuItem(subMenu.getItem(j), color, alpha);
|
|
|
|
|
colorMenuItem(subMenu.getItem(j), color);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
final View home = activity.findViewById(android.R.id.home);
|
|
|
|
|
if (home != null) {
|
|
|
|
|
home.post(new Runnable() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
colorOverflow(activity, color, alpha);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets a color filter on a {@link MenuItem} */
|
|
|
|
|
public static void colorMenuItem(final MenuItem menuItem, final int color) {
|
|
|
|
|
colorMenuItem(menuItem, color, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets a color filter on a {@link MenuItem} */
|
|
|
|
|
public static void colorMenuItem(final MenuItem menuItem, final int color, final int alpha) {
|
|
|
|
|
private static void colorMenuItem(final MenuItem menuItem, final int color) {
|
|
|
|
|
final Drawable drawable = menuItem.getIcon();
|
|
|
|
|
if (drawable != null) {
|
|
|
|
|
// If we don't mutate the drawable, then all drawable's with this id will have a color
|
|
|
|
|
// filter applied to it.
|
|
|
|
|
drawable.mutate();
|
|
|
|
|
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
|
|
|
|
|
if (alpha > 0) {
|
|
|
|
|
drawable.setAlpha(alpha);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets a color filter on the OverflowMenuButton in an ActionBar or Toolbar */
|
|
|
|
|
public static void colorOverflow(final Activity activity, final int color) {
|
|
|
|
|
colorOverflow(activity, color, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sets a color filter on the OverflowMenuButton in an ActionBar or Toolbar */
|
|
|
|
|
@SuppressLint("NewApi")
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
public static void colorOverflow(final Activity activity, final int color, final int alpha) {
|
|
|
|
|
final ImageButton overflow = getOverflowMenu(activity);
|
|
|
|
|
if (overflow != null) {
|
|
|
|
|
overflow.setColorFilter(color);
|
|
|
|
|
if (alpha > 0) {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
|
|
|
|
overflow.setImageAlpha(alpha);
|
|
|
|
|
} else {
|
|
|
|
|
overflow.setAlpha(alpha);
|
|
|
|
|
}
|
|
|
|
|
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find that OverflowMenuButton */
|
|
|
|
|
private static ImageButton getOverflowMenu(final Activity activity,
|
|
|
|
|
final ViewGroup... viewGroup) {
|
|
|
|
|
final ViewGroup group;
|
|
|
|
|
if (viewGroup == null || viewGroup.length == 0) {
|
|
|
|
|
final int resId = activity.getResources().getIdentifier("action_bar", "id", "android");
|
|
|
|
|
if (resId != 0) {
|
|
|
|
|
group = (ViewGroup) activity.findViewById(resId);
|
|
|
|
|
} else {
|
|
|
|
|
group = (ViewGroup) activity.findViewById(android.R.id.content).getRootView();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
group = viewGroup[0];
|
|
|
|
|
}
|
|
|
|
|
ImageButton overflow = null;
|
|
|
|
|
for (int i = 0, l = group.getChildCount(); i < l; i++) {
|
|
|
|
|
final View v = group.getChildAt(i);
|
|
|
|
|
if (v instanceof ImageButton
|
|
|
|
|
&& v.getClass().getSimpleName().equals("OverflowMenuButton")) {
|
|
|
|
|
overflow = (ImageButton) v;
|
|
|
|
|
} else if (v instanceof ViewGroup) {
|
|
|
|
|
overflow = getOverflowMenu(activity, (ViewGroup) v);
|
|
|
|
|
}
|
|
|
|
|
if (overflow != null) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return overflow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|