android.support.customtabs.CustomTabsCallback Java Examples

The following examples show how to use android.support.customtabs.CustomTabsCallback. 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: CustomTabsConnection.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Notifies the application of a page load metric.
 *
 * TODD(lizeb): Move this to a proper method in {@link CustomTabsCallback} once one is
 * available.
 *
 * @param session Session identifier.
 * @param metricName Name of the page load metric.
 * @param navigationStartTick Absolute navigation start time, as TimeTicks taken from native.
 *
 * @param offsetMs Offset in ms from navigationStart.
 */
boolean notifyPageLoadMetric(CustomTabsSessionToken session, String metricName,
        long navigationStartTick, long offsetMs) {
    CustomTabsCallback callback = mClientManager.getCallbackForSession(session);
    if (callback == null) return false;

    if (!mNativeTickOffsetUsComputed) {
        // Compute offset from time ticks to uptimeMillis.
        mNativeTickOffsetUsComputed = true;
        long nativeNowUs = TimeUtils.nativeGetTimeTicksNowUs();
        long javaNowUs = SystemClock.uptimeMillis() * 1000;
        mNativeTickOffsetUs = nativeNowUs - javaNowUs;
    }

    Bundle args = new Bundle();
    args.putLong(PageLoadMetrics.NAVIGATION_START,
            (navigationStartTick - mNativeTickOffsetUs) / 1000);
    args.putLong(metricName, offsetMs);
    try {
        callback.extraCallback(PAGE_LOAD_METRICS_CALLBACK, args);
    } catch (Exception e) {
        // Pokemon exception handling, see above and crbug.com/517023.
        return false;
    }
    return true;
}
 
Example #2
Source File: CustomTabObserver.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public void onPageLoadFinished(Tab tab) {
    long pageLoadFinishedTimestamp = SystemClock.elapsedRealtime();
    mCustomTabsConnection.notifyNavigationEvent(
            mSession, CustomTabsCallback.NAVIGATION_FINISHED);
    // Both histograms (commit and PLT) are reported here, to make sure
    // that they are always recorded together, and that we only record
    // commits for successful navigations.
    if (mCurrentState == STATE_WAITING_LOAD_FINISH && mIntentReceivedTimestamp > 0) {
        long timeToPageLoadStartedMs = mPageLoadStartedTimestamp - mIntentReceivedTimestamp;
        long timeToPageLoadFinishedMs =
                pageLoadFinishedTimestamp - mIntentReceivedTimestamp;
        // Same bounds and bucket count as "Startup.FirstCommitNavigationTime"
        RecordHistogram.recordCustomTimesHistogram(
                "CustomTabs.IntentToFirstCommitNavigationTime", timeToPageLoadStartedMs,
                1, TimeUnit.MINUTES.toMillis(1), TimeUnit.MILLISECONDS, 225);
        // Same bounds and bucket count as PLT histograms.
        RecordHistogram.recordCustomTimesHistogram("CustomTabs.IntentToPageLoadedTime",
                timeToPageLoadFinishedMs, 10, TimeUnit.MINUTES.toMillis(10),
                TimeUnit.MILLISECONDS, 100);
    }
    resetPageLoadTracking();
}
 
Example #3
Source File: CustomTabObserver.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void onPageLoadStarted(Tab tab, String url) {
    if (mCurrentState == STATE_WAITING_LOAD_START) {
        mPageLoadStartedTimestamp = SystemClock.elapsedRealtime();
        mCurrentState = STATE_WAITING_LOAD_FINISH;
    } else if (mCurrentState == STATE_WAITING_LOAD_FINISH) {
        if (mCustomTabsConnection != null) {
            mCustomTabsConnection.notifyNavigationEvent(
                    mSession, CustomTabsCallback.NAVIGATION_ABORTED);
            mCustomTabsConnection.sendNavigationInfo(
                    mSession, tab.getUrl(), tab.getTitle(), null);
        }
        mPageLoadStartedTimestamp = SystemClock.elapsedRealtime();
    }
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.setSendNavigationInfoForSession(mSession, false);
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_STARTED);
        mScreenshotTakenForCurrentNavigation = false;
    }
}
 
