• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.car.carlauncher.homescreen.assistive;
18 
19 import android.view.View;
20 
21 import com.android.car.carlauncher.homescreen.CardPresenter;
22 import com.android.car.carlauncher.homescreen.HomeCardInterface;
23 
24 import java.util.List;
25 
26 /**
27  * The {@link CardPresenter} for an assistive card.
28  */
29 public class AssistiveCardPresenter extends CardPresenter {
30 
31     private HomeCardInterface.Model mCurrentModel;
32     private List<HomeCardInterface.Model> mModels;
33 
34     @Override
setModels(List<HomeCardInterface.Model> models)35     public void setModels(List<HomeCardInterface.Model> models) {
36         mModels = models;
37     }
38 
39     /**
40      * Called when the View is created
41      */
42     @Override
onViewCreated()43     public void onViewCreated() {
44         for (HomeCardInterface.Model model : mModels) {
45             model.setPresenter(this);
46             model.onCreate(getFragment().requireContext());
47         }
48     }
49 
50     /**
51      * Called when the View is destroyed
52      */
53     @Override
onViewDestroyed()54     public void onViewDestroyed() {
55         if (mModels != null) {
56             for (HomeCardInterface.Model model : mModels) {
57                 model.onDestroy(getFragment().requireContext());
58             }
59         }
60     }
61 
62     /**
63      * Called when the View is clicked
64      */
65     @Override
onViewClicked(View v)66     public void onViewClicked(View v) {
67         mCurrentModel.onClick(v);
68     }
69 
70     /**
71      * Called when a Model is updated.
72      */
73     @Override
onModelUpdated(HomeCardInterface.Model model)74     public void onModelUpdated(HomeCardInterface.Model model) {
75         if (model.getCardHeader() == null) {
76             if (mCurrentModel != null && model.getClass() == mCurrentModel.getClass()) {
77                 if (mModels != null) {
78                     // Check if any other models have content to display
79                     for (HomeCardInterface.Model candidate : mModels) {
80                         if (candidate.getCardHeader() != null) {
81                             mCurrentModel = candidate;
82                             super.onModelUpdated(candidate);
83                             return;
84                         }
85                     }
86                 }
87             } else {
88                 // Otherwise, another model is already on display,
89                 return;
90             }
91         }
92         mCurrentModel = model;
93         super.onModelUpdated(model);
94     }
95 }
96