<commit_before>// Copyright (c) 2013 Intel Corporation. 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.xwalk.runtime.extension.api.presentation;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;
import android.view.Display;
import android.view.ViewGroup;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;

import org.chromium.base.ThreadUtils;

import org.xwalk.runtime.extension.api.XWalkDisplayManager;
import org.xwalk.runtime.extension.XWalkExtension;
import org.xwalk.runtime.extension.XWalkExtensionContext;

/**
 * A XWalk extension for Presentation API implementation on Android.
 */
public class PresentationExtension extends XWalkExtension {
    public final static String TAG = "PresentationExtension";
    public final static String JS_API_PATH = "jsapi/presentation_api.js";
    public final static String NAME = "navigator.presentation";

    // Tags:
    private final static String TAG_BASE_URL = "baseUrl";
    private final static String TAG_CMD = "cmd";
    private final static String TAG_DATA = "data";
    private final static String TAG_REQUEST_ID = "requestId";
    private final static String TAG_URL = "url";

    // Command messages:
    private final static String CMD_DISPLAY_AVAILABLE_CHANGE = "DisplayAvailableChange";
    private final static String CMD_QUERY_DISPLAY_AVAILABILITY = "QueryDisplayAvailability";
    private final static String CMD_REQUEST_SHOW = "RequestShow";
    private final static String CMD_SHOW_SUCCEEDED = "ShowSucceeded";
    private final static String CMD_SHOW_FAILED = "ShowFailed";

    // Error messages:
    private final static String ERROR_INVALID_ACCESS = "InvalidAccessError";
    private final static String ERROR_INVALID_PARAMETER = "InvalidParameterError";
    private final static String ERROR_INVALID_STATE = "InvalidStateError";
    private final static String ERROR_NOT_FOUND = "NotFoundError";
    private final static String ERROR_NOT_SUPPORTED = "NotSupportedError";

    private XWalkDisplayManager mDisplayManager;

    // The number of available presentation displays on the system.
    private int mAvailableDisplayCount = 0;

    // The presentation content and view to be showed on the secondary display.
    // Currently, only one presentation is allowed to show at the same time.
    private XWalkPresentationContent mPresentationContent;
    private XWalkPresentationContent.PresentationDelegate mPresentationDelegate;
    private PresentationView mPresentationView;

    /**
     * Listens for the secondary display arrival and removal.
     *
     * We rely on onDisplayAdded/onDisplayRemoved callback to trigger the display
     * availability change event. The presentation display becomes available if
     * the first secondary display is arrived, and becomes unavailable if one
     * of the last secondary display is removed.
     *
     * Note the display id is a system-wide unique number for each physical connection.
     * It means that for the same display device, the display id assigned by the system
     * would be different if it is re-connected again.
     */
    private final XWalkDisplayManager.DisplayListener mDisplayListener =
            new XWalkDisplayManager.DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {
            ++mAvailableDisplayCount;

            // Notify that the secondary display for presentation show becomes
            // available now if the first one is added.
            if (mAvailableDisplayCount == 1) notifyAvailabilityChanged(true);
        }

        @Override
        public void onDisplayRemoved(int displayId) {
            --mAvailableDisplayCount;

            // Notify that the secondary display for presentation show becomes
            // unavailable now if the last one is removed already.
            if (mAvailableDisplayCount == 0) {
                notifyAvailabilityChanged(false);
                // Destroy the presentation content if there is no available secondary display
                // any more.
                if (mPresentationContent != null) {
                    mPresentationContent.close();
                    mPresentationContent = null;
                }
            }
        }

