Home | History | Annotate | Download | only in configuration
      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.resources.configuration;
     18 
     19 import java.util.regex.Matcher;
     20 import java.util.regex.Pattern;
     21 
     22 /**
     23  * Resource Qualifier for Mobile Network Code Pixel Density.
     24  */
     25 public final class NetworkCodeQualifier extends ResourceQualifier {
     26     /** Default pixel density value. This means the property is not set. */
     27     private final static int DEFAULT_CODE = -1;
     28 
     29     private final static Pattern sNetworkCodePattern = Pattern.compile("^mnc(\\d{1,3})$"); //$NON-NLS-1$
     30 
     31     private final int mCode;
     32 
     33     public final static String NAME = "Mobile Network Code";
     34 
     35     /**
     36      * Creates and returns a qualifier from the given folder segment. If the segment is incorrect,
     37      * <code>null</code> is returned.
     38      * @param segment the folder segment from which to create a qualifier.
     39      * @return a new {@link CountryCodeQualifier} object or <code>null</code>
     40      */
     41     public static NetworkCodeQualifier getQualifier(String segment) {
     42         Matcher m = sNetworkCodePattern.matcher(segment);
     43         if (m.matches()) {
     44             String v = m.group(1);
     45 
     46             int code = -1;
     47             try {
     48                 code = Integer.parseInt(v);
     49             } catch (NumberFormatException e) {
     50                 // looks like the string we extracted wasn't a valid number.
     51                 return null;
     52             }
     53 
     54             NetworkCodeQualifier qualifier = new NetworkCodeQualifier(code);
     55             return qualifier;
     56         }
     57 
     58         return null;
     59     }
     60 
     61     /**
     62      * Returns the folder name segment for the given value. This is equivalent to calling
     63      * {@link #toString()} on a {@link NetworkCodeQualifier} object.
     64      * @param code the value of the qualifier, as returned by {@link #getCode()}.
     65      */
     66     public static String getFolderSegment(int code) {
     67         if (code != DEFAULT_CODE && code >= 1 && code <= 999) { // code is 1-3 digit.
     68             return String.format("mnc%1$d", code); //$NON-NLS-1$
     69         }
     70 
     71         return ""; //$NON-NLS-1$
     72     }
     73 
     74     public NetworkCodeQualifier() {
     75         this(DEFAULT_CODE);
     76     }
     77 
     78     public NetworkCodeQualifier(int code) {
     79         mCode = code;
     80     }
     81 
     82     public int getCode() {
     83         return mCode;
     84     }
     85 
     86     @Override
     87     public String getName() {
     88         return NAME;
     89     }
     90 
     91     @Override
     92     public String getShortName() {
     93         return "Network Code";
     94     }
     95 
     96     @Override
     97     public int since() {
     98         return 1;
     99     }
    100 
    101     @Override
    102     public boolean isValid() {
    103         return mCode != DEFAULT_CODE;
    104     }
    105 
    106     @Override
    107     public boolean hasFakeValue() {
    108         return false;
    109     }
    110 
    111     @Override
    112     public boolean checkAndSet(String value, FolderConfiguration config) {
    113         Matcher m = sNetworkCodePattern.matcher(value);
    114         if (m.matches()) {
    115             String v = m.group(1);
    116 
    117             int code = -1;
    118             try {
    119                 code = Integer.parseInt(v);
    120             } catch (NumberFormatException e) {
    121                 // looks like the string we extracted wasn't a valid number.
    122                 return false;
    123             }
    124 
    125             NetworkCodeQualifier qualifier = new NetworkCodeQualifier(code);
    126             config.setNetworkCodeQualifier(qualifier);
    127             return true;
    128         }
    129 
    130         return false;
    131     }
    132 
    133     @Override
    134     public boolean equals(Object qualifier) {
    135         if (qualifier instanceof NetworkCodeQualifier) {
    136             return mCode == ((NetworkCodeQualifier)qualifier).mCode;
    137         }
    138 
    139         return false;
    140     }
    141 
    142     @Override
    143     public int hashCode() {
    144         return mCode;
    145     }
    146 
    147     /**
    148      * Returns the string used to represent this qualifier in the folder name.
    149      */
    150     @Override
    151     public String getFolderSegment() {
    152         return getFolderSegment(mCode);
    153     }
    154 
    155     @Override
    156     public String getShortDisplayValue() {
    157         if (mCode != DEFAULT_CODE) {
    158             return String.format("MNC %1$d", mCode);
    159         }
    160 
    161         return ""; //$NON-NLS-1$
    162     }
    163 
    164     @Override
    165     public String getLongDisplayValue() {
    166         return getShortDisplayValue();
    167     }
    168 
    169 }
    170