OSDN Git Service

Fix various problems with notification layouts
authorJorim Jaggi <jjaggi@google.com>
Thu, 28 Aug 2014 15:52:27 +0000 (17:52 +0200)
committerJorim Jaggi <jjaggi@google.com>
Fri, 29 Aug 2014 13:26:50 +0000 (15:26 +0200)
- Fix end padding for inbox notification.
- Fix that actions in BigText were clipped off.
- Fix that second line was never shown.
- Fix top padding in combination with badges.
- Fix that actions in InboxStyle were clipped off.
- Make badge style consistent with small icon.
- Fix that third line on Inbox/BigText is always 12sp (before it was
  sometimes 14sp)

Bug: 17315502
Bug: 17285898
Bug: 17311783
Bug: 17200814
Change-Id: Ic7e5e4fa07b6302a76631d0238b88239871acf49

core/java/android/app/Notification.java
core/res/res/layout/notification_template_material_big_base.xml
core/res/res/layout/notification_template_material_big_text.xml
core/res/res/layout/notification_template_material_inbox.xml
core/res/res/layout/notification_template_part_line2.xml
core/res/res/layout/notification_template_part_line3.xml
core/res/res/values/dimens.xml
core/res/res/values/symbols.xml

index e1dc8bf..8a26ba5 100644 (file)
@@ -1868,6 +1868,15 @@ public class Notification implements Parcelable
         private Notification mRebuildNotification = null;
 
         /**
+         * Whether the build notification has three lines. This is used to make the top padding for
+         * both the contracted and expanded layout consistent.
+         *
+         * <p>
+         * This field is only valid during the build phase.
+         */
+        private boolean mHasThreeLines;
+
+        /**
          * Constructs a new Builder with the defaults:
          *
 
@@ -2564,19 +2573,23 @@ public class Notification implements Parcelable
             return this;
         }
 
-        private Bitmap getProfileBadge() {
+        private Drawable getProfileBadgeDrawable() {
             // Note: This assumes that the current user can read the profile badge of the
             // originating user.
             UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
-            Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
+            return userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
+        }
+
+        private Bitmap getProfileBadge() {
+            Drawable badge = getProfileBadgeDrawable();
             if (badge == null) {
                 return null;
             }
-            final int width = badge.getIntrinsicWidth();
-            final int height = badge.getIntrinsicHeight();
-            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
+            final int size = mContext.getResources().getDimensionPixelSize(
+                    R.dimen.notification_badge_size);
+            Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
             Canvas canvas = new Canvas(bitmap);
-            badge.setBounds(0, 0, width, height);
+            badge.setBounds(0, 0, size, size);
             badge.draw(canvas);
             return bitmap;
         }
@@ -2602,6 +2615,12 @@ public class Notification implements Parcelable
             return false;
         }
 
+        private void shrinkLine3Text(RemoteViews contentView) {
+            float subTextSize = mContext.getResources().getDimensionPixelSize(
+                    R.dimen.notification_subtext_size);
+            contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
+        }
+
         private RemoteViews applyStandardTemplate(int resId) {
             RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(),
                     mOriginatingUserId, resId);
@@ -2674,10 +2693,7 @@ public class Notification implements Parcelable
             if (showLine2) {
 
                 // need to shrink all the type to make sure everything fits
-                final Resources res = mContext.getResources();
-                final float subTextSize = res.getDimensionPixelSize(
-                        R.dimen.notification_subtext_size);
-                contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
+                shrinkLine3Text(contentView);
             }
 
             if (mWhen != 0 && mShowWhen) {
@@ -2696,7 +2712,7 @@ public class Notification implements Parcelable
 
             // Adjust padding depending on line count and font size.
             contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
-                    hasThreeLines(), mContext.getResources().getConfiguration().fontScale),
+                    mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
                     0, 0);
 
             // We want to add badge to first line of text.
@@ -2721,7 +2737,12 @@ public class Notification implements Parcelable
          *         is going to have one or two lines
          */
         private boolean hasThreeLines() {
-            boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0;
+            boolean contentTextInLine2 = mSubText != null && mContentText != null;
+
+            // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
+            boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
+            boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
+                    || badgeInLine3;
             boolean hasLine2 = (mSubText != null && mContentText != null) ||
                     (mSubText == null && (mProgressMax != 0 || mProgressIndeterminate));
             return hasLine2 && hasLine3;
@@ -3092,6 +3113,7 @@ public class Notification implements Parcelable
             if (mRebuildNotification == null) {
                 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
             }
+            mHasThreeLines = hasThreeLines();
 
             Bundle extras = mRebuildNotification.extras;
 
@@ -3124,6 +3146,7 @@ public class Notification implements Parcelable
             }
             extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
 
