Android O 修改吉字节为GB

中文状态下,流量使用、存储容量等位置的单位为中文表述:字节、吉字节、兆字节。需要将其改为 B、GB、MB 等。

1.修改流量使用单位
  • package/apps/Settings/src/com/android/settings/datausage/DataUsagePreference.java
  @Override
  public void setTemplate(NetworkTemplate template, int subId,
            NetworkServices services) {
      mTemplate = template;
      mSubId = subId;
      DataUsageController controller = new DataUsageController(getContext());
      DataUsageController.DataUsageInfo usageInfo = controller.getDataUsageInfo(mTemplate);
      setSummary(getContext().getString(R.string.data_usage_template,
              Formatter.formatFileSize(getContext(), usageInfo.usageLevel), usageInfo.period));
      setIntent(getIntent());
  }
  • frameworks/base/core/java/android/text/format/Formatter.java
  private static String formatRoundedBytesResult(
            @NonNull Context context, @NonNull RoundedBytesResult input) {
     //add for change 吉字节 to GB      
     final Locale locale = /*localeFromContext(context)*/Locale.US;
     //add end
     final NumberFormat numberFormatter = getNumberFormatter(locale, input.fractionDigits);
     if (input.units == MeasureUnit.BYTE || input.units == PETABYTE) {
         // ICU spells out "byte" instead of "B", and can't format petabytes yet.
         final String formattedNumber = numberFormatter.format(input.value);
         return context.getString(com.android.internal.R.string.fileSizeSuffix,
                    formattedNumber, getSuffixOverride(context.getResources(), input.units));
     } else {
         return formatMeasureShort(locale, numberFormatter, input.value, input.units);
     }
  }
  
  /** {@hide} */
  public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) {
     final RoundedBytesResult rounded = RoundedBytesResult.roundBytes(sizeBytes, flags);
     // add for change 吉字节 to GB   
     final Locale locale = /*res.getConfiguration().getLocales().get(0)*/Locale.US;
     // add end
     final NumberFormat numberFormatter = getNumberFormatter(locale, rounded.fractionDigits);
     final String formattedNumber = numberFormatter.format(rounded.value);
     final String units;
     if (rounded.units == MeasureUnit.BYTE || rounded.units == PETABYTE) {
         // ICU spells out "byte" instead of "B", and can't format petabytes yet.
         units = getSuffixOverride(res, rounded.units);
     } else { 
         // Since ICU does not give us access to the pattern, we need to extract the unit string
         // from ICU, which we do by taking out the formatted number out of the formatted string
         // and trimming the result of spaces and controls.
         final String formattedMeasure = formatMeasureShort(
                    locale, numberFormatter, rounded.value, rounded.units);
         final String numberRemoved = deleteFirstFromString(formattedMeasure, formattedNumber);
         units = SPACES_AND_CONTROLS.trim(numberRemoved).toString();
     }
     return new BytesResult(formattedNumber, units, rounded.roundedBytes);
  }
2.修改存储容量单位
  • package/apps/Settings/src/com/android/settings/deviceinfo/PrivateVolumeSettings.java
  // 不同类型标签
  private static final int[] ITEMS_SHOW_SHARED = new int[] {
          R.string.storage_detail_apps,
          R.string.storage_detail_images,
          R.string.storage_detail_videos,
          R.string.storage_detail_audio,
          R.string.storage_detail_system,
          R.string.storage_detail_other,
  };
  ...
  private void addDetailItems(PreferenceGroup category, boolean showShared, int userId) {
        final int[] itemsToAdd = (showShared ? ITEMS_SHOW_SHARED : ITEMS_NO_SHOW_SHARED);
        for (int i = 0; i < itemsToAdd.length; ++i) {
            addItem(category, itemsToAdd[i], null, userId);
        }
  }

  private void addItem(PreferenceGroup group, int titleRes, CharSequence title, int userId) {
        if (titleRes == R.string.storage_detail_system) {
            if (mSystemSize <= 0) {
                Log.w(TAG, "Skipping System storage because its size is " + mSystemSize);
                return;
            }
            if (userId != UserHandle.myUserId()) {
                // Only display system on current user.
                return;
            }
        }
        StorageItemPreference item;
        if (mItemPoolIndex < mItemPreferencePool.size()) {
            item = mItemPreferencePool.get(mItemPoolIndex);
        } else {
            item = buildItem();
            mItemPreferencePool.add(item);
        }
        if (title != null) {
            item.setTitle(title);
            item.setKey(title.toString());
        } else {
            item.setTitle(titleRes);
            item.setKey(Integer.toString(titleRes));
        }
        item.setSummary(R.string.memory_calculating_size);
        item.userHandle = userId;
        addPreference(group, item);  //全部添加到布局中
        ++mItemPoolIndex;
    }

追踪下来可以知道,这些标签都是StorageItemPreference

  • package/apps/Settings/src/com/android/settings/deviceinfo/StorageItemPreference.java
  public void setStorageSize(long size, long total) {
        setSummary(
                FileSizeFormatter.formatFileSize(
                        getContext(),
                        size,
                        MeasureUnit.GIGABYTE,
                        FileSizeFormatter.GIGABYTE_IN_BYTES));
        if (total == 0) {
            mProgressPercent = 0;
        } else {
            mProgressPercent = (int)(size * PROGRESS_MAX / total);
        }
        updateProgressBar();
  }

更新Summary有调用到FileSizeFormatter中的formatFileSize方法

  • package/apps/Settings/src/com/android/settings/utils/FileSizeFormatter.java
  private static String formatRoundedBytesResult(
            @NonNull Context context, @NonNull RoundedBytesResult input) {
        // add for change 吉字节 to GB   
        final Locale locale = /*localeFromContext(context)*/Locale.US;
        // add end
        final NumberFormat numberFormatter = getNumberFormatter(locale, input.fractionDigits);
        return formatMeasureShort(locale, numberFormatter, input.value, input.units);
  }
  
  public static String formatFileSize(
            @Nullable Context context, long sizeBytes, MeasureUnit unit, long mult) {
        if (context == null) {
            return "";
        }
        final RoundedBytesResult res = formatBytes(sizeBytes, unit, mult);
        return bidiWrap(context, formatRoundedBytesResult(context, res));
  }

Android O 里的网络流量使用、存储容量这两处位置的单位显示并非使用了不同语言的 values,而是使用了 Formatter 类来控制修改。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值