Home | History | Annotate | Download | only in api
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
      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 
     17 package com.android.ide.common.rendering.api;
     18 
     19 import com.android.layoutlib.api.IResourceValue;
     20 import com.android.layoutlib.api.IStyleResourceValue;
     21 import com.android.resources.ResourceType;
     22 import com.android.util.Pair;
     23 
     24 import java.util.HashMap;
     25 
     26 /**
     27  * Represents an android style resources with a name and a list of children {@link ResourceValue}.
     28  */
     29 @SuppressWarnings("deprecation")
     30 public final class StyleResourceValue extends ResourceValue implements IStyleResourceValue {
     31 
     32     private String mParentStyle = null;
     33     private HashMap<Pair<String, Boolean>, ResourceValue> mItems = new HashMap<Pair<String, Boolean>, ResourceValue>();
     34 
     35     public StyleResourceValue(ResourceType type, String name, boolean isFramework) {
     36         super(type, name, isFramework);
     37     }
     38 
     39     public StyleResourceValue(ResourceType type, String name, String parentStyle,
     40             boolean isFramework) {
     41         super(type, name, isFramework);
     42         mParentStyle = parentStyle;
     43     }
     44 
     45     /**
     46      * Returns the parent style name or <code>null</code> if unknown.
     47      */
     48     @Override
     49     public String getParentStyle() {
     50         return mParentStyle;
     51     }
     52 
     53     /**
     54      * Finds a value in the list by name
     55      * @param name the name of the resource
     56      *
     57      * @deprecated use {@link #findValue(String, boolean)}
     58      */
     59     @Deprecated
     60     public ResourceValue findValue(String name) {
     61         return mItems.get(Pair.of(name, isFramework()));
     62     }
     63 
     64     /**
     65      * Finds a value in the list by name
     66      * @param name the name of the resource
     67      */
     68     public ResourceValue findValue(String name, boolean isFrameworkAttr) {
     69         return mItems.get(Pair.of(name, isFrameworkAttr));
     70     }
     71 
     72     public void addValue(ResourceValue value, boolean isFrameworkAttr) {
     73         mItems.put(Pair.of(value.getName(), isFrameworkAttr), value);
     74     }
     75 
     76     @Override
     77     public void replaceWith(ResourceValue value) {
     78         assert value instanceof StyleResourceValue;
     79         super.replaceWith(value);
     80 
     81         if (value instanceof StyleResourceValue) {
     82             mItems.clear();
     83             mItems.putAll(((StyleResourceValue)value).mItems);
     84         }
     85     }
     86 
     87     /**
     88      * Legacy method.
     89      * @deprecated use {@link #getValue()}
     90      */
     91     @Override
     92     @Deprecated
     93     public IResourceValue findItem(String name) {
     94         return mItems.get(name);
     95     }
     96 }
     97