1 /*
2  * Copyright (C) 2018 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.dialer.promotion;
18 
19 import com.android.dialer.promotion.Promotion.PromotionType;
20 import com.google.common.collect.ImmutableList;
21 import java.util.Optional;
22 import javax.inject.Inject;
23 
24 /**
25  * A class to manage all promotion cards/bottom sheet.
26  *
27  * <p>Only one promotion with highest priority will be shown at a time no matter type. So if there
28  * are one card and one bottom sheet promotion, either one will be shown instead of both.
29  */
30 public final class PromotionManager {
31 
32   /** Promotion priority order list. Promotions with higher priority must be added first. */
33   private ImmutableList<Promotion> priorityPromotionList;
34 
35   @Inject
PromotionManager(ImmutableList<Promotion> priorityPromotionList)36   public PromotionManager(ImmutableList<Promotion> priorityPromotionList) {
37     this.priorityPromotionList = priorityPromotionList;
38   }
39 
40   /**
41    * Returns promotion should show with highest priority. {@link Optional#empty()} if no promotion
42    * should be shown with given {@link PromotionType}.
43    *
44    * <p>e.g. if FooPromotion(card, high priority) and BarPromotion(bottom sheet, low priority) are
45    * both enabled, getHighestPriorityPromotion(CARD) returns Optional.of(FooPromotion) but
46    * getHighestPriorityPromotion(BOTTOM_SHEET) returns {@link Optional#empty()}.
47    *
48    * <p>Currently it only supports promotion in call log tab.
49    *
50    * <p>TODO(wangqi): add support for other tabs.
51    */
getHighestPriorityPromotion(@romotionType int type)52   public Optional<Promotion> getHighestPriorityPromotion(@PromotionType int type) {
53     for (Promotion promotion : priorityPromotionList) {
54       if (promotion.isEligibleToBeShown()) {
55         if (promotion.getType() == type) {
56           return Optional.of(promotion);
57         } else {
58           // Returns empty promotion since it's not the type looking for and only one promotion
59           // should be shown at a time.
60           return Optional.empty();
61         }
62       }
63     }
64     return Optional.empty();
65   }
66 }
67