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 17 package com.android.tv.dvr.ui.browse; 18 19 import android.app.Activity; 20 import android.graphics.drawable.BitmapDrawable; 21 import android.graphics.drawable.ColorDrawable; 22 import android.graphics.drawable.Drawable; 23 import android.os.Handler; 24 import androidx.leanback.app.BackgroundManager; 25 26 /** The Background Helper. */ 27 public class DetailsViewBackgroundHelper { 28 // Background delay serves to avoid kicking off expensive bitmap loading 29 // in case multiple backgrounds are set in quick succession. 30 private static final int SET_BACKGROUND_DELAY_MS = 100; 31 32 private final BackgroundManager mBackgroundManager; 33 34 class LoadBackgroundRunnable implements Runnable { 35 final Drawable mBackGround; 36 LoadBackgroundRunnable(Drawable background)37 LoadBackgroundRunnable(Drawable background) { 38 mBackGround = background; 39 } 40 41 @Override run()42 public void run() { 43 if (!mBackgroundManager.isAttached()) { 44 return; 45 } 46 if (mBackGround instanceof BitmapDrawable) { 47 mBackgroundManager.setBitmap(((BitmapDrawable) mBackGround).getBitmap()); 48 } 49 mRunnable = null; 50 } 51 } 52 53 private LoadBackgroundRunnable mRunnable; 54 55 private final Handler mHandler = new Handler(); 56 DetailsViewBackgroundHelper(Activity activity)57 public DetailsViewBackgroundHelper(Activity activity) { 58 mBackgroundManager = BackgroundManager.getInstance(activity); 59 mBackgroundManager.attach(activity.getWindow()); 60 mBackgroundManager.setAutoReleaseOnStop(false); 61 } 62 63 /** Sets the given image to background. */ setBackground(Drawable background)64 public void setBackground(Drawable background) { 65 if (mRunnable != null) { 66 mHandler.removeCallbacks(mRunnable); 67 } 68 mRunnable = new LoadBackgroundRunnable(background); 69 mHandler.postDelayed(mRunnable, SET_BACKGROUND_DELAY_MS); 70 } 71 72 /** Sets the background color. */ setBackgroundColor(int color)73 public void setBackgroundColor(int color) { 74 if (mBackgroundManager.isAttached()) { 75 mBackgroundManager.setColor(color); 76 } 77 } 78 79 /** Sets the background scrim. */ setScrim(int color)80 public void setScrim(int color) { 81 if (mBackgroundManager.isAttached()) { 82 mBackgroundManager.setDimLayer(new ColorDrawable(color)); 83 } 84 } 85 } 86