summaryrefslogtreecommitdiff
path: root/Host/app/renderer/src/main/java/com/android/car/libraries/templates/host/view/presenters/common/LongMessageTemplatePresenter.java
blob: afd208f7b626cb7a242c8f7f14029dc384c3c34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.libraries.templates.host.view.presenters.common;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static java.util.Objects.requireNonNull;

import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleableRes;
import androidx.car.app.model.Action;
import androidx.car.app.model.ActionStrip;
import androidx.car.app.model.CarText;
import androidx.car.app.model.LongMessageTemplate;
import androidx.car.app.model.TemplateWrapper;
import androidx.recyclerview.widget.RecyclerView;
import com.android.car.libraries.apphost.common.StatusBarManager.StatusBarState;
import com.android.car.libraries.apphost.common.TemplateContext;
import com.android.car.libraries.apphost.distraction.constraints.ActionsConstraints;
import com.android.car.libraries.apphost.view.AbstractTemplatePresenter;
import com.android.car.libraries.apphost.view.common.ActionButtonListParams;
import com.android.car.libraries.apphost.view.common.CarTextUtils;
import com.android.car.libraries.templates.host.R;
import com.android.car.libraries.templates.host.view.widgets.common.ActionButtonListView;
import com.android.car.libraries.templates.host.view.widgets.common.ActionButtonListView.Gravity;
import com.android.car.libraries.templates.host.view.widgets.common.HeaderView;
import com.android.car.libraries.templates.host.view.widgets.common.ParkedOnlyFrameLayout;
import com.android.car.ui.recyclerview.CarUiRecyclerView;
import com.android.car.ui.recyclerview.CarUiRecyclerView.OnScrollListener;
import com.android.car.ui.widget.CarUiTextView;
import java.util.List;
import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;

/**
 * An {@link AbstractTemplatePresenter} that shows a scrolling long form message and some actions.
 */
public class LongMessageTemplatePresenter extends AbstractTemplatePresenter {
  // TODO(b/183643108): Use a common value for this constant
  private static final int MAX_ALLOWED_ACTIONS = 2;

  private final ViewGroup mRootView;
  private final HeaderView mHeaderView;
  private final CarUiRecyclerView mRecyclerView;
  private final ActionButtonListView mStickyActionButtonListView;
  private final ActionButtonListView.Gravity mActionButtonListGravity;
  private final ActionButtonListParams mActionButtonListParams;
  private final String mDisabledActionButtonToastMessage;

  private final LongMessageAdapter mAdapter;

  /** Create a LongMessageTemplatePresenter */
  static LongMessageTemplatePresenter create(
      TemplateContext templateContext, TemplateWrapper templateWrapper) {
    LongMessageTemplatePresenter presenter =
        new LongMessageTemplatePresenter(templateContext, templateWrapper);
    presenter.update();
    return presenter;
  }

  @Override
  public void onTemplateChanged() {
    update();
  }

  @Override
  public View getView() {
    return mRootView;
  }

  /** Updates the view with current values in the {@link LongMessageTemplate}. */
  private void update() {
    LongMessageTemplate template = (LongMessageTemplate) getTemplate();
    ActionStrip actionStrip = template.getActionStrip();

    // If we have a title or a header action, show the header; hide it otherwise.
    CarText title = template.getTitle();
    Action headerAction = template.getHeaderAction();
    if (!CarText.isNullOrEmpty(title) || headerAction != null) {
      mHeaderView.setContent(getTemplateContext(), title, headerAction);
    } else {
      mHeaderView.setContent(getTemplateContext(), null, null);
    }

    mHeaderView.setActionStrip(actionStrip, ActionsConstraints.ACTIONS_CONSTRAINTS_SIMPLE);

    mAdapter.setMessage(template.getMessage());
    if (mActionButtonListGravity == Gravity.CENTER) {
      // In the case of Gravity.CENTER, put the buttons in a row along with the rest of the content.
      mAdapter.setActions(template.getActions());
      mStickyActionButtonListView.setVisibility(GONE);
    } else {
      // If action button list gravity is not Gravity.CENTER, put the buttons in the sticky action
      // button list view so they stay on screen at all times.
      mAdapter.setActions(null);
      mStickyActionButtonListView.setVisibility(VISIBLE);
      mStickyActionButtonListView.setActionList(
          getTemplateContext(), template.getActions(), mActionButtonListParams);
    }

    // If this update is not due to a refresh, scroll back to the top. Template presenters can
    // be reused for templates of the same type, so a scroll reset would be needed for the case
    // where an app pushes two long message templates in the same flow, for example, or if we at
    // some point implement a pool of presenters.
    // TODO(b/186244619): Add unit test to cover this path.
    if (!getTemplateWrapper().isRefresh()) {
      mRecyclerView.scrollToPosition(0);
    }

    setActionButtonEnabledState();
  }

