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.systemui.controls.management 18 19 import android.view.LayoutInflater 20 import android.view.View 21 import android.view.ViewGroup 22 import androidx.recyclerview.widget.GridLayoutManager 23 import androidx.recyclerview.widget.RecyclerView 24 import com.android.systemui.res.R 25 import com.android.systemui.utils.SafeIconLoader 26 27 class StructureAdapter( 28 private val models: List<StructureContainer>, 29 private val currentUserId: Int, 30 private val safeIconLoader: SafeIconLoader, 31 ) : RecyclerView.Adapter<StructureAdapter.StructureHolder>() { 32 onCreateViewHoldernull33 override fun onCreateViewHolder(parent: ViewGroup, p1: Int): StructureHolder { 34 val layoutInflater = LayoutInflater.from(parent.context) 35 return StructureHolder( 36 layoutInflater.inflate(R.layout.controls_structure_page, parent, false), 37 currentUserId, 38 safeIconLoader, 39 ) 40 } 41 getItemCountnull42 override fun getItemCount() = models.size 43 44 override fun onBindViewHolder(holder: StructureHolder, index: Int) { 45 holder.bind(models[index].model) 46 } 47 48 class StructureHolder(view: View, currentUserId: Int, safeIconLoader: SafeIconLoader) : 49 RecyclerView.ViewHolder(view) { 50 51 private val recyclerView: RecyclerView 52 private val controlAdapter: ControlAdapter 53 54 init { 55 recyclerView = itemView.requireViewById<RecyclerView>(R.id.listAll) 56 val elevation = itemView.context.resources.getFloat(R.dimen.control_card_elevation) 57 controlAdapter = ControlAdapter(elevation, currentUserId, safeIconLoader) 58 setUpRecyclerView() 59 } 60 bindnull61 fun bind(model: ControlsModel) { 62 controlAdapter.changeModel(model) 63 } 64 setUpRecyclerViewnull65 private fun setUpRecyclerView() { 66 val margin = 67 itemView.context.resources.getDimensionPixelSize(R.dimen.controls_card_margin) 68 val itemDecorator = MarginItemDecorator(margin, margin) 69 val spanCount = ControlAdapter.findMaxColumns(itemView.resources) 70 71 recyclerView.apply { 72 this.adapter = controlAdapter 73 layoutManager = 74 GridLayoutManager(recyclerView.context, spanCount).apply { 75 spanSizeLookup = 76 object : GridLayoutManager.SpanSizeLookup() { 77 override fun getSpanSize(position: Int): Int { 78 return if ( 79 adapter?.getItemViewType(position) != 80 ControlAdapter.TYPE_CONTROL 81 ) 82 spanCount 83 else 1 84 } 85 } 86 } 87 addItemDecoration(itemDecorator) 88 } 89 } 90 } 91 } 92