+            mHasThreeLines = false;
             return mRebuildNotification;
         }
 
@@ -3238,6 +3261,7 @@ public class Notification implements Parcelable
          */
         public Notification build() {
             mOriginatingUserId = mContext.getUserId();
+            mHasThreeLines = hasThreeLines();
 
             Notification n = buildUnstyled();
 
@@ -3259,6 +3283,7 @@ public class Notification implements Parcelable
                 mStyle.addExtras(n.extras);
             }
 
+            mHasThreeLines = false;
             return n;
         }
 
@@ -3388,7 +3413,7 @@ public class Notification implements Parcelable
          */
         protected void applyTopPadding(RemoteViews contentView) {
             int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
-                    mBuilder.hasThreeLines(),
+                    mBuilder.mHasThreeLines,
                     mBuilder.mContext.getResources().getConfiguration().fontScale);
             contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
         }
@@ -3661,6 +3686,8 @@ public class Notification implements Parcelable
 
             applyTopPadding(contentView);
 
+            mBuilder.shrinkLine3Text(contentView);
+
             mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
 
             return contentView;
@@ -3800,6 +3827,8 @@ public class Notification implements Parcelable
 
             applyTopPadding(contentView);
 
+            mBuilder.shrinkLine3Text(contentView);
+
             mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
 
             return contentView;
index 3595033..ef916ed 100644 (file)
@@ -53,8 +53,8 @@
                 android:visibility="gone"
                 />
             <ImageView android:id="@+id/profile_badge_large_template"
-                android:layout_width="20dp"
-                android:layout_height="20dp"
+                android:layout_width="@dimen/notification_badge_size"
+                android:layout_height="@dimen/notification_badge_size"
                 android:layout_weight="0"
                 android:layout_marginStart="4dp"
                 android:scaleType="fitCenter"
index d601d4a..3415814 100644 (file)
         <include layout="@layout/notification_template_part_line2" />
         <LinearLayout
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
+            android:layout_height="0dp"
             android:layout_marginEnd="8dp"
             android:layout_marginBottom="10dp"
             android:orientation="horizontal"
             android:gravity="top"
+            android:layout_weight="1"
             >
             <TextView android:id="@+id/big_text"
                 android:textAppearance="@style/TextAppearance.Material.Notification"
@@ -54,8 +55,8 @@
                 android:visibility="gone"
                 />
             <ImageView android:id="@+id/profile_badge_large_template"
-                android:layout_width="20dp"
-                android:layout_height="20dp"
+                android:layout_width="@dimen/notification_badge_size"
+                android:layout_height="@dimen/notification_badge_size"
                 android:layout_weight="0"
                 android:layout_marginStart="4dp"
                 android:scaleType="fitCenter"
index 2bd9f81..2382d18 100644 (file)
         >
         <include layout="@layout/notification_template_part_line1" />
         <include layout="@layout/notification_template_part_line2" />
+
+        <!-- We can't have another vertical linear layout here with weight != 0 so this forces us to
+             put the badge on the first line. -->
         <LinearLayout
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:layout_height="0dp"
             android:orientation="horizontal"
-            android:gravity="top"
             >
-            <LinearLayout
+            <TextView android:id="@+id/inbox_text0"
+                android:textAppearance="@style/TextAppearance.Material.Notification"
                 android:layout_width="0dp"
-                android:layout_weight="1"
                 android:layout_height="wrap_content"
