1525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov/*
2525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * Copyright (C) 2014 The Android Open Source Project
3525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov *
4525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * Licensed under the Apache License, Version 2.0 (the "License");
5525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * you may not use this file except in compliance with the License.
6525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * You may obtain a copy of the License at
7525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov *
8525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov *      http://www.apache.org/licenses/LICENSE-2.0
9525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov *
10525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * Unless required by applicable law or agreed to in writing, software
11525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * distributed under the License is distributed on an "AS IS" BASIS,
12525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * See the License for the specific language governing permissions and
14525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * limitations under the License.
15525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov */
16525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
17525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovpackage com.android.printspooler.widget;
18525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
19525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport android.content.Context;
20525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport android.graphics.drawable.BitmapDrawable;
216552bf3da60159607d9266eb295ee3c448f6c3deSvetoslavimport android.graphics.drawable.Drawable;
22525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport android.print.PrintAttributes.MediaSize;
23525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport android.print.PrintAttributes.Margins;
24525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport android.util.AttributeSet;
25525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport android.view.View;
26525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport com.android.printspooler.model.PageContentRepository;
27525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovimport com.android.printspooler.model.PageContentRepository.RenderSpec;
286552bf3da60159607d9266eb295ee3c448f6c3deSvetoslavimport com.android.printspooler.model.PageContentRepository.PageContentProvider;
29525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
30525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov/**
31525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * This class represents a page in the print preview list. The width of the page
32525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * is determined by stretching it to take maximal horizontal space while the height
33525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * is computed from the width using the page aspect ratio. Note that different media
34525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov * sizes have different aspect ratios.
35525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov */
36525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganovpublic class PageContentView extends View
37525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        implements PageContentRepository.OnPageContentAvailableCallback {
38e652b02d45bbda11c9cb8f663fa7f25903b90225Svetoslav
39525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    private PageContentProvider mProvider;
40525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
416f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav    private MediaSize mMediaSize;
426f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav
436f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav    private Margins mMinMargins;
446f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav
456552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav    private Drawable mEmptyState;
466552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav
47525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    private boolean mContentRequested;
48525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
49525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    public PageContentView(Context context, AttributeSet attrs) {
50525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        super(context, attrs);
51525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    }
52525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
53525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    @Override
54e6ecba59fd618b80dd13f581b625eea420672a0aSvetoslav    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
557fd5ada98aa9e035682531d9fe25633fdd24a058Svetoslav        mContentRequested = false;
56525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        requestPageContentIfNeeded();
57525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    }
58525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
59525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    @Override
60525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    public void onPageContentAvailable(BitmapDrawable content) {
61df6444931b030d3cdd9769e23f16f0a16fe9c654Svet Ganov        setBackground(content);
62525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    }
63525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
64525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    public PageContentProvider getPageContentProvider() {
65525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        return mProvider;
66525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    }
67525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
686552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav    public void init(PageContentProvider provider, Drawable emptyState,
696552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav            MediaSize mediaSize, Margins minMargins) {
706f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav        final boolean providerChanged = (mProvider == null)
716f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav                ? provider != null : !mProvider.equals(provider);
726552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav        final boolean loadingDrawableChanged = (mEmptyState == null)
737fd5ada98aa9e035682531d9fe25633fdd24a058Svetoslav                ? emptyState != null : !mEmptyState.equals(emptyState);
746f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav        final boolean mediaSizeChanged = (mMediaSize == null)
756f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav                ? mediaSize != null : !mMediaSize.equals(mediaSize);
766f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav        final boolean marginsChanged = (mMinMargins == null)
776f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav                ? minMargins != null : !mMinMargins.equals(minMargins);
786f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav
796552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav        if (!providerChanged && !mediaSizeChanged
806552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav                && !marginsChanged && !loadingDrawableChanged) {
81525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov            return;
82525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        }
83525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
84525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        mProvider = provider;
856f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav        mMediaSize = mediaSize;
866f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav        mMinMargins = minMargins;
876f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav
886552bf3da60159607d9266eb295ee3c448f6c3deSvetoslav        mEmptyState = emptyState;
89525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        mContentRequested = false;
90525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
9138781bd2c2c61385651314098b4a615cb8e8efa1Svet Ganov        // If there is no provider we want immediately to switch to
92525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        // the empty state, so pages with no content appear blank.
93525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        if (mProvider == null && getBackground() != mEmptyState) {
94525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov            setBackground(mEmptyState);
95525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        }
96525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
97525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        requestPageContentIfNeeded();
98525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    }
99525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov
100525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    private void requestPageContentIfNeeded() {
1016f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav        if (getWidth() > 0 && getHeight() > 0 && !mContentRequested
102e6ecba59fd618b80dd13f581b625eea420672a0aSvetoslav                && mProvider != null) {
103525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov            mContentRequested = true;
1046f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav            mProvider.getPageContent(new RenderSpec(getWidth(), getHeight(),
1056f249835a4ff9e7e7e3ca0190b7ecf72e689656dSvetoslav                    mMediaSize, mMinMargins), this);
106525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov        }
107525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov    }
108525a66b2bb5abf844aff2109bdc9ed819566beceSvet Ganov}
109