Example #4
Source File: CustomTabObserver.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public void onPageLoadStarted(Tab tab, String url) {
    if (mCurrentState == STATE_WAITING_LOAD_START) {
        mPageLoadStartedTimestamp = SystemClock.elapsedRealtime();
        mCurrentState = STATE_WAITING_LOAD_FINISH;
    } else if (mCurrentState == STATE_WAITING_LOAD_FINISH) {
        if (mCustomTabsConnection != null) {
            mCustomTabsConnection.notifyNavigationEvent(
                    mSession, CustomTabsCallback.NAVIGATION_ABORTED);
            mCustomTabsConnection.sendNavigationInfo(
                    mSession, tab.getUrl(), tab.getTitle(), null);
        }
        mPageLoadStartedTimestamp = SystemClock.elapsedRealtime();
    }
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.setSendNavigationInfoForSession(mSession, false);
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_STARTED);
        mScreenshotTakenForCurrentNavigation = false;
    }
}
 
Example #5
Source File: SettingsActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    client.warmup(0);
    customTabsSession = client.newSession(new CustomTabsCallback());
    if (customTabsSession == null) {
        return;
    }
    // Delay to not slow down native app loading
    customTabsSession.mayLaunchUrl(Uri.parse(Common.CROWDIN_URL), null, null);
}
 
Example #6
Source File: CustomTabsConnection.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the application of a navigation event.
 *
 * Delivers the {@link CustomTabsConnectionCallback#onNavigationEvent}
 * callback to the application.
 *
 * @param session The Binder object identifying the session.
 * @param navigationEvent The navigation event code, defined in {@link CustomTabsCallback}
 * @return true for success.
 */
boolean notifyNavigationEvent(CustomTabsSessionToken session, int navigationEvent) {
    CustomTabsCallback callback = mClientManager.getCallbackForSession(session);
    if (callback == null) return false;
    try {
        callback.onNavigationEvent(navigationEvent, null);
    } catch (Exception e) {
        // Catching all exceptions is really bad, but we need it here,
        // because Android exposes us to client bugs by throwing a variety
        // of exceptions. See crbug.com/517023.
        return false;
    }
    return true;
}
 
Example #7
Source File: CustomTabObserver.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onPageLoadFailed(Tab tab, int errorCode) {
    resetPageLoadTracking();
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_FAILED);
    }
}
 
Example #8
Source File: CustomTabObserver.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onDidAttachInterstitialPage(Tab tab) {
    if (tab.getSecurityLevel() != ConnectionSecurityLevel.DANGEROUS) return;
    resetPageLoadTracking();
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_FAILED);
    }
}
 
Example #9
Source File: CustomTabObserver.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onShown(Tab tab) {
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.TAB_SHOWN);
    }
}
 
Example #10
Source File: CustomTabsControllerTest.java    From Auth0.Android with MIT License 5 votes vote down vote up
private void connectBoundService() throws Exception {
    CustomTabsSession session = mock(CustomTabsSession.class);
    ComponentName componentName = new ComponentName(DEFAULT_BROWSER_PACKAGE, DEFAULT_BROWSER_PACKAGE + ".CustomTabsService");
    //This depends on an implementation detail but is the only way to test it because of methods visibility
    PowerMockito.when(session, "getComponentName").thenReturn(componentName);

    when(customTabsClient.newSession(Matchers.<CustomTabsCallback>eq(null))).thenReturn(session);
    CustomTabsServiceConnection conn = serviceConnectionCaptor.getValue();
    conn.onCustomTabsServiceConnected(componentName, customTabsClient);
    verify(customTabsClient).newSession(Matchers.<CustomTabsCallback>eq(null));
    verify(customTabsClient).warmup(eq(0L));
}
 
Example #11
Source File: StackOverflowUserAdapter.java    From Learning-Resources with MIT License 5 votes vote down vote up
private CustomTabsSession getSession() {
    return mClient.newSession(new CustomTabsCallback() {
        @Override
        public void onNavigationEvent(int navigationEvent, Bundle extras) {
            super.onNavigationEvent(navigationEvent, extras);
        }
    });
}
 
