Java Code Examples for org.chromium.chrome.browser.tabmodel.TabModel#INVALID_TAB_INDEX

The following examples show how to use org.chromium.chrome.browser.tabmodel.TabModel#INVALID_TAB_INDEX . 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: TabWebContentsDelegateAndroid.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public void activateContents() {
    boolean activityIsDestroyed = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        activityIsDestroyed = mTab.getActivity().isDestroyed();
    }
    if (activityIsDestroyed || !mTab.isInitialized()) {
        Log.e(TAG, "Activity destroyed before calling activateContents().  Bailing out.");
        return;
    }

    TabModel model = getTabModel();
    int index = model.indexOf(mTab);
    if (index == TabModel.INVALID_TAB_INDEX) return;
    TabModelUtils.setIndex(model, index);
    bringActivityToForeground();
}
 
Example 2
Source File: TabWebContentsDelegateAndroid.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public void activateContents() {
    boolean activityIsDestroyed = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        activityIsDestroyed = mTab.getActivity().isDestroyed();
    }
    if (activityIsDestroyed || !mTab.isInitialized()) {
        Log.e(TAG, "Activity destroyed before calling activateContents().  Bailing out.");
        return;
    }

    TabModel model = getTabModel();
    int index = model.indexOf(mTab);
    if (index == TabModel.INVALID_TAB_INDEX) return;
    TabModelUtils.setIndex(model, index);
    bringActivityToForeground();
}
 
Example 3
Source File: StripLayoutHelper.java    From delion with Apache License 2.0 5 votes vote down vote up
private int findIndexForTab(int id) {
    if (mStripTabs == null) return TabModel.INVALID_TAB_INDEX;
    for (int i = 0; i < mStripTabs.length; i++) {
        if (mStripTabs[i].getId() == id) return i;
    }
    return TabModel.INVALID_TAB_INDEX;
}
 
Example 4
Source File: SuggestionsNavigationDelegateImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private boolean openRecentTabSnippet(SnippetArticle article) {
    TabModel tabModel = mTabModelSelector.getModel(false);
    int tabId = article.getRecentTabId();
    int tabIndex = TabModelUtils.getTabIndexById(tabModel, tabId);
    if (tabIndex == TabModel.INVALID_TAB_INDEX) return false;
    TabModelUtils.setIndex(tabModel, tabIndex);
    return true;
}
 
Example 5
Source File: TabWebContentsDelegateAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void activateContents() {
    ChromeActivity activity = mTab.getActivity();
    if (activity == null) {
        Log.e(TAG, "Activity not set activateContents().  Bailing out.");
        return;
    }
    if (activity.isActivityDestroyed()) {
        Log.e(TAG, "Activity destroyed before calling activateContents().  Bailing out.");
        return;
    }
    if (!mTab.isInitialized()) {
        Log.e(TAG, "Tab not initialized before calling activateContents().  Bailing out.");
        return;
    }

    // Do nothing if the tab can currently be interacted with by the user.
    if (mTab.isUserInteractable()) return;

    TabModel model = getTabModel();
    int index = model.indexOf(mTab);
    if (index == TabModel.INVALID_TAB_INDEX) return;
    TabModelUtils.setIndex(model, index);

    // Do nothing if the activity is visible (STOPPED is the only valid invisible state as we
    // explicitly check isActivityDestroyed above).
    if (ApplicationStatus.getStateForActivity(activity) == ActivityState.STOPPED) {
        bringActivityToForeground();
    }
}
 
Example 6
Source File: VrShellImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@CalledByNative
private void showTab(int id) {
    Tab tab = mActivity.getTabModelSelector().getTabById(id);
    if (tab == null) {
        return;
    }
    int index = mActivity.getTabModelSelector().getModel(tab.isIncognito()).indexOf(tab);
    if (index == TabModel.INVALID_TAB_INDEX) {
        return;
    }
    TabModelUtils.setIndex(mActivity.getTabModelSelector().getModel(tab.isIncognito()), index);
}
 
