| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.chromoting; | 5 package org.chromium.chromoting; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.Resources; |
| 8 import android.graphics.PorterDuff; | 9 import android.graphics.PorterDuff; |
| 9 import android.graphics.drawable.Drawable; | 10 import android.graphics.drawable.Drawable; |
| 10 import android.util.TypedValue; | 11 import android.util.TypedValue; |
| 11 import android.view.Menu; | 12 import android.view.Menu; |
| 12 | 13 |
| 13 import org.chromium.base.ApiCompatibilityUtils; | 14 import org.chromium.base.ApiCompatibilityUtils; |
| 14 import org.chromium.base.Log; | |
| 15 | 15 |
| 16 /** Utility methods for chromoting code. */ | 16 /** Utility methods for chromoting code. */ |
| 17 public abstract class ChromotingUtil { | 17 public abstract class ChromotingUtil { |
| 18 private static final String TAG = "Chromoting"; | 18 private static final String TAG = "Chromoting"; |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Tints all icons of a toolbar menu so they have the same color as the 'bac
k' navigation icon | 21 * Tints all icons of a toolbar menu so they have the same color as the 'bac
k' navigation icon |
| 22 * and the three-dots overflow icon. | 22 * and the three-dots overflow icon. |
| 23 * @param context Context for getting theme and resource information. | 23 * @param context Context for getting theme and resource information. |
| 24 * @param menu Menu with icons to be tinted. | 24 * @param menu Menu with icons to be tinted. |
| 25 */ | 25 */ |
| 26 public static void tintMenuIcons(Context context, Menu menu) { | 26 public static void tintMenuIcons(Context context, Menu menu) { |
| 27 TypedValue typedValue = new TypedValue(); | 27 int color = getColorAttribute(context, R.attr.colorControlNormal); |
| 28 if (!context.getTheme().resolveAttribute(R.attr.colorControlNormal, type
dValue, true)) { | |
| 29 Log.e(TAG, "Failed to resolve colorControlNormal attribute."); | |
| 30 return; | |
| 31 } | |
| 32 | |
| 33 int color; | |
| 34 if (typedValue.resourceId != 0) { | |
| 35 // Attribute is a resource. | |
| 36 color = ApiCompatibilityUtils.getColor(context.getResources(), typed
Value.resourceId); | |
| 37 } else if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT | |
| 38 && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) { | |
| 39 // Attribute is a raw color value. | |
| 40 color = typedValue.data; | |
| 41 } else { | |
| 42 // The resource compiler should prevent this from happening. | |
| 43 Log.e(TAG, "Invalid colorControlNormal attribute: %s", typedValue); | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 int items = menu.size(); | 28 int items = menu.size(); |
| 48 for (int i = 0; i < items; i++) { | 29 for (int i = 0; i < items; i++) { |
| 49 Drawable icon = menu.getItem(i).getIcon(); | 30 Drawable icon = menu.getItem(i).getIcon(); |
| 50 if (icon != null) { | 31 if (icon != null) { |
| 51 icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); | 32 icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); |
| 52 } | 33 } |
| 53 } | 34 } |
| 54 } | 35 } |
| 36 |
| 37 /** |
| 38 * Returns a color from a theme attribute. |
| 39 * @param context Context with resources to look up. |
| 40 * @param attribute Attribute such as R.attr.colorControlNormal. |
| 41 * @return Color value. |
| 42 * @throws Resources.NotFoundException |
| 43 */ |
| 44 public static int getColorAttribute(Context context, int attribute) { |
| 45 TypedValue typedValue = new TypedValue(); |
| 46 if (!context.getTheme().resolveAttribute(attribute, typedValue, true)) { |
| 47 throw new Resources.NotFoundException("Attribute not found."); |
| 48 } |
| 49 |
| 50 if (typedValue.resourceId != 0) { |
| 51 // Attribute is a resource. |
| 52 return ApiCompatibilityUtils.getColor(context.getResources(), typedV
alue.resourceId); |
| 53 } else if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT |
| 54 && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) { |
| 55 // Attribute is a raw color value. |
| 56 return typedValue.data; |
| 57 } else { |
| 58 throw new Resources.NotFoundException("Attribute not a color."); |
| 59 } |
| 60 } |
| 55 } | 61 } |
| OLD | NEW |