Eleven: Replace ViewPager with ViewPager2
We can replace SquareFrame and SquareViewPager by just using a
Constraintlayout and use ViewPager2 instead of ViewPager and
FragmentStateAdapter instead of FragmentStatePagerAdapter
Change-Id: Ie3816a3d907346cfb8fa93bc511b1173b10ab97d
diff --git a/Android.bp b/Android.bp
index c07ea54..9e49821 100644
--- a/Android.bp
+++ b/Android.bp
@@ -34,6 +34,7 @@
"androidx.palette_palette",
"androidx.preference_preference",
"androidx.recyclerview_recyclerview",
+ "androidx.viewpager2_viewpager2"
],
overrides: ["Music"],
diff --git a/res/layout/activity_player_fragment.xml b/res/layout/activity_player_fragment.xml
index 75b46ff..0d03187 100644
--- a/res/layout/activity_player_fragment.xml
+++ b/res/layout/activity_player_fragment.xml
@@ -39,11 +39,12 @@
<include
android:id="@+id/main_album_flow"
layout="@layout/main_album_flow"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
+ android:layout_width="0dp"
+ android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintDimensionRatio="1:1"/>
<org.lineageos.eleven.widgets.MainPlaybackControls
android:id="@+id/main_playback_controls"
diff --git a/res/layout/main_album_flow.xml b/res/layout/main_album_flow.xml
index 5411b06..f9acbe9 100644
--- a/res/layout/main_album_flow.xml
+++ b/res/layout/main_album_flow.xml
@@ -15,44 +15,64 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
-<org.lineageos.eleven.widgets.SquareFrame
+<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- tools:background="@drawable/default_artwork">
+ tools:background="@drawable/default_artwork"
+ app:layout_constraintDimensionRatio="1:1">
- <org.lineageos.eleven.widgets.SquareViewPager
+ <androidx.viewpager2.widget.ViewPager2
android:id="@+id/audio_player_album_art_viewpager"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
+ android:layout_width="0dp"
+ android:layout_height="0dp"
+ app:layout_constraintDimensionRatio="1:1"
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintEnd_toEndOf="parent" />
<include
layout="@layout/loading_empty_container"
- tools:visibility="gone" />
+ tools:visibility="gone"
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintEnd_toEndOf="parent" />
<org.lineageos.eleven.widgets.VisualizerView
android:id="@+id/visualizerView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
+ android:layout_width="0dp"
+ android:layout_height="0dp"
android:layout_gravity="bottom"
android:gravity="bottom"
android:visibility="visible"
- tools:visibility="gone" />
+ tools:visibility="gone"
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/audio_player_lyrics"
- android:layout_width="fill_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:alpha="0.0"
android:background="@color/lyrics_background_color"
android:gravity="center"
android:minHeight="66dp"
- android:paddingBottom="6dp"
android:paddingLeft="15dp"
- android:paddingRight="15dp"
android:paddingTop="6dp"
+ android:paddingRight="15dp"
+ android:paddingBottom="6dp"
android:textColor="@color/audio_player_lyrics_text_color"
- android:textSize="@dimen/text_size_small" />
-</org.lineageos.eleven.widgets.SquareFrame>
+ android:textSize="@dimen/text_size_small"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="parent"
+ tools:text="Abcd" />
+</androidx.constraintlayout.widget.ConstraintLayout>
diff --git a/src/org/lineageos/eleven/adapters/AlbumArtPagerAdapter.java b/src/org/lineageos/eleven/adapters/AlbumArtPagerAdapter.java
index 7d54d48..7b4bdae 100644
--- a/src/org/lineageos/eleven/adapters/AlbumArtPagerAdapter.java
+++ b/src/org/lineageos/eleven/adapters/AlbumArtPagerAdapter.java
@@ -29,8 +29,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
-import androidx.fragment.app.FragmentManager;
-import androidx.fragment.app.FragmentStatePagerAdapter;
+import androidx.viewpager2.adapter.FragmentStateAdapter;
import org.lineageos.eleven.BuildConstants;
import org.lineageos.eleven.MusicPlaybackService;
@@ -47,9 +46,9 @@
import java.util.concurrent.Executors;
/**
- * A {@link androidx.fragment.app.FragmentStatePagerAdapter} class for swiping between album art
+ * A {@link androidx.viewpager2.adapter.FragmentStateAdapter} class for swiping between album art
*/
-public class AlbumArtPagerAdapter extends FragmentStatePagerAdapter {
+public class AlbumArtPagerAdapter extends FragmentStateAdapter {
private static final boolean DEBUG = false;
private static final String TAG = AlbumArtPagerAdapter.class.getSimpleName();
@@ -99,25 +98,27 @@
// the length of the playlist
private int mPlaylistLen = 0;
- public AlbumArtPagerAdapter(FragmentManager fm) {
- super(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
+ public AlbumArtPagerAdapter(Fragment f) {
+ super(f);
}
- @Override
+
@NonNull
- public Fragment getItem(final int position) {
+ @Override
+ public Fragment createFragment(int position) {
long trackID = getTrackId(position);
return AlbumArtFragment.newInstance(trackID);
}
@Override
- public int getCount() {
+ public int getItemCount() {
return mPlaylistLen;
}
public void setPlaylistLength(final int len) {
+ notifyItemRangeChanged(0, mPlaylistLen);
mPlaylistLen = len;
- notifyDataSetChanged();
+ notifyItemRangeChanged(0, mPlaylistLen);
}
/**
@@ -198,11 +199,6 @@
}
@Override
- public void onDestroy() {
- super.onDestroy();
- }
-
- @Override
public void onDestroyView() {
super.onDestroyView();
@@ -242,7 +238,6 @@
mTask = new AlbumArtistLoader(this, getActivity());
mTask.execute(mAudioId);
}
-
}
/**
diff --git a/src/org/lineageos/eleven/widgets/SquareFrame.java b/src/org/lineageos/eleven/widgets/SquareFrame.java
deleted file mode 100644
index cb6303b..0000000
--- a/src/org/lineageos/eleven/widgets/SquareFrame.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2021 The LineageOS Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */package org.lineageos.eleven.widgets;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.widget.FrameLayout;
-
-public class SquareFrame extends FrameLayout {
- public SquareFrame(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- public void onMeasure(final int widthSpec, final int heightSpec) {
- super.onMeasure(widthSpec, heightSpec);
- final int mSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
- setMeasuredDimension(mSize, mSize);
- }
-}
diff --git a/src/org/lineageos/eleven/widgets/SquareViewPager.java b/src/org/lineageos/eleven/widgets/SquareViewPager.java
deleted file mode 100644
index 9ab8a8c..0000000
--- a/src/org/lineageos/eleven/widgets/SquareViewPager.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2014 The CyanogenMod Project
- * Copyright (C) 2021 The LineageOS Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */package org.lineageos.eleven.widgets;
-
-import android.content.Context;
-import android.util.AttributeSet;
-
-import androidx.viewpager.widget.ViewPager;
-
-/**
- * A custom {@link ViewPager} that is sized to be a perfect square, otherwise
- * functions like a typical {@link ViewPager}.
- */
-public class SquareViewPager extends ViewPager {
-
- /**
- * @param context The {@link Context} to use
- * @param attrs The attributes of the XML tag that is inflating the view.
- */
- public SquareViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- final int mSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
- setMeasuredDimension(mSize, mSize);
- }
-}