Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9c62b6f authored by George Mount's avatar George Mount
Browse files

Don't require IDs for most bound views.

parent 10dd0cb6
Loading
Loading
Loading
Loading
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.databinding.testapp;

import com.android.databinding.testapp.generated.NoIdTestBinder;

import android.test.UiThreadTest;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NoIdTest extends BaseDataBinderTest<NoIdTestBinder> {
    public NoIdTest() {
        super(NoIdTestBinder.class, R.layout.no_id_test);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBinder.setName("hello");
                    mBinder.setOrientation(LinearLayout.VERTICAL);
                    mBinder.rebindDirty();
                }
            });
        } catch (Throwable throwable) {
            throw new Exception(throwable);
        }
    }

    @UiThreadTest
    public void testOnRoot() {
        LinearLayout linearLayout = (LinearLayout) mBinder.getRoot();
        assertEquals(LinearLayout.VERTICAL, linearLayout.getOrientation());
        mBinder.setOrientation(LinearLayout.HORIZONTAL);
        mBinder.rebindDirty();
        assertEquals(LinearLayout.HORIZONTAL, linearLayout.getOrientation());
    }

    @UiThreadTest
    public void testNormal() {
        LinearLayout linearLayout = (LinearLayout) mBinder.getRoot();
        TextView view = (TextView) linearLayout.getChildAt(0);
        assertEquals("hello world", view.getTag());
        assertEquals("hello", view.getText().toString());
        mBinder.setName("world");
        mBinder.rebindDirty();
        assertEquals("world", view.getText().toString());
    }

    @UiThreadTest
    public void testNoTag() {
        LinearLayout linearLayout = (LinearLayout) mBinder.getRoot();
        TextView view = (TextView) linearLayout.getChildAt(1);
        assertNull(view.getTag());
    }

    @UiThreadTest
    public void testResourceTag() {
        LinearLayout linearLayout = (LinearLayout) mBinder.getRoot();
        TextView view = (TextView) linearLayout.getChildAt(2);
        String expectedValue = view.getResources().getString(R.string.app_name);
        assertEquals(expectedValue, view.getTag());
    }

    @UiThreadTest
    public void testAndroidResourceTag() {
        LinearLayout linearLayout = (LinearLayout) mBinder.getRoot();
        TextView view = (TextView) linearLayout.getChildAt(3);
        String expectedValue = view.getResources().getString(android.R.string.ok);
        assertEquals(expectedValue, view.getTag());
    }
}
+17 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="@{orientation}"
        >
    <variable name="name" type="String"/>
    <variable name="orientation" type="int"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{name}" android:tag="hello world"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:text="@{name}"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:text="@{name}" android:tag="@string/app_name"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:text="@{name}" android:tag="@android:string/ok"/>
</LinearLayout>
 No newline at end of file
+13 −5
Original line number Diff line number Diff line
@@ -39,12 +39,15 @@ public class ProcessExpressions extends AbstractProcessor {
        ResourceBundle resourceBundle = null;

        for (Element element : roundEnv.getElementsAnnotatedWith(BindingAppInfo.class)) {
            final BindingAppInfo appInfo = element.getAnnotation(BindingAppInfo.class);
            if (appInfo == null) {
                continue; // It gets confused between BindingAppInfo and BinderBundle
            }
            if (element.getKind() != ElementKind.CLASS) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                        "BindingAppInfo associated with wrong type. Should be a class.", element);
                continue;
            }
            BindingAppInfo appInfo = element.getAnnotation(BindingAppInfo.class);
            if (resourceBundle == null) {
                resourceBundle = new ResourceBundle(appInfo.applicationPackage());
                processLayouts(resourceBundle, roundEnv);
@@ -60,6 +63,10 @@ public class ProcessExpressions extends AbstractProcessor {
    private void processLayouts(ResourceBundle resourceBundle, RoundEnvironment roundEnv) {
        Unmarshaller unmarshaller = null;
        for (Element element : roundEnv.getElementsAnnotatedWith(BinderBundle.class)) {
            final BinderBundle binderBundle = element.getAnnotation(BinderBundle.class);
            if (binderBundle == null) {
                continue;
            }
            if (element.getKind() != ElementKind.CLASS) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                        "BinderBundle associated with wrong type. Should be a class.", element);
@@ -72,7 +79,6 @@ public class ProcessExpressions extends AbstractProcessor {
                            JAXBContext.newInstance(ResourceBundle.LayoutFileBundle.class);
                    unmarshaller = context.createUnmarshaller();
                }
                BinderBundle binderBundle = element.getAnnotation(BinderBundle.class);
                String binderBundle64 = binderBundle.value();
                byte[] buf = Base64.decodeBase64(binderBundle64);
                in = new ByteArrayInputStream(buf);
@@ -98,8 +104,10 @@ public class ProcessExpressions extends AbstractProcessor {

        CompilerChef compilerChef = CompilerChef.createChef(resourceBundle,
                new AnnotationJavaFileWriter(processingEnv));
        if (compilerChef.hasAnythingToGenerate()) {
            compilerChef.writeDbrFile();
            compilerChef.writeViewBinderInterfaces();
            compilerChef.writeViewBinders();
        }
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ import android.binding.BindingAdapter;
import android.binding.BindingConversion;
import android.binding.BindingMethod;
import android.binding.BindingMethods;
import android.binding.Untaggable;

import com.android.databinding.store.SetterStore;

import java.io.IOException;
@@ -42,6 +44,7 @@ import javax.tools.Diagnostic;

@SupportedAnnotationTypes({
        "android.binding.BindingAdapter",
        "android.binding.Untaggable",
        "android.binding.BindingMethods",
        "android.binding.BindingConversion"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@@ -63,6 +66,8 @@ public class ProcessMethodAdapters extends AbstractProcessor {
        addBindingAdapters(roundEnv, store);
        addRenamed(roundEnv, store);
        addConversions(roundEnv, store);
        addUntaggable(roundEnv, store);

        try {
            store.write(processingEnv);
        } catch (IOException e) {
@@ -140,6 +145,13 @@ public class ProcessMethodAdapters extends AbstractProcessor {
        }
    }

    private void addUntaggable(RoundEnvironment roundEnv, SetterStore store) {
        for (Element element : roundEnv.getElementsAnnotatedWith(Untaggable.class)) {
            Untaggable untaggable = element.getAnnotation(Untaggable.class);
            store.addUntaggableTypes(untaggable.value(), (TypeElement) element);
        }
    }

    private void clearIncrementalClasses(RoundEnvironment roundEnv, SetterStore store) {
        HashSet<String> classes = new HashSet<>();

@@ -153,6 +165,9 @@ public class ProcessMethodAdapters extends AbstractProcessor {
        for (Element element : roundEnv.getElementsAnnotatedWith(BindingConversion.class)) {
            classes.add(((TypeElement) element.getEnclosingElement()).getQualifiedName().toString());
        }
        for (Element element : roundEnv.getElementsAnnotatedWith(Untaggable.class)) {
            classes.add(((TypeElement) element).getQualifiedName().toString());
        }
        store.clear(classes);
    }
}
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.binding;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
public @interface Untaggable {
    String[] value();
}
Loading