1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.arch.paging.integration.testapp;
18
19import android.arch.paging.BoundedDataSource;
20import android.graphics.Color;
21import android.support.annotation.ColorInt;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Sample data source with artificial data.
28 */
29class ItemDataSource extends BoundedDataSource<Item> {
30    private static final int COUNT = 500;
31
32    @ColorInt
33    private static final int[] COLORS = new int[] {
34            Color.RED,
35            Color.BLUE,
36            Color.BLACK,
37    };
38
39    private static int sGenerationId;
40    private final int mGenerationId = sGenerationId++;
41
42    @Override
43    public int countItems() {
44        return COUNT;
45    }
46
47    @Override
48    public List<Item> loadRange(int startPosition, int loadCount) {
49        if (isInvalid()) {
50            // abort!
51            return null;
52        }
53
54        List<Item> items = new ArrayList<>();
55        int end = Math.min(COUNT, startPosition + loadCount);
56        int bgColor = COLORS[mGenerationId % COLORS.length];
57
58        try {
59            Thread.sleep(1000);
60        } catch (InterruptedException e) {
61            e.printStackTrace();
62        }
63        for (int i = startPosition; i != end; i++) {
64            items.add(new Item(i, "item " + i, bgColor));
65        }
66
67        if (isInvalid()) {
68            // abort!
69            return null;
70        }
71        return items;
72    }
73}
74