• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.virtualization.terminal
17 
18 import androidx.core.os.bundleOf
19 import androidx.fragment.app.Fragment
20 import androidx.fragment.app.FragmentActivity
21 import androidx.viewpager2.adapter.FragmentStateAdapter
22 import java.util.UUID
23 
24 class TabMetadata(val id: String)
25 
26 class TerminalTabAdapter(fragmentActivity: FragmentActivity) :
27     FragmentStateAdapter(fragmentActivity) {
28     val tabs = ArrayList<TabMetadata>()
29 
createFragmentnull30     override fun createFragment(position: Int): Fragment {
31         val terminalTabFragment = TerminalTabFragment()
32 
33         terminalTabFragment.arguments = bundleOf("id" to tabs[position].id)
34         return terminalTabFragment
35     }
36 
getItemCountnull37     override fun getItemCount(): Int {
38         return tabs.size
39     }
40 
getItemIdnull41     override fun getItemId(position: Int): Long {
42         return tabs[position].id.hashCode().toLong()
43     }
44 
containsItemnull45     override fun containsItem(itemId: Long): Boolean {
46         return tabs.any { it.id.hashCode().toLong() == itemId }
47     }
48 
addTabnull49     fun addTab(): String {
50         val id = UUID.randomUUID().toString()
51         tabs.add(TabMetadata(id))
52         return id
53     }
54 
deleteTabnull55     fun deleteTab(position: Int) {
56         if (position in 0 until tabs.size) {
57             tabs.removeAt(position)
58             notifyItemRemoved(position)
59         }
60     }
61 }
62