• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.layoutlib.bridge.resources;
18 
19 import com.android.layoutlib.bridge.bars.Config;
20 import com.android.resources.Density;
21 import com.android.resources.LayoutDirection;
22 
23 import java.io.InputStream;
24 
25 public class IconLoader {
26 
27     private final String mIconName;
28     private final Density mDesiredDensity;
29     private final int mPlatformVersion;
30     private final LayoutDirection mDirection;
31 
32     private Density mCurrentDensity;
33     private StringBuilder mCurrentPath;
34 
IconLoader(String iconName, Density density, int platformVersion, LayoutDirection direction)35     public IconLoader(String iconName, Density density, int platformVersion, LayoutDirection
36             direction) {
37         mIconName = iconName;
38         mDesiredDensity = density;
39         mPlatformVersion = platformVersion;
40         mDirection = direction;
41         // An upper bound on the length of the path for the icon: /bars/v21/ldrtl-xxxhdpi/
42         final int iconPathLength = 24;
43         mCurrentPath = new StringBuilder(iconPathLength + iconName.length());
44     }
45 
getIcon()46     public InputStream getIcon() {
47         for (String resourceDir : Config.getResourceDirs(mPlatformVersion)) {
48             mCurrentDensity = null;
49             InputStream stream = getIcon(resourceDir);
50             if (stream != null) {
51                 return stream;
52             }
53         }
54         return null;
55     }
56 
57     /**
58      * Should only be called after {@link #getIcon()}. Returns the density of the icon, if found by
59      * {@code getIcon()}. If no icon was found, then the return value has no meaning.
60      */
getDensity()61     public Density getDensity() {
62         return mCurrentDensity;
63     }
64 
65     /**
66      * Should only be called after {@link #getIcon()}. Returns the path to the icon, if found by
67      * {@code getIcon()}. If no icon was found, then the return value has no meaning.
68      */
getPath()69     public String getPath() {
70         return mCurrentPath.toString();
71     }
72 
73     /**
74      * Search for icon in the resource directory. This iterates over all densities.
75      * If a match is found, mCurrentDensity will be set to the icon's density.
76      */
getIcon(String resourceDir)77     private InputStream getIcon(String resourceDir) {
78         // First check for the desired density.
79         InputStream stream = getIcon(resourceDir, mDesiredDensity);
80         if (stream != null) {
81             mCurrentDensity = mDesiredDensity;
82             return stream;
83         }
84         // Didn't find in the desired density. Search in all.
85         for (Density density : Density.values()) {
86             if (density == mDesiredDensity) {
87                 // Skip the desired density since it's already been checked.
88                 continue;
89             }
90             stream = getIcon(resourceDir, density);
91             if (stream != null) {
92                 mCurrentDensity = density;
93                 return stream;
94             }
95         }
96         return null;
97     }
98 
99     /**
100      * Returns the icon for given density present in the given resource directory, taking layout
101      * direction into consideration.
102      */
getIcon(String resourceDir, Density density)103     private InputStream getIcon(String resourceDir, Density density) {
104         mCurrentPath.setLength(0);
105         // Currently we don't have any LTR only resources and hence the check is skipped. If they
106         // are ever added, change to:
107         // if (mDirection == LayoutDirection.RTL || mDirection == LayoutDirection.LTR) {
108         if (mDirection == LayoutDirection.RTL) {
109             mCurrentPath.append(resourceDir)
110                     .append(mDirection.getResourceValue())
111                     .append('-')
112                     .append(density.getResourceValue())
113                     .append('/')
114                     .append(mIconName);
115             InputStream stream = getClass().getResourceAsStream(mCurrentPath.toString());
116             if (stream != null) {
117                 return stream;
118             }
119             mCurrentPath.setLength(0);
120         }
121         mCurrentPath.append(resourceDir)
122                 .append(density.getResourceValue())
123                 .append('/')
124                 .append(mIconName);
125         return getClass().getResourceAsStream(mCurrentPath.toString());
126     }
127 }
128