Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90c8f47a authored by Selim Cinek's avatar Selim Cinek
Browse files

Adapted the layout for when the header would overflow

Now first the App name shrinks up to a minumum and then
the subtext shrinks. This way the expand button is always
visible.

Change-Id: Ibda8e9efbac7119cc31ce4c129be33a7a192f974
parent 5bf069a0
Loading
Loading
Loading
Loading
+108 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source 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 android.view;

import android.annotation.Nullable;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

import com.android.internal.R;

/**
 * A header of a notification view
 *
 * @hide
 */
@RemoteViews.RemoteView
public class NotificationHeaderView extends LinearLayout {
    private final int mHeaderMinWidth;
    private View mAppName;
    private View mSubTextView;

    public NotificationHeaderView(Context context) {
        this(context, null);
    }

    public NotificationHeaderView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NotificationHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public NotificationHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mHeaderMinWidth = getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.notification_header_shrink_min_width);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mAppName = findViewById(com.android.internal.R.id.app_name_text);
        mSubTextView = findViewById(com.android.internal.R.id.header_sub_text);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int givenWidth = MeasureSpec.getSize(widthMeasureSpec);
        final int givenHeight = MeasureSpec.getSize(heightMeasureSpec);
        int wrapContentWidthSpec = MeasureSpec.makeMeasureSpec(givenWidth,
                MeasureSpec.AT_MOST);
        int wrapContentHeightSpec = MeasureSpec.makeMeasureSpec(givenHeight,
                MeasureSpec.AT_MOST);
        int totalWidth = getPaddingStart() + getPaddingEnd();
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() == GONE) {
                // We'll give it the rest of the space in the end
                continue;
            }
            final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidthSpec = getChildMeasureSpec(wrapContentWidthSpec,
                    lp.leftMargin + lp.rightMargin, lp.width);
            int childHeightSpec = getChildMeasureSpec(wrapContentHeightSpec,
                    lp.topMargin + lp.bottomMargin, lp.height);
            child.measure(childWidthSpec, childHeightSpec);
            totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth();
        }
        if (totalWidth > givenWidth) {
            int overFlow = totalWidth - givenWidth;
            // We are overflowing, lets shrink
            final int appWidth = mAppName.getMeasuredWidth();
            if (appWidth > mHeaderMinWidth) {
                int newSize = appWidth - Math.min(appWidth - mHeaderMinWidth, overFlow);
                int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST);
                mAppName.measure(childWidthSpec, wrapContentHeightSpec);
                overFlow -= appWidth - newSize;
            }
            if (overFlow > 0 && mSubTextView.getVisibility() != GONE) {
                // we're still too big
                final int subTextWidth = mSubTextView.getMeasuredWidth();
                int newSize = Math.max(0, subTextWidth - overFlow);
                int childWidthSpec = MeasureSpec.makeMeasureSpec(newSize, MeasureSpec.AT_MOST);
                mSubTextView.measure(childWidthSpec, wrapContentHeightSpec);
            }
            totalWidth = givenWidth;
        }
        setMeasuredDimension(totalWidth, givenHeight);
    }
}
+9 −7
Original line number Diff line number Diff line
@@ -15,18 +15,18 @@
  ~ limitations under the License
  -->

<LinearLayout
<NotificationHeaderView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_header"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:clipChildren="false"
    android:layout_gravity="start|top"
    android:gravity="center_vertical"
    android:paddingBottom="16dp"
    android:paddingStart="@dimen/notification_content_margin_start"
    android:paddingEnd="8dp">
    android:paddingEnd="16dp">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="18dp"
@@ -41,6 +41,7 @@
        android:layout_marginEnd="3dp"
        android:layout_marginStart="2dp"
        android:visibility="gone"
        android:singleLine="true"
        />
    <TextView
        android:id="@+id/app_name_text"
@@ -49,6 +50,7 @@
        android:textAppearance="@style/TextAppearance.Material.Notification.Info"
        android:layout_marginStart="3dp"
        android:layout_marginEnd="4dp"
        android:singleLine="true"
        />
    <TextView
        android:id="@+id/app_title_sub_text_divider"
@@ -64,7 +66,8 @@
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.Material.Notification.Info"
        android:layout_marginEnd="4dp"
        android:visibility="gone"/>
        android:visibility="gone"
        android:singleLine="true"/>
    <TextView
        android:id="@+id/sub_text_time_divider"
        android:layout_width="wrap_content"
@@ -72,13 +75,13 @@
        android:textAppearance="@style/TextAppearance.Material.Notification.Info"
        android:layout_marginEnd="4dp"
        android:text="@string/notification_header_divider_symbol"
        android:singleLine="true"
        android:visibility="gone"/>
    <ViewStub
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_weight="0"
        android:layout="@layout/notification_template_part_time"
        android:visibility="gone"
        />
@@ -87,7 +90,6 @@
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_weight="0"
        android:layout="@layout/notification_template_part_chronometer"
        android:visibility="gone"
        />
@@ -100,5 +102,5 @@
        android:src="@drawable/ic_arrow_drop_down"
        android:visibility="gone"
        />
</LinearLayout>
</NotificationHeaderView>
+3 −0
Original line number Diff line number Diff line
@@ -153,6 +153,9 @@
    <!-- The width of the big icons in notifications. -->
    <dimen name="notification_large_icon_height">64dp</dimen>

    <!-- The minimum with of the app name in the header if it shrinks -->
    <dimen name="notification_header_shrink_min_width">72dp</dimen>

    <!-- Minimum width of the search view text entry area. -->
    <dimen name="search_view_text_min_width">160dip</dimen>

+1 −0
Original line number Diff line number Diff line
@@ -2357,6 +2357,7 @@
  <java-symbol type="id" name="expand_button" />
  <java-symbol type="id" name="line2" />
  <java-symbol type="id" name="notification_header" />
  <java-symbol type="dimen" name="notification_header_shrink_min_width" />
  <java-symbol type="dimen" name="notification_content_margin_start" />
  <java-symbol type="dimen" name="notification_content_margin_end" />
  <java-symbol type="dimen" name="notification_content_picture_margin" />