BackgroundHelper.java revision c39d9c75590eca86a5e7e32a8824ba04a0d42e9b
1/* 2 * Copyright (C) 2015 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 */ 16package android.support.v17.leanback.widget; 17 18import android.graphics.drawable.Drawable; 19import android.os.Build; 20import android.support.annotation.RestrictTo; 21import android.support.v17.leanback.widget.BackgroundHelperKitkat; 22import android.view.View; 23 24import static android.support.annotation.RestrictTo.Scope.GROUP_ID; 25 26 27/** 28 * Helper for view backgrounds. 29 * @hide 30 */ 31@RestrictTo(GROUP_ID) 32public final class BackgroundHelper { 33 34 final static BackgroundHelperVersionImpl sImpl; 35 36 interface BackgroundHelperVersionImpl { 37 void setBackgroundPreservingAlpha(View view, Drawable drawable); 38 } 39 40 private static final class BackgroundHelperStubImpl implements BackgroundHelperVersionImpl { 41 BackgroundHelperStubImpl() { 42 } 43 44 @Override 45 public void setBackgroundPreservingAlpha(View view, Drawable drawable) { 46 // Cannot query drawable alpha 47 view.setBackground(drawable); 48 } 49 } 50 51 private static final class BackgroundHelperKitkatImpl implements BackgroundHelperVersionImpl { 52 BackgroundHelperKitkatImpl() { 53 } 54 55 @Override 56 public void setBackgroundPreservingAlpha(View view, Drawable drawable) { 57 BackgroundHelperKitkat.setBackgroundPreservingAlpha(view, drawable); 58 } 59 } 60 61 private BackgroundHelper() { 62 } 63 64 static { 65 if (Build.VERSION.SDK_INT >= 19) { 66 sImpl = new BackgroundHelperKitkatImpl(); 67 } else { 68 sImpl = new BackgroundHelperStubImpl(); 69 } 70 } 71 72 public static void setBackgroundPreservingAlpha(View view, Drawable drawable) { 73 sImpl.setBackgroundPreservingAlpha(view, drawable); 74 } 75} 76