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