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