aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXin Li <delphij@google.com>2018-08-06 16:51:47 -0700
committerXin Li <delphij@google.com>2018-08-06 16:51:47 -0700
commitaf7f8a98adb84833ab33ca5d1eba25883ac63d5a (patch)
tree33312bb6edc73900094bbe79edf4718110fdac6c
parentf6f661b424f646bc5ddf9e1b1e644abb492b7547 (diff)
parentec3da48f25a3df5ae2ea605dba909a61172d0922 (diff)
downloadcheckcolor-oreo-mr1-1.2-iot-release.tar.gz
Bug: 112104996 Change-Id: I8f692578259af65d0b8d83233c5a694b4513ac7f
-rw-r--r--README.md107
-rw-r--r--checkcolor.jarbin0 -> 5733 bytes
-rwxr-xr-xcheckcolor.py102
3 files changed, 209 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6ed4305
--- /dev/null
+++ b/README.md
@@ -0,0 +1,107 @@
+# Lint check for hardcoded colors
+
+## What is this lint check for
+
+This check detects whether hardcoded colors have been added in a CL.
+
+Starting in Android O, multiple device themes will exist on devices, enabling
+changing the look and feel of all system UI. In order to make that effort
+possible, colors in component that uses this check must be specified using
+theme attributes, rather than hardcoded colors. Otherwise, when the theme
+changes, this UI will not update properly.
+
+## Examples of hardcoded colors
+
+### Color files
+
+```xml
+<!-- File: res/values/colors.xml -->
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <color name="hardcoded_color">#FFFFFF</color>
+</resources>
+```
+
+### Layout files
+
+```xml
+<!-- File: res/layout/my_layout.xml -->
+<?xml version="1.0" encoding="utf-8"?>
+<TextView
+ android:textColor="#FF000000" />
+```
+
+Or
+
+```xml
+<!-- File: res/layout/my_layout.xml -->
+<?xml version="1.0" encoding="utf-8"?>
+<TextView
+ android:textColor="@color/hardcoded_color" />
+```
+
+### Style files
+
+```xml
+<!-- File: res/values/styles.xml -->
+<style name="MyStyle">
+ <item name="android:textColor">#ff3c3c3c</item>
+</style>
+```
+
+## How to fix it
+
+### Use attributes in theming as much as possible
+
+Here are some tips to choose the colors.
+
+#### Choose colors for text
+
+Use color attributes specified in the theme. Some examples:
+
+1. `textColorPrimary`
+2. `textColorSecondary`
+3. `colorAccent`
+
+#### Choose colors for icon
+
+For `vector drawable`, please use full opacity color as `fillColor` and `tint`
+it with theme attribute.
+[example](https://googleplex-android-review.git.corp.google.com/#/c/1606392/2/packages/SettingsLib/res/drawable/ic_menu.xml)
+
+#### Others
+
+Please check the following table for more available options.
+
+| Attribute | Description |
+|---|---|
+| colorAccent | Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated). |
+| colorForeground | Color for foreground imagery. Most text and image colors will be based on some alpha of colorForeground. |
+| colorBackground | Color of background imagery, ex. full-screen windows. |
+| colorBackgroundFloating | Color of background imagery for floating components, ex. dialogs, popups, and cards. |
+| colorPrimary | The primary branding color for the app. This is the color applied to the action bar background. |
+| colorPrimaryDark | Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor). |
+| colorError | Color used for error states and things that need to be drawn to the users attention. |
+| textColorPrimary (colorPrimaryText) | Is now constructed out of colorForeground and primaryContentAlpha. |
+| textColorSecondary (colorSecondaryText) | Is now constructed out of colorForeground and primaryContentAlpha. |
+
+## How to bypass it
+
+**We strongly discourage bypassing color lint check**.
+
+However, if you need to bypass the check, please update the `baseline.xml` by running following
+command in package root folder(i.e. package/app/Settings/)
+
+```
+export ANDROID_LINT_JARS=$(gettop)/prebuilts/checkcolor/checkcolor.jar
+lint --check HardCodedColor --xml color-check-baseline.xml .
+```
+
+After update the `baseline.xml`, your hardcoded color will be ignored in check. Please submit the
+new `baseline.xml` within your cl.
+
+## Contact us
+
+1. For help to remove hardcoded colors or report issue in color check, please contact
+jackqdyulei@google.com
+
diff --git a/checkcolor.jar b/checkcolor.jar
new file mode 100644
index 0000000..8ed3f2f
--- /dev/null
+++ b/checkcolor.jar
Binary files differ
diff --git a/checkcolor.py b/checkcolor.py
new file mode 100755
index 0000000..204c7e5
--- /dev/null
+++ b/checkcolor.py
@@ -0,0 +1,102 @@
+#!/usr/bin/python
+
+#
+# Copyright 2017, 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.
+#
+
+"""Script used by developers to run hardcoded color check."""
+
+import argparse
+import errno
+import os
+import shutil
+import subprocess
+import sys
+
+MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__))
+CHECKCOLOR_JAR = os.path.join(MAIN_DIRECTORY, 'checkcolor.jar')
+LINT_BASELINE_FILENAME = 'color-check-baseline.xml'
+COLOR_CHECK_README_PATH = 'prebuilts/checkcolor/README.md'
+TMP_FOLDER = '/tmp/color'
+
+
+def CopyProject(root):
+ """Copy the project to tmp folder and remove the translation files.
+
+ Args:
+ root: Root folder of the project
+ """
+ cmd = 'cp -r {0}/* {1}/&& rm -r {1}/res/values-?? {1}/res/values-??-*'.format(root, TMP_FOLDER)
+ os.system(cmd)
+
+def Mkdir():
+ """Create the tmp folder is necessary."""
+ if os.path.exists(TMP_FOLDER):
+ shutil.rmtree(TMP_FOLDER)
+ os.makedirs(TMP_FOLDER)
+
+
+def RunCheckOnProject(root):
+ """Run color lint check on project.
+
+ Args:
+ root: Root folder of the project.
+ Returns:
+ exitcode and stdout
+ """
+ checkcolor_env = os.environ.copy()
+ checkcolor_env['ANDROID_LINT_JARS'] = CHECKCOLOR_JAR
+
+ Mkdir()
+ CopyProject(root)
+
+ try:
+ baseline = os.path.join(root, LINT_BASELINE_FILENAME)
+ check = subprocess.Popen(['lint', '--check', 'HardCodedColor', '--disable', 'LintError',
+ '--baseline', baseline, '--exitcode', TMP_FOLDER],
+ stdout=subprocess.PIPE, env=checkcolor_env)
+ stdout, _ = check.communicate()
+ exitcode = check.returncode
+ except OSError as e:
+ if e.errno == errno.ENOENT:
+ print 'Error in color lint check'
+ if not os.environ.get('ANDROID_BUILD_TOP'):
+ print 'Try setting up your environment first:'
+ print ' source build/envsetup.sh && lunch <target>'
+ sys.exit(1)
+
+ return (exitcode, stdout)
+
+
+def main():
+ """Run color lint check on all projects listed in parameter -p."""
+ parser = argparse.ArgumentParser(description='Check hardcoded colors.')
+ parser.add_argument('--project', '-p', nargs='+')
+ args = parser.parse_args()
+ exitcode = 0
+
+ for rootdir in args.project:
+ code, message = RunCheckOnProject(rootdir)
+ if code != 0:
+ exitcode = 1
+ print message
+
+ if exitcode == 1:
+ print 'Please check link for more info:', COLOR_CHECK_README_PATH
+ sys.exit(exitcode)
+
+
+if __name__ == '__main__':
+ main()