org.chromium.chrome.browser.offlinepages.OfflinePageBridge Java Examples

The following examples show how to use org.chromium.chrome.browser.offlinepages.OfflinePageBridge. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SuggestionsSection.java    From 365browser with Apache License 2.0 6 votes vote down vote up
public SuggestionsSection(Delegate delegate, SuggestionsUiDelegate uiDelegate,
        SuggestionsRanker ranker, OfflinePageBridge offlinePageBridge,
        SuggestionsCategoryInfo info) {
    mDelegate = delegate;
    mCategoryInfo = info;

    mHeader = new SectionHeader(info.getTitle());
    mSuggestionsList = new SuggestionsList(uiDelegate, ranker, info);
    mStatus = StatusItem.createNoSuggestionsItem(info);
    mMoreButton = new ActionItem(this, ranker);
    mProgressIndicator = new ProgressItem();
    addChildren(mHeader, mSuggestionsList, mStatus, mMoreButton, mProgressIndicator);

    mOfflineModelObserver = new OfflineModelObserver(offlinePageBridge);
    uiDelegate.addDestructionObserver(mOfflineModelObserver);

    mStatus.setVisible(!hasSuggestions());
}
 
Example #2
Source File: DownloadUtils.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Whether the user should be allowed to download the current page.
 * @param tab Tab displaying the page that will be downloaded.
 * @return    Whether the "Download Page" button should be enabled.
 */
public static boolean isAllowedToDownloadPage(Tab tab) {
    if (tab == null) return false;

    // Offline pages isn't supported in Incognito. This should be checked before calling
    // OfflinePageBridge.getForProfile because OfflinePageBridge instance will not be found
    // for incognito profile.
    if (tab.isIncognito()) return false;

    // Check if the page url is supported for saving. Only HTTP and HTTPS pages are allowed.
    if (!OfflinePageBridge.canSavePage(tab.getUrl())) return false;

    // Download will only be allowed for the error page if download button is shown in the page.
    if (tab.isShowingErrorPage()) {
        final OfflinePageBridge bridge = OfflinePageBridge.getForProfile(tab.getProfile());
        return bridge.isShowingDownloadButtonInErrorPage(tab.getWebContents());
    }

    if (tab.isShowingInterstitialPage()) return false;

    // Don't allow re-downloading the currently displayed offline page.
    if (OfflinePageUtils.isOfflinePage(tab)) return false;

    return true;
}
 
Example #3
Source File: SuggestionsSection.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
public SuggestionsSection(NodeParent parent, SuggestionsCategoryInfo info,
        NewTabPageManager manager, OfflinePageBridge offlinePageBridge) {
    super(parent);
    mCategoryInfo = info;
    mOfflinePageBridge = offlinePageBridge;

    mHeader = new SectionHeader(info.getTitle());
    mSuggestionsList = new SuggestionsList(this);
    mStatus = StatusItem.createNoSuggestionsItem(this);
    mMoreButton = new ActionItem(this);
    mProgressIndicator = new ProgressItem(this);
    initializeChildren();

    setupOfflinePageBridgeObserver(manager);
}
 
Example #4
Source File: SuggestionsNavigationDelegateImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void saveUrlForOffline(String url) {
    if (mHost.getActiveTab() != null) {
        OfflinePageBridge.getForProfile(mProfile).scheduleDownload(
                mHost.getActiveTab().getWebContents(), "ntp_suggestions", url,
                DownloadUiActionFlags.ALL);
    } else {
        OfflinePageBridge.getForProfile(mProfile).savePageLater(
                url, "ntp_suggestions", true /* userRequested */);
    }
}
 
Example #5
Source File: TileGrid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public TileGrid(SuggestionsUiDelegate uiDelegate, ContextMenuManager contextMenuManager,
        TileGroup.Delegate tileGroupDelegate, OfflinePageBridge offlinePageBridge) {
    mTileGroup = new TileGroup(ContextUtils.getApplicationContext(), uiDelegate,
            contextMenuManager, tileGroupDelegate,
            /* observer = */ this, offlinePageBridge, getTileTitleLines());
    mTileGroup.startObserving(getMaxTileRows() * MAX_TILE_COLUMNS);
}
 
Example #6
Source File: TileGroup.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @param context Used for initialisation and resolving resources.
 * @param uiDelegate Delegate used to interact with the rest of the system.
 * @param contextMenuManager Used to handle context menu invocations on the tiles.
 * @param tileGroupDelegate Used for interactions with the Most Visited backend.
 * @param observer Will be notified of changes to the tile data.
 * @param titleLines The number of text lines to use for each tile title.
 */
public TileGroup(Context context, SuggestionsUiDelegate uiDelegate,
        ContextMenuManager contextMenuManager, Delegate tileGroupDelegate, Observer observer,
        OfflinePageBridge offlinePageBridge, int titleLines) {
    mContext = context;
    mUiDelegate = uiDelegate;
    mContextMenuManager = contextMenuManager;
    mTileGroupDelegate = tileGroupDelegate;
    mObserver = observer;
    mTitleLinesCount = titleLines;

    Resources resources = mContext.getResources();
    mDesiredIconSize = resources.getDimensionPixelSize(R.dimen.tile_view_icon_size);
    // On ldpi devices, mDesiredIconSize could be even smaller than ICON_MIN_SIZE_PX.
    mMinIconSize = Math.min(mDesiredIconSize, ICON_MIN_SIZE_PX);
    int desiredIconSizeDp =
            Math.round(mDesiredIconSize / resources.getDisplayMetrics().density);
    int iconColor =
            ApiCompatibilityUtils.getColor(resources, R.color.default_favicon_background_color);
    mIconGenerator = new RoundedIconGenerator(mContext, desiredIconSizeDp, desiredIconSizeDp,
            ICON_CORNER_RADIUS_DP, iconColor, ICON_TEXT_SIZE_DP);

    if (ChromeFeatureList.isEnabled(ChromeFeatureList.NTP_OFFLINE_PAGES_FEATURE_NAME)) {
        mOfflineModelObserver = new OfflineModelObserver(offlinePageBridge);
        mUiDelegate.addDestructionObserver(mOfflineModelObserver);
    } else {
        mOfflineModelObserver = null;
    }
}
 
Example #7
Source File: SectionList.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public SectionList(SuggestionsUiDelegate uiDelegate, OfflinePageBridge offlinePageBridge) {
    mUiDelegate = uiDelegate;
    mUiDelegate.getSuggestionsSource().setObserver(this);
    mOfflinePageBridge = offlinePageBridge;

    mUiDelegate.addDestructionObserver(new DestructionObserver() {
        @Override
        public void onDestroy() {
            removeAllSections();
        }
    });
}
 
Example #8
Source File: NewTabPageAdapter.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the adapter that will manage all the cards to display on the NTP.
 *
 * @param manager the NewTabPageManager to use to interact with the rest of the system.
 * @param aboveTheFoldView the layout encapsulating all the above-the-fold elements
 *                         (logo, search box, most visited tiles)
 * @param uiConfig the NTP UI configuration, to be passed to created views.
 * @param offlinePageBridge the OfflinePageBridge used to determine if articles are available
 *                              offline.
 *
 */
public NewTabPageAdapter(NewTabPageManager manager, View aboveTheFoldView, UiConfig uiConfig,
        OfflinePageBridge offlinePageBridge) {
    mNewTabPageManager = manager;
    mAboveTheFoldView = aboveTheFoldView;
    mUiConfig = uiConfig;
    mOfflinePageBridge = offlinePageBridge;
    mRoot = new InnerNode(this) {
        @Override
        protected List<TreeNode> getChildren() {
            return mChildren;
        }

        @Override
        public void onItemRangeChanged(TreeNode child, int index, int count) {
            if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
            super.onItemRangeChanged(child, index, count);
        }

        @Override
        public void onItemRangeInserted(TreeNode child, int index, int count) {
            if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
            super.onItemRangeInserted(child, index, count);
        }

        @Override
        public void onItemRangeRemoved(TreeNode child, int index, int count) {
            if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
            super.onItemRangeRemoved(child, index, count);
        }
    };

    mSigninPromo = new SignInPromo(mRoot, this);
    DestructionObserver signInObserver = mSigninPromo.getObserver();
    if (signInObserver != null) mNewTabPageManager.addDestructionObserver(signInObserver);

    resetSections(/*alwaysAllowEmptySections=*/false);
    mNewTabPageManager.getSuggestionsSource().setObserver(this);
}
 
