File indexing completed on 2024-12-22 04:41:39
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com> 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 android.os.Bundle; 0010 import android.view.KeyEvent; 0011 0012 import androidx.annotation.NonNull; 0013 import androidx.annotation.StringRes; 0014 import androidx.appcompat.app.AppCompatActivity; 0015 import androidx.fragment.app.Fragment; 0016 import androidx.fragment.app.FragmentActivity; 0017 0018 import com.google.android.material.tabs.TabLayoutMediator; 0019 0020 import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeFragment; 0021 import org.kde.kdeconnect_tp.R; 0022 import org.kde.kdeconnect_tp.databinding.ActivityMprisBinding; 0023 0024 import java.util.Objects; 0025 0026 public class MprisActivity extends AppCompatActivity { 0027 0028 private ActivityMprisBinding activityMprisBinding; 0029 private MprisPagerAdapter mprisPagerAdapter; 0030 0031 @Override 0032 public boolean onKeyDown(int keyCode, KeyEvent event) { 0033 switch (keyCode) { 0034 case KeyEvent.KEYCODE_VOLUME_UP: 0035 if (activityMprisBinding != null && mprisPagerAdapter != null) { 0036 int pagePosition = activityMprisBinding.mprisTabs.getSelectedTabPosition(); 0037 mprisPagerAdapter.onVolumeUp(pagePosition); 0038 } 0039 return true; 0040 case KeyEvent.KEYCODE_VOLUME_DOWN: 0041 if (activityMprisBinding != null && mprisPagerAdapter != null) { 0042 int pagePosition = activityMprisBinding.mprisTabs.getSelectedTabPosition(); 0043 mprisPagerAdapter.onVolumeDown(pagePosition); 0044 } 0045 return true; 0046 default: 0047 return super.onKeyDown(keyCode, event); 0048 } 0049 } 0050 0051 @Override 0052 public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { 0053 switch (keyCode) { 0054 case KeyEvent.KEYCODE_VOLUME_UP: 0055 case KeyEvent.KEYCODE_VOLUME_DOWN: 0056 return true; 0057 default: 0058 return super.onKeyUp(keyCode, event); 0059 } 0060 } 0061 0062 @Override 0063 protected void onCreate(Bundle savedInstanceState) { 0064 super.onCreate(savedInstanceState); 0065 0066 activityMprisBinding = ActivityMprisBinding.inflate(getLayoutInflater()); 0067 0068 setContentView(activityMprisBinding.getRoot()); 0069 0070 String deviceId = getIntent().getStringExtra(MprisPlugin.DEVICE_ID_KEY); 0071 0072 mprisPagerAdapter = new MprisPagerAdapter(this, deviceId); 0073 activityMprisBinding.mprisPager.setAdapter(mprisPagerAdapter); 0074 0075 TabLayoutMediator tabLayoutMediator = new TabLayoutMediator( 0076 activityMprisBinding.mprisTabs, 0077 activityMprisBinding.mprisPager, 0078 (tab, position) -> tab.setText(mprisPagerAdapter.getTitle(position)) 0079 ); 0080 0081 activityMprisBinding.mprisTabs.getSelectedTabPosition(); 0082 0083 tabLayoutMediator.attach(); 0084 0085 setSupportActionBar(activityMprisBinding.toolbar); 0086 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); 0087 } 0088 0089 static class MprisPagerAdapter extends ExtendedFragmentAdapter { 0090 0091 private final String deviceId; 0092 0093 public MprisPagerAdapter(@NonNull FragmentActivity fragmentActivity, String deviceId) { 0094 super(fragmentActivity); 0095 this.deviceId = deviceId; 0096 } 0097 0098 @NonNull 0099 @Override 0100 public Fragment createFragment(int position) { 0101 if (position == 1) { 0102 return SystemVolumeFragment.newInstance(deviceId); 0103 } else { 0104 return MprisNowPlayingFragment.newInstance(deviceId); 0105 } 0106 } 0107 0108 @Override 0109 public int getItemCount() { 0110 return 2; 0111 } 0112 0113 @StringRes 0114 int getTitle(int position) { 0115 if (position == 1) { 0116 return R.string.devices; 0117 } else { 0118 return R.string.mpris_play; 0119 } 0120 } 0121 0122 void onVolumeUp(int page) { 0123 Fragment requestedFragment = getFragment(page); 0124 if (requestedFragment == null) return; 0125 0126 if (requestedFragment instanceof VolumeKeyListener) { 0127 ((VolumeKeyListener) requestedFragment).onVolumeUp(); 0128 } 0129 } 0130 0131 void onVolumeDown(int page) { 0132 Fragment requestedFragment = getFragment(page); 0133 if (requestedFragment == null) return; 0134 0135 if (requestedFragment instanceof VolumeKeyListener) { 0136 ((VolumeKeyListener) requestedFragment).onVolumeDown(); 0137 } 0138 } 0139 } 0140 0141 @Override 0142 public boolean onSupportNavigateUp() { 0143 super.onBackPressed(); 0144 return true; 0145 } 0146 }