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