12590c39739945451f80dcf8667709b93293fc8bcCraig Stout/*
22590c39739945451f80dcf8667709b93293fc8bcCraig Stout * Copyright (C) 2015 The Android Open Source Project
32590c39739945451f80dcf8667709b93293fc8bcCraig Stout *
42590c39739945451f80dcf8667709b93293fc8bcCraig Stout * Licensed under the Apache License, Version 2.0 (the "License");
52590c39739945451f80dcf8667709b93293fc8bcCraig Stout * you may not use this file except in compliance with the License.
62590c39739945451f80dcf8667709b93293fc8bcCraig Stout * You may obtain a copy of the License at
72590c39739945451f80dcf8667709b93293fc8bcCraig Stout *
82590c39739945451f80dcf8667709b93293fc8bcCraig Stout *      http://www.apache.org/licenses/LICENSE-2.0
92590c39739945451f80dcf8667709b93293fc8bcCraig Stout *
102590c39739945451f80dcf8667709b93293fc8bcCraig Stout * Unless required by applicable law or agreed to in writing, software
112590c39739945451f80dcf8667709b93293fc8bcCraig Stout * distributed under the License is distributed on an "AS IS" BASIS,
122590c39739945451f80dcf8667709b93293fc8bcCraig Stout * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132590c39739945451f80dcf8667709b93293fc8bcCraig Stout * See the License for the specific language governing permissions and
142590c39739945451f80dcf8667709b93293fc8bcCraig Stout * limitations under the License
152590c39739945451f80dcf8667709b93293fc8bcCraig Stout */
162590c39739945451f80dcf8667709b93293fc8bcCraig Stoutpackage android.support.v17.leanback.widget;
172590c39739945451f80dcf8667709b93293fc8bcCraig Stout
182590c39739945451f80dcf8667709b93293fc8bcCraig Stoutimport android.graphics.drawable.Drawable;
192590c39739945451f80dcf8667709b93293fc8bcCraig Stoutimport android.os.Build;
20c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo;
212590c39739945451f80dcf8667709b93293fc8bcCraig Stoutimport android.support.v17.leanback.widget.BackgroundHelperKitkat;
222590c39739945451f80dcf8667709b93293fc8bcCraig Stoutimport android.view.View;
232590c39739945451f80dcf8667709b93293fc8bcCraig Stout
24c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport static android.support.annotation.RestrictTo.Scope.GROUP_ID;
25c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette
262590c39739945451f80dcf8667709b93293fc8bcCraig Stout
272590c39739945451f80dcf8667709b93293fc8bcCraig Stout/**
282590c39739945451f80dcf8667709b93293fc8bcCraig Stout * Helper for view backgrounds.
292590c39739945451f80dcf8667709b93293fc8bcCraig Stout * @hide
302590c39739945451f80dcf8667709b93293fc8bcCraig Stout */
31c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette@RestrictTo(GROUP_ID)
322590c39739945451f80dcf8667709b93293fc8bcCraig Stoutpublic final class BackgroundHelper {
332590c39739945451f80dcf8667709b93293fc8bcCraig Stout
342590c39739945451f80dcf8667709b93293fc8bcCraig Stout    final static BackgroundHelperVersionImpl sImpl;
352590c39739945451f80dcf8667709b93293fc8bcCraig Stout
36c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    interface BackgroundHelperVersionImpl {
37c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette        void setBackgroundPreservingAlpha(View view, Drawable drawable);
382590c39739945451f80dcf8667709b93293fc8bcCraig Stout    }
392590c39739945451f80dcf8667709b93293fc8bcCraig Stout
402590c39739945451f80dcf8667709b93293fc8bcCraig Stout    private static final class BackgroundHelperStubImpl implements BackgroundHelperVersionImpl {
4199ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        BackgroundHelperStubImpl() {
4299ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        }
4399ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas
442590c39739945451f80dcf8667709b93293fc8bcCraig Stout        @Override
452590c39739945451f80dcf8667709b93293fc8bcCraig Stout        public void setBackgroundPreservingAlpha(View view, Drawable drawable) {
462590c39739945451f80dcf8667709b93293fc8bcCraig Stout            // Cannot query drawable alpha
472590c39739945451f80dcf8667709b93293fc8bcCraig Stout            view.setBackground(drawable);
482590c39739945451f80dcf8667709b93293fc8bcCraig Stout        }
492590c39739945451f80dcf8667709b93293fc8bcCraig Stout    }
502590c39739945451f80dcf8667709b93293fc8bcCraig Stout
512590c39739945451f80dcf8667709b93293fc8bcCraig Stout    private static final class BackgroundHelperKitkatImpl implements BackgroundHelperVersionImpl {
5299ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        BackgroundHelperKitkatImpl() {
5399ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        }
5499ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas
552590c39739945451f80dcf8667709b93293fc8bcCraig Stout        @Override
562590c39739945451f80dcf8667709b93293fc8bcCraig Stout        public void setBackgroundPreservingAlpha(View view, Drawable drawable) {
572590c39739945451f80dcf8667709b93293fc8bcCraig Stout            BackgroundHelperKitkat.setBackgroundPreservingAlpha(view, drawable);
582590c39739945451f80dcf8667709b93293fc8bcCraig Stout        }
592590c39739945451f80dcf8667709b93293fc8bcCraig Stout    }
602590c39739945451f80dcf8667709b93293fc8bcCraig Stout
612590c39739945451f80dcf8667709b93293fc8bcCraig Stout    private BackgroundHelper() {
622590c39739945451f80dcf8667709b93293fc8bcCraig Stout    }
632590c39739945451f80dcf8667709b93293fc8bcCraig Stout
642590c39739945451f80dcf8667709b93293fc8bcCraig Stout    static {
652590c39739945451f80dcf8667709b93293fc8bcCraig Stout        if (Build.VERSION.SDK_INT >= 19) {
662590c39739945451f80dcf8667709b93293fc8bcCraig Stout            sImpl = new BackgroundHelperKitkatImpl();
672590c39739945451f80dcf8667709b93293fc8bcCraig Stout        } else {
682590c39739945451f80dcf8667709b93293fc8bcCraig Stout            sImpl = new BackgroundHelperStubImpl();
692590c39739945451f80dcf8667709b93293fc8bcCraig Stout        }
702590c39739945451f80dcf8667709b93293fc8bcCraig Stout    }
712590c39739945451f80dcf8667709b93293fc8bcCraig Stout
722590c39739945451f80dcf8667709b93293fc8bcCraig Stout    public static void setBackgroundPreservingAlpha(View view, Drawable drawable) {
732590c39739945451f80dcf8667709b93293fc8bcCraig Stout        sImpl.setBackgroundPreservingAlpha(view, drawable);
742590c39739945451f80dcf8667709b93293fc8bcCraig Stout    }
752590c39739945451f80dcf8667709b93293fc8bcCraig Stout}
76