Debian Codegraph @

tests/SilkFX/src/com/android/test/silkfx/Main.kt

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