-                android:orientation="vertical"
-                >
-                <TextView android:id="@+id/inbox_text0"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_text1"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_text2"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_text3"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_text4"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_text5"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_text6"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    />
-                <TextView android:id="@+id/inbox_more"
-                    android:textAppearance="@style/TextAppearance.Material.Notification"
-                    android:layout_width="match_parent"
-                    android:layout_height="0dp"
-                    android:singleLine="true"
-                    android:ellipsize="end"
-                    android:visibility="gone"
-                    android:layout_weight="1"
-                    android:text="@android:string/ellipsis"
-                    />
-            </LinearLayout>
+                android:singleLine="true"
+                android:ellipsize="end"
+                android:visibility="gone"
+                android:layout_weight="1"
+                />
             <ImageView android:id="@+id/profile_badge_large_template"
-                android:layout_width="20dp"
-                android:layout_height="20dp"
+                android:layout_width="@dimen/notification_badge_size"
+                android:layout_height="@dimen/notification_badge_size"
                 android:layout_weight="0"
                 android:layout_marginStart="4dp"
                 android:layout_marginEnd="8dp"
                 android:visibility="gone"
                 />
         </LinearLayout>
+        <TextView android:id="@+id/inbox_text1"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            />
+        <TextView android:id="@+id/inbox_text2"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            />
+        <TextView android:id="@+id/inbox_text3"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            />
+        <TextView android:id="@+id/inbox_text4"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            />
+        <TextView android:id="@+id/inbox_text5"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            />
+        <TextView android:id="@+id/inbox_text6"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            />
+        <TextView android:id="@+id/inbox_more"
+            android:textAppearance="@style/TextAppearance.Material.Notification"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            android:layout_marginEnd="8dp"
+            android:singleLine="true"
+            android:ellipsize="end"
+            android:visibility="gone"
+            android:layout_weight="1"
+            android:text="@android:string/ellipsis"
+            />
         <FrameLayout
             android:id="@+id/inbox_end_pad"
             android:layout_width="match_parent"
index 28d2bef..7e99c5e 100644 (file)
@@ -20,8 +20,6 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginEnd="8dp"
-        android:visibility="gone"
-        android:layout_weight="0"
         android:orientation="horizontal"
         android:gravity="center_vertical"
         >
@@ -39,8 +37,8 @@
             android:layout_weight="1"
         />
         <ImageView android:id="@+id/profile_badge_line2"
-            android:layout_width="20dp"
-            android:layout_height="20dp"
+            android:layout_width="@dimen/notification_badge_size"
+            android:layout_height="@dimen/notification_badge_size"
             android:layout_weight="0"
             android:layout_marginStart="4dp"
             android:scaleType="fitCenter"
index 56de5c9..6c043a0 100644 (file)
@@ -44,8 +44,8 @@
         android:paddingStart="8dp"
         />
     <ImageView android:id="@+id/profile_badge_line3"
-        android:layout_width="20dp"
-        android:layout_height="20dp"
+        android:layout_width="@dimen/notification_badge_size"
+        android:layout_height="@dimen/notification_badge_size"
         android:layout_gravity="center"
         android:layout_weight="0"
         android:layout_marginStart="4dp"
index 4e3abb9..77b451f 100644 (file)
     <!-- Padding for notification icon when drawn with circle around it -->
     <dimen name="notification_large_icon_circle_padding">11dp</dimen>
 
+    <!-- Size of the profile badge for notifications -->
+    <dimen name="notification_badge_size">16dp</dimen>
+
     <!-- Keyguard dimensions -->
     <!-- TEMP -->
     <dimen name="kg_security_panel_height">600dp</dimen>
index d8c29b0..622a01a 100644 (file)
   <java-symbol type="dimen" name="notification_top_pad_large_text" />
   <java-symbol type="dimen" name="notification_top_pad_large_text_narrow" />
   <java-symbol type="dimen" name="notification_large_icon_circle_padding" />
+  <java-symbol type="dimen" name="notification_badge_size" />
   <java-symbol type="dimen" name="immersive_mode_cling_width" />
   <java-symbol type="dimen" name="circular_display_mask_offset" />