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 boolean mContentRequested; 48 PageContentView(Context context, AttributeSet attrs)49 public PageContentView(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 } 52 53 @Override onSizeChanged(int w, int h, int oldw, int oldh)54 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 55 mContentRequested = false; 56 requestPageContentIfNeeded(); 57 } 58 59 @Override onPageContentAvailable(BitmapDrawable content)60 public void onPageContentAvailable(BitmapDrawable content) { 61 setBackground(content); 62 } 63 getPageContentProvider()64 public PageContentProvider getPageContentProvider() { 65 return mProvider; 66 } 67 init(PageContentProvider provider, Drawable emptyState, MediaSize mediaSize, Margins minMargins)68 public void init(PageContentProvider provider, Drawable emptyState, 69 MediaSize mediaSize, Margins minMargins) { 70 final boolean providerChanged = (mProvider == null) 71 ? provider != null : !mProvider.equals(provider); 72 final boolean loadingDrawableChanged = (mEmptyState == null) 73 ? emptyState != null : !mEmptyState.equals(emptyState); 74 final boolean mediaSizeChanged = (mMediaSize == null) 75 ? mediaSize != null : !mMediaSize.equals(mediaSize); 76 final boolean marginsChanged = (mMinMargins == null) 77 ? minMargins != null : !mMinMargins.equals(minMargins); 78 79 if (!providerChanged && !mediaSizeChanged 80 && !marginsChanged && !loadingDrawableChanged) { 81 return; 82 } 83 84 mProvider = provider; 85 mMediaSize = mediaSize; 86 mMinMargins = minMargins; 87 88 mEmptyState = emptyState; 89 mContentRequested = false; 90 91 // If there is no provider we want immediately to switch to 92 // the empty state, so pages with no content appear blank. 93 if (mProvider == null && getBackground() != mEmptyState) { 94 setBackground(mEmptyState); 95 } 96 97 requestPageContentIfNeeded(); 98 } 99 requestPageContentIfNeeded()100 private void requestPageContentIfNeeded() { 101 if (getWidth() > 0 && getHeight() > 0 && !mContentRequested 102 && mProvider != null) { 103 mContentRequested = true; 104 mProvider.getPageContent(new RenderSpec(getWidth(), getHeight(), 105 mMediaSize, mMinMargins), this); 106 } 107 } 108 } 109