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.settings.homepage.contextualcards.conditional; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.widget.ImageView; 24 import android.widget.LinearLayout; 25 26 import androidx.annotation.LayoutRes; 27 import androidx.recyclerview.widget.RecyclerView; 28 29 import com.android.settings.R; 30 import com.android.settings.homepage.contextualcards.ContextualCard; 31 import com.android.settings.homepage.contextualcards.ContextualCardRenderer; 32 import com.android.settings.homepage.contextualcards.ControllerRendererPool; 33 import com.android.settings.overlay.FeatureFactory; 34 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 35 36 public class ConditionHeaderContextualCardRenderer implements ContextualCardRenderer { 37 public static final int VIEW_TYPE = R.layout.conditional_card_header; 38 private static final String TAG = "ConditionHeaderRenderer"; 39 40 private final Context mContext; 41 private final ControllerRendererPool mControllerRendererPool; 42 ConditionHeaderContextualCardRenderer(Context context, ControllerRendererPool controllerRendererPool)43 public ConditionHeaderContextualCardRenderer(Context context, 44 ControllerRendererPool controllerRendererPool) { 45 mContext = context; 46 mControllerRendererPool = controllerRendererPool; 47 } 48 49 @Override createViewHolder(View view, @LayoutRes int viewType)50 public RecyclerView.ViewHolder createViewHolder(View view, @LayoutRes int viewType) { 51 return new ConditionHeaderCardHolder(view); 52 } 53 54 @Override bindView(RecyclerView.ViewHolder holder, ContextualCard contextualCard)55 public void bindView(RecyclerView.ViewHolder holder, ContextualCard contextualCard) { 56 final ConditionHeaderContextualCard headerCard = 57 (ConditionHeaderContextualCard) contextualCard; 58 final ConditionHeaderCardHolder view = (ConditionHeaderCardHolder) holder; 59 final MetricsFeatureProvider metricsFeatureProvider = FeatureFactory.getFactory( 60 mContext).getMetricsFeatureProvider(); 61 view.icons.removeAllViews(); 62 headerCard.getConditionalCards().stream().forEach(card -> { 63 final ImageView icon = (ImageView) LayoutInflater.from(mContext).inflate( 64 R.layout.conditional_card_header_icon, view.icons, false); 65 icon.setImageDrawable(card.getIconDrawable()); 66 view.icons.addView(icon); 67 }); 68 view.itemView.setOnClickListener(v -> { 69 metricsFeatureProvider.action(SettingsEnums.PAGE_UNKNOWN, 70 SettingsEnums.ACTION_SETTINGS_CONDITION_EXPAND, 71 SettingsEnums.SETTINGS_HOMEPAGE, 72 null /* key */, 73 1 /* true */); 74 final ConditionContextualCardController controller = 75 mControllerRendererPool.getController(mContext, 76 ContextualCard.CardType.CONDITIONAL_HEADER); 77 controller.setIsExpanded(true); 78 controller.onConditionsChanged(); 79 }); 80 } 81 82 public static class ConditionHeaderCardHolder extends RecyclerView.ViewHolder { 83 public final LinearLayout icons; 84 public final ImageView expandIndicator; 85 ConditionHeaderCardHolder(View itemView)86 public ConditionHeaderCardHolder(View itemView) { 87 super(itemView); 88 icons = itemView.findViewById(R.id.header_icons_container); 89 expandIndicator = itemView.findViewById(R.id.expand_indicator); 90 } 91 } 92 } 93