1/* 2 * Copyright (C) 2016 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 com.google.android.leanbackjank.data; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.graphics.Bitmap.CompressFormat; 22import android.graphics.Canvas; 23import android.graphics.Color; 24import android.graphics.LinearGradient; 25import android.graphics.Paint; 26import android.graphics.Rect; 27import android.graphics.Shader; 28import android.graphics.Shader.TileMode; 29import android.net.Uri; 30import android.util.Log; 31 32import com.google.android.leanbackjank.model.VideoInfo; 33 34import java.io.File; 35import java.io.FileOutputStream; 36import java.io.IOException; 37import java.util.ArrayList; 38import java.util.Arrays; 39import java.util.Collections; 40import java.util.HashMap; 41import java.util.List; 42import java.util.Locale; 43 44/** 45 * Generates fake data for populating the video cards. 46 */ 47public final class VideoProvider { 48 49 private static final String TAG = "JankVideoProvider"; 50 private static final int STUDIO_COUNT = 13; 51 52 private static final List<Integer> COLORS = Arrays.asList( 53 Color.parseColor("#4285F4"), 54 Color.parseColor("#EA4335"), 55 Color.parseColor("#FABB05"), 56 Color.parseColor("#34A853") 57 ); 58 59 private VideoProvider() { 60 } 61 62 public static HashMap<String, List<VideoInfo>> buildMedia(int categoryCount, int entriesPerCat, 63 int width, int height, Context context, boolean useSingleBitmap) { 64 HashMap<String, List<VideoInfo>> ret = new HashMap<>(); 65 66 int count = 0; 67 String rootPath = String.format(Locale.US, "%s/%d_%d/", context.getFilesDir(), width, 68 height); 69 File rootDirectory = new File(rootPath); 70 rootDirectory.mkdirs(); 71 72 for (int i = 0; i < categoryCount; i++) { 73 List<VideoInfo> list = new ArrayList<>(); 74 String category = "Category " + Integer.toString(i); 75 ret.put(category, list); 76 for (int j = 0; j < entriesPerCat; j++) { 77 String description = String.format(Locale.US, 78 "The gripping yet whimsical description of videoInfo %d in category %d", j, 79 i); 80 String title = String.format(Locale.US, "Video %d-%d", i, j); 81 String studio = String.format(Locale.US, "Studio %d", count % STUDIO_COUNT); 82 83 VideoInfo videoInfo = new VideoInfo(); 84 videoInfo.setId(Integer.toString(count)); 85 videoInfo.setTitle(title); 86 videoInfo.setDescription(description); 87 videoInfo.setStudio(studio); 88 videoInfo.setCategory(category); 89 90 int videoNumber = useSingleBitmap ? 0 : count; 91 File file = new File(rootPath + videoNumber + ".jpg"); 92 if (!file.exists()) { 93 makeIcon(width, height, "Jank", file); 94 } 95 videoInfo.setImageUri(Uri.fromFile(file)); 96 97 count++; 98 99 list.add(videoInfo); 100 } 101 } 102 103 return ret; 104 } 105 106 public static void makeIcon(int width, int height, String string, File file) { 107 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 108 Canvas canvas = new Canvas(bitmap); 109 110 Collections.shuffle(COLORS); 111 112 Paint paint = new Paint(); 113 paint.setAntiAlias(true); 114 paint.setTextAlign(Paint.Align.CENTER); 115 116 // Draw background gradient. 117 Shader shader = new LinearGradient(0, 0, width - 1, height - 1, COLORS.get(0), 118 COLORS.get(1), TileMode.CLAMP); 119 paint.setShader(shader); 120 canvas.drawRect(0, 0, width - 1, height - 1, paint); 121 122 paint.setTextSize(height * 0.5f); 123 Rect rect = new Rect(); 124 paint.getTextBounds(string, 0, string.length(), rect); 125 126 int hOffset = (height - rect.height()) / 2; 127 int wOffset = (width - rect.width()) / 2; 128 shader = new LinearGradient(wOffset, height - hOffset, width - wOffset, hOffset, 129 COLORS.get(2), COLORS.get(3), TileMode.CLAMP); 130 paint.setShader(shader); 131 132 canvas.drawText(string, width / 2, (height + rect.height()) / 2, paint); 133 134 try { 135 FileOutputStream outputStream = new FileOutputStream(file); 136 bitmap.compress(CompressFormat.JPEG, 90, outputStream); 137 } catch (IOException e) { 138 Log.e(TAG, "Cannot write image to file: " + file, e); 139 } 140 } 141} 142