• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.tools.layoutlib.create;
18 
19 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
20 import com.android.tools.layoutlib.java.LinkedHashMap_Delegate;
21 import com.android.tools.layoutlib.java.System_Delegate;
22 
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 
29 /**
30  * Describes the work to be done by {@link AsmGenerator}.
31  */
32 public final class CreateInfo implements ICreateInfo {
33 
34     @Override
getInjectedClasses()35     public Class<?>[] getInjectedClasses() {
36         return INJECTED_CLASSES;
37     }
38 
39     @Override
getDelegateMethods()40     public String[] getDelegateMethods() {
41         return DELEGATE_METHODS;
42     }
43 
44     @Override
getDelegateClassNatives()45     public String[] getDelegateClassNatives() {
46         return DELEGATE_CLASS_NATIVES;
47     }
48 
49     @Override
getRenamedClasses()50     public String[] getRenamedClasses() {
51         return RENAMED_CLASSES;
52     }
53 
54     @Override
getDeleteReturns()55     public String[] getDeleteReturns() {
56         return DELETE_RETURNS;
57     }
58 
59     @Override
getJavaPkgClasses()60     public String[] getJavaPkgClasses() {
61       return JAVA_PKG_CLASSES;
62     }
63 
64     @Override
getRefactoredClasses()65     public String[] getRefactoredClasses() {
66         return REFACTOR_CLASSES;
67     }
68 
69     @Override
getExcludedClasses()70     public String[] getExcludedClasses() {
71         String[] refactoredClasses = getJavaPkgClasses();
72         int count = refactoredClasses.length / 2 + EXCLUDED_CLASSES.length;
73         Set<String> excludedClasses = new HashSet<>(count);
74         for (int i = 0; i < refactoredClasses.length; i+=2) {
75             excludedClasses.add(refactoredClasses[i]);
76         }
77         excludedClasses.addAll(Arrays.asList(EXCLUDED_CLASSES));
78         return excludedClasses.toArray(new String[0]);
79     }
80 
81     @Override
getPromotedFields()82     public String[] getPromotedFields() {
83         return PROMOTED_FIELDS;
84     }
85 
86     @Override
getPromotedClasses()87     public String[] getPromotedClasses() {
88         return PROMOTED_CLASSES;
89     }
90 
91     @Override
getInjectedMethodsMap()92     public Map<String, InjectMethodRunnable> getInjectedMethodsMap() {
93         return INJECTED_METHODS;
94     }
95 
96     //-----
97 
98     /**
99      * The list of class from layoutlib_create to inject in layoutlib.
100      */
101     private final static Class<?>[] INJECTED_CLASSES = new Class<?>[] {
102             OverrideMethod.class,
103             MethodListener.class,
104             MethodAdapter.class,
105             ICreateInfo.class,
106             CreateInfo.class,
107             LayoutlibDelegate.class,
108             InjectMethodRunnable.class,
109             InjectMethodRunnables.class,
110             /* Java package classes */
111             System_Delegate.class,
112             LinkedHashMap_Delegate.class,
113         };
114 
115     /**
116      * The list of methods to rewrite as delegates.
117      */
118     public final static String[] DELEGATE_METHODS = new String[] {
119         "android.app.Fragment#instantiate", //(Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)Landroid/app/Fragment;",
120         "android.content.res.Resources#getAnimation",
121         "android.content.res.Resources#getAttributeSetSourceResId",
122         "android.content.res.Resources#getBoolean",
123         "android.content.res.Resources#getColor",
124         "android.content.res.Resources#getColorStateList",
125         "android.content.res.Resources#getDimension",
126         "android.content.res.Resources#getDimensionPixelOffset",
127         "android.content.res.Resources#getDimensionPixelSize",
128         "android.content.res.Resources#getDrawable",
129         "android.content.res.Resources#getFloat",
130         "android.content.res.Resources#getFont",
131         "android.content.res.Resources#getIdentifier",
132         "android.content.res.Resources#getIntArray",
133         "android.content.res.Resources#getInteger",
134         "android.content.res.Resources#getLayout",
135         "android.content.res.Resources#getQuantityString",
136         "android.content.res.Resources#getQuantityText",
137         "android.content.res.Resources#getResourceEntryName",
138         "android.content.res.Resources#getResourceName",
139         "android.content.res.Resources#getResourcePackageName",
140         "android.content.res.Resources#getResourceTypeName",
141         "android.content.res.Resources#getString",
142         "android.content.res.Resources#getStringArray",
143         "android.content.res.Resources#getText",
144         "android.content.res.Resources#getTextArray",
145         "android.content.res.Resources#getValue",
146         "android.content.res.Resources#getValueForDensity",
147         "android.content.res.Resources#getXml",
148         "android.content.res.Resources#loadXmlResourceParser",
149         "android.content.res.Resources#obtainAttributes",
150         "android.content.res.Resources#obtainTypedArray",
151         "android.content.res.Resources#openRawResource",
152         "android.content.res.Resources#openRawResourceFd",
153         "android.content.res.Resources$Theme#obtainStyledAttributes",
154         "android.content.res.Resources$Theme#resolveAttribute",
155         "android.content.res.Resources$Theme#resolveAttributes",
156         "android.content.res.AssetManager#nativeCreate",
157         "android.content.res.AssetManager#nativeDestroy",
158         "android.content.res.AssetManager#nativeThemeCreate",
159         "android.content.res.AssetManager#nativeThemeDestroy",
160         "android.content.res.AssetManager#getAssignedPackageIdentifiers",
161         "android.content.res.AssetManager#nativeCreateIdmapsForStaticOverlaysTargetingAndroid",
162         "android.content.res.TypedArray#getValueAt",
163         "android.content.res.TypedArray#obtain",
164         "android.graphics.BitmapFactory#finishDecode",
165         "android.graphics.BitmapFactory#setDensityFromOptions",
166         "android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT#useLastSeenTarget",
167         "android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT#onDraw",
168         "android.graphics.drawable.GradientDrawable#buildRing",
169         "android.graphics.drawable.AdaptiveIconDrawable#<init>",
170         "android.graphics.drawable.DrawableInflater#inflateFromClass",
171         "android.graphics.drawable.NinePatchDrawable#getOpacity",
172         "android.graphics.FontFamily#addFont",
173         "com.google.android.apps.common.testing.accessibility.framework.uielement" +
174                 ".AccessibilityHierarchyAndroid$ViewElementClassNamesAndroid#getClassByName",
175         "android.graphics.Typeface#create",
176         "android.graphics.Typeface$Builder#createAssetUid",
177         "android.graphics.fonts.Font$Builder#createBuffer",
178         "android.graphics.fonts.SystemFonts#getSystemFontConfigInternal",
179         "android.os.Binder#getNativeBBinderHolder",
180         "android.os.Binder#getNativeFinalizer",
181         "android.os.Handler#sendMessageAtTime",
182         "android.os.HandlerThread#run",
183         "android.preference.Preference#getView",
184         "android.text.format.DateFormat#is24HourFormat",
185         "android.util.Xml#newPullParser",
186         "android.view.Choreographer#getInstance",
187         "android.view.Choreographer#getRefreshRate",
188         "android.view.Choreographer#scheduleVsyncLocked",
189         "android.view.Display#updateDisplayInfoLocked",
190         "android.view.Display#getWindowManager",
191         "android.view.HandlerActionQueue#postDelayed",
192         "android.view.LayoutInflater#initPrecompiledViews",
193         "android.view.LayoutInflater#rInflate",
194         "android.view.LayoutInflater#parseInclude",
195         "android.view.View#draw",
196         "android.view.View#dispatchDetachedFromWindow",
197         "android.view.View#layout",
198         "android.view.View#measure",
199         "android.view.View#getWindowToken",
200         "android.view.View#isInEditMode",
201         "android.view.ViewRootImpl#isInTouchMode",
202         "android.view.WindowManagerGlobal#getWindowManagerService",
203         "android.view.inputmethod.InputMethodManager#isInEditMode",
204         "android.view.MenuInflater#registerMenu",
205         "android.graphics.RenderNode#getMatrix",
206         "android.graphics.RenderNode#nCreate",
207         "android.graphics.RenderNode#nGetNativeFinalizer",
208         "android.graphics.RenderNode#nSetElevation",
209         "android.graphics.RenderNode#nGetElevation",
210         "android.graphics.RenderNode#nSetTranslationX",
211         "android.graphics.RenderNode#nGetTranslationX",
212         "android.graphics.RenderNode#nSetTranslationY",
213         "android.graphics.RenderNode#nGetTranslationY",
214         "android.graphics.RenderNode#nSetTranslationZ",
215         "android.graphics.RenderNode#nGetTranslationZ",
216         "android.graphics.RenderNode#nSetRotation",
217         "android.graphics.RenderNode#nGetRotation",
218         "android.graphics.RenderNode#nSetLeft",
219         "android.graphics.RenderNode#nSetTop",
220         "android.graphics.RenderNode#nSetRight",
221         "android.graphics.RenderNode#nSetBottom",
222         "android.graphics.RenderNode#nSetLeftTopRightBottom",
223         "android.graphics.RenderNode#nSetPivotX",
224         "android.graphics.RenderNode#nGetPivotX",
225         "android.graphics.RenderNode#nSetPivotY",
226         "android.graphics.RenderNode#nGetPivotY",
227         "android.graphics.RenderNode#nSetScaleX",
228         "android.graphics.RenderNode#nGetScaleX",
229         "android.graphics.RenderNode#nSetScaleY",
230         "android.graphics.RenderNode#nGetScaleY",
231         "android.graphics.RenderNode#nIsPivotExplicitlySet",
232         "android.provider.DeviceConfig#getBoolean",
233         "android.provider.DeviceConfig#getFloat",
234         "android.provider.DeviceConfig#getInt",
235         "android.provider.DeviceConfig#getLong",
236         "android.provider.DeviceConfig#getString",
237         "android.view.PointerIcon#loadResource",
238         "android.view.PointerIcon#registerDisplayListener",
239         "android.view.SurfaceControl#nativeCreateTransaction",
240         "android.view.SurfaceControl#nativeGetNativeTransactionFinalizer",
241         "android.view.ViewGroup#drawChild",
242         "com.android.internal.view.menu.MenuBuilder#createNewMenuItem",
243         "com.android.internal.util.XmlUtils#convertValueToInt",
244         "dalvik.system.VMRuntime#getNotifyNativeInterval",
245         "dalvik.system.VMRuntime#newUnpaddedArray",
246         "libcore.io.MemoryMappedFile#mmapRO",
247         "libcore.io.MemoryMappedFile#close",
248         "libcore.io.MemoryMappedFile#bigEndianIterator",
249         "libcore.util.NativeAllocationRegistry#applyFreeFunction",
250         "libcore.util.NativeAllocationRegistry#registerNativeAllocation",
251     };
252 
253     /**
254      * The list of classes on which to delegate all native methods.
255      */
256     public final static String[] DELEGATE_CLASS_NATIVES = new String[] {
257         "android.animation.PropertyValuesHolder",
258         "android.graphics.BaseCanvas",
259         "android.graphics.Bitmap",
260         "android.graphics.BitmapFactory",
261         "android.graphics.BitmapShader",
262         "android.graphics.BlendModeColorFilter",
263         "android.graphics.BlurMaskFilter",
264         "android.graphics.Canvas",
265         "android.graphics.Color",
266         "android.graphics.ColorFilter",
267         "android.graphics.ColorMatrixColorFilter",
268         "android.graphics.ColorSpace$Rgb",
269         "android.graphics.ComposePathEffect",
270         "android.graphics.ComposeShader",
271         "android.graphics.CornerPathEffect",
272         "android.graphics.DashPathEffect",
273         "android.graphics.DiscretePathEffect",
274         "android.graphics.DrawFilter",
275         "android.graphics.EmbossMaskFilter",
276         "android.graphics.FontFamily",
277         "android.graphics.LightingColorFilter",
278         "android.graphics.LinearGradient",
279         "android.graphics.MaskFilter",
280         "android.graphics.Matrix",
281         "android.graphics.NinePatch",
282         "android.graphics.Paint",
283         "android.graphics.PaintFlagsDrawFilter",
284         "android.graphics.Path",
285         "android.graphics.PathDashPathEffect",
286         "android.graphics.PathEffect",
287         "android.graphics.PathMeasure",
288         "android.graphics.PorterDuffColorFilter",
289         "android.graphics.RadialGradient",
290         "android.graphics.Region",
291         "android.graphics.Shader",
292         "android.graphics.SumPathEffect",
293         "android.graphics.SweepGradient",
294         "android.graphics.Typeface",
295         "android.graphics.animation.NativeInterpolatorFactory",
296         "android.graphics.drawable.AnimatedVectorDrawable",
297         "android.graphics.drawable.VectorDrawable",
298         "android.graphics.fonts.Font$Builder",
299         "android.graphics.fonts.FontFamily$Builder",
300         "android.graphics.text.MeasuredText",
301         "android.graphics.text.MeasuredText$Builder",
302         "android.graphics.text.LineBreaker",
303         "android.os.SystemClock",
304         "android.os.SystemProperties",
305         "android.util.PathParser",
306         "android.view.Display",
307         "com.android.internal.util.VirtualRefBasePtr",
308         "libcore.icu.ICU",
309     };
310 
311     /**
312      *  The list of classes to rename, must be an even list: the binary FQCN
313      *  of class to replace followed by the new FQCN.
314      */
315     private final static String[] RENAMED_CLASSES =
316         new String[] {
317             "android.os.ServiceManager",                       "android.os._Original_ServiceManager",
318             "android.view.textservice.TextServicesManager",    "android.view.textservice._Original_TextServicesManager",
319             "android.view.SurfaceView",                        "android.view._Original_SurfaceView",
320             "android.view.WindowManagerImpl",                  "android.view._Original_WindowManagerImpl",
321             "android.view.accessibility.AccessibilityManager", "android.view.accessibility._Original_AccessibilityManager",
322             "android.view.accessibility.AccessibilityNodeIdManager", "android.view.accessibility._Original_AccessibilityNodeIdManager",
323             "android.webkit.WebView",                          "android.webkit._Original_WebView",
324             "android.graphics.ImageDecoder",                   "android.graphics._Original_ImageDecoder",
325         };
326 
327     /**
328      * The list of class references to update, must be an even list: the binary
329      * FQCN of class to replace followed by the new FQCN. The classes to
330      * replace are to be excluded from the output.
331      */
332     private final static String[] JAVA_PKG_CLASSES =
333         new String[] {
334                 "sun.misc.Cleaner",                                "com.android.layoutlib.bridge.libcore.util.Cleaner",
335         };
336 
337     /**
338      * List of classes to refactor. This is similar to combining {@link #getRenamedClasses()} and
339      * {@link #getJavaPkgClasses()}.
340      * Classes included here will be renamed and then all their references in any other classes
341      * will be also modified.
342      * FQCN of class to refactor followed by its new FQCN.
343      */
344     private final static String[] REFACTOR_CLASSES =
345             new String[] {
346                     "android.os.Build",                                "android.os._Original_Build",
347             };
348 
349     private final static String[] EXCLUDED_CLASSES =
350         new String[] {
351             "android.preference.PreferenceActivity",
352             "java.**",
353             "org.kxml2.io.KXmlParser",
354             "org.xmlpull.**",
355             "sun.**",
356         };
357 
358     /**
359      * List of fields for which we will update the visibility to be public. This is sometimes
360      * needed when access from the delegate classes is needed.
361      */
362     private final static String[] PROMOTED_FIELDS = new String[] {
363         "android.graphics.drawable.VectorDrawable#mVectorState",
364         "android.view.Choreographer#mLastFrameTimeNanos",
365         "android.graphics.FontFamily#mBuilderPtr",
366         "android.graphics.Typeface#DEFAULT_FAMILY",
367         "android.graphics.Typeface#sDynamicTypefaceCache",
368         "android.graphics.drawable.AdaptiveIconDrawable#sMask",
369         "android.graphics.drawable.DrawableInflater#mRes",
370         "android.animation.PropertyValuesHolder#sSetterPropertyMap",
371         "android.animation.PropertyValuesHolder#sGetterPropertyMap",
372         "android.animation.PropertyValuesHolder$IntPropertyValuesHolder#sJNISetterPropertyMap",
373         "android.animation.PropertyValuesHolder$FloatPropertyValuesHolder#sJNISetterPropertyMap",
374         "android.animation.PropertyValuesHolder$MultiFloatValuesHolder#sJNISetterPropertyMap",
375         "android.animation.PropertyValuesHolder$MultiIntValuesHolder#sJNISetterPropertyMap",
376     };
377 
378     /**
379      * List of classes to be promoted to public visibility. Prefer using PROMOTED_FIELDS to this
380      * if possible.
381      */
382     private final static String[] PROMOTED_CLASSES = new String[] {
383     };
384 
385     /**
386      * List of classes for which the methods returning them should be deleted.
387      * The array contains a list of null terminated section starting with the name of the class
388      * to rename in which the methods are deleted, followed by a list of return types identifying
389      * the methods to delete.
390      */
391     private final static String[] DELETE_RETURNS =
392         new String[] {
393             null };                         // separator, for next class/methods list.
394 
395     private final static Map<String, InjectMethodRunnable> INJECTED_METHODS =
396             new HashMap<String, InjectMethodRunnable>(1) {{
397                 put("android.content.Context",
398                         InjectMethodRunnables.CONTEXT_GET_FRAMEWORK_CLASS_LOADER);
399             }};
400 }
401