Andrii Kulian | 9260eed | 2021-08-02 22:01:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 androidx.window.extensions.embedding; |
| 18 | |
| 19 | import android.annotation.SuppressLint; |
| 20 | import android.app.Activity; |
| 21 | import android.content.Intent; |
| 22 | import android.os.Build; |
| 23 | |
| 24 | import androidx.annotation.NonNull; |
| 25 | import androidx.annotation.RequiresApi; |
Andrii Kulian | 9260eed | 2021-08-02 22:01:13 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.function.Predicate; |
| 28 | |
| 29 | /** |
| 30 | * Split configuration rule for individual activities. |
| 31 | */ |
Andrii Kulian | 9260eed | 2021-08-02 22:01:13 -0700 | [diff] [blame] | 32 | public class ActivityRule extends EmbeddingRule { |
| 33 | @NonNull |
| 34 | private final Predicate<Activity> mActivityPredicate; |
| 35 | @NonNull |
| 36 | private final Predicate<Intent> mIntentPredicate; |
| 37 | private final boolean mShouldAlwaysExpand; |
| 38 | |
| 39 | ActivityRule(@NonNull Predicate<Activity> activityPredicate, |
| 40 | @NonNull Predicate<Intent> intentPredicate, boolean shouldAlwaysExpand) { |
| 41 | mActivityPredicate = activityPredicate; |
| 42 | mIntentPredicate = intentPredicate; |
| 43 | mShouldAlwaysExpand = shouldAlwaysExpand; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Checks if the rule is applicable to the provided activity. |
| 48 | */ |
| 49 | @SuppressLint("ClassVerificationFailure") // Only called by Extensions implementation on device. |
| 50 | @RequiresApi(api = Build.VERSION_CODES.N) |
| 51 | public boolean matchesActivity(@NonNull Activity activity) { |
| 52 | return mActivityPredicate.test(activity); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks if the rule is applicable to the provided activity intent. |
| 57 | */ |
| 58 | @SuppressLint("ClassVerificationFailure") // Only called by Extensions implementation on device. |
| 59 | @RequiresApi(api = Build.VERSION_CODES.N) |
| 60 | public boolean matchesIntent(@NonNull Intent intent) { |
| 61 | return mIntentPredicate.test(intent); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Indicates whether the activity or activities that are covered by this rule should always be |
| 66 | * launched in an expanded state and avoid the splits. |
| 67 | */ |
| 68 | public boolean shouldAlwaysExpand() { |
| 69 | return mShouldAlwaysExpand; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Builder for {@link ActivityRule}. |
| 74 | */ |
| 75 | public static final class Builder { |
| 76 | @NonNull |
| 77 | private final Predicate<Activity> mActivityPredicate; |
| 78 | @NonNull |
| 79 | private final Predicate<Intent> mIntentPredicate; |
| 80 | private boolean mAlwaysExpand; |
| 81 | |
| 82 | public Builder(@NonNull Predicate<Activity> activityPredicate, |
| 83 | @NonNull Predicate<Intent> intentPredicate) { |
| 84 | mActivityPredicate = activityPredicate; |
| 85 | mIntentPredicate = intentPredicate; |
| 86 | } |
| 87 | |
| 88 | /** @see ActivityRule#shouldAlwaysExpand() */ |
| 89 | @NonNull |
| 90 | public Builder setShouldAlwaysExpand(boolean alwaysExpand) { |
| 91 | mAlwaysExpand = alwaysExpand; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | /** Builds a new instance of {@link ActivityRule}. */ |
| 96 | @NonNull |
| 97 | public ActivityRule build() { |
| 98 | return new ActivityRule(mActivityPredicate, mIntentPredicate, mAlwaysExpand); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean equals(Object o) { |
| 104 | if (this == o) return true; |
| 105 | if (!(o instanceof ActivityRule)) return false; |
| 106 | ActivityRule that = (ActivityRule) o; |
| 107 | return mShouldAlwaysExpand == that.mShouldAlwaysExpand |
| 108 | && mActivityPredicate.equals(that.mActivityPredicate) |
| 109 | && mIntentPredicate.equals(that.mIntentPredicate); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int hashCode() { |
| 114 | int result = mActivityPredicate.hashCode(); |
| 115 | result = 31 * result + mIntentPredicate.hashCode(); |
| 116 | result = 31 * result + (mShouldAlwaysExpand ? 1 : 0); |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | @NonNull |
| 121 | @Override |
| 122 | public String toString() { |
| 123 | return "ActivityRule{" + "mShouldAlwaysExpand=" + mShouldAlwaysExpand + '}'; |
| 124 | } |
| 125 | } |