// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. package org.chromium.chrome.browser.ntp; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; import org.chromium.chrome.R; import org.chromium.chrome.browser.util.ViewUtils; /** * The New Tab Page for use in the incognito profile. */ public class IncognitoNewTabPageView extends FrameLayout { private IncognitoNewTabPageManager mManager; private boolean mFirstShow = true; private NewTabPageScrollView mScrollView; private int mSnapshotWidth; private int mSnapshotHeight; private int mSnapshotScrollY; /** * Manages the view interaction with the rest of the system. */ interface IncognitoNewTabPageManager { /** Loads a page explaining details about incognito mode in the current tab. */ void loadIncognitoLearnMore(); /** * Called when the NTP has completely finished loading (all views will be inflated * and any dependent resources will have been loaded). */ void onLoadingComplete(); } /** Default constructor needed to inflate via XML. */ public IncognitoNewTabPageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); mScrollView = (NewTabPageScrollView) findViewById(R.id.ntp_scrollview); mScrollView.setBackgroundColor( NtpStyleUtils.getBackgroundColorResource(getResources(), true)); // FOCUS_BEFORE_DESCENDANTS is needed to support keyboard shortcuts. Otherwise, pressing // any shortcut causes the UrlBar to be focused. See ViewRootImpl.leaveTouchMode(). mScrollView.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); View learnMore = findViewById(R.id.learn_more); learnMore.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mManager.loadIncognitoLearnMore(); } }); } /** * Initialize the incognito New Tab Page. * @param manager The manager that handles external dependencies of the view. */ void initialize(IncognitoNewTabPageManager manager) { mManager = manager; } /** * @see org.chromium.chrome.browser.compositor.layouts.content. * InvalidationAwareThumbnailProvider#shouldCaptureThumbnail() */ boolean shouldCaptureThumbnail() { if (getWidth() == 0 || getHeight() == 0) return false; return getWidth() != mSnapshotWidth || getHeight() != mSnapshotHeight || mScrollView.getScrollY() != mSnapshotScrollY; } /** * @see org.chromium.chrome.browser.compositor.layouts.content. * InvalidationAwareThumbnailProvider#captureThumbnail(Canvas) */ void captureThumbnail(Canvas canvas) { ViewUtils.captureBitmap(this, canvas); mSnapshotWidth = getWidth(); mSnapshotHeight = getHeight(); mSnapshotScrollY = mScrollView.getScrollY(); } @Override public void onAttachedToWindow() { super.onAttachedToWindow(); assert mManager != null; if (mFirstShow) { mManager.onLoadingComplete(); mFirstShow = false; } } }