private void updatePreference(StorageItemPreference pref, long size) {
   if (size > 0) {
     pref.setSummary(formatSize(size));
     final int order = pref.getOrder();
     mUsageBarPreference.addEntry(order, size / (float) mTotalSize, pref.color);
   } else {
     removePreference(pref);
   }
 }
  public void updateApproximate(long totalSize, long availSize) {
    mItemTotal.setSummary(formatSize(totalSize));
    mItemAvailable.setSummary(formatSize(availSize));

    mTotalSize = totalSize;

    final long usedSize = totalSize - availSize;

    mUsageBarPreference.clear();
    mUsageBarPreference.addEntry(0, usedSize / (float) totalSize, android.graphics.Color.GRAY);
    mUsageBarPreference.commit();

    updatePreferencesFromState();
  }
  public void updateDetails(MeasurementDetails details) {
    final boolean showDetails = mVolume == null || mVolume.isPrimary();
    if (!showDetails) return;

    // Count caches as available space, since system manages them
    mItemTotal.setSummary(formatSize(details.totalSize));
    mItemAvailable.setSummary(formatSize(details.availSize));

    mUsageBarPreference.clear();

    updatePreference(mItemApps, details.appsSize);

    //		Log.i("zhao", "details.mediaSize000000000000" );

    final long dcimSize =
        totalValues(
            details.mediaSize,
            Environment.DIRECTORY_DCIM,
            Environment.DIRECTORY_MOVIES,
            Environment.DIRECTORY_PICTURES);
    updatePreference(mItemDcim, dcimSize);

    //		Log.i("zhao", "details.mediaSize11111111111111" );
    final long musicSize =
        totalValues(
            details.mediaSize,
            Environment.DIRECTORY_MUSIC,
            Environment.DIRECTORY_ALARMS,
            Environment.DIRECTORY_NOTIFICATIONS,
            Environment.DIRECTORY_RINGTONES,
            Environment.DIRECTORY_PODCASTS);
    updatePreference(mItemMusic, musicSize);

    //		Log.i("zhao", "details.mediaSize222222222222" );
    final long downloadsSize = totalValues(details.mediaSize, Environment.DIRECTORY_DOWNLOADS);
    updatePreference(mItemDownloads, downloadsSize);

    updatePreference(mItemCache, details.cacheSize);
    updatePreference(mItemMisc, details.miscSize);

    for (StorageItemPreference userPref : mItemUsers) {
      final long userSize = details.usersSize.get(userPref.userHandle);
      updatePreference(userPref, userSize);
    }

    mUsageBarPreference.commit();
  }
  private void updatePreferencesFromState() {
    // Only update for physical volumes
    if (mVolume == null) return;

    if (mMountTogglePreference != null) {
      mMountTogglePreference.setEnabled(true);
    }

    final String state = mStorageManager.getVolumeState(mVolume.getPath());

    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
      mItemAvailable.setSummary(R.string.memory_available_read_only);
      if (mFormatPreference != null) {
        removePreference(mFormatPreference);
      }
    } else {
      mItemAvailable.setSummary(R.string.memory_available);
    }

    if (Environment.MEDIA_MOUNTED.equals(state)
        || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
      if (mMountTogglePreference != null) {
        mMountTogglePreference.setEnabled(true);
        mMountTogglePreference.setTitle(mResources.getString(R.string.sd_eject));
        mMountTogglePreference.setSummary(mResources.getString(R.string.sd_eject_summary));
      }
    } else {
      if (Environment.MEDIA_UNMOUNTED.equals(state)
          || Environment.MEDIA_NOFS.equals(state)
          || Environment.MEDIA_UNMOUNTABLE.equals(state)) {
        if (mMountTogglePreference != null) {
          mMountTogglePreference.setEnabled(true);
          mMountTogglePreference.setTitle(mResources.getString(R.string.sd_mount));
          mMountTogglePreference.setSummary(mResources.getString(R.string.sd_mount_summary));
        }
      } else {
        if (mMountTogglePreference != null) {
          mMountTogglePreference.setEnabled(false);
          mMountTogglePreference.setTitle(mResources.getString(R.string.sd_mount));
          mMountTogglePreference.setSummary(mResources.getString(R.string.sd_insert_summary));
        }
      }

      removePreference(mUsageBarPreference);
      removePreference(mItemTotal);
      removePreference(mItemAvailable);
      if (mFormatPreference != null) {
        removePreference(mFormatPreference);
      }
    }

    if (mUsbConnected
        && (UsbManager.USB_FUNCTION_MTP.equals(mUsbFunction)
            || UsbManager.USB_FUNCTION_PTP.equals(mUsbFunction))) {
      if (mMountTogglePreference != null) {
        mMountTogglePreference.setEnabled(false);
      }

      if (Environment.MEDIA_MOUNTED.equals(state)
          || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        if (mMountTogglePreference != null) {
          mMountTogglePreference.setSummary(mResources.getString(R.string.mtp_ptp_mode_summary));
        }
      }

      if (mFormatPreference != null) {
        mFormatPreference.setEnabled(false);
        mFormatPreference.setSummary(mResources.getString(R.string.mtp_ptp_mode_summary));
      }
    } else if (mFormatPreference != null) {
      mFormatPreference.setEnabled(true);
      mFormatPreference.setSummary(mResources.getString(R.string.sd_format_summary));
    }
  }
  public void init() {
    final Context context = getContext();

    final UserInfo currentUser;
    try {
      currentUser = ActivityManagerNative.getDefault().getCurrentUser();
    } catch (RemoteException e) {
      throw new RuntimeException("Failed to get current user");
    }

    final List<UserInfo> otherUsers = getUsersExcluding(currentUser);
    final boolean showUsers = mVolume == null && otherUsers.size() > 0;

    mUsageBarPreference = new UsageBarPreference(context);
    mUsageBarPreference.setOrder(ORDER_USAGE_BAR);
    addPreference(mUsageBarPreference);

    mItemTotal = buildItem(R.string.memory_size, 0);
    mItemAvailable = buildItem(R.string.memory_available, R.color.memory_avail);
    addPreference(mItemTotal);
    addPreference(mItemAvailable);

    mItemApps = buildItem(R.string.memory_apps_usage, R.color.memory_apps_usage);
    mItemDcim = buildItem(R.string.memory_dcim_usage, R.color.memory_dcim);
    mItemMusic = buildItem(R.string.memory_music_usage, R.color.memory_music);
    mItemDownloads = buildItem(R.string.memory_downloads_usage, R.color.memory_downloads);
    mItemCache = buildItem(R.string.memory_media_cache_usage, R.color.memory_cache);
    mItemMisc = buildItem(R.string.memory_media_misc_usage, R.color.memory_misc);

    mItemCache.setKey(KEY_CACHE);

    final boolean showDetails = mVolume == null || mVolume.isPrimary();
    if (showDetails) {
      if (showUsers) {
        addPreference(new PreferenceHeader(context, currentUser.name));
      }

      addPreference(mItemApps);
      addPreference(mItemDcim);
      addPreference(mItemMusic);
      addPreference(mItemDownloads);
      addPreference(mItemCache);
      addPreference(mItemMisc);

      if (showUsers) {
        addPreference(new PreferenceHeader(context, R.string.storage_other_users));

        int count = 0;
        for (UserInfo info : otherUsers) {
          final int colorRes =
              count++ % 2 == 0 ? R.color.memory_user_light : R.color.memory_user_dark;
          final StorageItemPreference userPref =
              new StorageItemPreference(getContext(), info.name, colorRes, info.id);
          mItemUsers.add(userPref);
          addPreference(userPref);
        }
      }
    }

    final boolean isRemovable = mVolume != null ? mVolume.isRemovable() : false;
    if (isRemovable) {
      mMountTogglePreference = new Preference(context);
      mMountTogglePreference.setTitle(R.string.sd_eject);
      mMountTogglePreference.setSummary(R.string.sd_eject_summary);
      addPreference(mMountTogglePreference);
    }

    // Only allow formatting of primary physical storage
    // TODO: enable for non-primary volumes once MTP is fixed
    final boolean allowFormat = mVolume != null ? mVolume.isPrimary() : false;
    if (allowFormat) {
      mFormatPreference = new Preference(context);
      mFormatPreference.setTitle(R.string.sd_format);
      mFormatPreference.setSummary(R.string.sd_format_summary);
      addPreference(mFormatPreference);
    }

    final IPackageManager pm = ActivityThread.getPackageManager();
    try {
      if (pm.isStorageLow()) {
        mStorageLow = new Preference(context);
        mStorageLow.setOrder(ORDER_STORAGE_LOW);
        mStorageLow.setTitle(R.string.storage_low_title);
        mStorageLow.setSummary(R.string.storage_low_summary);
        addPreference(mStorageLow);
      } else if (mStorageLow != null) {
        removePreference(mStorageLow);
        mStorageLow = null;
      }
    } catch (RemoteException e) {
    }
  }