Home | History | Annotate | Download | only in calculator2
      1 /*
      2  * Copyright (C) 2014 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.calculator2;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.widget.Button;
     22 import android.view.View;
     23 import java.text.DecimalFormat;
     24 import java.text.DecimalFormatSymbols;
     25 import java.text.NumberFormat;
     26 import java.util.Locale;
     27 
     28 public class CalculatorNumericPadLayout extends CalculatorPadLayout {
     29 
     30     public CalculatorNumericPadLayout(Context context) {
     31         this(context, null);
     32     }
     33 
     34     public CalculatorNumericPadLayout(Context context, AttributeSet attrs) {
     35         this(context, attrs, 0);
     36     }
     37 
     38     public CalculatorNumericPadLayout(Context context, AttributeSet attrs, int defStyle) {
     39         super(context, attrs, defStyle);
     40     }
     41 
     42     @Override
     43     public void onFinishInflate() {
     44         super.onFinishInflate();
     45 
     46         Locale locale = getResources().getConfiguration().locale;
     47         if (!getResources().getBoolean(R.bool.use_localized_digits)) {
     48             locale = new Locale.Builder()
     49                 .setLocale(locale)
     50                 .setUnicodeLocaleKeyword("nu", "latn")
     51                 .build();
     52         }
     53 
     54         final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
     55         final char zeroDigit = symbols.getZeroDigit();
     56         for (int childIndex = getChildCount() - 1; childIndex >= 0; --childIndex) {
     57             final View v = getChildAt(childIndex);
     58             if (v instanceof Button) {
     59                 final Button b = (Button) v;
     60                 switch (b.getId()) {
     61                     case R.id.digit_0:
     62                         b.setText(String.valueOf(zeroDigit));
     63                         break;
     64                     case R.id.digit_1:
     65                         b.setText(String.valueOf((char) (zeroDigit + 1)));
     66                         break;
     67                     case R.id.digit_2:
     68                         b.setText(String.valueOf((char) (zeroDigit + 2)));
     69                         break;
     70                     case R.id.digit_3:
     71                         b.setText(String.valueOf((char) (zeroDigit + 3)));
     72                         break;
     73                     case R.id.digit_4:
     74                         b.setText(String.valueOf((char) (zeroDigit + 4)));
     75                         break;
     76                     case R.id.digit_5:
     77                         b.setText(String.valueOf((char) (zeroDigit + 5)));
     78                         break;
     79                     case R.id.digit_6:
     80                         b.setText(String.valueOf((char) (zeroDigit + 6)));
     81                         break;
     82                     case R.id.digit_7:
     83                         b.setText(String.valueOf((char) (zeroDigit + 7)));
     84                         break;
     85                     case R.id.digit_8:
     86                         b.setText(String.valueOf((char) (zeroDigit + 8)));
     87                         break;
     88                     case R.id.digit_9:
     89                         b.setText(String.valueOf((char) (zeroDigit + 9)));
     90                         break;
     91                     case R.id.dec_point:
     92                         b.setText(String.valueOf(symbols.getDecimalSeparator()));
     93                         break;
     94                 }
     95             }
     96         }
     97     }
     98 }
     99 
    100