1 /* 2 * Copyright (C) 2020 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.idea.validator; 18 19 import com.android.tools.idea.validator.ValidatorData.Level; 20 import com.android.tools.idea.validator.ValidatorData.Policy; 21 import com.android.tools.idea.validator.ValidatorData.Type; 22 import com.android.tools.idea.validator.accessibility.AccessibilityValidator; 23 import com.android.tools.layoutlib.annotations.NotNull; 24 import com.android.tools.layoutlib.annotations.Nullable; 25 26 import android.view.View; 27 28 import java.awt.image.BufferedImage; 29 import java.util.EnumSet; 30 31 /** 32 * Main class for validating layout. 33 */ 34 public class LayoutValidator { 35 36 public static final ValidatorData.Policy DEFAULT_POLICY = new Policy( 37 EnumSet.of(Type.ACCESSIBILITY, Type.RENDER), 38 EnumSet.of(Level.ERROR, Level.WARNING)); 39 40 private static ValidatorData.Policy sPolicy = DEFAULT_POLICY; 41 42 /** 43 * Validate the layout using the default policy. 44 * Precondition: View must be attached to the window. 45 * 46 * @return The validation results. If no issue is found it'll return empty result. 47 */ 48 @NotNull validate(@otNull View view, @Nullable BufferedImage image)49 public static ValidatorResult validate(@NotNull View view, @Nullable BufferedImage image) { 50 if (view.isAttachedToWindow()) { 51 return AccessibilityValidator.validateAccessibility(view, image, sPolicy); 52 } 53 // TODO: Add non-a11y layout validation later. 54 return new ValidatorResult.Builder().build(); 55 } 56 57 /** 58 * Update the policy with which to run the validation call. 59 * @param policy new policy. 60 */ updatePolicy(@otNull ValidatorData.Policy policy)61 public static void updatePolicy(@NotNull ValidatorData.Policy policy) { 62 sPolicy = policy; 63 } 64 } 65