Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PlaceholderView.java « widget « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f27feb3f67da42a81c67639621c451bba853e301 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.mapswithme.maps.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;

public class PlaceholderView extends LinearLayout
{
  @SuppressWarnings("NullableProblems")
  @NonNull
  private ImageView mImage;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mTitle;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mSubtitle;

  private int mImgMaxHeight;
  private int mImgMinHeight;

  @DrawableRes
  private int mImgSrcDefault;
  @StringRes
  private int mTitleResIdDefault;
  @StringRes
  private int mSubtitleResIdDefault;

  public PlaceholderView(Context context)
  {
    this(context, null, 0);
  }

  public PlaceholderView(Context context, @Nullable AttributeSet attrs)
  {
    this(context, attrs, 0);
  }

  public PlaceholderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
  {
    super(context, attrs, defStyleAttr);

    init(context, attrs);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public PlaceholderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
                         int defStyleRes)
  {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs)
  {
    Resources res = getResources();
    mImgMaxHeight = res.getDimensionPixelSize(R.dimen.placeholder_size);
    mImgMinHeight = res.getDimensionPixelSize(R.dimen.placeholder_size_small);
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.placeholder_image, this, true);
    inflater.inflate(R.layout.placeholder_title, this, true);
    inflater.inflate(R.layout.placeholder_subtitle, this, true);

    setOrientation(VERTICAL);
    initDefaultValues(context, attrs);
  }

  private void initDefaultValues(Context context, AttributeSet attrs)
  {
    TypedArray attrsArray = null;
    try
    {
      attrsArray =
          context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlaceholderView, 0, 0);
      mImgSrcDefault = attrsArray.getResourceId(
          R.styleable.PlaceholderView_imgSrcDefault,
          UiUtils.NO_ID);
      mTitleResIdDefault = attrsArray.getResourceId(
          R.styleable.PlaceholderView_titleDefault,
          UiUtils.NO_ID);
      mSubtitleResIdDefault = attrsArray.getResourceId(
          R.styleable.PlaceholderView_subTitleDefault,
          UiUtils.NO_ID);
    }
    finally
    {
      if (attrsArray != null)
        attrsArray.recycle();
    }
  }

  @Override
  protected void onFinishInflate()
  {
    super.onFinishInflate();

    mImage = findViewById(R.id.image);
    mTitle = findViewById(R.id.title);
    mSubtitle = findViewById(R.id.subtitle);

    setupDefaultContent();
  }

  private void setupDefaultContent()
  {
    if (isDefaultValueValid(mImgSrcDefault))
    {
      mImage.setImageResource(mImgSrcDefault);
    }
    if (isDefaultValueValid(mTitleResIdDefault))
    {
      mTitle.setText(mTitleResIdDefault);
    }
    if (isDefaultValueValid(mSubtitleResIdDefault))
    {
      mSubtitle.setText(mSubtitleResIdDefault);
    }
  }

  private static boolean isDefaultValueValid(int defaultResId)
  {
    return defaultResId != UiUtils.NO_ID;
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    int childrenTextTotalHeight = calcTotalTextChildrenHeight(widthMeasureSpec, heightMeasureSpec);

    final int defHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    MarginLayoutParams imgParams = (MarginLayoutParams) mImage.getLayoutParams();
    int potentialHeight =
        defHeight - getPaddingBottom() - getPaddingTop() - childrenTextTotalHeight -
        imgParams.bottomMargin - imgParams.topMargin;


    int imgSpaceRaw = Math.min(potentialHeight, mImgMaxHeight);
    imgParams.height = imgSpaceRaw;
    imgParams.width = imgSpaceRaw;
    measureChildWithMargins(mImage, widthMeasureSpec, 0, heightMeasureSpec, 0);

    boolean isImageSpaceAllowed = imgSpaceRaw > mImgMinHeight;
    int childrenTotalHeight = childrenTextTotalHeight;
    if (isImageSpaceAllowed)
      childrenTotalHeight += calcHeightWithMargins(mImage);

    UiUtils.showIf(isImageSpaceAllowed, mImage);
    final int height = childrenTotalHeight  + getPaddingTop() + getPaddingBottom();
    final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    setMeasuredDimension(width, height);
  }

  private int calcTotalTextChildrenHeight(int widthMeasureSpec, int heightMeasureSpec)
  {
    int totalHeight = 0;
    for (int index = 0; index < getChildCount(); index++)
    {
      View child = getChildAt(index);
      if (child.getVisibility() == VISIBLE && child != mImage)
      {
        measureChildWithMargins(child, widthMeasureSpec , 0, heightMeasureSpec, 0);
        totalHeight += calcHeightWithMargins(child);
      }
    }
    return totalHeight;
  }

  private static int calcHeightWithMargins(@NonNull View view) {
    MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
    return view.getMeasuredHeight() + params.bottomMargin + params.topMargin;
  }

  public void setContent(@DrawableRes int imageRes, @StringRes int titleRes,
                         @StringRes int subtitleRes)
  {
    mImage.setImageResource(imageRes);
    mTitle.setText(titleRes);
    mSubtitle.setText(subtitleRes);
  }
}