Example #12
Source File: StackOverflowUserAdapter.java    From Learning-Resources with MIT License 5 votes vote down vote up
private CustomTabsSession getSession() {
    return mClient.newSession(new CustomTabsCallback() {
        @Override
        public void onNavigationEvent(int navigationEvent, Bundle extras) {
            super.onNavigationEvent(navigationEvent, extras);
        }
    });
}
 
Example #13
Source File: StackOverflowUserAdapter.java    From Learning-Resources with MIT License 5 votes vote down vote up
private CustomTabsSession getSession() {
    return mClient.newSession(new CustomTabsCallback() {
        @Override
        public void onNavigationEvent(int navigationEvent, Bundle extras) {
            super.onNavigationEvent(navigationEvent, extras);
        }
    });
}
 
Example #14
Source File: CustomTabsConnection.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the application of a page load metric.
 *
 * TODD(lizeb): Move this to a proper method in {@link CustomTabsCallback} once one is
 * available.
 *
 * @param session Session identifier.
 * @param metricName Name of the page load metric.
 * @param offsetMs Offset in ms from navigationStart.
 */
boolean notifyPageLoadMetric(CustomTabsSessionToken session, String metricName, long offsetMs) {
    CustomTabsCallback callback = mClientManager.getCallbackForSession(session);
    if (callback == null) return false;
    Bundle args = new Bundle();
    args.putLong(metricName, offsetMs);
    try {
        callback.extraCallback(PAGE_LOAD_METRICS_CALLBACK, args);
    } catch (Exception e) {
        // Pokemon exception handling, see above and crbug.com/517023.
        return false;
    }
    return true;
}
 
Example #15
Source File: CustomTabsConnection.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the application of a navigation event.
 *
 * Delivers the {@link CustomTabsConnectionCallback#onNavigationEvent}
 * callback to the application.
 *
 * @param session The Binder object identifying the session.
 * @param navigationEvent The navigation event code, defined in {@link CustomTabsCallback}
 * @return true for success.
 */
boolean notifyNavigationEvent(CustomTabsSessionToken session, int navigationEvent) {
    CustomTabsCallback callback = mClientManager.getCallbackForSession(session);
    if (callback == null) return false;
    try {
        callback.onNavigationEvent(navigationEvent, null);
    } catch (Exception e) {
        // Catching all exceptions is really bad, but we need it here,
        // because Android exposes us to client bugs by throwing a variety
        // of exceptions. See crbug.com/517023.
        return false;
    }
    return true;
}
 
Example #16
Source File: CustomTabObserver.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onPageLoadFailed(Tab tab, int errorCode) {
    resetPageLoadTracking();
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_FAILED);
    }
}
 
Example #17
Source File: CustomTabObserver.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onPageLoadFinished(Tab tab) {
    long pageLoadFinishedTimestamp = SystemClock.elapsedRealtime();
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_FINISHED);
    }
    // Both histograms (commit and PLT) are reported here, to make sure
    // that they are always recorded together, and that we only record
    // commits for successful navigations.
    if (mCurrentState == STATE_WAITING_LOAD_FINISH && mIntentReceivedTimestamp > 0) {
        long timeToPageLoadStartedMs = mPageLoadStartedTimestamp - mIntentReceivedTimestamp;
        long timeToPageLoadFinishedMs =
                pageLoadFinishedTimestamp - mIntentReceivedTimestamp;

        String histogramPrefix = mOpenedByChrome ? "ChromeGeneratedCustomTab" : "CustomTabs";
        RecordHistogram.recordCustomTimesHistogram(
                histogramPrefix + ".IntentToFirstCommitNavigationTime2.ZoomedOut",
                timeToPageLoadStartedMs,
                50, TimeUnit.MINUTES.toMillis(10), TimeUnit.MILLISECONDS, 50);
        RecordHistogram.recordCustomTimesHistogram(
                histogramPrefix + ".IntentToFirstCommitNavigationTime2.ZoomedIn",
                timeToPageLoadStartedMs, 200, 1000, TimeUnit.MILLISECONDS, 100);
        // Same bounds and bucket count as PLT histograms.
        RecordHistogram.recordCustomTimesHistogram(histogramPrefix + ".IntentToPageLoadedTime",
                timeToPageLoadFinishedMs, 10, TimeUnit.MINUTES.toMillis(10),
                TimeUnit.MILLISECONDS, 100);
    }
    resetPageLoadTracking();
    captureNavigationInfo(tab);
}
 
