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.graphics.drawable.BitmapDrawable;
21 import android.graphics.drawable.Drawable;
22 import android.print.PrintAttributes.MediaSize;
23 import android.print.PrintAttributes.Margins;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import com.android.printspooler.model.PageContentRepository;
27 import com.android.printspooler.model.PageContentRepository.RenderSpec;
28 import com.android.printspooler.model.PageContentRepository.PageContentProvider;
29 
30 /**
31  * This class represents a page in the print preview list. The width of the page
32  * is determined by stretching it to take maximal horizontal space while the height
33  * is computed from the width using the page aspect ratio. Note that different media
34  * sizes have different aspect ratios.
35  */
36 public class PageContentView extends View
37         implements PageContentRepository.OnPageContentAvailableCallback {
38 
39     private PageContentProvider mProvider;
40 
41     private MediaSize mMediaSize;
42 
43     private Margins mMinMargins;
44 
45     private Drawable mEmptyState;
46 
47     private Drawable mErrorState;
48 
49     private boolean mContentRequested;
50 
51     private boolean mIsFailed;
52 
PageContentView(Context context, AttributeSet attrs)53     public PageContentView(Context context, AttributeSet attrs) {
54         super(context, attrs);
55     }
56 
57     @Override
onSizeChanged(int w, int h, int oldw, int oldh)58     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
59         mContentRequested = false;
60 
61         requestPageContentIfNeeded();
62     }
63 
64     @Override
onPageContentAvailable(BitmapDrawable renderedPage)65     public void onPageContentAvailable(BitmapDrawable renderedPage) {
66         mIsFailed = (renderedPage == null);
67 
68         if (mIsFailed) {
69             setBackground(mErrorState);
70         } else {
71             setBackground(renderedPage);
72         }
73     }
74 
getPageContentProvider()75     public PageContentProvider getPageContentProvider() {
76         return mProvider;
77     }
78 
init(PageContentProvider provider, Drawable emptyState, Drawable errorState, MediaSize mediaSize, Margins minMargins)79     public void init(PageContentProvider provider, Drawable emptyState, Drawable errorState,
80             MediaSize mediaSize, Margins minMargins) {
81         final boolean providerChanged = (mProvider == null)
82                 ? provider != null : !mProvider.equals(provider);
83         final boolean loadingDrawableChanged = (mEmptyState == null)
84                 ? emptyState != null : !mEmptyState.equals(emptyState);
85         final boolean mediaSizeChanged = (mMediaSize == null)
86                 ? mediaSize != null : !mMediaSize.equals(mediaSize);
87         final boolean marginsChanged = (mMinMargins == null)
88                 ? minMargins != null : !mMinMargins.equals(minMargins);
89 
90         if (!providerChanged && !mediaSizeChanged
91                 && !marginsChanged && !loadingDrawableChanged) {
92             return;
93         }
94 
95         mIsFailed = false;
96         mProvider = provider;
97         mMediaSize = mediaSize;
98         mMinMargins = minMargins;
99 
100         mEmptyState = emptyState;
101         mErrorState = errorState;
102         mContentRequested = false;
103 
104         // If there is no provider we want immediately to switch to
105         // the empty state, so pages with no content appear blank.
106         if (mProvider == null) {
107             setBackground(mEmptyState);
108         } else if (mIsFailed) {
109             setBackground(mErrorState);
110         }
111 
112         requestPageContentIfNeeded();
113     }
114 
requestPageContentIfNeeded()115     private void requestPageContentIfNeeded() {
116         if (getWidth() > 0 && getHeight() > 0 && !mContentRequested
117                 && mProvider != null) {
118             mContentRequested = true;
119             mProvider.getPageContent(new RenderSpec(getWidth(), getHeight(),
120                     mMediaSize, mMinMargins), this);
121         }
122     }
123 }
124