Home | History | Annotate | Download | only in assetstudiolib
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.assetstudiolib;
     17 
     18 import com.android.assetstudiolib.Util.Effect;
     19 import com.android.assetstudiolib.Util.FillEffect;
     20 import com.android.assetstudiolib.Util.ShadowEffect;
     21 
     22 import java.awt.Color;
     23 import java.awt.GradientPaint;
     24 import java.awt.Graphics2D;
     25 import java.awt.Rectangle;
     26 import java.awt.image.BufferedImage;
     27 import java.util.Map;
     28 
     29 /**
     30  * Generate icons for the notifications bar
     31  */
     32 public class NotificationIconGenerator extends GraphicGenerator {
     33     /** Creates a new {@link NotificationIconGenerator} */
     34     public NotificationIconGenerator() {
     35     }
     36 
     37     @Override
     38     public BufferedImage generate(GraphicGeneratorContext context, Options options) {
     39         Rectangle iconSizeMdpi;
     40         Rectangle targetRectMdpi;
     41         NotificationOptions notificationOptions = (NotificationOptions) options;
     42         if (notificationOptions.version == Version.OLDER) {
     43             iconSizeMdpi = new Rectangle(0, 0, 25, 25);
     44             targetRectMdpi = new Rectangle(4, 4, 17, 17);
     45         } else if (notificationOptions.version == Version.V11) {
     46             iconSizeMdpi = new Rectangle(0, 0, 24, 24);
     47             targetRectMdpi = new Rectangle(1, 1, 22, 22);
     48         } else {
     49             assert notificationOptions.version == Version.V9;
     50             iconSizeMdpi = new Rectangle(0, 0, 16, 25);
     51             targetRectMdpi = new Rectangle(0, 5, 16, 16);
     52         }
     53 
     54         final float scaleFactor = GraphicGenerator.getMdpiScaleFactor(options.density);
     55         Rectangle imageRect = Util.scaleRectangle(iconSizeMdpi, scaleFactor);
     56         Rectangle targetRect = Util.scaleRectangle(targetRectMdpi, scaleFactor);
     57 
     58         BufferedImage outImage = Util.newArgbBufferedImage(imageRect.width, imageRect.height);
     59         Graphics2D g = (Graphics2D) outImage.getGraphics();
     60 
     61         BufferedImage tempImage = Util.newArgbBufferedImage(
     62                 imageRect.width, imageRect.height);
     63         Graphics2D g2 = (Graphics2D) tempImage.getGraphics();
     64 
     65         if (notificationOptions.version == Version.OLDER) {
     66             BufferedImage mBackImage = context.loadImageResource(
     67                     "/images/notification_stencil/"
     68                             + notificationOptions.shape.id + '/' +
     69                             notificationOptions.density.getResourceValue()
     70                             + ".png");
     71             g.drawImage(mBackImage, 0, 0, null);
     72             BufferedImage top = options.sourceImage;
     73             BufferedImage filled = Util.filledImage(top, Color.WHITE);
     74             Util.drawCenterInside(g, filled, targetRect);
     75         } else if (notificationOptions.version == Version.V11) {
     76             Util.drawCenterInside(g2, options.sourceImage, targetRect);
     77             Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
     78                     new FillEffect(Color.WHITE),
     79             });
     80         } else {
     81             assert notificationOptions.version == Version.V9;
     82             Util.drawCenterInside(g2, options.sourceImage, targetRect);
     83             Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
     84                     new FillEffect(
     85                             new GradientPaint(
     86                                     0, 0,
     87                                     new Color(0x919191),
     88                                     0, imageRect.height,
     89                                     new Color(0x828282))),
     90                     new ShadowEffect(
     91                             0,
     92                             1,
     93                             0,
     94                             Color.WHITE,
     95                             0.10,
     96                             true),
     97             });
     98         }
     99 
    100         g.dispose();
    101         g2.dispose();
    102 
    103         return outImage;
    104     }
    105 
    106     @Override
    107     public void generate(String category, Map<String, Map<String, BufferedImage>> categoryMap,
    108             GraphicGeneratorContext context, Options baseOptions, String name) {
    109         NotificationOptions options = (NotificationOptions) baseOptions;
    110         if (options.minSdk < 9) {
    111             options.version = Version.OLDER;
    112             super.generate(options.version.getDisplayName(), categoryMap, context, options, name);
    113         }
    114         if (options.minSdk < 11) {
    115             options.version = Version.V9;
    116             super.generate(options.version.getDisplayName(), categoryMap, context, options, name);
    117         }
    118         options.version = Version.V11;
    119         super.generate(options.minSdk < 11 ? options.version.getDisplayName() : null,
    120                 categoryMap, context, options, name);
    121     }
    122 
    123     @Override
    124     protected String getIconFolder(Options options) {
    125         String folder = super.getIconFolder(options);
    126         Version version = ((NotificationOptions) options).version;
    127         if (version == Version.V11 && options.minSdk < 11) {
    128             return folder + "-v11"; //$NON-NLS-1$
    129         } else if (version == Version.V9 && options.minSdk < 9) {
    130             return folder + "-v9"; //$NON-NLS-1$
    131         } else {
    132             return folder;
    133         }
    134     }
    135 
    136     /**
    137      * Options specific to generating notification icons
    138      */
    139     public static class NotificationOptions extends GraphicGenerator.Options {
    140         /**
    141          * The shape to use for graphics behind the icon (for {@link Version#OLDER} only)
    142          */
    143         public Shape shape = Shape.SQUARE;
    144 
    145         /**
    146          * The version of the icon to generate - different styles are used for different
    147          * versions of Android
    148          */
    149         public Version version = Version.V9;
    150     }
    151 
    152     /**
    153      * The version of the icon to generate - different styles are used for different
    154      * versions of Android
    155      */
    156     public enum Version {
    157         /** Icon style used for -v9 and -v10 */
    158         V9("V9"),
    159 
    160         /** Icon style used for -v11 (Honeycomb) and later */
    161         V11("V11"),
    162 
    163         /** Icon style used for versions older than v9 */
    164         OLDER("Other");
    165 
    166         private final String mDisplayName;
    167 
    168         Version(String displayName) {
    169             mDisplayName = displayName;
    170         }
    171 
    172         /**
    173          * Returns the display name for this version, typically shown as a
    174          * category
    175          *
    176          * @return the display name, never null
    177          */
    178         public String getDisplayName() {
    179             return mDisplayName;
    180         }
    181     }
    182 }
    183