Example #9
Source File: SuggestionsSection.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
private void setupOfflinePageBridgeObserver(NewTabPageManager manager) {
    final OfflinePageBridge.OfflinePageModelObserver observer =
            new OfflinePageBridge.OfflinePageModelObserver() {
                @Override
                public void offlinePageModelLoaded() {
                    updateAllSnippetOfflineAvailability();
                }

                @Override
                public void offlinePageModelChanged() {
                    updateAllSnippetOfflineAvailability();
                }

                @Override
                public void offlinePageDeleted(long offlineId, ClientId clientId) {
                    for (SnippetArticle article : mSuggestionsList) {
                        if (article.requiresExactOfflinePage()) continue;
                        Long articleOfflineId = article.getOfflinePageOfflineId();
                        if (articleOfflineId == null) continue;
                        if (articleOfflineId.longValue() != offlineId) continue;
                        // The old value cannot be simply removed without a request to the
                        // model, because there may be an older offline page for the same
                        // URL.
                        updateSnippetOfflineAvailability(article);
                    }
                }
            };

    mOfflinePageBridge.addObserver(observer);

    manager.addDestructionObserver(new DestructionObserver() {
        @Override
        public void onDestroy() {
            mIsNtpDestroyed = true;
            mOfflinePageBridge.removeObserver(observer);
        }
    });
}
 
Example #10
Source File: TabContextMenuItemDelegate.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void onSavePageLater(String linkUrl) {
    OfflinePageBridge bridge = OfflinePageBridge.getForProfile(mTab.getProfile());
    Random random = new Random();
    long offline_id = random.nextLong();
    ClientId clientId = new ClientId("async_loading", Long.toString(offline_id));
    bridge.savePageLater(linkUrl, clientId);
}
 
Example #11
Source File: ChromeActivity.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
private void triggerShare(
        final Tab currentTab, final boolean shareDirectly, boolean isIncognito) {
    final Activity mainActivity = this;
    WebContents webContents = currentTab.getWebContents();

    RecordHistogram.recordBooleanHistogram(
            "OfflinePages.SharedPageWasOffline", currentTab.isOfflinePage());
    boolean canShareOfflinePage = OfflinePageBridge.isPageSharingEnabled();

    // Share an empty blockingUri in place of screenshot file. The file ready notification is
    // sent by onScreenshotReady call below when the file is written.
    final Uri blockingUri = (isIncognito || webContents == null)
            ? null
            : ChromeFileProvider.generateUriAndBlockAccess(mainActivity);
    if (canShareOfflinePage) {
        OfflinePageUtils.shareOfflinePage(shareDirectly, true, mainActivity, null,
                blockingUri, null, currentTab);
    } else {
        ShareHelper.share(shareDirectly, true, mainActivity, currentTab.getTitle(), null,
                currentTab.getUrl(), null, blockingUri, null);
        if (shareDirectly) {
            RecordUserAction.record("MobileMenuDirectShare");
        } else {
            RecordUserAction.record("MobileMenuShare");
        }
    }

    if (blockingUri == null) return;

    // Start screenshot capture and notify the provider when it is ready.
    ContentBitmapCallback callback = new ContentBitmapCallback() {
        @Override
        public void onFinishGetBitmap(Bitmap bitmap, int response) {
            ShareHelper.saveScreenshotToDisk(bitmap, mainActivity,
                    new Callback<Uri>() {
                        @Override
                        public void onResult(Uri result) {
                            // Unblock the file once it is saved to disk.
                            ChromeFileProvider.notifyFileReady(blockingUri, result);
                        }
                    });
        }
    };
    if (!mScreenshotCaptureSkippedForTesting) {
        webContents.getContentBitmapAsync(Bitmap.Config.ARGB_8888, 1.f, EMPTY_RECT, callback);
    } else {
        callback.onFinishGetBitmap(null, ReadbackResponse.SURFACE_UNAVAILABLE);
    }
}
 
Example #12
Source File: TabContextMenuItemDelegate.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
public void onSavePageLater(String linkUrl) {
    OfflinePageBridge.getForProfile(mTab.getProfile())
            .savePageLater(linkUrl, "async_loading", true /* userRequested */);
}
 
Example #13
Source File: SuggestionsSection.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public OfflineModelObserver(OfflinePageBridge bridge) {
    super(bridge);
}
 
Example #14
Source File: NewTabPageAdapter.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the adapter that will manage all the cards to display on the NTP.
 * @param uiDelegate used to interact with the rest of the system.
 * @param aboveTheFoldView the layout encapsulating all the above-the-fold elements
 *         (logo, search box, most visited tiles), or null if only suggestions should
 *         be displayed.
 * @param uiConfig the NTP UI configuration, to be passed to created views.
 * @param offlinePageBridge used to determine if articles are available.
 * @param contextMenuManager used to build context menus.
 * @param tileGroupDelegate if not null this is used to build a {@link TileGrid}.
 */
