Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 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.internal.util;
     18 
     19 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
     20 
     21 
     22 /**
     23  * Delegate used to provide new implementation of a select few methods of {@link XmlUtils}
     24  *
     25  * Through the layoutlib_create tool, the original  methods of XmlUtils have been replaced
     26  * by calls to methods of the same name in this delegate class.
     27  *
     28  */
     29 public class XmlUtils_Delegate {
     30 
     31     @LayoutlibDelegate
     32     /*package*/ static final int convertValueToInt(CharSequence charSeq, int defaultValue) {
     33         if (null == charSeq)
     34             return defaultValue;
     35 
     36         String nm = charSeq.toString();
     37 
     38         // This code is copied from the original implementation. The issue is that
     39         // The Dalvik libraries are able to handle Integer.parse("XXXXXXXX", 16) where XXXXXXX
     40         // is > 80000000 but the Java VM cannot.
     41 
     42         int sign = 1;
     43         int index = 0;
     44         int len = nm.length();
     45         int base = 10;
     46 
     47         if ('-' == nm.charAt(0)) {
     48             sign = -1;
     49             index++;
     50         }
     51 
     52         if ('0' == nm.charAt(index)) {
     53             //  Quick check for a zero by itself
     54             if (index == (len - 1))
     55                 return 0;
     56 
     57             char c = nm.charAt(index + 1);
     58 
     59             if ('x' == c || 'X' == c) {
     60                 index += 2;
     61                 base = 16;
     62             } else {
     63                 index++;
     64                 base = 8;
     65             }
     66         }
     67         else if ('#' == nm.charAt(index)) {
     68             index++;
     69             base = 16;
     70         }
     71 
     72         return ((int)Long.parseLong(nm.substring(index), base)) * sign;
     73     }
     74 }
     75