summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/browse/ConversationItemViewCoordinates.java
blob: aca8f43c899bb7864a429856795877f5040ff381 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * Copyright (C) 2012 Google Inc.
 * Licensed to 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.mail.browse;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextPaint;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewParent;
import android.widget.TextView;
import com.android.mail.R;
import com.android.mail.ui.ViewMode;
import com.google.common.base.Objects;

/**
 * Represents the coordinates of elements inside a CanvasConversationHeaderView
 * (eg, checkmark, star, subject, sender, folders, etc.) It will inflate a view,
 * and record the coordinates of each element after layout. This will allows us
 * to easily improve performance by creating custom view while still defining
 * layout in XML files.
 *
 * @author phamm
 */
public class ConversationItemViewCoordinates {
    // Modes.
    private static final int WIDE_MODE = 0;
    static final int NORMAL_MODE = 1;

    // Static threshold.
    private static int TOTAL_FOLDER_WIDTH = -1;
    private static int TOTAL_FOLDER_WIDTH_WIDE = -1;
    private static int FOLDER_CELL_WIDTH = -1;
    private static int sConversationHeights[];

    // For combined views
    private static int COLOR_BLOCK_WIDTH = -1;
    private static int COLOR_BLOCK_HEIGHT = -1;

    // Checkmark.
    int checkmarkX;
    int checkmarkY;

    // Star.
    int starX;
    int starY;

    // Personal level.
    int personalLevelX;
    int personalLevelY;
    boolean showPersonalLevel;
    boolean inlinePersonalLevel;

    // Senders.
    int sendersX;
    int sendersY;
    int sendersWidth;
    int sendersLineCount;
    int sendersLineHeight;
    int sendersFontSize;
    int sendersAscent;

    // Subject.
    int subjectX;
    int subjectY;
    int subjectWidth;
    int subjectLineCount;
    int subjectFontSize;
    int subjectAscent;

    // Folders.
    int foldersXEnd;
    int foldersY;
    int foldersHeight;
    int foldersTopPadding;
    int foldersFontSize;
    int foldersAscent;
    int foldersTextBottomPadding;
    boolean showFolders;
    boolean showColorBlock;

    // Date.
    int dateXEnd;
    int dateY;
    int dateFontSize;
    int dateAscent;

    // Paperclip.
    int paperclipY;

    // Reply state of a conversation.
    boolean showReplyState;
    int replyStateX;
    int replyStateY;

    // Minimum height of this view; used for animating.
    int minHeight;
    TextView sendersView;

    int contactImagesHeight;
    int contactImagesWidth;
    float contactImagesX;
    float contactImagesY;

    int dateTextWidth;
    int dateWidth;
    int dateHeight;

    // Cache to save Coordinates based on view width.
    private static SparseArray<ConversationItemViewCoordinates> sCache =
            new SparseArray<ConversationItemViewCoordinates>();

    private static TextPaint sPaint = new TextPaint();

    static {
        sPaint.setAntiAlias(true);
    }

    /**
     * Returns whether to show a background on the attachment icon.
     * Currently, we don't show a background in wide mode.
     */
    public static boolean showAttachmentBackground(int mode) {
        return mode != WIDE_MODE;
    }

    /**
     * Returns the mode of the header view (Wide/Normal/Narrow).
     */
    public static int getMode(Context context, int viewMode) {
        final Resources res = context.getResources();
        switch (viewMode) {
            case ViewMode.CONVERSATION_LIST:
                return res.getInteger(R.integer.conversation_list_header_mode);

            case ViewMode.SEARCH_RESULTS_LIST:
                return res.getInteger(R.integer.conversation_list_search_header_mode);

            default:
                return res.getInteger(R.integer.conversation_header_mode);
        }
    }

    /**
     * Returns the mode of the header view (Wide/Normal/Narrow).
     */
    public static int getMode(Context context, ViewMode viewMode) {
        return getMode(context, viewMode.getMode());
    }

    /**
     * Returns the layout id to be inflated in this mode.
     */
    private static int getLayoutId(int mode, boolean checkboxesEnabled) {
        if (checkboxesEnabled) {
            switch (mode) {
                case WIDE_MODE:
                    return R.layout.conversation_item_view_wide;
                case NORMAL_MODE:
                    return R.layout.conversation_item_view_normal;
                default:
                    throw new IllegalArgumentException("Unknown conversation header view mode "
                            + mode);
            }
        } else {
            switch (mode) {
                case WIDE_MODE:
                    return R.layout.conversation_item_view_wide_images;
                case NORMAL_MODE:
                    return R.layout.conversation_item_view_normal_images;
                default:
                    throw new IllegalArgumentException("Unknown conversation header view mode "
                            + mode);
            }
        }
    }

    /**
     * Returns a value array multiplied by the specified density.
     */
    public static int[] getDensityDependentArray(int[] values, float density) {
        int result[] = new int[values.length];
        for (int i = 0; i < values.length; ++i) {
            result[i] = (int) (values[i] * density);
        }
        return result;
    }