  private LongMessageTemplatePresenter(
      TemplateContext templateContext, TemplateWrapper templateWrapper) {
    super(templateContext, templateWrapper, StatusBarState.LIGHT);

    mRootView =
        (ViewGroup)
            LayoutInflater.from(templateContext)
                .inflate(R.layout.long_message_template_layout, null);

    mRecyclerView = mRootView.findViewById(R.id.list_view);

    ParkedOnlyFrameLayout contentContainer = mRootView.findViewById(R.id.park_only_container);
    mHeaderView = HeaderView.install(templateContext, contentContainer);
    contentContainer.setTemplateContext(templateContext);

    mStickyActionButtonListView = mRootView.requireViewById(R.id.sticky_action_button_list_view);

    @StyleableRes
    final int[] themeAttrs = {
      R.attr.templateActionButtonListGravity, R.attr.templatePlainContentBackgroundColor
    };

    TypedArray ta = templateContext.obtainStyledAttributes(themeAttrs);
    mActionButtonListGravity = ActionButtonListView.Gravity.values()[ta.getInt(0, 0)];
    @ColorInt int surroundingColor = ta.getColor(1, 0);
    ta.recycle();

    mDisabledActionButtonToastMessage =
        templateContext
            .getResources()
            .getString(
                templateContext.getHostResourceIds().getLongMessageTemplateDisabledActionText());

    mActionButtonListParams =
        ActionButtonListParams.builder()
            .setMaxActions(MAX_ALLOWED_ACTIONS)
            .setOemReorderingAllowed(true)
            .setOemColorOverrideAllowed(true)
            .setSurroundingColor(surroundingColor)
            .build();

    mAdapter = new LongMessageAdapter();
    mRecyclerView.setAdapter(mAdapter);

    mRecyclerView.addOnScrollListener(
        new OnScrollListener() {
          @Override
          public void onScrolled(CarUiRecyclerView recyclerView, int dx, int dy) {
            // no-op
          }

          @Override
          public void onScrollStateChanged(CarUiRecyclerView recyclerView, int newState) {
            if (newState != RecyclerView.SCROLL_STATE_IDLE) {
              return;
            }

            setActionButtonEnabledState();
          }
        });
    // {@link View#OnLayoutChangeListener} is required to disable sticky action buttons on first
    // load.
    mRecyclerView.addOnLayoutChangeListener(
        (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) ->
            setActionButtonEnabledState());
  }

  @RequiresNonNull({
    "mRecyclerView",
    "mStickyActionButtonListView",
    "mDisabledActionButtonToastMessage"
  })
  private void setActionButtonEnabledState(
      @UnknownInitialization LongMessageTemplatePresenter this) {
    if (!mRecyclerView.getView().isAttachedToWindow()) {
      return;
    }
    // Only need to set active state for sticky action buttons since they stay on screen at all
    // times.
    if (mActionButtonListGravity == Gravity.CENTER) {
      return;
    }

    boolean enabled = !mRecyclerView.getView().canScrollVertically(/* direction= */ 1);

    if (enabled) {
      mStickyActionButtonListView.enableActionButtons();
    } else {
      mStickyActionButtonListView.disableActionButtons(mDisabledActionButtonToastMessage);
    }
  }

  /** Adapter used for rendering the long text and buttons in this template. */
  private class LongMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    static final int ITEM_TYPE_MESSAGE = 1;
    static final int ITEM_TYPE_ACTION = 2;

    private String mMessage;
    @Nullable private List<Action> mActions;

    public void setMessage(CarText message) {
      mMessage = CarTextUtils.toCharSequenceOrEmpty(getTemplateContext(), message).toString();
      notifyDataSetChanged();
    }

    public void setActions(@Nullable List<Action> actions) {
      mActions = actions;
      notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
      if (position == 0) {
        return ITEM_TYPE_MESSAGE;
      } else {
        return ITEM_TYPE_ACTION;
      }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
      switch (viewType) {
        case ITEM_TYPE_ACTION:
          return new ActionsViewHolder(
              LayoutInflater.from(getTemplateContext())
                  .inflate(R.layout.long_message_action_layout, viewGroup, false));

        case ITEM_TYPE_MESSAGE:
        default:
          return new MessageViewHolder(
              LayoutInflater.from(getTemplateContext())
                  .inflate(R.layout.long_message_layout, viewGroup, false));
      }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
      if (viewHolder instanceof MessageViewHolder) {
        ((MessageViewHolder) viewHolder).bind(mMessage);
      } else if (viewHolder instanceof ActionsViewHolder) {
        ((ActionsViewHolder) viewHolder).bind(getTemplateContext(), requireNonNull(mActions));
      }
    }

    @Override
    public int getItemCount() {
      return mActions == null ? 1 : 2;
    }

    /** ViewHolder for a message list item */
    private class MessageViewHolder extends RecyclerView.ViewHolder {

      private final CarUiTextView mMessage;

      private MessageViewHolder(@NonNull View view) {
        super(view);
        mMessage = view.requireViewById(R.id.message_text);
      }

      private void bind(String message) {
        mMessage.setText(message);
      }
    }

    /** ViewHolder for actions list item */
    private class ActionsViewHolder extends RecyclerView.ViewHolder {

      private final ActionButtonListView mActionButtonListView;

      private ActionsViewHolder(@NonNull View view) {
        super(view);
        mActionButtonListView = view.requireViewById(R.id.action_button_list_view);
      }

      private void bind(TemplateContext templateContext, List<Action> actions) {
        if (!actions.isEmpty()) {
          mActionButtonListView.setActionList(templateContext, actions, mActionButtonListParams);
          mActionButtonListView.setVisibility(VISIBLE);
        } else {
          mActionButtonListView.setVisibility(GONE);
        }
      }
    }
  }
}