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.server.connectivity.mdns; 18 19 import android.annotation.Nullable; 20 21 import java.time.Duration; 22 import java.util.Objects; 23 24 /** 25 * API configuration parameters for advertising the mDNS service. 26 * 27 * <p>Use {@link MdnsAdvertisingOptions.Builder} to create {@link MdnsAdvertisingOptions}. 28 * 29 * @hide 30 */ 31 public class MdnsAdvertisingOptions { 32 33 private static MdnsAdvertisingOptions sDefaultOptions; 34 private final boolean mIsOnlyUpdate; 35 @Nullable 36 private final Duration mTtl; 37 38 /** 39 * Parcelable constructs for a {@link MdnsAdvertisingOptions}. 40 */ MdnsAdvertisingOptions(boolean isOnlyUpdate, @Nullable Duration ttl)41 MdnsAdvertisingOptions(boolean isOnlyUpdate, @Nullable Duration ttl) { 42 this.mIsOnlyUpdate = isOnlyUpdate; 43 this.mTtl = ttl; 44 } 45 46 /** 47 * Returns a {@link Builder} for {@link MdnsAdvertisingOptions}. 48 */ newBuilder()49 public static Builder newBuilder() { 50 return new Builder(); 51 } 52 53 /** 54 * Returns a default search options. 55 */ getDefaultOptions()56 public static synchronized MdnsAdvertisingOptions getDefaultOptions() { 57 if (sDefaultOptions == null) { 58 sDefaultOptions = newBuilder().build(); 59 } 60 return sDefaultOptions; 61 } 62 63 /** 64 * @return {@code true} if the advertising request is an update request. 65 */ isOnlyUpdate()66 public boolean isOnlyUpdate() { 67 return mIsOnlyUpdate; 68 } 69 70 /** 71 * Returns the TTL for all records in a service. 72 */ 73 @Nullable getTtl()74 public Duration getTtl() { 75 return mTtl; 76 } 77 78 @Override equals(Object other)79 public boolean equals(Object other) { 80 if (this == other) { 81 return true; 82 } else if (!(other instanceof MdnsAdvertisingOptions)) { 83 return false; 84 } else { 85 final MdnsAdvertisingOptions otherOptions = (MdnsAdvertisingOptions) other; 86 return mIsOnlyUpdate == otherOptions.mIsOnlyUpdate 87 && Objects.equals(mTtl, otherOptions.mTtl); 88 } 89 } 90 91 @Override hashCode()92 public int hashCode() { 93 return Objects.hash(mIsOnlyUpdate, mTtl); 94 } 95 96 @Override toString()97 public String toString() { 98 return "MdnsAdvertisingOptions{" + "mIsOnlyUpdate=" + mIsOnlyUpdate + ", mTtl=" + mTtl 99 + '}'; 100 } 101 102 /** 103 * A builder to create {@link MdnsAdvertisingOptions}. 104 */ 105 public static final class Builder { 106 private boolean mIsOnlyUpdate = false; 107 @Nullable 108 private Duration mTtl; 109 Builder()110 private Builder() { 111 } 112 113 /** 114 * Sets if the advertising request is an update request. 115 */ setIsOnlyUpdate(boolean isOnlyUpdate)116 public Builder setIsOnlyUpdate(boolean isOnlyUpdate) { 117 this.mIsOnlyUpdate = isOnlyUpdate; 118 return this; 119 } 120 121 /** 122 * Sets the TTL duration for all records of the service. 123 */ setTtl(@ullable Duration ttl)124 public Builder setTtl(@Nullable Duration ttl) { 125 this.mTtl = ttl; 126 return this; 127 } 128 129 /** 130 * Builds a {@link MdnsAdvertisingOptions} with the arguments supplied to this builder. 131 */ build()132 public MdnsAdvertisingOptions build() { 133 return new MdnsAdvertisingOptions(mIsOnlyUpdate, mTtl); 134 } 135 } 136 } 137