    /**
     * Returns the height of the view in this mode.
     */
    public static int getHeight(Context context, int mode) {
        Resources res = context.getResources();
        float density = res.getDisplayMetrics().scaledDensity;
        if (sConversationHeights == null) {
            sConversationHeights = getDensityDependentArray(
                    res.getIntArray(R.array.conversation_heights), density);
        }
        return sConversationHeights[mode];
    }

    /**
     * Refreshes the conversation heights array.
     */
    public static void refreshConversationHeights(Context context) {
        Resources res = context.getResources();
        float density = res.getDisplayMetrics().scaledDensity;
        sConversationHeights = getDensityDependentArray(
                res.getIntArray(R.array.conversation_heights), density);
    }

    /**
     * Returns the x coordinates of a view by tracing up its hierarchy.
     */
    private static int getX(View view) {
        int x = 0;
        while (view != null) {
            x += (int) view.getX();
            ViewParent parent = view.getParent();
            view = parent != null ? (View) parent : null;
        }
        return x;
    }

    /**
     * Returns the y coordinates of a view by tracing up its hierarchy.
     */
    private static int getY(View view) {
        int y = 0;
        while (view != null) {
            y += (int) view.getY();
            ViewParent parent = view.getParent();
            view = parent != null ? (View) parent : null;
        }
        return y;
    }

    /**
     * Returns the number of lines of this text view.
     */
    private static int getLineCount(TextView textView) {
        return Math.round(((float) textView.getHeight()) / textView.getLineHeight());
    }

    /**
     * Returns the length (maximum of characters) of subject in this mode.
     */
    public static int getSendersLength(Context context, int mode, boolean hasAttachments) {
        final Resources res = context.getResources();
        if (hasAttachments) {
            return res.getIntArray(R.array.senders_with_attachment_lengths)[mode];
        } else {
            return res.getIntArray(R.array.senders_lengths)[mode];
        }
    }

    public static int getColorBlockWidth(Context context) {
        Resources res = context.getResources();
        if (COLOR_BLOCK_WIDTH <= 0) {
            COLOR_BLOCK_WIDTH = res.getDimensionPixelSize(R.dimen.color_block_width);
        }
        return COLOR_BLOCK_WIDTH;
    }

    public static int getColorBlockHeight(Context context) {
        Resources res = context.getResources();
        if (COLOR_BLOCK_HEIGHT <= 0) {
            COLOR_BLOCK_HEIGHT = res.getDimensionPixelSize(R.dimen.color_block_height);
        }
        return COLOR_BLOCK_HEIGHT;
    }

    /**
     * Returns the width available to draw folders in this mode.
     */
    public static int getFoldersWidth(Context context, int mode) {
        Resources res = context.getResources();
        if (TOTAL_FOLDER_WIDTH <= 0) {
            TOTAL_FOLDER_WIDTH = res.getDimensionPixelSize(R.dimen.max_total_folder_width);
            TOTAL_FOLDER_WIDTH_WIDE = res.getDimensionPixelSize(R.dimen.max_total_folder_width_wide);
        }
        switch (mode) {
            case WIDE_MODE:
                return TOTAL_FOLDER_WIDTH_WIDE;
            case NORMAL_MODE:
                return TOTAL_FOLDER_WIDTH;
            default:
                throw new IllegalArgumentException("Unknown conversation header view mode " + mode);
        }
    }

    /**
     * Returns the width of a cell to draw folders.
     */
    public static int getLabelCellWidth(Context context, int mode, int foldersCount) {
        Resources res = context.getResources();
        if (FOLDER_CELL_WIDTH <= 0) {
            FOLDER_CELL_WIDTH = res.getDimensionPixelSize(R.dimen.folder_cell_width);
        }
        switch (mode) {
            case WIDE_MODE:
            case NORMAL_MODE:
                return FOLDER_CELL_WIDTH;
            default:
                throw new IllegalArgumentException("Unknown conversation header view mode " + mode);
        }
    }

    public static boolean displaySendersInline(int mode) {
        switch (mode) {
            case WIDE_MODE:
                return false;
            case NORMAL_MODE:
                return true;
            default:
                throw new IllegalArgumentException("Unknown conversation header view mode " + mode);
        }
    }

