summaryrefslogtreecommitdiff
path: root/android/slice/views/GridView.java
blob: 18a90f7d1405d4b37f26be9219d3a25bf58bd8e0 (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
/*
 * Copyright (C) 2017 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 android.slice.views;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import android.content.Context;
import android.graphics.Color;
import android.slice.Slice;
import android.slice.SliceItem;
import android.slice.views.LargeSliceAdapter.SliceListView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.internal.R;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @hide
 */
public class GridView extends LinearLayout implements SliceListView {

    private static final String TAG = "GridView";

    private static final int MAX_IMAGES = 3;
    private static final int MAX_ALL = 5;
    private boolean mIsAllImages;

    public GridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mIsAllImages) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = width / getChildCount();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
                    height);
            getLayoutParams().height = height;
            for (int i = 0; i < getChildCount(); i++) {
                getChildAt(i).getLayoutParams().height = height;
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void setSliceItem(SliceItem slice) {
        mIsAllImages = true;
        removeAllViews();
        int total = 1;
        if (slice.getType() == SliceItem.TYPE_SLICE) {
            SliceItem[] items = slice.getSlice().getItems();
            total = items.length;
            for (int i = 0; i < total; i++) {
                SliceItem item = items[i];
                if (isFull()) {
                    continue;
                }
                if (!addItem(item)) {
                    mIsAllImages = false;
                }
            }
        } else {
            if (!isFull()) {
                if (!addItem(slice)) {
                    mIsAllImages = false;
                }
            }
        }
        if (total > getChildCount() && mIsAllImages) {
            addExtraCount(total - getChildCount());
        }
    }

    private void addExtraCount(int numExtra) {
        View last = getChildAt(getChildCount() - 1);
        FrameLayout frame = new FrameLayout(getContext());
        frame.setLayoutParams(last.getLayoutParams());

        removeView(last);
        frame.addView(last, new LayoutParams(MATCH_PARENT, MATCH_PARENT));

        TextView v = new TextView(getContext());
        v.setTextColor(Color.WHITE);
        v.setBackgroundColor(0x4d000000);
        v.setText(getResources().getString(R.string.slice_more_content, numExtra));
        v.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        v.setGravity(Gravity.CENTER);
        frame.addView(v, new LayoutParams(MATCH_PARENT, MATCH_PARENT));

        addView(frame);
    }

    private boolean isFull() {
        return getChildCount() >= (mIsAllImages ? MAX_IMAGES : MAX_ALL);
    }

    /**
     * Returns true if this item is just an image.
     */
    private boolean addItem(SliceItem item) {
        if (item.getType() == SliceItem.TYPE_IMAGE) {
            ImageView v = new ImageView(getContext());
            v.setImageIcon(item.getIcon());
            v.setScaleType(ScaleType.CENTER_CROP);
            addView(v, new LayoutParams(0, MATCH_PARENT, 1));
            return true;
        } else {
            LinearLayout v = new LinearLayout(getContext());
            int s = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    12, getContext().getResources().getDisplayMetrics());
            v.setPadding(0, s, 0, 0);
            v.setOrientation(LinearLayout.VERTICAL);
            v.setGravity(Gravity.CENTER_HORIZONTAL);
            // TODO: Unify sporadic inflates that happen throughout the code.
            ArrayList<SliceItem> items = new ArrayList<>();
            if (item.getType() == SliceItem.TYPE_SLICE) {
                items.addAll(Arrays.asList(item.getSlice().getItems()));
            }
            items.forEach(i -> {
                Context context = getContext();
                switch (i.getType()) {
                    case SliceItem.TYPE_TEXT:
                        boolean title = false;
                        if ((item.hasAnyHints(new String[] {
                                Slice.HINT_LARGE, Slice.HINT_TITLE
                        }))) {
                            title = true;
                        }
                        TextView tv = (TextView) LayoutInflater.from(context).inflate(
                                title ? R.layout.slice_title : R.layout.slice_secondary_text, null);
                        tv.setText(i.getText());
                        v.addView(tv);
                        break;
                    case SliceItem.TYPE_IMAGE:
                        ImageView iv = new ImageView(context);
                        iv.setImageIcon(i.getIcon());
                        if (item.hasHint(Slice.HINT_LARGE)) {
                            iv.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
                        } else {
                            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                    48, context.getResources().getDisplayMetrics());
                            iv.setLayoutParams(new LayoutParams(size, size));
                        }
                        v.addView(iv);
                        break;
                    case SliceItem.TYPE_REMOTE_VIEW:
                        v.addView(i.getRemoteView().apply(context, v));
                        break;
                    case SliceItem.TYPE_COLOR:
                        // TODO: Support color to tint stuff here.
                        break;
                }
            });
            addView(v, new LayoutParams(0, WRAP_CONTENT, 1));
            return false;
        }
    }
}