Bug 822421: Lightweight theme based text colors on about:home. [r=mfinkle] [a=bbajaj]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 17 Dec 2012 12:31:52 -0800
changeset 119186 5bd9a130677e121af5e752bdd04c9b94eac96115
parent 119185 9883439ebee8e97aa79bcb9cebe84f3c500552a4
child 119187 47cfe3f06afe17ba3a0fbe5f1fc08a5096828a57
push id3102
push usersramasubramanian@mozilla.com
push dateFri, 04 Jan 2013 19:10:57 +0000
treeherdermozilla-aurora@8dde2d32f984 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, bbajaj
bugs822421
milestone19.0a2
Bug 822421: Lightweight theme based text colors on about:home. [r=mfinkle] [a=bbajaj]
mobile/android/base/AboutHomeContent.java
mobile/android/base/AboutHomeSection.java
mobile/android/base/GeckoView.java.frag
mobile/android/base/LightweightTheme.java
mobile/android/base/Makefile.in
mobile/android/base/resources/color/abouthome_section_more_text.xml.in
mobile/android/base/resources/color/abouthome_section_subtitle.xml.in
mobile/android/base/resources/color/abouthome_section_title.xml.in
mobile/android/base/resources/layout-xlarge-land-v11/abouthome_content.xml.in
mobile/android/base/resources/layout/abouthome_addon_row.xml
mobile/android/base/resources/layout/abouthome_content.xml.in
mobile/android/base/resources/layout/abouthome_last_tabs_row.xml
mobile/android/base/resources/layout/abouthome_remote_tab_row.xml
mobile/android/base/resources/layout/abouthome_section.xml
mobile/android/base/resources/values/attrs.xml
--- a/mobile/android/base/AboutHomeContent.java
+++ b/mobile/android/base/AboutHomeContent.java
@@ -684,21 +684,37 @@ public class AboutHomeContent extends Sc
     @Override
     public void onLightweightThemeChanged() {
         LightweightThemeDrawable drawable = mActivity.getLightweightTheme().getColorDrawable(this);
         if (drawable == null)
             return;
 
          drawable.setAlpha(255, 0);
          setBackgroundDrawable(drawable);
+
+         boolean isLight = mActivity.getLightweightTheme().isLightTheme();
+
+         if (mAddons != null) {
+             mAddons.setTheme(isLight);
+             mLastTabs.setTheme(isLight);
+             mRemoteTabs.setTheme(isLight);
+             ((GeckoTextView) findViewById(R.id.top_sites_title)).setTheme(isLight);
+         }
     }
 
     @Override
     public void onLightweightThemeReset() {
         setBackgroundResource(R.drawable.abouthome_bg_repeat);
+
+        if (mAddons != null) {
+            mAddons.resetTheme();
+            mLastTabs.resetTheme();
+            mRemoteTabs.resetTheme();
+            ((GeckoTextView) findViewById(R.id.top_sites_title)).resetTheme();
+        }
     }
 
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         super.onLayout(changed, left, top, right, bottom);
         onLightweightThemeChanged();
     }
 
--- a/mobile/android/base/AboutHomeSection.java
+++ b/mobile/android/base/AboutHomeSection.java
@@ -8,17 +8,17 @@ import android.content.Context;
 import android.content.res.TypedArray;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
