1 /* 2 * Copyright (C) 2021 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 17 package com.google.android.car.kitchensink.bluetooth; 18 19 import android.os.Bundle; 20 import android.util.Log; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.fragment.app.Fragment; 28 import androidx.viewpager2.adapter.FragmentStateAdapter; 29 import androidx.viewpager2.widget.ViewPager2; 30 31 import com.google.android.car.kitchensink.R; 32 import com.google.android.material.tabs.TabLayout; 33 import com.google.android.material.tabs.TabLayoutMediator; 34 35 import java.util.Arrays; 36 import java.util.List; 37 38 public class BluetoothUuidFragment extends Fragment { 39 private static final String TAG = "CAR.BLUETOOTH.KS"; 40 41 private final List<FragmentTabEntry> mTabFragments = Arrays.asList( 42 new FragmentTabEntry("BT device", BluetoothDeviceFragment.class), 43 new FragmentTabEntry("Tx Custom Uuids EIR", CustomUuidEirFragment.class)); 44 45 TabFragmentStateAdapter mTabFragmentStateAdapter; 46 ViewPager2 mViewPager; 47 48 @Nullable 49 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)50 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 51 @Nullable Bundle savedInstanceState) { 52 View v = inflater.inflate(R.layout.bluetooth_uuid, container, false); 53 54 mTabFragmentStateAdapter = new TabFragmentStateAdapter(this); 55 mViewPager = v.findViewById(R.id.pager); 56 mViewPager.setAdapter(mTabFragmentStateAdapter); 57 58 // Link the {@link TabLayout} to the {@link ViewPager2}, and attach it. 59 TabLayout tabLayout = v.findViewById(R.id.tab_layout); 60 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 61 new TabLayoutMediator(tabLayout, mViewPager, 62 (tab, position) -> tab.setText(mTabFragments.get(position).getText()) 63 ).attach(); 64 65 return v; 66 } 67 68 private final class FragmentTabEntry<T extends Fragment> { 69 private final String mTabText; 70 final Class<T> mFragmentClass; 71 private T mFragment = null; 72 FragmentTabEntry(String text, Class<T> clazz)73 FragmentTabEntry(String text, Class<T> clazz) { 74 mTabText = text; 75 mFragmentClass = clazz; 76 } 77 getText()78 String getText() { 79 return mTabText; 80 } 81 getFragment()82 T getFragment() { 83 if (mFragment == null) { 84 try { 85 mFragment = mFragmentClass.newInstance(); 86 } catch (java.lang.InstantiationException | java.lang.IllegalAccessException e) { 87 Log.e(TAG, "FragmentTabEntry unable to create fragment:", e); 88 } 89 } 90 return mFragment; 91 } 92 } 93 94 private final class TabFragmentStateAdapter extends FragmentStateAdapter { TabFragmentStateAdapter(Fragment fragment)95 TabFragmentStateAdapter(Fragment fragment) { 96 super(fragment); 97 } 98 99 @NonNull 100 @Override createFragment(int position)101 public Fragment createFragment(int position) { 102 Fragment fragment = mTabFragments.get(position).getFragment(); 103 return fragment; 104 } 105 106 @Override getItemCount()107 public int getItemCount() { 108 return mTabFragments.size(); 109 } 110 } 111 } 112