File indexing completed on 2024-12-22 04:41:39
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Art Pinch <leonardo906@mail.ru> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.MprisPlugin; 0008 0009 import androidx.annotation.NonNull; 0010 import androidx.collection.LongSparseArray; 0011 import androidx.fragment.app.Fragment; 0012 import androidx.fragment.app.FragmentActivity; 0013 import androidx.fragment.app.FragmentManager; 0014 import androidx.lifecycle.Lifecycle; 0015 import androidx.viewpager2.adapter.FragmentStateAdapter; 0016 0017 import java.lang.reflect.Field; 0018 0019 /** 0020 * Provides access to adapter fragments 0021 */ 0022 public abstract class ExtendedFragmentAdapter extends FragmentStateAdapter { 0023 0024 public ExtendedFragmentAdapter(@NonNull FragmentActivity fragmentActivity) { 0025 super(fragmentActivity); 0026 } 0027 0028 public ExtendedFragmentAdapter(@NonNull Fragment fragment) { 0029 super(fragment); 0030 } 0031 0032 public ExtendedFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) { 0033 super(fragmentManager, lifecycle); 0034 } 0035 0036 protected LongSparseArray<Fragment> getFragments() { 0037 try { 0038 Field fragmentsField = FragmentStateAdapter.class.getDeclaredField("mFragments"); 0039 fragmentsField.setAccessible(true); 0040 Object fieldData = fragmentsField.get(this); 0041 if (fieldData instanceof LongSparseArray) { 0042 //noinspection unchecked 0043 return (LongSparseArray<Fragment>) fieldData; 0044 } 0045 } catch (NoSuchFieldException | IllegalAccessException e) { 0046 e.printStackTrace(); 0047 } 0048 0049 return null; 0050 } 0051 0052 protected Fragment getFragment(int position) { 0053 LongSparseArray<Fragment> adapterFragments = getFragments(); 0054 if (adapterFragments == null) return null; 0055 0056 return adapterFragments.get(position); 0057 } 0058 }