    /**
     * Returns coordinates for elements inside a conversation header view given
     * the view width.
     */
    public static ConversationItemViewCoordinates forWidth(Context context, int width, int mode,
            int standardScaledDimen, boolean checkboxesEnabled) {
        ConversationItemViewCoordinates coordinates = sCache.get(Objects.hashCode(width, mode,
                checkboxesEnabled));
        if (coordinates == null) {
            coordinates = new ConversationItemViewCoordinates();
            sCache.put(Objects.hashCode(width, mode, checkboxesEnabled), coordinates);

            // Layout the appropriate view.
            int height = getHeight(context, mode);
            View view = LayoutInflater.from(context).inflate(getLayoutId(mode, checkboxesEnabled),
                    null);
            int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            Resources res = context.getResources();

            view.measure(widthSpec, heightSpec);
            view.layout(0, 0, width, height);

            // Records coordinates.
            View checkmark = view.findViewById(R.id.checkmark);
            coordinates.checkmarkX = getX(checkmark);
            coordinates.checkmarkY = getY(checkmark);

            View star = view.findViewById(R.id.star);
            coordinates.starX = getX(star);
            coordinates.starY = getY(star);

            View personalLevel = view.findViewById(R.id.personal_level);
            if (personalLevel != null) {
                coordinates.showPersonalLevel = true;
                coordinates.personalLevelX = getX(personalLevel);
                coordinates.personalLevelY = getY(personalLevel);
                coordinates.inlinePersonalLevel = res.getBoolean(R.bool.inline_personal_level);
            } else {
                coordinates.showPersonalLevel = false;
                coordinates.inlinePersonalLevel = false;
            }

            TextView senders = (TextView) view.findViewById(R.id.senders);
            coordinates.sendersView = senders;
            coordinates.sendersX = getX(senders);
            coordinates.sendersY = getY(senders);
            coordinates.sendersWidth = senders.getWidth();
            coordinates.sendersLineCount = getLineCount(senders);
            coordinates.sendersLineHeight = senders.getLineHeight();
            coordinates.sendersFontSize = (int) senders.getTextSize();
            sPaint.setTextSize(coordinates.sendersFontSize);
            coordinates.sendersAscent = (int) sPaint.ascent();

            TextView subject = (TextView) view.findViewById(R.id.subject);
            coordinates.subjectX = getX(subject);
            coordinates.subjectY = getY(subject);
            coordinates.subjectWidth = subject.getWidth();
            coordinates.subjectLineCount = getLineCount(subject);
            coordinates.subjectFontSize = (int) subject.getTextSize();
            sPaint.setTextSize(coordinates.subjectFontSize);
            coordinates.subjectAscent = (int) sPaint.ascent();

            View folders = view.findViewById(R.id.folders);
            if (folders != null) {
                coordinates.showFolders = true;
                coordinates.foldersXEnd = getX(folders) + folders.getWidth();
                coordinates.foldersY = getY(folders);
                coordinates.foldersHeight = folders.getHeight();
                coordinates.foldersTopPadding = folders.getPaddingTop();
                coordinates.foldersTextBottomPadding = res
                        .getDimensionPixelSize(R.dimen.folders_text_bottom_padding);
                if (folders instanceof TextView) {
                    coordinates.foldersFontSize = (int) ((TextView) folders).getTextSize();
                    sPaint.setTextSize(coordinates.foldersFontSize);
                    coordinates.foldersAscent = (int) sPaint.ascent();
                }
            } else {
                coordinates.showFolders = false;
            }

            View colorBlock = view.findViewById(R.id.color_block);
            if (colorBlock != null) {
                coordinates.showColorBlock = true;
            }

            View replyState = view.findViewById(R.id.reply_state);
            if (replyState != null) {
                coordinates.showReplyState = true;
                coordinates.replyStateX = getX(replyState);
                coordinates.replyStateY = getY(replyState);
            } else {
                coordinates.showReplyState = false;
            }

            TextView date = (TextView) view.findViewById(R.id.date);
            coordinates.dateXEnd = getX(date) + date.getWidth();
            coordinates.dateY = getY(date);
            coordinates.dateFontSize = (int) date.getTextSize();
            coordinates.dateWidth = date.getWidth();
            coordinates.dateHeight = date.getHeight();
            sPaint.setTextSize(coordinates.dateFontSize);
            coordinates.dateAscent = (int) sPaint.ascent();

            View paperclip = view.findViewById(R.id.paperclip);
            coordinates.paperclipY = getY(paperclip);

            // Contact images view
            if (!checkboxesEnabled) {
            View contactImagesView = view.findViewById(R.id.contact_image);
                if (contactImagesView != null) {
                    coordinates.contactImagesWidth = contactImagesView.getWidth();
                    coordinates.contactImagesHeight = contactImagesView.getHeight();
                    coordinates.contactImagesX = getX(contactImagesView);
                    coordinates.contactImagesY = getY(contactImagesView);
                }
            }
        }
        return coordinates;
    }

    public static boolean displayFoldersAboveDate(int mode) {
        return mode == WIDE_MODE;
    }

    public static int getFolderCellWidth(Context context, int mode, int foldersCount) {
        Resources res = context.getResources();
        if (FOLDER_CELL_WIDTH <= 0) {
            FOLDER_CELL_WIDTH = res.getDimensionPixelSize(R.dimen.folder_cell_width);
        }
        switch (mode) {
            case WIDE_MODE:
            case NORMAL_MODE:
                return FOLDER_CELL_WIDTH;
            default:
                throw new IllegalArgumentException("Unknown conversation header view mode " + mode);
        }
    }

    public static boolean isWideMode(int mode) {
        return mode == WIDE_MODE;
    }

    public static int getMinHeight(Context context, ViewMode viewMode) {
        int mode = ConversationItemViewCoordinates.getMode(context, viewMode);
        return context.getResources().getDimensionPixelSize(
                mode == WIDE_MODE ?
                        R.dimen.conversation_item_height_wide : R.dimen.conversation_item_height);
    }
}