1 /* 2 * Copyright (C) 2014 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.printspooler.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.accessibility.AccessibilityEvent; 22 import android.view.accessibility.AccessibilityNodeInfo; 23 import android.widget.CompoundButton; 24 import android.widget.LinearLayout; 25 import com.android.printspooler.R; 26 27 /** 28 * This class represents the frame of page in the print preview list 29 * that contains the page and a footer. 30 */ 31 public final class PreviewPageFrame extends LinearLayout { 32 private final float mSelectedElevation; 33 private final float mNotSelectedElevation; 34 35 private final float mSelectedPageAlpha; 36 private final float mNotSelectedAlpha; 37 38 public PreviewPageFrame(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 mSelectedElevation = mContext.getResources().getDimension( 41 R.dimen.selected_page_elevation); 42 mNotSelectedElevation = mContext.getResources().getDimension( 43 R.dimen.unselected_page_elevation); 44 mSelectedPageAlpha = mContext.getResources().getFraction( 45 R.fraction.page_selected_alpha, 1, 1); 46 mNotSelectedAlpha = mContext.getResources().getFraction( 47 R.fraction.page_unselected_alpha, 1, 1); 48 } 49 50 @Override 51 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 52 super.onInitializeAccessibilityEvent(event); 53 event.setClassName(CompoundButton.class.getName()); 54 event.setChecked(isSelected()); 55 } 56 57 @Override 58 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 59 super.onInitializeAccessibilityNodeInfo(info); 60 info.setClassName(CompoundButton.class.getName()); 61 info.setSelected(false); 62 info.setCheckable(true); 63 info.setChecked(isSelected()); 64 } 65 66 public void setSelected(boolean selected, boolean animate) { 67 if (isSelected() == selected) { 68 return; 69 } 70 setSelected(selected); 71 if (selected) { 72 if (animate) { 73 animate().translationZ(mSelectedElevation) 74 .alpha(mSelectedPageAlpha); 75 } else { 76 setTranslationZ(mSelectedElevation); 77 setAlpha(mSelectedPageAlpha); 78 } 79 } else { 80 if (animate) { 81 animate().translationZ(mNotSelectedElevation) 82 .alpha(mNotSelectedAlpha); 83 } else { 84 setTranslationZ(mNotSelectedElevation); 85 setAlpha(mNotSelectedAlpha); 86 } 87 } 88 } 89 } 90