Notch support via WindowInsets

This commit is contained in:
Karim Abou Zeid 2018-08-25 15:14:35 +02:00
commit 6464307c7d
15 changed files with 161 additions and 188 deletions

View file

@ -43,7 +43,6 @@ import com.kabouzeid.gramophone.ui.activities.intro.AppIntroActivity;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.folders.FoldersFragment;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.LibraryFragment;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.kabouzeid.gramophone.util.Util;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import java.util.ArrayList;
@ -76,19 +75,11 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDrawUnderStatusbar(true);
ButterKnife.bind(this);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Util.setStatusBarTranslucent(getWindow());
drawerLayout.setFitsSystemWindows(false);
navigationView.setFitsSystemWindows(false);
//noinspection ConstantConditions
findViewById(R.id.drawer_content_container).setFitsSystemWindows(false);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawerLayout.setOnApplyWindowInsetsListener((view, windowInsets) -> {
navigationView.dispatchApplyWindowInsets(windowInsets);
return windowInsets.replaceSystemWindowInsets(0, 0, 0, 0);
});
navigationView.setFitsSystemWindows(false); // for header to go below statusbar
}
setUpDrawerLayout();

View file

@ -1,13 +1,8 @@
package com.kabouzeid.gramophone.ui.fragments.mainactivity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import com.kabouzeid.appthemehelper.ThemeStore;
import com.kabouzeid.appthemehelper.util.ColorUtil;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.ui.activities.MainActivity;
/**
@ -24,24 +19,4 @@ public abstract class AbsMainActivityFragment extends Fragment {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
// WORKAROUND
public void setStatusbarColor(View view, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final View statusBar = view.findViewById(R.id.status_bar);
if (statusBar != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statusBar.setBackgroundColor(ColorUtil.darkenColor(color));
getMainActivity().setLightStatusbarAuto(color);
} else {
statusBar.setBackgroundColor(color);
}
}
}
}
public void setStatusbarColorAuto(View view) {
// we don't want to use statusbar color because we are doing the color darkening on our own to support KitKat
setStatusbarColor(view, ThemeStore.primaryColor(getContext()));
}
}

View file

@ -156,7 +156,7 @@ public class FoldersFragment extends AbsMainActivityFragment implements MainActi
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
setStatusbarColorAuto(view);
getMainActivity().setStatusbarColorAuto();
getMainActivity().setNavigationbarColorAuto();
getMainActivity().setTaskDescriptionColorAuto();

View file

@ -90,7 +90,7 @@ public class LibraryFragment extends AbsMainActivityFragment implements CabHolde
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
PreferenceUtil.getInstance(getActivity()).registerOnSharedPreferenceChangedListener(this);
setStatusbarColorAuto(view);
getMainActivity().setStatusbarColorAuto();
getMainActivity().setNavigationbarColorAuto();
getMainActivity().setTaskDescriptionColorAuto();

View file

@ -463,10 +463,9 @@ public class CardPlayerFragment extends AbsPlayerFragment implements PlayerAlbum
public AnimatorSet createDefaultColorChangeAnimatorSet(int newColor) {
Animator backgroundAnimator;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int topMargin = fragment.getResources().getDimensionPixelSize(R.dimen.status_bar_padding);
//noinspection ConstantConditions
int x = (int) (fragment.playbackControlsFragment.playPauseFab.getX() + fragment.playbackControlsFragment.playPauseFab.getWidth() / 2 + fragment.playbackControlsFragment.getView().getX());
int y = (int) (topMargin + fragment.playbackControlsFragment.playPauseFab.getY() + fragment.playbackControlsFragment.playPauseFab.getHeight() / 2 + fragment.playbackControlsFragment.getView().getY());
int y = (int) (fragment.playbackControlsFragment.playPauseFab.getY() + fragment.playbackControlsFragment.playPauseFab.getHeight() / 2 + fragment.playbackControlsFragment.getView().getY() + fragment.playbackControlsFragment.progressSlider.getHeight());
float startRadius = Math.max(fragment.playbackControlsFragment.playPauseFab.getWidth() / 2, fragment.playbackControlsFragment.playPauseFab.getHeight() / 2);
float endRadius = Math.max(fragment.colorBackground.getWidth(), fragment.colorBackground.getHeight());
fragment.colorBackground.setBackgroundColor(newColor);
@ -552,10 +551,9 @@ public class CardPlayerFragment extends AbsPlayerFragment implements PlayerAlbum
@Override
public void setUpPanelAndAlbumCoverHeight() {
WidthFitSquareLayout albumCoverContainer = fragment.getView().findViewById(R.id.album_cover_container);
int topMargin = fragment.getResources().getDimensionPixelSize(R.dimen.status_bar_padding);
final int availablePanelHeight = fragment.slidingUpPanelLayout.getHeight() - fragment.getView().findViewById(R.id.player_content).getHeight() + topMargin;
final int minPanelHeight = (int) ViewUtil.convertDpToPixel(72 + 24, fragment.getResources()) + topMargin;
final int availablePanelHeight = fragment.slidingUpPanelLayout.getHeight() - fragment.getView().findViewById(R.id.player_content).getHeight() + (int) ViewUtil.convertDpToPixel(8, fragment.getResources());
final int minPanelHeight = (int) ViewUtil.convertDpToPixel(72 + 24, fragment.getResources());
if (availablePanelHeight < minPanelHeight) {
albumCoverContainer.getLayoutParams().height = albumCoverContainer.getHeight() - (minPanelHeight - availablePanelHeight);
albumCoverContainer.forceSquare(false);
@ -595,8 +593,7 @@ public class CardPlayerFragment extends AbsPlayerFragment implements PlayerAlbum
@Override
public void setUpPanelAndAlbumCoverHeight() {
int topMargin = fragment.getResources().getDimensionPixelSize(R.dimen.status_bar_padding);
int panelHeight = fragment.slidingUpPanelLayout.getHeight() - fragment.playbackControlsFragment.getView().getHeight() + topMargin;
int panelHeight = fragment.slidingUpPanelLayout.getHeight() - fragment.playbackControlsFragment.getView().getHeight();
fragment.slidingUpPanelLayout.setPanelHeight(panelHeight);
((AbsSlidingMusicPanelActivity) fragment.getActivity()).setAntiDragView(fragment.slidingUpPanelLayout.findViewById(R.id.player_panel));

View file

@ -0,0 +1,33 @@
package com.kabouzeid.gramophone.views;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.WindowInsets;
import android.widget.FrameLayout;
public class StatusBarMarginFrameLayout extends FrameLayout {
public StatusBarMarginFrameLayout(Context context) {
super(context);
}
public StatusBarMarginFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StatusBarMarginFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
lp.topMargin = insets.getSystemWindowInsetTop();
setLayoutParams(lp);
}
return super.onApplyWindowInsets(insets);
}
}

View file

@ -1,13 +1,11 @@
package com.kabouzeid.gramophone.views;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
/*
This is a temporally fix
*/
import android.view.ViewGroup;
import android.view.WindowInsets;
public class StatusBarView extends View {
@ -25,17 +23,12 @@ public class StatusBarView extends View {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
MeasureSpec.getSize(widthMeasureSpec), getStatusBarHeight(getResources()));
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = insets.getSystemWindowInsetTop();
setLayoutParams(lp);
}
public static int getStatusBarHeight(Resources r) {
int result = 0;
int resourceId = r.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = r.getDimensionPixelSize(resourceId);
}
return result;
return super.onApplyWindowInsets(insets);
}
}