        @Override
        public void onDisplayChanged(int displayId) {
            // TODO(hmin): Figure out the behaviour when the display is changed.
        }
    };

    public PresentationExtension(String name, String jsApi, XWalkExtensionContext context) {
        super(name, jsApi, context);

        mDisplayManager = XWalkDisplayManager.getInstance(context.getContext());
    }

    private Display getPreferredDisplay() {
        Display[] displays = mDisplayManager.getPresentationDisplays();
        if (displays.length > 0) return displays[0];
        else return null;
    }

    private void notifyAvailabilityChanged(boolean isAvailable) {
        StringWriter contents = new StringWriter();
        JsonWriter writer = new JsonWriter(contents);

        try {
            writer.beginObject();
            writer.name(TAG_CMD).value(CMD_DISPLAY_AVAILABLE_CHANGE);
            writer.name(TAG_DATA).value(isAvailable);
            writer.endObject();
            writer.close();
<commit_msg>Query the number of presentation displays when extension is created.<commit_after>// Copyright (c) 2013 Intel Corporation. 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.xwalk.runtime.extension.api.presentation;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;
import android.view.Display;
import android.view.ViewGroup;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;

import org.chromium.base.ThreadUtils;

import org.xwalk.runtime.extension.api.XWalkDisplayManager;
import org.xwalk.runtime.extension.XWalkExtension;
import org.xwalk.runtime.extension.XWalkExtensionContext;

/**
 * A XWalk extension for Presentation API implementation on Android.
 */
public class PresentationExtension extends XWalkExtension {
    public final static String TAG = "PresentationExtension";
    public final static String JS_API_PATH = "jsapi/presentation_api.js";
    public final static String NAME = "navigator.presentation";

    // Tags:
    private final static String TAG_BASE_URL = "baseUrl";
    private final static String TAG_CMD = "cmd";
    private final static String TAG_DATA = "data";
    private final static String TAG_REQUEST_ID = "requestId";
    private final static String TAG_URL = "url";

    // Command messages:
    private final static String CMD_DISPLAY_AVAILABLE_CHANGE = "DisplayAvailableChange";
    private final static String CMD_QUERY_DISPLAY_AVAILABILITY = "QueryDisplayAvailability";
    private final static String CMD_REQUEST_SHOW = "RequestShow";
    private final static String CMD_SHOW_SUCCEEDED = "ShowSucceeded";
    private final static String CMD_SHOW_FAILED = "ShowFailed";

    // Error messages:
    private final static String ERROR_INVALID_ACCESS = "InvalidAccessError";
    private final static String ERROR_INVALID_PARAMETER = "InvalidParameterError";
    private final static String ERROR_INVALID_STATE = "InvalidStateError";
    private final static String ERROR_NOT_FOUND = "NotFoundError";
    private final static String ERROR_NOT_SUPPORTED = "NotSupportedError";

    private XWalkDisplayManager mDisplayManager;

    // The number of available presentation displays on the system.
    private int mAvailableDisplayCount = 0;

    // The presentation content and view to be showed on the secondary display.
    // Currently, only one presentation is allowed to show at the same time.
    private XWalkPresentationContent mPresentationContent;
    private XWalkPresentationContent.PresentationDelegate mPresentationDelegate;
    private PresentationView mPresentationView;

    /**
     * Listens for the secondary display arrival and removal.
     *
     * We rely on onDisplayAdded/onDisplayRemoved callback to trigger the display
     * availability change event. The presentation display becomes available if
     * the first secondary display is arrived, and becomes unavailable if one
     * of the last secondary display is removed.
     *
     * Note the display id is a system-wide unique number for each physical connection.
     * It means that for the same display device, the display id assigned by the system
     * would be different if it is re-connected again.
     */
    private final XWalkDisplayManager.DisplayListener mDisplayListener =
            new XWalkDisplayManager.DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {
            ++mAvailableDisplayCount;

            // Notify that the secondary display for presentation show becomes
            // available now if the first one is added.
            if (mAvailableDisplayCount == 1) notifyAvailabilityChanged(true);
        }

        @Override
        public void onDisplayRemoved(int displayId) {
            --mAvailableDisplayCount;

            // Notify that the secondary display for presentation show becomes
            // unavailable now if the last one is removed already.
            if (mAvailableDisplayCount == 0) {
                notifyAvailabilityChanged(false);
                // Destroy the presentation content if there is no available secondary display
                // any more.
                if (mPresentationContent != null) {
                    mPresentationContent.close();
                    mPresentationContent = null;
                }
            }
        }

        @Override
        public void onDisplayChanged(int displayId) {
            // TODO(hmin): Figure out the behaviour when the display is changed.
        }
    };

    public PresentationExtension(String name, String jsApi, XWalkExtensionContext context) {
        super(name, jsApi, context);

        mDisplayManager = XWalkDisplayManager.getInstance(context.getContext());
        Display[] displays = mDisplayManager.getPresentationDisplays();
        mAvailableDisplayCount = displays.length;
    }

    private Display getPreferredDisplay() {
        Display[] displays = mDisplayManager.getPresentationDisplays();
        if (displays.length > 0) return displays[0];
        else return null;
    }

    private void notifyAvailabilityChanged(boolean isAvailable) {
        StringWriter contents = new StringWriter();
        JsonWriter writer = new JsonWriter(contents);

        try {
            writer.beginObject();
            writer.name(TAG_CMD).value(CMD_DISPLAY_AVAILABLE_CHANGE);
            writer.name(TAG_DATA).value(isAvailable);
            writer.endObject();
            writer.close();

{'fraction_non_alphanumeric': 0.06663098742306663, 'fraction_numerical': 0.0016055659620016055, 'mean_word_length': 3.1433850702143387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3026162', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2672, 'n_words': 932}