• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tv.twopanelsettings;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.content.res.Resources;
22 import android.graphics.drawable.Drawable;
23 import android.graphics.drawable.LayerDrawable;
24 import android.util.Log;
25 import android.view.Gravity;
26 import androidx.core.content.ContextCompat;
27 
28 /** Create two panel settings style icon */
29 public class IconUtil {
30   private static final int INSET = 12;
31   private static final String TAG = "IconUtil";
32 
33   /** Add the border and return the compound icon. */
getCompoundIcon(Context context, Drawable icon)34   public static Drawable getCompoundIcon(Context context, Drawable icon) {
35     Drawable container = ContextCompat.getDrawable(context, R.drawable.compound_icon_background);
36 
37     Resources res = context.getResources();
38     try {
39       ColorStateList colorStateList =
40           ColorStateList.createFromXml(res, res.getXml(R.color.two_panel_preference_icon_color));
41       icon.setTintList(colorStateList);
42     } catch (Exception e) {
43       Log.e(TAG, "Cannot set tint", e);
44     }
45 
46     LayerDrawable compoundDrawable = new LayerDrawable(new Drawable[] {container, icon});
47     compoundDrawable.setLayerGravity(0, Gravity.CENTER);
48     compoundDrawable.setLayerGravity(1, Gravity.CENTER);
49     compoundDrawable.setLayerInset(1, INSET, INSET, INSET, INSET);
50     return compoundDrawable;
51   }
52 }
53