1 /* 2 * Copyright (C) 2021 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.onboarding.bedsteadonboarding.queryable 18 19 import android.os.Parcel 20 import android.os.Parcelable 21 import com.android.onboarding.nodes.OnboardingGraphNode 22 import com.android.queryable.Queryable 23 import com.android.queryable.util.ParcelableUtils 24 25 /** 26 * Implementation of [NodeSequenceQuery]. 27 */ 28 class NodeSequenceQueryHelper<E : Queryable> : NodeSequenceQuery<E> { 29 30 @Transient private lateinit var query: E 31 32 private var targetValue: Boolean? = null 33 34 var nodeToCompareWith: OnboardingGraphNode? = null 35 36 constructor(query: E) { 37 this.query = query 38 } 39 40 constructor(parcel: Parcel) { 41 targetValue = ParcelableUtils.readNullableBoolean(parcel) 42 nodeToCompareWith = parcel.readParcelable(NodeSequenceQueryHelper::class.java.classLoader)!! 43 } 44 isTruenull45 override fun isTrue(): E { 46 check(targetValue == null) { "Cannot set multiple node sequence filters" } 47 targetValue = true 48 return query 49 } 50 isFalsenull51 override fun isFalse(): E { 52 check(targetValue == null) { "Cannot set multiple node sequence filters" } 53 targetValue = false 54 return query 55 } 56 isEmptyQuerynull57 override fun isEmptyQuery(): Boolean { 58 return targetValue == null || nodeToCompareWith == null 59 } 60 matchesnull61 override fun matches(value: Boolean): Boolean { 62 return targetValue == null || targetValue == value 63 } 64 describeQuerynull65 override fun describeQuery(fieldName: String): String? { 66 return if (targetValue == null) { 67 null 68 } else "$fieldName=$targetValue" 69 } 70 describeContentsnull71 override fun describeContents(): Int { 72 return 0 73 } 74 writeToParcelnull75 override fun writeToParcel(out: Parcel, flags: Int) { 76 ParcelableUtils.writeNullableBoolean(out, targetValue) 77 } 78 79 companion object CREATOR : Parcelable.Creator<NodeSequenceQueryHelper<Queryable>> { createFromParcelnull80 override fun createFromParcel(parcel: Parcel): NodeSequenceQueryHelper<Queryable> { 81 return NodeSequenceQueryHelper(parcel) 82 } 83 newArraynull84 override fun newArray(size: Int): Array<NodeSequenceQueryHelper<Queryable>?> { 85 return arrayOfNulls(size) 86 } 87 } 88 } 89