2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 05:07:40 +00:00

Use view binding in MprisActivity.

This commit is contained in:
Isira Seneviratne 2020-10-23 06:29:17 +05:30 committed by Nicolas Fella
parent baa7032e9d
commit df1a9fb557
2 changed files with 53 additions and 108 deletions

View File

@ -19,6 +19,7 @@
<include <include
layout="@layout/mpris_control" layout="@layout/mpris_control"
android:id="@+id/mpris_control"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />

View File

@ -23,12 +23,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar; import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -46,13 +41,12 @@ import org.kde.kdeconnect.NetworkPacket;
import org.kde.kdeconnect.Plugins.SystemvolumePlugin.SystemvolumeFragment; import org.kde.kdeconnect.Plugins.SystemvolumePlugin.SystemvolumeFragment;
import org.kde.kdeconnect.UserInterface.ThemeUtil; import org.kde.kdeconnect.UserInterface.ThemeUtil;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import org.kde.kdeconnect_tp.databinding.ActivityMprisBinding;
import org.kde.kdeconnect_tp.databinding.MprisControlBinding;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.List; import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MprisActivity extends AppCompatActivity { public class MprisActivity extends AppCompatActivity {
private String deviceId; private String deviceId;
@ -60,54 +54,8 @@ public class MprisActivity extends AppCompatActivity {
private Runnable positionSeekUpdateRunnable = null; private Runnable positionSeekUpdateRunnable = null;
private MprisPlugin.MprisPlayer targetPlayer = null; private MprisPlugin.MprisPlayer targetPlayer = null;
@BindView(R.id.play_button) private ActivityMprisBinding activityMprisBinding;
ImageButton playButton; private MprisControlBinding mprisControlBinding;
@BindView(R.id.prev_button)
ImageButton prevButton;
@BindView(R.id.next_button)
ImageButton nextButton;
@BindView(R.id.rew_button)
ImageButton rewButton;
@BindView(R.id.ff_button)
ImageButton ffButton;
@BindView(R.id.time_textview)
TextView timeText;
@BindView(R.id.album_art)
ImageView albumArtView;
@BindView(R.id.player_spinner)
Spinner playerSpinner;
@BindView(R.id.no_players)
TextView noPlayers;
@BindView(R.id.now_playing_textview)
TextView nowPlayingText;
@BindView(R.id.positionSeek)
SeekBar positionBar;
@BindView(R.id.progress_slider)
LinearLayout progressSlider;
@BindView(R.id.volume_seek)
SeekBar volumeSeek;
@BindView(R.id.volume_layout)
LinearLayout volumeLayout;
@BindView(R.id.stop_button)
ImageButton stopButton;
@BindView(R.id.progress_textview)
TextView progressText;
private static String milisToProgress(long milis) { private static String milisToProgress(long milis) {
int length = (int) (milis / 1000); //From milis to seconds int length = (int) (milis / 1000); //From milis to seconds
@ -128,7 +76,6 @@ public class MprisActivity extends AppCompatActivity {
} }
private void connectToPlugin(final String targetPlayerName) { private void connectToPlugin(final String targetPlayerName) {
BackgroundService.RunWithPlugin(this, deviceId, MprisPlugin.class, mpris -> { BackgroundService.RunWithPlugin(this, deviceId, MprisPlugin.class, mpris -> {
targetPlayer = mpris.getPlayerStatus(targetPlayerName); targetPlayer = mpris.getPlayerStatus(targetPlayerName);
@ -152,19 +99,18 @@ public class MprisActivity extends AppCompatActivity {
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
runOnUiThread(() -> { runOnUiThread(() -> {
mprisControlBinding.playerSpinner.setAdapter(adapter);
playerSpinner.setAdapter(adapter);
if (playerList.isEmpty()) { if (playerList.isEmpty()) {
noPlayers.setVisibility(View.VISIBLE); mprisControlBinding.noPlayers.setVisibility(View.VISIBLE);
playerSpinner.setVisibility(View.GONE); mprisControlBinding.playerSpinner.setVisibility(View.GONE);
nowPlayingText.setText(""); mprisControlBinding.nowPlayingTextview.setText("");
} else { } else {
noPlayers.setVisibility(View.GONE); mprisControlBinding.noPlayers.setVisibility(View.GONE);
playerSpinner.setVisibility(View.VISIBLE); mprisControlBinding.playerSpinner.setVisibility(View.VISIBLE);
} }
playerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { mprisControlBinding.playerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) { public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) {
@ -196,7 +142,7 @@ public class MprisActivity extends AppCompatActivity {
if (targetPlayer != null) { if (targetPlayer != null) {
int targetIndex = adapter.getPosition(targetPlayer.getPlayer()); int targetIndex = adapter.getPosition(targetPlayer.getPlayer());
if (targetIndex >= 0) { if (targetIndex >= 0) {
playerSpinner.setSelection(targetIndex); mprisControlBinding.playerSpinner.setSelection(targetIndex);
} else { } else {
targetPlayer = null; targetPlayer = null;
} }
@ -204,18 +150,16 @@ public class MprisActivity extends AppCompatActivity {
//If no player selected, select the first one (if any) //If no player selected, select the first one (if any)
if (targetPlayer == null && !playerList.isEmpty()) { if (targetPlayer == null && !playerList.isEmpty()) {
targetPlayer = mpris.getPlayerStatus(playerList.get(0)); targetPlayer = mpris.getPlayerStatus(playerList.get(0));
playerSpinner.setSelection(0); mprisControlBinding.playerSpinner.setSelection(0);
} }
updatePlayerStatus(mpris); updatePlayerStatus(mpris);
}); });
} }
}); });
}); });
} }
private void addSystemVolumeFragment() { private void addSystemVolumeFragment() {
if (findViewById(R.id.systemvolume_fragment) == null) if (findViewById(R.id.systemvolume_fragment) == null)
return; return;
@ -249,8 +193,8 @@ public class MprisActivity extends AppCompatActivity {
} }
String song = playerStatus.getCurrentSong(); String song = playerStatus.getCurrentSong();
if (!nowPlayingText.getText().toString().equals(song)) { if (!mprisControlBinding.nowPlayingTextview.getText().toString().equals(song)) {
nowPlayingText.setText(song); mprisControlBinding.nowPlayingTextview.setText(song);
} }
Bitmap albumArt = playerStatus.getAlbumArt(); Bitmap albumArt = playerStatus.getAlbumArt();
@ -259,47 +203,47 @@ public class MprisActivity extends AppCompatActivity {
assert drawable != null; assert drawable != null;
Drawable placeholder_art = DrawableCompat.wrap(drawable); Drawable placeholder_art = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(placeholder_art, ContextCompat.getColor(this, R.color.primary)); DrawableCompat.setTint(placeholder_art, ContextCompat.getColor(this, R.color.primary));
albumArtView.setImageDrawable(placeholder_art); activityMprisBinding.albumArt.setImageDrawable(placeholder_art);
} else { } else {
albumArtView.setImageBitmap(albumArt); activityMprisBinding.albumArt.setImageBitmap(albumArt);
} }
if (playerStatus.isSeekAllowed()) { if (playerStatus.isSeekAllowed()) {
timeText.setText(milisToProgress(playerStatus.getLength())); mprisControlBinding.timeTextview.setText(milisToProgress(playerStatus.getLength()));
positionBar.setMax((int) (playerStatus.getLength())); mprisControlBinding.positionSeek.setMax((int) (playerStatus.getLength()));
positionBar.setProgress((int) (playerStatus.getPosition())); mprisControlBinding.positionSeek.setProgress((int) (playerStatus.getPosition()));
progressSlider.setVisibility(View.VISIBLE); mprisControlBinding.progressSlider.setVisibility(View.VISIBLE);
} else { } else {
progressSlider.setVisibility(View.GONE); mprisControlBinding.progressSlider.setVisibility(View.GONE);
} }
int volume = playerStatus.getVolume(); int volume = playerStatus.getVolume();
volumeSeek.setProgress(volume); mprisControlBinding.volumeSeek.setProgress(volume);
boolean isPlaying = playerStatus.isPlaying(); boolean isPlaying = playerStatus.isPlaying();
if (isPlaying) { if (isPlaying) {
playButton.setImageResource(R.drawable.ic_pause_black); mprisControlBinding.playButton.setImageResource(R.drawable.ic_pause_black);
playButton.setEnabled(playerStatus.isPauseAllowed()); mprisControlBinding.playButton.setEnabled(playerStatus.isPauseAllowed());
} else { } else {
playButton.setImageResource(R.drawable.ic_play_black); mprisControlBinding.playButton.setImageResource(R.drawable.ic_play_black);
playButton.setEnabled(playerStatus.isPlayAllowed()); mprisControlBinding.playButton.setEnabled(playerStatus.isPlayAllowed());
} }
volumeLayout.setVisibility(playerStatus.isSetVolumeAllowed() ? View.VISIBLE : View.GONE); mprisControlBinding.volumeLayout.setVisibility(playerStatus.isSetVolumeAllowed() ? View.VISIBLE : View.GONE);
rewButton.setVisibility(playerStatus.isSeekAllowed() ? View.VISIBLE : View.GONE); mprisControlBinding.rewButton.setVisibility(playerStatus.isSeekAllowed() ? View.VISIBLE : View.GONE);
ffButton.setVisibility(playerStatus.isSeekAllowed() ? View.VISIBLE : View.GONE); mprisControlBinding.ffButton.setVisibility(playerStatus.isSeekAllowed() ? View.VISIBLE : View.GONE);
invalidateOptionsMenu(); invalidateOptionsMenu();
//Show and hide previous/next buttons simultaneously //Show and hide previous/next buttons simultaneously
if (playerStatus.isGoPreviousAllowed() || playerStatus.isGoNextAllowed()) { if (playerStatus.isGoPreviousAllowed() || playerStatus.isGoNextAllowed()) {
prevButton.setVisibility(View.VISIBLE); mprisControlBinding.prevButton.setVisibility(View.VISIBLE);
prevButton.setEnabled(playerStatus.isGoPreviousAllowed()); mprisControlBinding.prevButton.setEnabled(playerStatus.isGoPreviousAllowed());
nextButton.setVisibility(View.VISIBLE); mprisControlBinding.nextButton.setVisibility(View.VISIBLE);
nextButton.setEnabled(playerStatus.isGoNextAllowed()); mprisControlBinding.nextButton.setEnabled(playerStatus.isGoNextAllowed());
} else { } else {
prevButton.setVisibility(View.GONE); mprisControlBinding.prevButton.setVisibility(View.GONE);
nextButton.setVisibility(View.GONE); mprisControlBinding.nextButton.setVisibility(View.GONE);
} }
} }
@ -366,8 +310,11 @@ public class MprisActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ThemeUtil.setUserPreferredTheme(this); ThemeUtil.setUserPreferredTheme(this);
setContentView(R.layout.activity_mpris);
ButterKnife.bind(this); activityMprisBinding = ActivityMprisBinding.inflate(getLayoutInflater());
mprisControlBinding = activityMprisBinding.mprisControl;
setContentView(activityMprisBinding.getRoot());
String targetPlayerName = getIntent().getStringExtra("player"); String targetPlayerName = getIntent().getStringExtra("player");
getIntent().removeExtra("player"); getIntent().removeExtra("player");
@ -388,19 +335,19 @@ public class MprisActivity extends AppCompatActivity {
BackgroundService.RunCommand(MprisActivity.this, service -> service.addConnectionListener(connectionReceiver)); BackgroundService.RunCommand(MprisActivity.this, service -> service.addConnectionListener(connectionReceiver));
connectToPlugin(targetPlayerName); connectToPlugin(targetPlayerName);
performActionOnClick(playButton, MprisPlugin.MprisPlayer::playPause); performActionOnClick(mprisControlBinding.playButton, MprisPlugin.MprisPlayer::playPause);
performActionOnClick(prevButton, MprisPlugin.MprisPlayer::previous); performActionOnClick(mprisControlBinding.prevButton, MprisPlugin.MprisPlayer::previous);
performActionOnClick(rewButton, p -> targetPlayer.seek(interval_time * -1)); performActionOnClick(mprisControlBinding.rewButton, p -> targetPlayer.seek(interval_time * -1));
performActionOnClick(ffButton, p -> p.seek(interval_time)); performActionOnClick(mprisControlBinding.ffButton, p -> p.seek(interval_time));
performActionOnClick(nextButton, MprisPlugin.MprisPlayer::next); performActionOnClick(mprisControlBinding.nextButton, MprisPlugin.MprisPlayer::next);
performActionOnClick(stopButton, MprisPlugin.MprisPlayer::stop); performActionOnClick(mprisControlBinding.stopButton, MprisPlugin.MprisPlayer::stop);
volumeSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { mprisControlBinding.volumeSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
} }
@ -416,22 +363,21 @@ public class MprisActivity extends AppCompatActivity {
targetPlayer.setVolume(seekBar.getProgress()); targetPlayer.setVolume(seekBar.getProgress());
}); });
} }
}); });
positionSeekUpdateRunnable = () -> BackgroundService.RunCommand(MprisActivity.this, service -> { positionSeekUpdateRunnable = () -> BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer != null) { if (targetPlayer != null) {
positionBar.setProgress((int) (targetPlayer.getPosition())); mprisControlBinding.positionSeek.setProgress((int) (targetPlayer.getPosition()));
} }
positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable);
positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 1000); positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 1000);
}); });
positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200); positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200);
positionBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { mprisControlBinding.positionSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean byUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean byUser) {
progressText.setText(milisToProgress(progress)); mprisControlBinding.progressTextview.setText(milisToProgress(progress));
} }
@Override @Override
@ -448,13 +394,11 @@ public class MprisActivity extends AppCompatActivity {
positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200); positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200);
}); });
} }
}); });
nowPlayingText.setSelected(true); mprisControlBinding.nowPlayingTextview.setSelected(true);
} }
final static int MENU_OPEN_URL = Menu.FIRST; final static int MENU_OPEN_URL = Menu.FIRST;
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {