• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 android.databinding.tool;
18 
19 import android.databinding.tool.reflection.ModelClass;
20 import android.databinding.tool.reflection.ModelMethod;
21 import android.databinding.tool.util.L;
22 import android.databinding.tool.util.Preconditions;
23 
24 /**
25  * As data-binding finds lambda expressions, it creates classes that can wrap those callbacks
26  * into methods that can be called into the ViewDataBinding classes.
27  * <p>
28  * The model keeps track of these wrappers and at the end data-binding generates all of them.
29  * These are stripped from library projects and re-generated.
30  */
31 public class CallbackWrapper {
32     public static String SOURCE_ID = "sourceId";
33     public static String ARG_PREFIX = "callbackArg_";
34     public final ModelClass klass;
35     public final ModelMethod method;
36     public final String key;
37     private static final String PACKAGE = "android.databinding.generated.callback";
38     private static final String LISTENER_NAME = "Listener";
39     private String mClassName;
40     private String mListenerMethodName;
41     private boolean mInitialized;
42 
CallbackWrapper(ModelClass klass, ModelMethod method)43     public CallbackWrapper(ModelClass klass, ModelMethod method) {
44         this.klass = klass;
45         this.method = method;
46         this.key = uniqueKey(klass, method);
47     }
48 
prepare(String className, String listenerMethodName)49     public void prepare(String className, String listenerMethodName) {
50         if (mInitialized) {
51             L.e("trying to initialize listener wrapper twice.");
52         }
53         mInitialized = true;
54         mClassName = className;
55         mListenerMethodName = listenerMethodName;
56     }
57 
getPackage()58     public String getPackage() {
59         return PACKAGE;
60     }
61 
getClassName()62     public String getClassName() {
63         Preconditions.check(mInitialized, "Listener wrapper is not initialized yet.");
64         return mClassName;
65     }
66 
getListenerInterfaceName()67     public String getListenerInterfaceName() {
68         return LISTENER_NAME;
69     }
70 
getListenerMethodName()71     public String getListenerMethodName() {
72         Preconditions.check(mInitialized, "Listener wrapper is not initialized yet.");
73         return mListenerMethodName;
74     }
75 
uniqueKey(ModelClass klass, ModelMethod method)76     public static String uniqueKey(ModelClass klass, ModelMethod method) {
77         String base = klass.getCanonicalName() + "#" + method.getName();
78         for (ModelClass param : method.getParameterTypes()) {
79             base += param + ",";
80         }
81         return base;
82     }
83 
getCannonicalName()84     public String getCannonicalName() {
85         return getPackage() + "." + getClassName();
86     }
87 
getCannonicalListenerName()88     public String getCannonicalListenerName() {
89         return getPackage() + "." + getClassName() + "." + getListenerInterfaceName();
90     }
91 
constructForIdentifier(int listenerId)92     public String constructForIdentifier(int listenerId) {
93         return "new " + getCannonicalName() + "(this, " + listenerId + ")";
94     }
95 
getMinApi()96     public int getMinApi() {
97         return Math.min(method.getMinApi(), klass.getMinApi());
98     }
99 }
100