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 private final boolean mSkipProbing; 38 39 /** 40 * Parcelable constructs for a {@link MdnsAdvertisingOptions}. 41 */ MdnsAdvertisingOptions(boolean isOnlyUpdate, @Nullable Duration ttl, boolean skipProbing)42 MdnsAdvertisingOptions(boolean isOnlyUpdate, @Nullable Duration ttl, boolean skipProbing) { 43 this.mIsOnlyUpdate = isOnlyUpdate; 44 this.mTtl = ttl; 45 this.mSkipProbing = skipProbing; 46 } 47 48 /** 49 * Returns a {@link Builder} for {@link MdnsAdvertisingOptions}. 50 */ newBuilder()51 public static Builder newBuilder() { 52 return new Builder(); 53 } 54 55 /** 56 * Returns a default search options. 57 */ getDefaultOptions()58 public static synchronized MdnsAdvertisingOptions getDefaultOptions() { 59 if (sDefaultOptions == null) { 60 sDefaultOptions = newBuilder().build(); 61 } 62 return sDefaultOptions; 63 } 64 65 /** 66 * @return {@code true} if the advertising request is an update request. 67 */ isOnlyUpdate()68 public boolean isOnlyUpdate() { 69 return mIsOnlyUpdate; 70 } 71 72 /** 73 * @return {@code true} if the probing step should be skipped. 74 */ skipProbing()75 public boolean skipProbing() { 76 return mSkipProbing; 77 } 78 79 /** 80 * Returns the TTL for all records in a service. 81 */ 82 @Nullable getTtl()83 public Duration getTtl() { 84 return mTtl; 85 } 86 87 @Override equals(Object other)88 public boolean equals(Object other) { 89 if (this == other) { 90 return true; 91 } else if (!(other instanceof MdnsAdvertisingOptions)) { 92 return false; 93 } else { 94 final MdnsAdvertisingOptions otherOptions = (MdnsAdvertisingOptions) other; 95 return mIsOnlyUpdate == otherOptions.mIsOnlyUpdate 96 && Objects.equals(mTtl, otherOptions.mTtl); 97 } 98 } 99 100 @Override hashCode()101 public int hashCode() { 102 return Objects.hash(mIsOnlyUpdate, mTtl); 103 } 104 105 @Override toString()106 public String toString() { 107 return "MdnsAdvertisingOptions{" + "mIsOnlyUpdate=" + mIsOnlyUpdate + ", mTtl=" + mTtl 108 + '}'; 109 } 110 111 /** 112 * A builder to create {@link MdnsAdvertisingOptions}. 113 */ 114 public static final class Builder { 115 private boolean mIsOnlyUpdate = false; 116 private boolean mSkipProbing = false; 117 @Nullable 118 private Duration mTtl; 119 Builder()120 private Builder() { 121 } 122 123 /** 124 * Sets if the advertising request is an update request. 125 */ setIsOnlyUpdate(boolean isOnlyUpdate)126 public Builder setIsOnlyUpdate(boolean isOnlyUpdate) { 127 this.mIsOnlyUpdate = isOnlyUpdate; 128 return this; 129 } 130 131 /** 132 * Sets the TTL duration for all records of the service. 133 */ setTtl(@ullable Duration ttl)134 public Builder setTtl(@Nullable Duration ttl) { 135 this.mTtl = ttl; 136 return this; 137 } 138 139 /** 140 * Sets whether to skip the probing step. 141 */ setSkipProbing(boolean skipProbing)142 public Builder setSkipProbing(boolean skipProbing) { 143 this.mSkipProbing = skipProbing; 144 return this; 145 } 146 147 /** 148 * Builds a {@link MdnsAdvertisingOptions} with the arguments supplied to this builder. 149 */ build()150 public MdnsAdvertisingOptions build() { 151 return new MdnsAdvertisingOptions(mIsOnlyUpdate, mTtl, mSkipProbing); 152 } 153 } 154 } 155