-public class AboutHomeSection extends LinearLayout {
+public class AboutHomeSection extends GeckoLinearLayout {
     private static final String LOGTAG = "GeckoAboutHomeSection";
 
     private TextView mTitle;
     private TextView mSubtitle;
     private LinearLayout mItemsContainer;
     private LinkTextView mMoreText;
 
     public AboutHomeSection(Context context, AttributeSet attrs) {
--- a/mobile/android/base/GeckoView.java.frag
+++ b/mobile/android/base/GeckoView.java.frag
@@ -4,12 +4,54 @@
 
 package org.mozilla.gecko;
 
 import android.content.Context;
 import android.util.AttributeSet;
 import android.widget.@VIEWTYPE@;
 
 public class Gecko@VIEWTYPE@ extends @VIEWTYPE@ {
+    private static final int[] STATE_LIGHT = { R.attr.state_light };
+    private static final int[] STATE_DARK = { R.attr.state_dark };
+
+    private boolean mIsLight = false;
+    private boolean mIsDark = false;
+
     public Gecko@VIEWTYPE@(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
+
+    @Override
+    public int[] onCreateDrawableState(int extraSpace) {
+        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
+
+        if (mIsLight)
+            mergeDrawableStates(drawableState, STATE_LIGHT);
+        else if (mIsDark)
+            mergeDrawableStates(drawableState, STATE_DARK);
+
+        return drawableState;
+    }
+
+    public void setTheme(boolean isLight) {
+        // Set the theme only if it is different from existing theme.
+        if ((isLight && mIsLight != isLight) ||
+            (!isLight && mIsDark == isLight)) {
+            if (isLight) {
+                mIsLight = true;
+                mIsDark = false;
+            } else {
+                mIsLight = false;
+                mIsDark = true;
+            }
+
+            refreshDrawableState();
+        }
+    }
+
+    public void resetTheme() {
+        if (mIsLight || mIsDark) {
+            mIsLight = false;
+            mIsDark = false;
+            refreshDrawableState();
+        }
+    } 
 }
--- a/mobile/android/base/LightweightTheme.java
+++ b/mobile/android/base/LightweightTheme.java
@@ -37,16 +37,17 @@ import java.util.ArrayList;
 import android.util.Log;
 
 public class LightweightTheme implements GeckoEventListener {
     private static final String LOGTAG = "GeckoLightweightTheme";
 
     private Application mApplication;
     private Bitmap mBitmap;
     private int mColor;
+    private boolean mIsLight;
 
     public static interface OnChangeListener {
         // This is the View's default post.
         // This is required to post the change/rest on UI thread.
         public boolean post(Runnable action);
 
         // The View should change its background/text color. 
         public void onLightweightThemeChanged();
@@ -93,16 +94,21 @@ public class LightweightTheme implements
             DisplayMetrics dm = mApplication.getResources().getDisplayMetrics();
             int maxWidth = Math.max(dm.widthPixels, dm.heightPixels);
             int height = (int) (mBitmap.getHeight() * 0.25);
             Bitmap cropped = Bitmap.createBitmap(mBitmap, mBitmap.getWidth() - maxWidth,
                                                           mBitmap.getHeight() - height, 
                                                           maxWidth, height);
             mColor = BitmapUtils.getDominantColor(cropped, false);
 
+            double luminance = (0.2125 * ((mColor & 0x00FF0000) >> 16)) + 
+                               (0.7154 * ((mColor & 0x0000FF00) >> 8)) + 
+                               (0.0721 * (mColor &0x000000FF));
+            mIsLight = (luminance > 110) ? true : false;
+
             notifyListeners();
         } catch(java.net.MalformedURLException e) {
             mBitmap = null;
         } catch(java.io.IOException e) {
             mBitmap = null;
         }
     }
 
@@ -151,16 +157,20 @@ public class LightweightTheme implements
             } else if (event.equals("LightweightTheme:Disable")) {
                 resetLightweightTheme();
             }
         } catch (Exception e) {
             Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
         }
     }
 
+    public boolean isLightTheme() {
+        return mIsLight;
+    }
+
     /**
      * Crop the image based on the position of the view on the window.
      * Either the View or one of its ancestors might have scrolled or translated.
      * This value should be taken into account while mapping the View to the Bitmap.
      *
      * @param view The view requesting a cropped bitmap.
      */
     private Bitmap getCroppedBitmap(View view) {
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -216,16 +216,19 @@ FENNEC_PP_JAVA_FILES = \
   db/FormHistoryProvider.java \
   db/TabsProvider.java \
   db/GeckoProvider.java \
   SmsManager.java \
   UpdateServiceHelper.java \
   $(NULL)
 
 FENNEC_PP_XML_FILES = \
+  res/color/abouthome_section_more_text.xml \
+  res/color/abouthome_section_subtitle.xml \
+  res/color/abouthome_section_title.xml \
   res/layout/abouthome_content.xml \
   res/layout/awesomebar_search.xml \
   res/layout/awesomebar_suggestion_row.xml \
   res/layout/browser_toolbar.xml \
   res/layout/browser_toolbar_menu.xml \
   res/layout-land-v14/browser_toolbar.xml \
   res/layout-land-v14/browser_toolbar_menu.xml \
   res/layout-large-v11/awesomebar_search.xml \
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/color/abouthome_section_more_text.xml.in
@@ -0,0 +1,19 @@
+#filter substitution
+<?xml version="1.0" encoding="utf-8"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+   - License, v. 2.0. If a copy of the MPL was not distributed with this
+   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android"
+          xmlns:gecko="http://schemas.android.com/apk/res/@ANDROID_PACKAGE_NAME@">
+
+    <!-- dark theme -->
+    <item gecko:state_dark="true" android:color="#80FFFFFF" />
+
+    <!-- light theme -->
+    <item gecko:state_light="true" android:color="#80222222" />
+
+    <!-- default -->
+    <item android:color="#FF22629E"/>
+
+</selector>
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/color/abouthome_section_subtitle.xml.in
@@ -0,0 +1,19 @@
+#filter substitution
+<?xml version="1.0" encoding="utf-8"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+   - License, v. 2.0. If a copy of the MPL was not distributed with this
+   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android"
+          xmlns:gecko="http://schemas.android.com/apk/res/@ANDROID_PACKAGE_NAME@">
+
+    <!-- dark theme -->
+    <item gecko:state_dark="true" android:color="#CCFFFFFF" />
+
+    <!-- light theme -->
+    <item gecko:state_light="true" android:color="#CC222222" />
+
+    <!-- default -->
+    <item android:color="#FF666666"/>
+
+</selector>
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/color/abouthome_section_title.xml.in
@@ -0,0 +1,19 @@
+#filter substitution
+<?xml version="1.0" encoding="utf-8"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+   - License, v. 2.0. If a copy of the MPL was not distributed with this
+   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android"
+          xmlns:gecko="http://schemas.android.com/apk/res/@ANDROID_PACKAGE_NAME@">
+
+    <!-- dark theme -->
+    <item gecko:state_dark="true" android:color="#FFFFFFFF" />
+
+    <!-- light theme -->
+    <item gecko:state_light="true" android:color="#FF222222" />
+
+    <!-- default -->
+    <item android:color="#FF000000"/>
+
+</selector>
--- a/mobile/android/base/resources/layout-xlarge-land-v11/abouthome_content.xml.in
+++ b/mobile/android/base/resources/layout-xlarge-land-v11/abouthome_content.xml.in
@@ -20,26 +20,26 @@
 
             <ImageView android:src="@drawable/abouthome_logo"
                        android:layout_width="wrap_content"
                        android:layout_height="24dip"
                        android:layout_marginTop="16dip"
                        android:layout_marginBottom="10dip"
                        android:layout_marginLeft="12dip"/>
 
-            <TextView android:id="@+id/top_sites_title"
-                      android:text="@string/abouthome_top_sites_title"
-                      android:layout_width="fill_parent"
-                      android:layout_height="26dip"
-                      android:background="#34FFFFFF"
-                      android:paddingLeft="12dip"
-                      android:textSize="12sp"
-                      android:textColor="#000000"
-                      android:textStyle="bold"
-                      android:gravity="left|center_vertical"/>
+            <Gecko.TextView android:id="@+id/top_sites_title"
+                            android:text="@string/abouthome_top_sites_title"
+                            android:layout_width="fill_parent"
+                            android:layout_height="26dip"
+                            android:background="#34FFFFFF"
+                            android:paddingLeft="12dip"
+                            android:textSize="12sp"
+                            android:textColor="@color/abouthome_section_title"
+                            android:textStyle="bold"
+                            android:gravity="left|center_vertical"/>
 
             <view class="org.mozilla.gecko.AboutHomeContent$TopSitesGridView"
                   android:id="@+id/top_sites_grid"
                   style="@style/AboutHome.Thumbnail.Grid"/>
 
             <org.mozilla.gecko.AboutHomePromoBox android:id="@+id/promo_box"
                                                  android:orientation="horizontal"
                                                  android:background="@drawable/abouthome_promo_box"
--- a/mobile/android/base/resources/layout/abouthome_addon_row.xml
+++ b/mobile/android/base/resources/layout/abouthome_addon_row.xml
@@ -2,42 +2,45 @@
 <!-- This Source Code Form is subject to the terms of the Mozilla Public
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="47dip"
                 android:gravity="left|center_vertical"
-                android:background="@drawable/action_bar_button">
+                android:background="@drawable/action_bar_button"
+                android:duplicateParentState="true">
 
     <ImageView android:id="@+id/addon_icon"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="12dip"
                android:src="@drawable/ic_addons_empty"/>
 
     <TextView android:id="@+id/addon_title"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_toRightOf="@id/addon_icon"
               android:layout_marginLeft="12dip"
               android:gravity="center_vertical"
               android:textSize="15sp"
-              android:textColor="#222222"/>
+              android:textColor="@color/abouthome_section_title"
+              android:duplicateParentState="true"/>
 
     <TextView android:id="@+id/addon_version"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_toRightOf="@id/addon_title"
               android:layout_alignBottom="@id/addon_title"
               android:layout_marginLeft="5dip"
               android:gravity="center_vertical"
               android:textSize="12sp"
-              android:textColor="#666666"/>
+              android:textColor="@color/abouthome_section_subtitle"
+              android:duplicateParentState="true"/>
 
     <View android:layout_width="fill_parent"
           android:layout_height="1dp"
           android:background="@drawable/abouthome_divider"
           android:layout_alignParentBottom="true"/>
 
 </RelativeLayout>
--- a/mobile/android/base/resources/layout/abouthome_content.xml.in
+++ b/mobile/android/base/resources/layout/abouthome_content.xml.in
@@ -17,27 +17,27 @@
                    android:layout_width="wrap_content"
                    android:layout_height="24dip"
                    android:layout_marginTop="16dip"
                    android:layout_marginBottom="10dip"
                    android:layout_marginLeft="12dip"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"/>
 
-        <TextView android:id="@+id/top_sites_title"
-                  android:text="@string/abouthome_top_sites_title"
-                  android:layout_width="fill_parent"
-                  android:layout_height="26dip"
-                  android:background="#34FFFFFF"
-                  android:paddingLeft="12dip"
-                  android:textSize="12sp"
-                  android:textColor="#000000"
-                  android:textStyle="bold"
-                  android:gravity="left|center_vertical"
-                  android:layout_below="@id/abouthome_logo"/>
+        <Gecko.TextView android:id="@+id/top_sites_title"
+                        android:text="@string/abouthome_top_sites_title"
+                        android:layout_width="fill_parent"
+                        android:layout_height="26dip"
+                        android:background="#34FFFFFF"
+                        android:paddingLeft="12dip"
+                        android:textSize="12sp"
+                        android:textColor="@color/abouthome_section_title"
+                        android:textStyle="bold"
+                        android:gravity="left|center_vertical"
+                        android:layout_below="@id/abouthome_logo"/>
 
         <ImageView android:src="@drawable/abouthome_icon"
                    android:layout_width="180dp"
                    android:layout_height="153dp"
                    android:layout_marginTop="4dp"
                    android:layout_marginRight="@dimen/abouthome_icon_crop"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"/>
--- a/mobile/android/base/resources/layout/abouthome_last_tabs_row.xml
+++ b/mobile/android/base/resources/layout/abouthome_last_tabs_row.xml
@@ -3,17 +3,18 @@
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="47dip"
                 android:gravity="left|center_vertical"
-                android:background="@drawable/action_bar_button">
+                android:background="@drawable/action_bar_button"
+                android:duplicateParentState="true">
 
     <ImageView android:id="@+id/last_tab_favicon"
                android:layout_width="32dip"
                android:layout_height="32dip"
                android:layout_centerVertical="true"
                android:layout_marginLeft="12dip"
                android:src="@drawable/favicon"/>
 
@@ -21,27 +22,29 @@
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_toRightOf="@id/last_tab_favicon"
               android:layout_marginTop="2dip"
               android:layout_marginLeft="12dip"
               android:layout_marginRight="12dip"
               android:textSize="15sp"
               android:singleLine="true"
-              android:textColor="#222222"/>
+              android:textColor="@color/abouthome_section_title"
+              android:duplicateParentState="true"/>
 
     <TextView android:id="@+id/last_tab_url"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginLeft="12dip"
               android:layout_marginRight="12dip"
               android:layout_toRightOf="@id/last_tab_favicon"
               android:layout_below="@id/last_tab_title"
               android:textSize="12sp"
               android:singleLine="true"
-              android:textColor="#666666"/>
+              android:textColor="@color/abouthome_section_subtitle"
+              android:duplicateParentState="true"/>
 
     <View android:layout_width="fill_parent"
           android:layout_height="1dp"
           android:background="@drawable/abouthome_divider"
           android:layout_alignParentBottom="true"/>
 
 </RelativeLayout>
--- a/mobile/android/base/resources/layout/abouthome_remote_tab_row.xml
+++ b/mobile/android/base/resources/layout/abouthome_remote_tab_row.xml
@@ -2,27 +2,29 @@
 <!-- This Source Code Form is subject to the terms of the Mozilla Public
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="47dip"
                 android:orientation="vertical"
-                android:background="@drawable/action_bar_button">
+                android:background="@drawable/action_bar_button"
+                android:duplicateParentState="true">
 
     <TextView android:id="@+id/remote_tab_title"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:paddingLeft="12dip"
               android:paddingRight="12dip"
               android:gravity="center_vertical"
               android:textSize="15sp"
-              android:textColor="#222222"
               android:singleLine="true"
-              android:ellipsize="middle"/>
+              android:ellipsize="middle"
+              android:textColor="@color/abouthome_section_title"
+              android:duplicateParentState="true"/>
 
     <View android:layout_width="fill_parent"
           android:layout_height="1dp"
           android:background="@drawable/abouthome_divider"
           android:layout_alignParentBottom="true"/>
 
 </RelativeLayout>
--- a/mobile/android/base/resources/layout/abouthome_section.xml
+++ b/mobile/android/base/resources/layout/abouthome_section.xml
@@ -6,36 +6,40 @@
 <merge xmlns:android="http://schemas.android.com/apk/res/android">
 
     <TextView android:id="@+id/title"
               android:layout_width="fill_parent"
               android:layout_height="26dip"
               android:background="#34FFFFFF"
               android:paddingLeft="12dip"
               android:textSize="12sp"
-              android:textColor="#000000"
+              android:textColor="@color/abouthome_section_title"
               android:textStyle="bold"
-              android:gravity="left|center_vertical"/>
+              android:gravity="left|center_vertical"
+              android:duplicateParentState="true"/>
 
     <TextView android:id="@+id/subtitle"
               android:layout_width="fill_parent"
               android:layout_height="26dip"
               android:layout_marginTop="1dip"
               android:background="#34FFFFFF"
               android:paddingLeft="12dip"
               android:textSize="12sp"
-              android:textColor="#666666"
-              android:gravity="left|center_vertical"/>
+              android:textColor="@color/abouthome_section_subtitle"
+              android:gravity="left|center_vertical"
+              android:duplicateParentState="true"/>
 
     <LinearLayout android:id="@+id/items_container"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
-                  android:isScrollContainer="false"/>
+                  android:isScrollContainer="false"
+                  android:duplicateParentState="true"/>
 
     <org.mozilla.gecko.LinkTextView android:id="@+id/more_text"
                                     android:layout_width="fill_parent"
                                     android:layout_height="47dip"
-                                    android:textColor="#22629e"
+                                    android:textColor="@color/abouthome_section_more_text"
                                     android:textSize="12sp"
-                                    android:gravity="center"/>
+                                    android:gravity="center"
+                                    android:duplicateParentState="true"/>
 
 </merge>
--- a/mobile/android/base/resources/values/attrs.xml
+++ b/mobile/android/base/resources/values/attrs.xml
@@ -52,10 +52,15 @@
     <declare-styleable name="TextSelectionHandle">
         <attr name="handleType">
             <flag name="start" value="0x01" />
             <flag name="middle" value="0x02" />
             <flag name="end" value="0x03" />
         </attr>
     </declare-styleable>
 
+    <declare-styleable name="LightweightTheme">
+        <attr name="state_light" format="boolean"/>
+        <attr name="state_dark" format="boolean"/>
+    </declare-styleable>
+
 </resources>