summaryrefslogtreecommitdiff
path: root/java/com/android/contacts/common/util/ContactListViewUtils.java
blob: 924789b16c68e1469767d1b9f3fb9a9238e1b7ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.common.util;

import android.content.res.Resources;
import android.view.View;
import android.widget.ListView;
import com.android.dialer.contacts.resources.R;
import com.android.dialer.util.ViewUtil;

/** Utilities for configuring ListViews with a card background. */
public class ContactListViewUtils {

  // These two constants will help add more padding for the text inside the card.
  private static final double TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO = 1.1;

  private static void addPaddingToView(
      ListView listView, int parentWidth, int listSpaceWeight, int listViewWeight) {
    if (listSpaceWeight > 0 && listViewWeight > 0) {
      double paddingPercent =
          (double) listSpaceWeight / (double) (listSpaceWeight * 2 + listViewWeight);
      listView.setPadding(
          (int) (parentWidth * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO),
          listView.getPaddingTop(),
          (int) (parentWidth * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO),
          listView.getPaddingBottom());
      // The EdgeEffect and ScrollBar need to span to the edge of the ListView's padding.
      listView.setClipToPadding(false);
      listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    }
  }

  /**
   * Add padding to {@param listView} if this configuration has set both space weight and view
   * weight on the layout. Use this util method instead of defining the padding in the layout file
   * so that the {@param listView}'s padding can be set proportional to the card padding.
   *
   * @param listView ListView that we add padding to
   * @param rootLayout layout that contains ListView and R.id.list_card
   */
  public static void applyCardPaddingToView(
      Resources resources, final ListView listView, final View rootLayout) {
    // Set a padding on the list view so it appears in the center of the card
    // in the layout if required.
    final int listSpaceWeight = resources.getInteger(R.integer.contact_list_space_layout_weight);
    final int listViewWeight = resources.getInteger(R.integer.contact_list_card_layout_weight);
    if (listSpaceWeight > 0 && listViewWeight > 0) {
      rootLayout.setBackgroundResource(0);
      // Set the card view visible
      View mCardView = rootLayout.findViewById(R.id.list_card);
      if (mCardView == null) {
        throw new RuntimeException(
            "Your content must have a list card view who can be turned visible "
                + "whenever it is necessary.");
      }
      mCardView.setVisibility(View.VISIBLE);

      // Add extra padding to the list view to make them appear in the center of the card.
      // In order to avoid jumping, we skip drawing the next frame of the ListView.
      ViewUtil.doOnPreDraw(
          listView,
          false,
          new Runnable() {
            @Override
            public void run() {
              // Use the rootLayout.getWidth() instead of listView.getWidth() since
              // we sometimes hide the listView until we finish loading data. This would
              // result in incorrect padding.
              ContactListViewUtils.addPaddingToView(
                  listView, rootLayout.getWidth(), listSpaceWeight, listViewWeight);
            }
          });
    }
  }
}