Example #18
Source File: CustomTabObserver.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onShown(Tab tab) {
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.TAB_SHOWN);
    }
}
 
Example #19
Source File: CustomTabObserver.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onDidAttachInterstitialPage(Tab tab) {
    if (tab.getSecurityLevel() != ConnectionSecurityLevel.DANGEROUS) return;
    resetPageLoadTracking();
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_FAILED);
    }
}
 
Example #20
Source File: HassActivity.java    From home-assistant-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
    client.warmup(0);
    customTabsSession = client.newSession(new CustomTabsCallback());
    if (customTabsSession == null) {
        return;
    }
    // Delay to not slow down native app loading
    communicationHandler.postDelayed(() -> customTabsSession.mayLaunchUrl(Uri.parse(Utils.getUrl(HassActivity.this)), null, null), 1500);
}
 
Example #21
Source File: ChromeCustomTabsManager.java    From Anecdote with Apache License 2.0 5 votes vote down vote up
private CustomTabsSession getSession() {
    if (mClient == null) {
        mCustomTabsSession = null;
    } else if (mCustomTabsSession == null) {
        mCustomTabsSession = mClient.newSession(new CustomTabsCallback());
    }
    return mCustomTabsSession;
}
 
Example #22
Source File: Browser.java    From OsmGo with MIT License 5 votes vote down vote up
public CustomTabsSession getCustomTabsSession() {
  if (customTabsClient == null) {
    return null;
  }

  if (currentSession == null) {
    currentSession = customTabsClient.newSession(new CustomTabsCallback(){
      @Override
      public void onNavigationEvent(int navigationEvent, Bundle extras) {
        switch (navigationEvent) {
          case NAVIGATION_FINISHED:
            notifyListeners("browserPageLoaded", new JSObject());
            break;
        }
      }
    });
  }

  return currentSession;
}
 
Example #23
Source File: CustomTabsConnection.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the application of a navigation event.
 *
 * Delivers the {@link CustomTabsConnectionCallback#onNavigationEvent}
 * callback to the application.
 *
 * @param session The Binder object identifying the session.
 * @param navigationEvent The navigation event code, defined in {@link CustomTabsCallback}
 * @return true for success.
 */
boolean notifyNavigationEvent(CustomTabsSessionToken session, int navigationEvent) {
    CustomTabsCallback callback = mClientManager.getCallbackForSession(session);
    if (callback == null) return false;
    try {
        callback.onNavigationEvent(navigationEvent, null);
    } catch (Exception e) {
        // Catching all exceptions is really bad, but we need it here,
        // because Android exposes us to client bugs by throwing a variety
        // of exceptions. See crbug.com/517023.
        return false;
    }
    return true;
}
 
Example #24
Source File: CustomTabObserver.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void onPageLoadStarted(Tab tab, String url) {
    if (mCurrentState == STATE_WAITING_LOAD_START) {
        mPageLoadStartedTimestamp = SystemClock.elapsedRealtime();
        mCurrentState = STATE_WAITING_LOAD_FINISH;
    } else if (mCurrentState == STATE_WAITING_LOAD_FINISH) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_ABORTED);
        mPageLoadStartedTimestamp = SystemClock.elapsedRealtime();
    }
    mCustomTabsConnection.notifyNavigationEvent(
            mSession, CustomTabsCallback.NAVIGATION_STARTED);
}
 
Example #25
Source File: CustomTabObserver.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void onDidAttachInterstitialPage(Tab tab) {
    if (tab.getSecurityLevel() != ConnectionSecurityLevel.SECURITY_ERROR) return;
    resetPageLoadTracking();
    mCustomTabsConnection.notifyNavigationEvent(
            mSession, CustomTabsCallback.NAVIGATION_FAILED);
}
 
