diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-09-13 16:16:26 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-09-13 16:16:26 +0000 |
commit | 338cfbdb6f81a0217934ea4e348738c957cb4092 (patch) | |
tree | f2adaa52a852ae541197ad3b7a7ed215e66df243 | |
parent | f6f661b424f646bc5ddf9e1b1e644abb492b7547 (diff) | |
parent | 11b02810ab9a15861b368b973b11b1ba4138b2db (diff) | |
download | checkcolor-338cfbdb6f81a0217934ea4e348738c957cb4092.tar.gz |
release-request-8244a883-eac1-43aa-b800-33dd285ccdd1-for-git_pi-release-4332766 snap-temp-L47800000102000453
Change-Id: I5a505e169d59c4cd5bd2cbfc958f25f391cfe7c9
-rw-r--r-- | README.md | 106 | ||||
-rw-r--r-- | checkcolor.jar | bin | 0 -> 5733 bytes | |||
-rwxr-xr-x | checkcolor.py | 99 |
3 files changed, 205 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7f26c6 --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +# 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/) + +``` +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 Binary files differnew file mode 100644 index 0000000..8ed3f2f --- /dev/null +++ b/checkcolor.jar diff --git a/checkcolor.py b/checkcolor.py new file mode 100755 index 0000000..048feb3 --- /dev/null +++ b/checkcolor.py @@ -0,0 +1,99 @@ +#!/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', '--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' + 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() |