Merge pull request #573 from kabouzeid/notch-support

Notch support via WindowInsets
This commit is contained in:
Karim Abou Zeid 2018-08-27 08:32:30 +02:00 committed by GitHub
commit 9d0e84bf8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 206 additions and 182 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.folders.FoldersFragment;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.LibraryFragment; import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.LibraryFragment;
import com.kabouzeid.gramophone.util.PreferenceUtil; import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.kabouzeid.gramophone.util.Util;
import com.sothree.slidinguppanel.SlidingUpPanelLayout; import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import java.util.ArrayList; import java.util.ArrayList;
@ -76,19 +75,11 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setDrawUnderStatusbar(true);
ButterKnife.bind(this); ButterKnife.bind(this);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Util.setStatusBarTranslucent(getWindow()); navigationView.setFitsSystemWindows(false); // for header to go below statusbar
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);
});
} }
setUpDrawerLayout(); setUpDrawerLayout();

View file

@ -1,13 +1,8 @@
package com.kabouzeid.gramophone.ui.fragments.mainactivity; package com.kabouzeid.gramophone.ui.fragments.mainactivity;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; 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; import com.kabouzeid.gramophone.ui.activities.MainActivity;
/** /**
@ -24,24 +19,4 @@ public abstract class AbsMainActivityFragment extends Fragment {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true); 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 @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
setStatusbarColorAuto(view); getMainActivity().setStatusbarColorAuto();
getMainActivity().setNavigationbarColorAuto(); getMainActivity().setNavigationbarColorAuto();
getMainActivity().setTaskDescriptionColorAuto(); getMainActivity().setTaskDescriptionColorAuto();

View file

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

View file

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

@ -0,0 +1,34 @@
package com.kabouzeid.gramophone.views;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
public class StatusBarView extends View {
public StatusBarView(Context context) {
super(context);
}
public StatusBarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StatusBarView(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) {
ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = insets.getSystemWindowInsetTop();
setLayoutParams(lp);
}
return super.onApplyWindowInsets(insets);
}
}

View file

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

View file

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

View file

@ -1,5 +1,22 @@
<?xml version="1.0" encoding="utf-8"?> <?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:id="@+id/fragment_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
</LinearLayout>

View file

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

View file

@ -6,8 +6,7 @@
<!--The unused FrameLayout layout is necessary to prevent a crash on rotate--> <!--The unused FrameLayout layout is necessary to prevent a crash on rotate-->
<com.sothree.slidinguppanel.SlidingUpPanelLayout <com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/player_sliding_layout" android:id="@+id/player_sliding_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -65,29 +64,42 @@
<include layout="@layout/shadow_statusbar_toolbar" /> <include layout="@layout/shadow_statusbar_toolbar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.kabouzeid.gramophone.views.StatusBarView
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_padding" />
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar
android:id="@+id/player_toolbar" android:id="@+id/player_toolbar"
style="@style/Toolbar" style="@style/Toolbar"
android:layout_marginTop="@dimen/status_bar_padding"
android:background="@android:color/transparent" /> android:background="@android:color/transparent" />
</LinearLayout>
</FrameLayout> </FrameLayout>
</FrameLayout> </FrameLayout>
<FrameLayout <com.kabouzeid.gramophone.views.StatusBarMarginFrameLayout
android:id="@+id/player_panel" android:id="@+id/player_panel"
android:layout_width="match_parent" 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.support.v7.widget.CardView
android:id="@+id/playing_queue_card" android:id="@+id/playing_queue_card"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:layout_marginBottom="-8dp"
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
android:layout_marginTop="@dimen/status_bar_padding" android:layout_marginTop="8dp"
app:cardBackgroundColor="?cardBackgroundColor" app:cardBackgroundColor="?cardBackgroundColor"
app:cardElevation="@dimen/card_elevation" app:cardElevation="@dimen/card_elevation"
app:cardUseCompatPadding="false"> app:cardUseCompatPadding="false">
@ -139,7 +151,7 @@
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
</FrameLayout> </com.kabouzeid.gramophone.views.StatusBarMarginFrameLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout> </com.sothree.slidinguppanel.SlidingUpPanelLayout>

View file

@ -8,12 +8,12 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<View <com.kabouzeid.gramophone.views.StatusBarView
android:id="@+id/player_status_bar" android:id="@+id/player_status_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_padding" /> android:layout_height="@dimen/status_bar_padding" />
<View <com.kabouzeid.gramophone.views.StatusBarView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_padding" android:layout_height="@dimen/status_bar_padding"
android:background="@color/twenty_percent_black_overlay" /> android:background="@color/twenty_percent_black_overlay" />

View file

@ -1,26 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" 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:id="@+id/coordinator_layout"
android:layout_width="match_parent" 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.support.design.widget.AppBarLayout
android:id="@+id/appbar" android:id="@+id/appbar"
@ -85,5 +70,3 @@
</FrameLayout> </FrameLayout>
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

View file

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

View file

@ -10,7 +10,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<View <com.kabouzeid.gramophone.views.StatusBarView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_padding" /> android:layout_height="@dimen/status_bar_padding" />

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android" <com.kabouzeid.gramophone.views.StatusBarView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/status_bar" android:id="@+id/status_bar"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

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