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 
PreviewPageFrame(Context context, AttributeSet attrs)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
getAccessibilityClassName()51     public CharSequence getAccessibilityClassName() {
52         return CompoundButton.class.getName();
53     }
54 
55     @Override
onInitializeAccessibilityEvent(AccessibilityEvent event)56     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
57         super.onInitializeAccessibilityEvent(event);
58         event.setChecked(isSelected());
59     }
60 
61     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)62     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
63         super.onInitializeAccessibilityNodeInfo(info);
64         info.setSelected(false);
65         info.setCheckable(true);
66         info.setChecked(isSelected());
67     }
68 
setSelected(boolean selected, boolean animate)69     public void setSelected(boolean selected, boolean animate) {
70         if (isSelected() == selected) {
71             return;
72         }
73         setSelected(selected);
74         if (selected) {
75             if (animate) {
76                 animate().translationZ(mSelectedElevation)
77                         .alpha(mSelectedPageAlpha);
78             } else {
79                 setTranslationZ(mSelectedElevation);
80                 setAlpha(mSelectedPageAlpha);
81             }
82         } else {
83             if (animate) {
84                 animate().translationZ(mNotSelectedElevation)
85                         .alpha(mNotSelectedAlpha);
86             } else {
87                 setTranslationZ(mNotSelectedElevation);
88                 setAlpha(mNotSelectedAlpha);
89             }
90         }
91     }
92 }
93