|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. |
| 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.google.firebase.auth; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | + |
| 21 | +import com.google.common.base.Strings; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | + |
| 24 | +import com.google.firebase.internal.NonNull; |
| 25 | +import java.net.MalformedURLException; |
| 26 | +import java.net.URL; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Defines the required continue/state URL with optional Android and iOS settings. Used when |
| 31 | + * invoking the email action link generation APIs in {@link FirebaseAuth}. |
| 32 | + */ |
| 33 | +public final class ActionCodeSettings { |
| 34 | + |
| 35 | + private final Map<String, Object> properties; |
| 36 | + |
| 37 | + private ActionCodeSettings(Builder builder) { |
| 38 | + checkArgument(!Strings.isNullOrEmpty(builder.url), "URL must not be null or empty"); |
| 39 | + try { |
| 40 | + new URL(builder.url); |
| 41 | + } catch (MalformedURLException e) { |
| 42 | + throw new IllegalArgumentException("Malformed URL string", e); |
| 43 | + } |
| 44 | + if (builder.androidInstallApp || !Strings.isNullOrEmpty(builder.androidMinimumVersion)) { |
| 45 | + checkArgument(!Strings.isNullOrEmpty(builder.androidPackageName), |
| 46 | + "Android package name is required when specifying other Android settings"); |
| 47 | + } |
| 48 | + ImmutableMap.Builder<String, Object> properties = ImmutableMap.<String, Object>builder() |
| 49 | + .put("continueUrl", builder.url) |
| 50 | + .put("canHandleCodeInApp", builder.handleCodeInApp); |
| 51 | + if (!Strings.isNullOrEmpty(builder.dynamicLinkDomain)) { |
| 52 | + properties.put("dynamicLinkDomain", builder.dynamicLinkDomain); |
| 53 | + } |
| 54 | + if (!Strings.isNullOrEmpty(builder.iosBundleId)) { |
| 55 | + properties.put("iOSBundleId", builder.iosBundleId); |
| 56 | + } |
| 57 | + if (!Strings.isNullOrEmpty(builder.androidPackageName)) { |
| 58 | + properties.put("androidPackageName", builder.androidPackageName); |
| 59 | + if (!Strings.isNullOrEmpty(builder.androidMinimumVersion)) { |
| 60 | + properties.put("androidMinimumVersion", builder.androidMinimumVersion); |
| 61 | + } |
| 62 | + if (builder.androidInstallApp) { |
| 63 | + properties.put("androidInstallApp", builder.androidInstallApp); |
| 64 | + } |
| 65 | + } |
| 66 | + this.properties = properties.build(); |
| 67 | + } |
| 68 | + |
| 69 | + Map<String, Object> getProperties() { |
| 70 | + return this.properties; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates a new {@link ActionCodeSettings.Builder}. |
| 75 | + * |
| 76 | + * @return A {@link ActionCodeSettings.Builder} instance. |
| 77 | + */ |
| 78 | + public static Builder builder() { |
| 79 | + return new Builder(); |
| 80 | + } |
| 81 | + |
| 82 | + public static final class Builder { |
| 83 | + |
| 84 | + private String url; |
| 85 | + private boolean handleCodeInApp; |
| 86 | + private String dynamicLinkDomain; |
| 87 | + private String iosBundleId; |
| 88 | + private String androidPackageName; |
| 89 | + private String androidMinimumVersion; |
| 90 | + private boolean androidInstallApp; |
| 91 | + |
| 92 | + private Builder() { } |
| 93 | + |
| 94 | + /** |
| 95 | + * Sets the link continue/state URL, which has different meanings in different contexts: |
| 96 | + * |
| 97 | + * <ul> |
| 98 | + * <li>When the link is handled in the web action widgets, this is the deep link in the |
| 99 | + * {@code continueUrl} query parameter.</li> |
| 100 | + * <li>When the link is handled in the app directly, this is the {@code continueUrl} query |
| 101 | + * parameter in the deep link of the Dynamic Link.</li> |
| 102 | + * </ul> |
| 103 | + * |
| 104 | + * <p>This parameter must be specified when creating a new {@link ActionCodeSettings} instance. |
| 105 | + * |
| 106 | + * @param url Continue/state URL string. |
| 107 | + * @return This builder. |
| 108 | + */ |
| 109 | + public Builder setUrl(@NonNull String url) { |
| 110 | + this.url = url; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Specifies whether to open the link via a mobile app or a browser. The default is false. |
| 116 | + * When set to true, the action code link is sent as a Universal Link or an Android App Link |
| 117 | + * and is opened by the app if installed. In the false case, the code is sent to the web widget |
| 118 | + * first and then redirects to the app if installed. |
| 119 | + * |
| 120 | + * @param handleCodeInApp true to open the link in the app, and false otherwise. |
| 121 | + * @return This builder. |
| 122 | + */ |
| 123 | + public Builder setHandleCodeInApp(boolean handleCodeInApp) { |
| 124 | + this.handleCodeInApp = handleCodeInApp; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Sets the dynamic link domain to use for the current link if it is to be opened using |
| 130 | + * Firebase Dynamic Links, as multiple dynamic link domains can be configured per project. This |
| 131 | + * setting provides the ability to explicitly choose one. If none is provided, the oldest |
| 132 | + * domain is used by default. |
| 133 | + * |
| 134 | + * @param dynamicLinkDomain Firebase Dynamic Link domain string. |
| 135 | + * @return This builder. |
| 136 | + */ |
| 137 | + public Builder setDynamicLinkDomain(String dynamicLinkDomain) { |
| 138 | + this.dynamicLinkDomain = dynamicLinkDomain; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Sets the bundle ID of the iOS app where the link should be handled if the |
| 144 | + * application is already installed on the device. |
| 145 | + * |
| 146 | + * @param iosBundleId The iOS bundle ID string. |
| 147 | + */ |
| 148 | + public Builder setIosBundleId(String iosBundleId) { |
| 149 | + this.iosBundleId = iosBundleId; |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the Android package name of the app where the link should be handled if the |
| 155 | + * Android app is installed. Must be specified when setting other Android-specific settings. |
| 156 | + * |
| 157 | + * @param androidPackageName Package name string. Must be specified, and must not be null |
| 158 | + * or empty. |
| 159 | + * @return This builder. |
| 160 | + */ |
| 161 | + public Builder setAndroidPackageName(String androidPackageName) { |
| 162 | + this.androidPackageName = androidPackageName; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Sets the minimum version for Android app. If the installed app is an older version, the user |
| 168 | + * is taken to the Play Store to upgrade the app. |
| 169 | + * |
| 170 | + * @param androidMinimumVersion Minimum version string. |
| 171 | + * @return This builder. |
| 172 | + */ |
| 173 | + public Builder setAndroidMinimumVersion(String androidMinimumVersion) { |
| 174 | + this.androidMinimumVersion = androidMinimumVersion; |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Specifies whether to install the Android app if the device supports it and the app is not |
| 180 | + * already installed. |
| 181 | + * |
| 182 | + * @param androidInstallApp true to install the app, and false otherwise. |
| 183 | + * @return This builder. |
| 184 | + */ |
| 185 | + public Builder setAndroidInstallApp(boolean androidInstallApp) { |
| 186 | + this.androidInstallApp = androidInstallApp; |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Builds a new {@link ActionCodeSettings}. |
| 192 | + * |
| 193 | + * @return A non-null {@link ActionCodeSettings}. |
| 194 | + */ |
| 195 | + public ActionCodeSettings build() { |
| 196 | + return new ActionCodeSettings(this); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments