Chromium Code Reviews| 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 import org.chromium.base.Log; |
| 15 | 16 |
| 16 /** Utility methods for chromoting code. */ | 17 /** Utility methods for chromoting code. */ |
| 17 public abstract class ChromotingUtil { | 18 public abstract class ChromotingUtil { |
| 18 private static final String TAG = "Chromoting"; | 19 private static final String TAG = "Chromoting"; |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * Tints all icons of a toolbar menu so they have the same color as the 'bac k' navigation icon | 22 * 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. | 23 * and the three-dots overflow icon. |
| 23 * @param context Context for getting theme and resource information. | 24 * @param context Context for getting theme and resource information. |
| 24 * @param menu Menu with icons to be tinted. | 25 * @param menu Menu with icons to be tinted. |
| 25 */ | 26 */ |
| 26 public static void tintMenuIcons(Context context, Menu menu) { | 27 public static void tintMenuIcons(Context context, Menu menu) { |
| 27 TypedValue typedValue = new TypedValue(); | 28 int color; |
| 28 if (!context.getTheme().resolveAttribute(R.attr.colorControlNormal, type dValue, true)) { | 29 try { |
| 29 Log.e(TAG, "Failed to resolve colorControlNormal attribute."); | 30 color = getColorAttribute(context, R.attr.colorControlNormal); |
| 31 } catch (Resources.NotFoundException e) { | |
| 32 // The resource compiler should prevent this from happening. | |
|
Sergey Ulanov
2015/11/23 22:01:13
If this is unexpected then why do we need to handl
Lambros
2015/11/23 23:48:56
No need, it shouldn't happen if the code+resources
| |
| 33 Log.e(TAG, "colorControlNormal attribute lookup failed.", e); | |
| 30 return; | 34 return; |
| 31 } | 35 } |
| 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(); | 36 int items = menu.size(); |
| 48 for (int i = 0; i < items; i++) { | 37 for (int i = 0; i < items; i++) { |
| 49 Drawable icon = menu.getItem(i).getIcon(); | 38 Drawable icon = menu.getItem(i).getIcon(); |
| 50 if (icon != null) { | 39 if (icon != null) { |
| 51 icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); | 40 icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); |
| 52 } | 41 } |
| 53 } | 42 } |
| 54 } | 43 } |
| 44 | |
| 45 /** | |
| 46 * Returns a color from a theme attribute. | |
| 47 * @param context Context with resources to look up. | |
| 48 * @param attribute Attribute such as R.attr.colorControlNormal. | |
| 49 * @return Color value. | |
| 50 * @throws Resources.NotFoundException | |
| 51 */ | |
| 52 public static int getColorAttribute(Context context, int attribute) { | |
| 53 TypedValue typedValue = new TypedValue(); | |
| 54 if (!context.getTheme().resolveAttribute(attribute, typedValue, true)) { | |
| 55 throw new Resources.NotFoundException("Attribute not found."); | |
| 56 } | |
| 57 | |
| 58 if (typedValue.resourceId != 0) { | |
| 59 // Attribute is a resource. | |
| 60 return ApiCompatibilityUtils.getColor(context.getResources(), typedV alue.resourceId); | |
| 61 } else if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT | |
| 62 && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) { | |
| 63 // Attribute is a raw color value. | |
| 64 return typedValue.data; | |
| 65 } else { | |
| 66 throw new Resources.NotFoundException("Attribute not a color."); | |
| 67 } | |
| 68 } | |
| 55 } | 69 } |
| OLD | NEW |