1 /*
<lambda>null2  * Copyright (C) 2022 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.test.silkfx
17 
18 import android.app.Activity
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Bundle
22 import android.view.LayoutInflater
23 import android.view.View
24 import android.view.ViewGroup
25 import android.widget.BaseExpandableListAdapter
26 import android.widget.ExpandableListView
27 import android.widget.TextView
28 import com.android.test.silkfx.app.CommonDemoActivity
29 import com.android.test.silkfx.app.EXTRA_COMMON_CONTROLS
30 import com.android.test.silkfx.app.EXTRA_LAYOUT
31 import com.android.test.silkfx.app.EXTRA_TITLE
32 import com.android.test.silkfx.hdr.GlowActivity
33 import com.android.test.silkfx.materials.GlassActivity
34 import com.android.test.silkfx.materials.BackgroundBlurActivity
35 import kotlin.reflect.KClass
36 
37 class Demo(val name: String, val makeIntent: (Context) -> Intent) {
38     constructor(name: String, activity: KClass<out Activity>) : this(name, { context ->
39         Intent(context, activity.java)
40     })
41     constructor(name: String, layout: Int, commonControls: Boolean = true) : this(name, { context ->
42         Intent(context, CommonDemoActivity::class.java).apply {
43             putExtra(EXTRA_LAYOUT, layout)
44             putExtra(EXTRA_TITLE, name)
45             putExtra(EXTRA_COMMON_CONTROLS, commonControls)
46         }
47     })
48 }
49 data class DemoGroup(val groupName: String, val demos: List<Demo>)
50 
51 private val AllDemos = listOf(
52         DemoGroup("HDR", listOf(
53                 Demo("Glow", GlowActivity::class),
54                 Demo("Blingy Notifications", R.layout.bling_notifications),
55                 Demo("Color Grid", R.layout.color_grid),
56                 Demo("Gradient Sweep", R.layout.gradient_sweep),
57                 Demo("Gainmap Image", R.layout.gainmap_image),
58                 Demo("Gainmap Decode Test", R.layout.gainmap_decode_test, commonControls = false),
59                 Demo("Gainmap Transform Test", R.layout.gainmap_transform_test,
60                         commonControls = false)
61         )),
62         DemoGroup("Materials", listOf(
63                 Demo("Glass", GlassActivity::class),
64                 Demo("Background Blur", BackgroundBlurActivity::class)
65         ))
66 )
67 
68 class Main : Activity() {
69 
onCreatenull70     public override fun onCreate(savedInstanceState: Bundle?) {
71         super.onCreate(savedInstanceState)
72 
73         val list = ExpandableListView(this)
74 
75         setContentView(list)
76 
77         val inflater = LayoutInflater.from(this)
78         list.setAdapter(object : BaseExpandableListAdapter() {
79             override fun getGroup(groupPosition: Int): DemoGroup {
80                 return AllDemos[groupPosition]
81             }
82 
83             override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true
84 
85             override fun hasStableIds(): Boolean = true
86 
87             override fun getGroupView(
88                 groupPosition: Int,
89                 isExpanded: Boolean,
90                 convertView: View?,
91                 parent: ViewGroup?
92             ): View {
93                 val view = (convertView ?: inflater.inflate(
94                         android.R.layout.simple_expandable_list_item_1, parent, false)) as TextView
95                 view.text = AllDemos[groupPosition].groupName
96                 return view
97             }
98 
99             override fun getChildrenCount(groupPosition: Int): Int {
100                 return AllDemos[groupPosition].demos.size
101             }
102 
103             override fun getChild(groupPosition: Int, childPosition: Int): Demo {
104                 return AllDemos[groupPosition].demos[childPosition]
105             }
106 
107             override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()
108 
109             override fun getChildView(
110                 groupPosition: Int,
111                 childPosition: Int,
112                 isLastChild: Boolean,
113                 convertView: View?,
114                 parent: ViewGroup?
115             ): View {
116                 val view = (convertView ?: inflater.inflate(
117                         android.R.layout.simple_expandable_list_item_1, parent, false)) as TextView
118                 view.text = AllDemos[groupPosition].demos[childPosition].name
119                 return view
120             }
121 
122             override fun getChildId(groupPosition: Int, childPosition: Int): Long {
123                 return (groupPosition.toLong() shl 32) or childPosition.toLong()
124             }
125 
126             override fun getGroupCount(): Int {
127                 return AllDemos.size
128             }
129         })
130 
131         list.setOnChildClickListener { _, _, groupPosition, childPosition, _ ->
132             val demo = AllDemos[groupPosition].demos[childPosition]
133             startActivity(demo.makeIntent(this))
134             return@setOnChildClickListener true
135         }
136 
137         AllDemos.forEachIndexed { index, _ -> list.expandGroup(index) }
138     }
139 }
140