public NewTabPageAdapter(SuggestionsUiDelegate uiDelegate, @Nullable View aboveTheFoldView,
        UiConfig uiConfig, OfflinePageBridge offlinePageBridge,
        ContextMenuManager contextMenuManager, @Nullable TileGroup.Delegate tileGroupDelegate) {
    mUiDelegate = uiDelegate;
    mContextMenuManager = contextMenuManager;

    mAboveTheFoldView = aboveTheFoldView;
    mUiConfig = uiConfig;
    mRoot = new InnerNode();

    mSections = new SectionList(mUiDelegate, offlinePageBridge);
    mSigninPromo = new SignInPromo(mUiDelegate);
    mAllDismissed = new AllDismissedItem();
    mFooter = new Footer();

    if (mAboveTheFoldView == null) {
        mAboveTheFold = null;
    } else {
        mAboveTheFold = new AboveTheFoldItem();
        mRoot.addChild(mAboveTheFold);
    }
    if (tileGroupDelegate == null) {
        mTileGrid = null;
    } else {
        mTileGrid = new TileGrid(
                uiDelegate, mContextMenuManager, tileGroupDelegate, offlinePageBridge);
        mRoot.addChild(mTileGrid);
    }
    mRoot.addChildren(mSections, mSigninPromo, mAllDismissed, mFooter);
    if (mAboveTheFoldView == null
            || ChromeFeatureList.isEnabled(ChromeFeatureList.NTP_CONDENSED_LAYOUT)) {
        mBottomSpacer = null;
    } else {
        mBottomSpacer = new SpacingItem();
        mRoot.addChild(mBottomSpacer);
    }

    updateAllDismissedVisibility();
    mRoot.setParent(this);
}
 
Example #15
Source File: ChromeActivity.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void triggerShare(
        final Tab currentTab, final boolean shareDirectly, boolean isIncognito) {
    final Activity mainActivity = this;
    WebContents webContents = currentTab.getWebContents();

    RecordHistogram.recordBooleanHistogram(
            "OfflinePages.SharedPageWasOffline", OfflinePageUtils.isOfflinePage(currentTab));
    boolean canShareOfflinePage = OfflinePageBridge.isPageSharingEnabled();

    // Share an empty blockingUri in place of screenshot file. The file ready notification is
    // sent by onScreenshotReady call below when the file is written.
    final Uri blockingUri = (isIncognito || webContents == null)
            ? null
            : ChromeFileProvider.generateUriAndBlockAccess(mainActivity);
    if (canShareOfflinePage) {
        OfflinePageUtils.shareOfflinePage(shareDirectly, true, mainActivity, null,
                blockingUri, null, currentTab);
    } else {
        ShareHelper.share(shareDirectly, true, mainActivity, currentTab.getTitle(), null,
                currentTab.getUrl(), null, blockingUri, null);
        if (shareDirectly) {
            RecordUserAction.record("MobileMenuDirectShare");
        } else {
            RecordUserAction.record("MobileMenuShare");
        }
    }

    if (blockingUri == null) return;

    // Start screenshot capture and notify the provider when it is ready.
    ContentBitmapCallback callback = new ContentBitmapCallback() {
        @Override
        public void onFinishGetBitmap(Bitmap bitmap, int response) {
            ShareHelper.saveScreenshotToDisk(bitmap, mainActivity,
                    new Callback<Uri>() {
                        @Override
                        public void onResult(Uri result) {
                            // Unblock the file once it is saved to disk.
                            ChromeFileProvider.notifyFileReady(blockingUri, result);
                        }
                    });
        }
    };
    if (mScreenshotCaptureSkippedForTesting) {
        callback.onFinishGetBitmap(null, ReadbackResponse.SURFACE_UNAVAILABLE);
    } else {
        webContents.getContentBitmapAsync(0, 0, callback);
    }
}
 
Example #16
Source File: TileGroup.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public OfflineModelObserver(OfflinePageBridge bridge) {
    super(bridge);
}
 
