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 
27 import androidx.core.content.ContextCompat;
28 
29 /**
30  * Create two panel settings style icon
31  */
32 public class IconUtil {
33     private static final int INSET = 12;
34     private static final String TAG = "IconUtil";
35 
36     /**
37      * Add the border and return the compound icon.
38      */
getCompoundIcon(Context context, Drawable icon)39     public static Drawable getCompoundIcon(Context context, Drawable icon) {
40         Drawable container =
41                 ContextCompat.getDrawable(context, R.drawable.compound_icon_background);
42 
43         Resources res = context.getResources();
44         try {
45             ColorStateList colorStateList = ColorStateList
46                     .createFromXml(res, res.getXml(R.color.two_panel_preference_icon_color));
47             icon.setTintList(colorStateList);
48         } catch (Exception e) {
49             Log.e(TAG, "Cannot set tint", e);
50 
51         }
52 
53         LayerDrawable compoundDrawable = new LayerDrawable(new Drawable[] {container, icon});
54         compoundDrawable.setLayerGravity(0, Gravity.CENTER);
55         compoundDrawable.setLayerGravity(1, Gravity.CENTER);
56         compoundDrawable.setLayerInset(1, INSET, INSET, INSET, INSET);
57         return compoundDrawable;
58     }
59 }
60