Example #26
Source File: CustomTabActivity.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void onPauseWithNative() {
    super.onPauseWithNative();
    CustomTabsConnection.getInstance(getApplication()).notifyNavigationEvent(
            mSession, CustomTabsCallback.TAB_HIDDEN);
}
 
Example #27
Source File: ClientManager.java    From delion with Apache License 2.0 4 votes vote down vote up
/**
 * @return The callback {@link CustomTabsSessionToken} for the given session.
 */
public synchronized CustomTabsCallback getCallbackForSession(CustomTabsSessionToken session) {
    return session != null ? session.getCallback() : null;
}
 
Example #28
Source File: CustomTabObserver.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void onPageLoadFinished(Tab tab) {
    long pageLoadFinishedTimestamp = SystemClock.elapsedRealtime();
    if (mCustomTabsConnection != null) {
        mCustomTabsConnection.notifyNavigationEvent(
                mSession, CustomTabsCallback.NAVIGATION_FINISHED);
    }

    if (mCurrentState == STATE_WAITING_LOAD_FINISH && mIntentReceivedTimestamp > 0) {
        String histogramPrefix = mOpenedByChrome ? "ChromeGeneratedCustomTab" : "CustomTabs";
        long timeToPageLoadFinishedMs = pageLoadFinishedTimestamp - mIntentReceivedTimestamp;
        if (mPageLoadStartedTimestamp > 0) {
            long timeToPageLoadStartedMs = mPageLoadStartedTimestamp - mIntentReceivedTimestamp;
            // Intent to Load Start is recorded here to make sure we do not record
            // failed/aborted page loads.
            RecordHistogram.recordCustomTimesHistogram(
                    histogramPrefix + ".IntentToFirstCommitNavigationTime2.ZoomedOut",
                    timeToPageLoadStartedMs, 50, TimeUnit.MINUTES.toMillis(10),
                    TimeUnit.MILLISECONDS, 50);
            RecordHistogram.recordCustomTimesHistogram(
                    histogramPrefix + ".IntentToFirstCommitNavigationTime2.ZoomedIn",
                    timeToPageLoadStartedMs, 200, 1000, TimeUnit.MILLISECONDS, 100);
        }
        // Same bounds and bucket count as PLT histograms.
        RecordHistogram.recordCustomTimesHistogram(histogramPrefix + ".IntentToPageLoadedTime",
                timeToPageLoadFinishedMs, 10, TimeUnit.MINUTES.toMillis(10),
                TimeUnit.MILLISECONDS, 100);

        // Not all page loads go through a navigation commit (prerender for instance).
        if (mPageLoadStartedTimestamp != 0) {
            long timeToFirstCommitMs = mFirstCommitTimestamp - mIntentReceivedTimestamp;
            // Current median is 550ms, and long tail is very long. ZoomedIn gives good view of
            // the median and ZoomedOut gives a good overview.
            RecordHistogram.recordCustomTimesHistogram(
                    "CustomTabs.IntentToFirstCommitNavigationTime3.ZoomedIn",
                    timeToFirstCommitMs, 200, 1000, TimeUnit.MILLISECONDS, 100);
            // For ZoomedOut very rarely is it under 50ms and this range matches
            // CustomTabs.IntentToFirstCommitNavigationTime2.ZoomedOut.
            RecordHistogram.recordCustomTimesHistogram(
                    "CustomTabs.IntentToFirstCommitNavigationTime3.ZoomedOut",
                    timeToFirstCommitMs, 50, TimeUnit.MINUTES.toMillis(10),
                    TimeUnit.MILLISECONDS, 50);
        }
    }
    resetPageLoadTracking();
    captureNavigationInfo(tab);
}
 
Example #29
Source File: CustomTabObserver.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
public void onShown(Tab tab) {
    mCustomTabsConnection.notifyNavigationEvent(
            mSession, CustomTabsCallback.TAB_SHOWN);
}
 
Example #30
Source File: ClientManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * @return The callback {@link CustomTabsSessionToken} for the given session.
 */
public synchronized CustomTabsCallback getCallbackForSession(CustomTabsSessionToken session) {
    return session != null ? session.getCallback() : null;
}