Example 7
Source File: ToolbarSwipeLayout.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void show(long time, boolean animate) {
    super.show(time, animate);
    init();
    if (mTabModelSelector == null) return;
    Tab tab = mTabModelSelector.getCurrentTab();
    if (tab != null && tab.isNativePage()) mTabContentManager.cacheTabThumbnail(tab);

    TabModel model = mTabModelSelector.getCurrentModel();
    if (model == null) return;
    int fromTabId = mTabModelSelector.getCurrentTabId();
    if (fromTabId == TabModel.INVALID_TAB_INDEX) return;
    mFromTab = createLayoutTab(fromTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
    prepareLayoutTabForSwipe(mFromTab, false);
}
 
Example 8
Source File: StripLayoutHelper.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private int findIndexForTab(int id) {
    if (mStripTabs == null) return TabModel.INVALID_TAB_INDEX;
    for (int i = 0; i < mStripTabs.length; i++) {
        if (mStripTabs[i].getId() == id) return i;
    }
    return TabModel.INVALID_TAB_INDEX;
}
 
Example 9
Source File: StripLayoutHelper.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Called on onDown event.
 * @param time      The time stamp in millisecond of the event.
 * @param x         The x position of the event.
 * @param y         The y position of the event.
 * @param fromMouse Whether the event originates from a mouse.
 * @param buttons   State of all buttons that are pressed.
 */
public void onDown(long time, float x, float y, boolean fromMouse, int buttons) {
    resetResizeTimeout(false);

    if (mNewTabButton.onDown(x, y)) {
        mRenderHost.requestRender();
        return;
    }

    final StripLayoutTab clickedTab = getTabAtPosition(x);
    final int index = clickedTab != null
            ? TabModelUtils.getTabIndexById(mModel, clickedTab.getId())
            : TabModel.INVALID_TAB_INDEX;
    // http://crbug.com/472186 : Needs to handle a case that index is invalid.
    // The case could happen when the current tab is touched while we're inflating the rest of
    // the tabs from disk.
    mInteractingTab = index != TabModel.INVALID_TAB_INDEX && index < mStripTabs.length
            ? mStripTabs[index]
            : null;
    boolean clickedClose = clickedTab != null
                           && clickedTab.checkCloseHitTest(x, y);
    if (clickedClose) {
        clickedTab.setClosePressed(true);
        mLastPressedCloseButton = clickedTab.getCloseButton();
        mRenderHost.requestRender();
    }

    if (!mScroller.isFinished()) {
        mScroller.forceFinished(true);
        mInteractingTab = null;
    }

    if (fromMouse && !clickedClose && clickedTab != null
            && clickedTab.getVisiblePercentage() >= 1.f
            && (buttons & MotionEvent.BUTTON_TERTIARY) == 0) {
        startReorderMode(time, x, x);
    }
}
 
Example 10
Source File: ToolbarSwipeLayout.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void show(long time, boolean animate) {
    super.show(time, animate);
    init();
    if (mTabModelSelector == null) return;
    Tab tab = mTabModelSelector.getCurrentTab();
    if (tab != null && tab.isNativePage()) mTabContentManager.cacheTabThumbnail(tab);

    TabModel model = mTabModelSelector.getCurrentModel();
    if (model == null) return;
    int fromTabId = mTabModelSelector.getCurrentTabId();
    if (fromTabId == TabModel.INVALID_TAB_INDEX) return;
    mFromTab = createLayoutTab(fromTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
    prepareLayoutTabForSwipe(mFromTab, false);
}
 
Example 11
Source File: StripLayoutHelper.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
private int findIndexForTab(int id) {
    if (mStripTabs == null) return TabModel.INVALID_TAB_INDEX;
    for (int i = 0; i < mStripTabs.length; i++) {
        if (mStripTabs[i].getId() == id) return i;
    }
    return TabModel.INVALID_TAB_INDEX;
}
 
Example 12
Source File: StripLayoutHelper.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Called on onDown event.
 * @param time      The time stamp in millisecond of the event.
 * @param x         The x position of the event.
 * @param y         The y position of the event.
 * @param fromMouse Whether the event originates from a mouse.
 * @param buttons   State of all buttons that are pressed.
 */
public void onDown(long time, float x, float y, boolean fromMouse, int buttons) {
    resetResizeTimeout(false);

    if (mNewTabButton.onDown(x, y)) {
        mRenderHost.requestRender();
        return;
    }

    final StripLayoutTab clickedTab = getTabAtPosition(x);
    final int index = clickedTab != null
            ? TabModelUtils.getTabIndexById(mModel, clickedTab.getId())
            : TabModel.INVALID_TAB_INDEX;
    // http://crbug.com/472186 : Needs to handle a case that index is invalid.
    // The case could happen when the current tab is touched while we're inflating the rest of
    // the tabs from disk.
    mInteractingTab = index != TabModel.INVALID_TAB_INDEX && index < mStripTabs.length
            ? mStripTabs[index]
            : null;
    boolean clickedClose = clickedTab != null
                           && clickedTab.checkCloseHitTest(x, y);
    if (clickedClose) {
        clickedTab.setClosePressed(true);
        mLastPressedCloseButton = clickedTab.getCloseButton();
        mRenderHost.requestRender();
    }

    if (!mScroller.isFinished()) {
        mScroller.forceFinished(true);
        mInteractingTab = null;
    }

    if (fromMouse && !clickedClose && clickedTab != null
            && clickedTab.getVisiblePercentage() >= 1.f
            && (buttons & MotionEvent.BUTTON_TERTIARY) == 0) {
        startReorderMode(time, x, x);
    }
}
 
Example 13
Source File: ToolbarSwipeLayout.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void show(long time, boolean animate) {
    super.show(time, animate);
    init();
    if (mTabModelSelector == null) return;
    Tab tab = mTabModelSelector.getCurrentTab();
    if (tab != null && tab.isNativePage()) mTabContentManager.cacheTabThumbnail(tab);

    TabModel model = mTabModelSelector.getCurrentModel();
    if (model == null) return;
    int fromTabId = mTabModelSelector.getCurrentTabId();
    if (fromTabId == TabModel.INVALID_TAB_INDEX) return;
    mFromTab = createLayoutTab(fromTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
    prepareLayoutTabForSwipe(mFromTab, false);
}
 
Example 14
Source File: StripLayoutHelper.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Called on onDown event.
 * @param time      The time stamp in millisecond of the event.
 * @param x         The x position of the event.
 * @param y         The y position of the event.
 * @param fromMouse Whether the event originates from a mouse.
 * @param buttons   State of all buttons that are pressed.
 */
public void onDown(long time, float x, float y, boolean fromMouse, int buttons) {
    resetResizeTimeout(false);

    if (mNewTabButton.onDown(x, y)) {
        mRenderHost.requestRender();
        return;
    }

    final StripLayoutTab clickedTab = getTabAtPosition(x);
    final int index = clickedTab != null
            ? TabModelUtils.getTabIndexById(mModel, clickedTab.getId())
            : TabModel.INVALID_TAB_INDEX;
    // http://crbug.com/472186 : Needs to handle a case that index is invalid.
    // The case could happen when the current tab is touched while we're inflating the rest of
    // the tabs from disk.
    mInteractingTab = index != TabModel.INVALID_TAB_INDEX && index < mStripTabs.length
            ? mStripTabs[index]
            : null;
    boolean clickedClose = clickedTab != null
                           && clickedTab.checkCloseHitTest(x, y);
    if (clickedClose) {
        clickedTab.setClosePressed(true);
        mLastPressedCloseButton = clickedTab.getCloseButton();
        mRenderHost.requestRender();
    }

    if (!mScroller.isFinished()) {
        mScroller.forceFinished(true);
        mInteractingTab = null;
    }

    if (fromMouse && !clickedClose && clickedTab != null
            && clickedTab.getVisiblePercentage() >= 1.f
            && (buttons & MotionEvent.BUTTON_TERTIARY) == 0) {
        startReorderMode(time, x, x);
    }
}
 
Example 15
Source File: ToolbarSwipeLayout.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
public void swipeStarted(long time, ScrollDirection direction, float x, float y) {
    if (mTabModelSelector == null || mToTab != null || direction == ScrollDirection.DOWN) {
        return;
    }

    boolean dragFromLeftEdge = direction == ScrollDirection.RIGHT;
    // Finish off any other animations.
    forceAnimationToFinish();

    // Determine which tabs we're showing.
    TabModel model = mTabModelSelector.getCurrentModel();
    if (model == null) return;
    int fromIndex = model.index();
    if (fromIndex == TabModel.INVALID_TAB_INDEX) return;

    // On RTL, edge-dragging to the left is the next tab.
    int toIndex = (LocalizationUtils.isLayoutRtl() ^ dragFromLeftEdge) ? fromIndex - 1
                                                                       : fromIndex + 1;
    int leftIndex = dragFromLeftEdge ? toIndex : fromIndex;
    int rightIndex = !dragFromLeftEdge ? toIndex : fromIndex;

    List<Integer> visibleTabs = new ArrayList<Integer>();
    if (0 <= leftIndex && leftIndex < model.getCount()) {
        int leftTabId = model.getTabAt(leftIndex).getId();
        mLeftTab = createLayoutTab(leftTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
        prepareLayoutTabForSwipe(mLeftTab, leftIndex != fromIndex);
        visibleTabs.add(leftTabId);
    }
    if (0 <= rightIndex && rightIndex < model.getCount()) {
        int rightTabId = model.getTabAt(rightIndex).getId();
        mRightTab =
                createLayoutTab(rightTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
        prepareLayoutTabForSwipe(mRightTab, rightIndex != fromIndex);
        visibleTabs.add(rightTabId);
    }

    updateCacheVisibleIds(visibleTabs);

    mToTab = null;

    // Reset the tab offsets.
    mOffsetStart = dragFromLeftEdge ? 0 : getWidth();
    mOffset = 0;
    mOffsetTarget = 0;

    if (mLeftTab != null && mRightTab != null) {
        mLayoutTabs = new LayoutTab[] {mLeftTab, mRightTab};
    } else if (mLeftTab != null) {
        mLayoutTabs = new LayoutTab[] {mLeftTab};
    } else if (mRightTab != null) {
        mLayoutTabs = new LayoutTab[] {mRightTab};
    } else {
        mLayoutTabs = null;
    }

    requestUpdate();
}
 
Example 16
Source File: StripLayoutHelper.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
private void updateReorderPosition(float deltaX) {
    if (!mInReorderMode || mInteractingTab == null) return;

    float offset = mInteractingTab.getOffsetX() + deltaX;
    int curIndex = findIndexForTab(mInteractingTab.getId());

    // 1. Compute the reorder threshold values.
    final float flipWidth = mCachedTabWidth - mTabOverlapWidth;
    final float flipThreshold = REORDER_OVERLAP_SWITCH_PERCENTAGE * flipWidth;

    // 2. Check if we should swap tabs and track the new destination index.
    int destIndex = TabModel.INVALID_TAB_INDEX;
    boolean pastLeftThreshold = offset < -flipThreshold;
    boolean pastRightThreshold = offset > flipThreshold;
    boolean isNotRightMost = curIndex < mStripTabs.length - 1;
    boolean isNotLeftMost = curIndex > 0;

    if (LocalizationUtils.isLayoutRtl()) {
        boolean oldLeft = pastLeftThreshold;
        pastLeftThreshold = pastRightThreshold;
        pastRightThreshold = oldLeft;
    }

    if (pastRightThreshold && isNotRightMost) {
        destIndex = curIndex + 2;
    } else if (pastLeftThreshold && isNotLeftMost) {
        destIndex = curIndex - 1;
    }

    // 3. If we should swap tabs, make the swap.
    if (destIndex != TabModel.INVALID_TAB_INDEX) {
        // 3.a. Since we're about to move the tab we're dragging, adjust it's offset so it
        // stays in the same apparent position.
        boolean shouldFlip =
                LocalizationUtils.isLayoutRtl() ? destIndex < curIndex : destIndex > curIndex;
        offset += MathUtils.flipSignIf(flipWidth, shouldFlip);

        // 3.b. Swap the tabs.
        reorderTab(mInteractingTab.getId(), curIndex, destIndex, true);
        mModel.moveTab(mInteractingTab.getId(), destIndex);

        // 3.c. Update our curIndex as we have just moved the tab.
        curIndex += destIndex > curIndex ? 1 : -1;

        // 3.d. Update visual tab ordering.
        updateVisualTabOrdering();
    }

    // 4. Limit offset based on tab position.  First tab can't drag left, last tab can't drag
    // right.
    if (curIndex == 0) {
        offset =
                LocalizationUtils.isLayoutRtl() ? Math.min(0.f, offset) : Math.max(0.f, offset);
    }
    if (curIndex == mStripTabs.length - 1) {
        offset =
                LocalizationUtils.isLayoutRtl() ? Math.max(0.f, offset) : Math.min(0.f, offset);
    }

    // 5. Set the new offset.
    mInteractingTab.setOffsetX(offset);
}
 
Example 17
Source File: StripLayoutHelper.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void updateReorderPosition(float deltaX) {
    if (!mInReorderMode || mInteractingTab == null) return;

    float offset = mInteractingTab.getOffsetX() + deltaX;
    int curIndex = findIndexForTab(mInteractingTab.getId());

    // 1. Compute the reorder threshold values.
    final float flipWidth = mCachedTabWidth - mTabOverlapWidth;
    final float flipThreshold = REORDER_OVERLAP_SWITCH_PERCENTAGE * flipWidth;

    // 2. Check if we should swap tabs and track the new destination index.
    int destIndex = TabModel.INVALID_TAB_INDEX;
    boolean pastLeftThreshold = offset < -flipThreshold;
    boolean pastRightThreshold = offset > flipThreshold;
    boolean isNotRightMost = curIndex < mStripTabs.length - 1;
    boolean isNotLeftMost = curIndex > 0;

    if (LocalizationUtils.isLayoutRtl()) {
        boolean oldLeft = pastLeftThreshold;
        pastLeftThreshold = pastRightThreshold;
        pastRightThreshold = oldLeft;
    }

    if (pastRightThreshold && isNotRightMost) {
        destIndex = curIndex + 2;
    } else if (pastLeftThreshold && isNotLeftMost) {
        destIndex = curIndex - 1;
    }

    // 3. If we should swap tabs, make the swap.
    if (destIndex != TabModel.INVALID_TAB_INDEX) {
        // 3.a. Since we're about to move the tab we're dragging, adjust it's offset so it
        // stays in the same apparent position.
        boolean shouldFlip =
                LocalizationUtils.isLayoutRtl() ? destIndex < curIndex : destIndex > curIndex;
        offset += MathUtils.flipSignIf(flipWidth, shouldFlip);

        // 3.b. Swap the tabs.
        reorderTab(mInteractingTab.getId(), curIndex, destIndex, true);
        mModel.moveTab(mInteractingTab.getId(), destIndex);

        // 3.c. Update our curIndex as we have just moved the tab.
        curIndex += destIndex > curIndex ? 1 : -1;

        // 3.d. Update visual tab ordering.
        updateVisualTabOrdering();
    }

    // 4. Limit offset based on tab position.  First tab can't drag left, last tab can't drag
    // right.
    if (curIndex == 0) {
        offset =
                LocalizationUtils.isLayoutRtl() ? Math.min(0.f, offset) : Math.max(0.f, offset);
    }
    if (curIndex == mStripTabs.length - 1) {
        offset =
                LocalizationUtils.isLayoutRtl() ? Math.max(0.f, offset) : Math.min(0.f, offset);
    }

    // 5. Set the new offset.
    mInteractingTab.setOffsetX(offset);
}
 
Example 18
Source File: ToolbarSwipeLayout.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
public void swipeStarted(long time, ScrollDirection direction, float x, float y) {
    if (mTabModelSelector == null || mToTab != null || direction == ScrollDirection.DOWN) {
        return;
    }

    boolean dragFromLeftEdge = direction == ScrollDirection.RIGHT;
    // Finish off any other animations.
    forceAnimationToFinish();

    // Determine which tabs we're showing.
    TabModel model = mTabModelSelector.getCurrentModel();
    if (model == null) return;
    int fromIndex = model.index();
    if (fromIndex == TabModel.INVALID_TAB_INDEX) return;

    // On RTL, edge-dragging to the left is the next tab.
    int toIndex = (LocalizationUtils.isLayoutRtl() ^ dragFromLeftEdge) ? fromIndex - 1
                                                                       : fromIndex + 1;
    int leftIndex = dragFromLeftEdge ? toIndex : fromIndex;
    int rightIndex = !dragFromLeftEdge ? toIndex : fromIndex;

    List<Integer> visibleTabs = new ArrayList<Integer>();
    if (0 <= leftIndex && leftIndex < model.getCount()) {
        int leftTabId = model.getTabAt(leftIndex).getId();
        mLeftTab = createLayoutTab(leftTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
        prepareLayoutTabForSwipe(mLeftTab, leftIndex != fromIndex);
        visibleTabs.add(leftTabId);
    }
    if (0 <= rightIndex && rightIndex < model.getCount()) {
        int rightTabId = model.getTabAt(rightIndex).getId();
        mRightTab =
                createLayoutTab(rightTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
        prepareLayoutTabForSwipe(mRightTab, rightIndex != fromIndex);
        visibleTabs.add(rightTabId);
    }

    updateCacheVisibleIds(visibleTabs);

    mToTab = null;

    // Reset the tab offsets.
    mOffsetStart = dragFromLeftEdge ? 0 : getWidth();
    mOffset = 0;
    mOffsetTarget = 0;

    if (mLeftTab != null && mRightTab != null) {
        mLayoutTabs = new LayoutTab[] {mLeftTab, mRightTab};
    } else if (mLeftTab != null) {
        mLayoutTabs = new LayoutTab[] {mLeftTab};
    } else if (mRightTab != null) {
        mLayoutTabs = new LayoutTab[] {mRightTab};
    } else {
        mLayoutTabs = null;
    }

    requestUpdate();
}
 
Example 19
Source File: ToolbarSwipeLayout.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void swipeStarted(long time, ScrollDirection direction, float x, float y) {
    if (mTabModelSelector == null || mToTab != null || direction == ScrollDirection.DOWN) {
        return;
    }

    boolean dragFromLeftEdge = direction == ScrollDirection.RIGHT;
    // Finish off any other animations.
    forceAnimationToFinish();

    // Determine which tabs we're showing.
    TabModel model = mTabModelSelector.getCurrentModel();
    if (model == null) return;
    int fromIndex = model.index();
    if (fromIndex == TabModel.INVALID_TAB_INDEX) return;

    // On RTL, edge-dragging to the left is the next tab.
    int toIndex = (LocalizationUtils.isLayoutRtl() ^ dragFromLeftEdge) ? fromIndex - 1
                                                                       : fromIndex + 1;
    int leftIndex = dragFromLeftEdge ? toIndex : fromIndex;
    int rightIndex = !dragFromLeftEdge ? toIndex : fromIndex;

    List<Integer> visibleTabs = new ArrayList<Integer>();
    if (0 <= leftIndex && leftIndex < model.getCount()) {
        int leftTabId = model.getTabAt(leftIndex).getId();
        mLeftTab = createLayoutTab(leftTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
        prepareLayoutTabForSwipe(mLeftTab, leftIndex != fromIndex);
        visibleTabs.add(leftTabId);
    }
    if (0 <= rightIndex && rightIndex < model.getCount()) {
        int rightTabId = model.getTabAt(rightIndex).getId();
        mRightTab =
                createLayoutTab(rightTabId, model.isIncognito(), NO_CLOSE_BUTTON, NEED_TITLE);
        prepareLayoutTabForSwipe(mRightTab, rightIndex != fromIndex);
        visibleTabs.add(rightTabId);
    }

    updateCacheVisibleIds(visibleTabs);

    mToTab = null;

    // Reset the tab offsets.
    mOffsetStart = dragFromLeftEdge ? 0 : getWidth();
    mOffset = 0;
    mOffsetTarget = 0;

    if (mLeftTab != null && mRightTab != null) {
        mLayoutTabs = new LayoutTab[] {mLeftTab, mRightTab};
    } else if (mLeftTab != null) {
        mLayoutTabs = new LayoutTab[] {mLeftTab};
    } else if (mRightTab != null) {
        mLayoutTabs = new LayoutTab[] {mRightTab};
    } else {
        mLayoutTabs = null;
    }

    requestUpdate();
}
 
Example 20
Source File: StripLayoutHelper.java    From delion with Apache License 2.0 4 votes vote down vote up
private void updateReorderPosition(float deltaX) {
    if (!mInReorderMode || mInteractingTab == null) return;

    float offset = mInteractingTab.getOffsetX() + deltaX;
    int curIndex = findIndexForTab(mInteractingTab.getId());

    // 1. Compute the reorder threshold values.
    final float flipWidth = mCachedTabWidth - mTabOverlapWidth;
    final float flipThreshold = REORDER_OVERLAP_SWITCH_PERCENTAGE * flipWidth;

    // 2. Check if we should swap tabs and track the new destination index.
    int destIndex = TabModel.INVALID_TAB_INDEX;
    boolean pastLeftThreshold = offset < -flipThreshold;
    boolean pastRightThreshold = offset > flipThreshold;
    boolean isNotRightMost = curIndex < mStripTabs.length - 1;
    boolean isNotLeftMost = curIndex > 0;

    if (LocalizationUtils.isLayoutRtl()) {
        boolean oldLeft = pastLeftThreshold;
        pastLeftThreshold = pastRightThreshold;
        pastRightThreshold = oldLeft;
    }

    if (pastRightThreshold && isNotRightMost) {
        destIndex = curIndex + 2;
    } else if (pastLeftThreshold && isNotLeftMost) {
        destIndex = curIndex - 1;
    }

    // 3. If we should swap tabs, make the swap.
    if (destIndex != TabModel.INVALID_TAB_INDEX) {
        // 3.a. Since we're about to move the tab we're dragging, adjust it's offset so it
        // stays in the same apparent position.
        boolean shouldFlip =
                LocalizationUtils.isLayoutRtl() ? destIndex < curIndex : destIndex > curIndex;
        offset += MathUtils.flipSignIf(flipWidth, shouldFlip);

        // 3.b. Swap the tabs.
        reorderTab(mInteractingTab.getId(), curIndex, destIndex, true);
        mModel.moveTab(mInteractingTab.getId(), destIndex);

        // 3.c. Update our curIndex as we have just moved the tab.
        curIndex += destIndex > curIndex ? 1 : -1;

        // 3.d. Update visual tab ordering.
        updateVisualTabOrdering();
    }

    // 4. Limit offset based on tab position.  First tab can't drag left, last tab can't drag
    // right.
    if (curIndex == 0) {
        offset =
                LocalizationUtils.isLayoutRtl() ? Math.min(0.f, offset) : Math.max(0.f, offset);
    }
    if (curIndex == mStripTabs.length - 1) {
        offset =
                LocalizationUtils.isLayoutRtl() ? Math.max(0.f, offset) : Math.min(0.f, offset);
    }

    // 5. Set the new offset.
    mInteractingTab.setOffsetX(offset);
}