Example #17
Source File: SuggestionsBottomSheetContent.java    From 365browser with Apache License 2.0 4 votes vote down vote up
public SuggestionsBottomSheetContent(final ChromeActivity activity, final BottomSheet sheet,
        TabModelSelector tabModelSelector, SnackbarManager snackbarManager) {
    Profile profile = Profile.getLastUsedProfile();
    SuggestionsNavigationDelegate navigationDelegate =
            new SuggestionsNavigationDelegateImpl(activity, profile, sheet, tabModelSelector);
    mTileGroupDelegate = new TileGroupDelegateImpl(
            activity, profile, tabModelSelector, navigationDelegate, snackbarManager);
    mSuggestionsUiDelegate = createSuggestionsDelegate(
            profile, navigationDelegate, sheet, activity.getReferencePool());

    mView = LayoutInflater.from(activity).inflate(
            R.layout.suggestions_bottom_sheet_content, null);
    mRecyclerView = (SuggestionsRecyclerView) mView.findViewById(R.id.recycler_view);

    TouchEnabledDelegate touchEnabledDelegate = new TouchEnabledDelegate() {
        @Override
        public void setTouchEnabled(boolean enabled) {
            activity.getBottomSheet().setTouchEnabled(enabled);
        }
    };
    mContextMenuManager =
            new ContextMenuManager(activity, navigationDelegate, touchEnabledDelegate);
    activity.getWindowAndroid().addContextMenuCloseListener(mContextMenuManager);
    mSuggestionsUiDelegate.addDestructionObserver(new DestructionObserver() {
        @Override
        public void onDestroy() {
            activity.getWindowAndroid().removeContextMenuCloseListener(mContextMenuManager);
        }
    });

    UiConfig uiConfig = new UiConfig(mRecyclerView);

    final NewTabPageAdapter adapter = new NewTabPageAdapter(mSuggestionsUiDelegate,
            /* aboveTheFoldView = */ null, uiConfig, OfflinePageBridge.getForProfile(profile),
            mContextMenuManager, mTileGroupDelegate);
    mRecyclerView.init(uiConfig, mContextMenuManager, adapter);

    mBottomSheetObserver = new SuggestionsSheetVisibilityChangeObserver(this, activity) {
        @Override
        public void onSheetOpened() {
            mRecyclerView.scrollToPosition(0);
            adapter.refreshSuggestions();
            mSuggestionsUiDelegate.getEventReporter().onSurfaceOpened();
            mRecyclerView.getScrollEventReporter().reset();

            if (ChromeFeatureList.isEnabled(
                        ChromeFeatureList.CONTEXTUAL_SUGGESTIONS_CAROUSEL)
                    && sheet.getActiveTab() != null) {
                updateContextualSuggestions(sheet.getActiveTab().getUrl());
            }

            super.onSheetOpened();
        }

        @Override
        public void onContentShown() {
            SuggestionsMetrics.recordSurfaceVisible();
        }

        @Override
        public void onContentHidden() {
            SuggestionsMetrics.recordSurfaceHidden();
        }

        @Override
        public void onContentStateChanged(@BottomSheet.SheetState int contentState) {
            if (contentState == BottomSheet.SHEET_STATE_HALF) {
                SuggestionsMetrics.recordSurfaceHalfVisible();
            } else if (contentState == BottomSheet.SHEET_STATE_FULL) {
                SuggestionsMetrics.recordSurfaceFullyVisible();
            }
        }
    };

    mShadowView = (FadingShadowView) mView.findViewById(R.id.shadow);
    mShadowView.init(
            ApiCompatibilityUtils.getColor(mView.getResources(), R.color.toolbar_shadow_color),
            FadingShadow.POSITION_TOP);

    mRecyclerView.addOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            boolean shadowVisible = mRecyclerView.canScrollVertically(-1);
            mShadowView.setVisibility(shadowVisible ? View.VISIBLE : View.GONE);
        }
    });

    final LocationBar locationBar = (LocationBar) sheet.findViewById(R.id.location_bar);
    mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        @SuppressLint("ClickableViewAccessibility")
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (locationBar != null && locationBar.isUrlBarFocused()) {
                locationBar.setUrlBarFocus(false);
            }

            // Never intercept the touch event.
            return false;
        }
    });
}
 
Example #18
Source File: SuggestionsOfflineModelObserver.java    From 365browser with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor for an offline model observer. It registers itself with the bridge, but the
 * unregistration will have to be done by the caller, either directly or by registering the
 * created observer as {@link DestructionObserver}.
 * @param bridge source of the offline state data.
 */
public SuggestionsOfflineModelObserver(OfflinePageBridge bridge) {
    mOfflinePageBridge = bridge;
    mOfflinePageBridge.addObserver(this);
}