• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 package com.android.customization.picker.clock.ui.view
17 
18 import android.content.Context
19 import android.util.AttributeSet
20 import android.view.LayoutInflater
21 import android.view.View
22 import android.view.ViewGroup
23 import android.widget.FrameLayout
24 import androidx.constraintlayout.helper.widget.Carousel
25 import androidx.core.view.get
26 import com.android.wallpaper.R
27 
28 class ClockCarouselView(
29     context: Context,
30     attrs: AttributeSet,
31 ) :
32     FrameLayout(
33         context,
34         attrs,
35     ) {
36 
37     private val carousel: Carousel
38     private lateinit var adapter: ClockCarouselAdapter
39 
40     init {
41         val view = LayoutInflater.from(context).inflate(R.layout.clock_carousel, this)
42         carousel = view.requireViewById(R.id.carousel)
43     }
44 
setUpClockCarouselViewnull45     fun setUpClockCarouselView(
46         clockIds: List<String>,
47         onGetClockPreview: (clockId: String) -> View,
48         onClockSelected: (clockId: String) -> Unit,
49     ) {
50         adapter = ClockCarouselAdapter(clockIds, onGetClockPreview, onClockSelected)
51         carousel.setAdapter(adapter)
52         carousel.refresh()
53     }
54 
setSelectedClockIndexnull55     fun setSelectedClockIndex(
56         index: Int,
57     ) {
58         carousel.jumpToIndex(index)
59     }
60 
61     class ClockCarouselAdapter(
62         val clockIds: List<String>,
63         val onGetClockPreview: (clockId: String) -> View,
64         val onClockSelected: (clockId: String) -> Unit,
65     ) : Carousel.Adapter {
66 
countnull67         override fun count(): Int {
68             return clockIds.size
69         }
70 
populatenull71         override fun populate(view: View?, index: Int) {
72             val viewRoot = view as ViewGroup
73             val clockHostView = viewRoot[0] as ViewGroup
74             clockHostView.removeAllViews()
75             val clockView = onGetClockPreview(clockIds[index])
76             // The clock view might still be attached to an existing parent. Detach before adding to
77             // another parent.
78             (clockView.parent as? ViewGroup)?.removeView(clockView)
79             clockHostView.addView(clockView)
80         }
81 
onNewItemnull82         override fun onNewItem(index: Int) {
83             onClockSelected.invoke(clockIds[index])
84         }
85     }
86 }
87