private static void getAppliedFilters(Path path, int[] result, boolean underCluster) {
    String[] segments = path.split();
    // Recurse into sub media sets.
    for (int i = 0; i < segments.length; i++) {
      if (segments[i].startsWith("{")) {
        String[] sets = Path.splitSequence(segments[i]);
        for (int j = 0; j < sets.length; j++) {
          Path sub = Path.fromString(sets[j]);
          getAppliedFilters(sub, result, underCluster);
        }
      }
    }

    // update current selection
    if (segments[0].equals("cluster")) {
      // if this is a clustered album, set underCluster to true.
      if (segments.length == 4) {
        underCluster = true;
      }

      int ctype = toClusterType(segments[2]);
      result[CLUSTER_TYPE] |= ctype;
      result[CLUSTER_CURRENT_TYPE] = ctype;
      if (underCluster) {
        result[CLUSTER_TYPE_F] |= ctype;
      }
    }
  }
  public static void setupMenuItems(GalleryActionBar actionBar, Path path, boolean inAlbum) {
    int[] result = new int[6];
    getAppliedFilters(path, result);
    int ctype = result[CLUSTER_TYPE];
    int ftype = result[FILTER_TYPE];
    int ftypef = result[FILTER_TYPE_F];
    int ccurrent = result[CLUSTER_CURRENT_TYPE];
    int fcurrent = result[FILTER_CURRENT_TYPE];

    setMenuItemApplied(
        actionBar,
        CLUSTER_BY_TIME,
        (ctype & CLUSTER_BY_TIME) != 0,
        (ccurrent & CLUSTER_BY_TIME) != 0);
    setMenuItemApplied(
        actionBar,
        CLUSTER_BY_LOCATION,
        (ctype & CLUSTER_BY_LOCATION) != 0,
        (ccurrent & CLUSTER_BY_LOCATION) != 0);
    setMenuItemApplied(
        actionBar, CLUSTER_BY_TAG, (ctype & CLUSTER_BY_TAG) != 0, (ccurrent & CLUSTER_BY_TAG) != 0);
    setMenuItemApplied(
        actionBar,
        CLUSTER_BY_FACE,
        (ctype & CLUSTER_BY_FACE) != 0,
        (ccurrent & CLUSTER_BY_FACE) != 0);

    actionBar.setClusterItemVisibility(CLUSTER_BY_ALBUM, !inAlbum || ctype == 0);

    setMenuItemApplied(actionBar, R.id.action_cluster_album, ctype == 0, ccurrent == 0);

    // A filtering is available if it's not applied, and the old filtering
    // (if any) is not fixed.
    setMenuItemAppliedEnabled(
        actionBar,
        R.string.show_images_only,
        (ftype & FILTER_IMAGE_ONLY) != 0,
        (ftype & FILTER_IMAGE_ONLY) == 0 && ftypef == 0,
        (fcurrent & FILTER_IMAGE_ONLY) != 0);
    setMenuItemAppliedEnabled(
        actionBar,
        R.string.show_videos_only,
        (ftype & FILTER_VIDEO_ONLY) != 0,
        (ftype & FILTER_VIDEO_ONLY) == 0 && ftypef == 0,
        (fcurrent & FILTER_VIDEO_ONLY) != 0);
    setMenuItemAppliedEnabled(
        actionBar, R.string.show_all, ftype == 0, ftype != 0 && ftypef == 0, fcurrent == 0);
  }
 // Gets the filters applied in the path.
 private static void getAppliedFilters(Path path, int[] result) {
   getAppliedFilters(path, result, false);
 }