View file

@ -70,6 +70,7 @@
</FrameLayout>
<FrameLayout
android:layout_marginTop="24dp"
android:id="@+id/player_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
@ -78,10 +79,10 @@
android:id="@+id/playing_queue_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginBottom="-8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="@dimen/status_bar_padding"
android:layout_marginTop="0dp"
app:cardBackgroundColor="?cardBackgroundColor"
app:cardElevation="@dimen/card_elevation"
app:cardUseCompatPadding="false">

View file

@ -11,12 +11,12 @@
android:elevation="@dimen/toolbar_elevation"
tools:ignore="UnusedAttribute">
<View
<com.kabouzeid.gramophone.views.StatusBarView
android:id="@+id/player_status_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_padding" />
<View
<com.kabouzeid.gramophone.views.StatusBarView
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_padding"
android:background="@color/twenty_percent_black_overlay" />

View file

@ -1,5 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
tools:ignore="UnusedAttribute">
<include layout="@layout/status_bar" />
</FrameLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View file

@ -3,14 +3,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
android:layout_height="match_parent">
<android.support.design.internal.ScrimInsetsFrameLayout
<FrameLayout
android:id="@+id/drawer_content_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" />
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"

View file

@ -71,7 +71,7 @@
<com.kabouzeid.gramophone.views.StatusBarView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="@dimen/status_bar_padding" />
<android.support.v7.widget.Toolbar
android:id="@+id/player_toolbar"
@ -85,19 +85,21 @@
</FrameLayout>
<FrameLayout
<com.kabouzeid.gramophone.views.StatusBarMarginFrameLayout
android:id="@+id/player_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_padding"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/playing_queue_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginBottom="-8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="@dimen/status_bar_padding"
android:layout_marginTop="8dp"
app:cardBackgroundColor="?cardBackgroundColor"
app:cardElevation="@dimen/card_elevation"
app:cardUseCompatPadding="false">
@ -149,7 +151,7 @@
</android.support.v7.widget.CardView>
</FrameLayout>
</com.kabouzeid.gramophone.views.StatusBarMarginFrameLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

View file

@ -1,26 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:orientation="vertical"
tools:context=".ui.fragments.mainactivity.folders.FoldersFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
tools:ignore="UnusedAttribute">
<include layout="@layout/status_bar" />
</FrameLayout>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
tools:context=".ui.fragments.mainactivity.folders.FoldersFragment">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
@ -85,5 +70,3 @@
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>

View file

@ -1,26 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:orientation="vertical"
tools:context=".ui.fragments.mainactivity.library.LibraryFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
tools:ignore="UnusedAttribute">
<include layout="@layout/status_bar" />
</FrameLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
@ -63,5 +48,3 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>

View file

@ -4,7 +4,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.1.4'
}
}