Java Code Examples for com.android.internal.util.Preconditions#checkArgumentInRange()

The following examples show how to use com.android.internal.util.Preconditions#checkArgumentInRange() . 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: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public void logSelectionModified(int start, int end,
        @Nullable TextClassification classification, @Nullable TextSelection selection) {
    try {
        if (hasActiveClassificationSession()) {
            Preconditions.checkArgumentInRange(start, 0, mText.length(), "start");
            Preconditions.checkArgumentInRange(end, start, mText.length(), "end");
            int[] wordIndices = getWordDelta(start, end);
            if (selection != null) {
                mClassificationSession.onSelectionEvent(
                        SelectionEvent.createSelectionModifiedEvent(
                                wordIndices[0], wordIndices[1], selection));
            } else if (classification != null) {
                mClassificationSession.onSelectionEvent(
                        SelectionEvent.createSelectionModifiedEvent(
                                wordIndices[0], wordIndices[1], classification));
            } else {
                mClassificationSession.onSelectionEvent(
                        SelectionEvent.createSelectionModifiedEvent(
                                wordIndices[0], wordIndices[1]));
            }
        }
    } catch (Exception e) {
        // Avoid crashes due to logging.
        Log.e(LOG_TAG, "" + e.getMessage(), e);
    }
}
 
Example 2
Source File: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public void logSelectionStarted(
        TextClassifier classificationSession,
        CharSequence text, int index,
        @InvocationMethod int invocationMethod) {
    try {
        Preconditions.checkNotNull(text);
        Preconditions.checkArgumentInRange(index, 0, text.length(), "index");
        if (mText == null || !mText.contentEquals(text)) {
            mText = text.toString();
        }
        mTokenIterator.setText(mText);
        mStartIndex = index;
        mClassificationSession = classificationSession;
        if (hasActiveClassificationSession()) {
            mClassificationSession.onSelectionEvent(
                    SelectionEvent.createSelectionStartedEvent(invocationMethod, 0));
        }
    } catch (Exception e) {
        // Avoid crashes due to logging.
        Log.e(LOG_TAG, "" + e.getMessage(), e);
    }
}
 
Example 3
Source File: SelectionActionModeHelper.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void logSelectionAction(
        int start, int end,
        @SelectionEvent.ActionType int action,
        @Nullable TextClassification classification) {
    try {
        if (hasActiveClassificationSession()) {
            Preconditions.checkArgumentInRange(start, 0, mText.length(), "start");
            Preconditions.checkArgumentInRange(end, start, mText.length(), "end");
            int[] wordIndices = getWordDelta(start, end);
            if (classification != null) {
                mClassificationSession.onSelectionEvent(
                        SelectionEvent.createSelectionActionEvent(
                                wordIndices[0], wordIndices[1], action,
                                classification));
            } else {
                mClassificationSession.onSelectionEvent(
                        SelectionEvent.createSelectionActionEvent(
                                wordIndices[0], wordIndices[1], action));
            }
            if (SelectionEvent.isTerminal(action)) {
                endTextClassificationSession();
            }
        }
    } catch (Exception e) {
        // Avoid crashes due to logging.
        Log.e(LOG_TAG, "" + e.getMessage(), e);
    }
}
 
Example 4
Source File: FillEventHistory.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new event.
 *
 * @param eventType The type of the event
 * @param datasetId The dataset the event was on, or {@code null} if the event was on the
 *                  whole response.
 * @param clientState The client state associated with the event.
 * @param selectedDatasetIds The ids of datasets selected by the user.
 * @param ignoredDatasetIds The ids of datasets NOT select by the user.
 * @param changedFieldIds The ids of fields changed by the user.
 * @param changedDatasetIds The ids of the datasets that havd values matching the
 * respective entry on {@code changedFieldIds}.
 * @param manuallyFilledFieldIds The ids of fields that were manually entered by the user
 * and belonged to datasets.
 * @param manuallyFilledDatasetIds The ids of datasets that had values matching the
 * respective entry on {@code manuallyFilledFieldIds}.
 * @param detectedFieldClassifications the field classification matches.
 *
 * @throws IllegalArgumentException If the length of {@code changedFieldIds} and
 * {@code changedDatasetIds} doesn't match.
 * @throws IllegalArgumentException If the length of {@code manuallyFilledFieldIds} and
 * {@code manuallyFilledDatasetIds} doesn't match.
 *
 * @hide
 */
public Event(int eventType, @Nullable String datasetId, @Nullable Bundle clientState,
        @Nullable List<String> selectedDatasetIds,
        @Nullable ArraySet<String> ignoredDatasetIds,
        @Nullable ArrayList<AutofillId> changedFieldIds,
        @Nullable ArrayList<String> changedDatasetIds,
        @Nullable ArrayList<AutofillId> manuallyFilledFieldIds,
        @Nullable ArrayList<ArrayList<String>> manuallyFilledDatasetIds,
        @Nullable AutofillId[] detectedFieldIds,
        @Nullable FieldClassification[] detectedFieldClassifications) {
    mEventType = Preconditions.checkArgumentInRange(eventType, 0, TYPE_CONTEXT_COMMITTED,
            "eventType");
    mDatasetId = datasetId;
    mClientState = clientState;
    mSelectedDatasetIds = selectedDatasetIds;
    mIgnoredDatasetIds = ignoredDatasetIds;
    if (changedFieldIds != null) {
        Preconditions.checkArgument(!ArrayUtils.isEmpty(changedFieldIds)
                && changedDatasetIds != null
                && changedFieldIds.size() == changedDatasetIds.size(),
                "changed ids must have same length and not be empty");
    }
    mChangedFieldIds = changedFieldIds;
    mChangedDatasetIds = changedDatasetIds;
    if (manuallyFilledFieldIds != null) {
        Preconditions.checkArgument(!ArrayUtils.isEmpty(manuallyFilledFieldIds)
                && manuallyFilledDatasetIds != null
                && manuallyFilledFieldIds.size() == manuallyFilledDatasetIds.size(),
                "manually filled ids must have same length and not be empty");
    }
    mManuallyFilledFieldIds = manuallyFilledFieldIds;
    mManuallyFilledDatasetIds = manuallyFilledDatasetIds;

    mDetectedFieldIds = detectedFieldIds;
    mDetectedFieldClassifications = detectedFieldClassifications;
}
 
Example 5
Source File: UsbPort.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** @hide */
public static void checkPowerRole(int dataRole) {
    Preconditions.checkArgumentInRange(dataRole, Constants.PortPowerRole.NONE,
            Constants.PortPowerRole.NUM_POWER_ROLES - 1, "powerRole");
}
 
Example 6
Source File: LongArray.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private  LongArray(long[] array, int size) {
    mValues = array;
    mSize = Preconditions.checkArgumentInRange(size, 0, array.length, "size");
}
 
Example 7
Source File: IntArray.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private  IntArray(int[] array, int size) {
    mValues = array;
    mSize = Preconditions.checkArgumentInRange(size, 0, array.length, "size");
}
 
Example 8
Source File: UsbRequest.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Queues the request to send or receive data on its endpoint.
 *
 * <p>For OUT endpoints, the remaining bytes of the buffer will be sent on the endpoint. For IN
 * endpoints, the endpoint will attempt to fill the remaining bytes of the buffer. If the
 * queueing operation is successful, return true. The result will be returned via
 * {@link UsbDeviceConnection#requestWait}</p>
 *
 * @param buffer the buffer containing the bytes to send, or the buffer to fill. The state
 *               of the buffer is undefined until the request is returned by
 *               {@link UsbDeviceConnection#requestWait}. If the request failed the buffer
 *               will be unchanged; if the request succeeded the position of the buffer is
 *               incremented by the number of bytes sent/received. Before
 *               {@value Build.VERSION_CODES#P}, a buffer of length larger than 16384 bytes
 *               would throw IllegalArgumentException. In API {@value Build.VERSION_CODES#P}
 *               and after, any size buffer is valid.
 *
 * @return true if the queueing operation succeeded
 */
public boolean queue(@Nullable ByteBuffer buffer) {
    // Request need to be initialized
    Preconditions.checkState(mNativeContext != 0, "request is not initialized");

    // Request can not be currently queued
    Preconditions.checkState(!mIsUsingNewQueue, "this request is currently queued");

    boolean isSend = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT);
    boolean wasQueued;

    synchronized (mLock) {
        mBuffer = buffer;

        if (buffer == null) {
            // Null buffers enqueue empty USB requests which is supported
            mIsUsingNewQueue = true;
            wasQueued = native_queue(null, 0, 0);
        } else {
            if (mConnection.getContext().getApplicationInfo().targetSdkVersion
                    < Build.VERSION_CODES.P) {
                // Can only send/receive MAX_USBFS_BUFFER_SIZE bytes at once
                Preconditions.checkArgumentInRange(buffer.remaining(), 0, MAX_USBFS_BUFFER_SIZE,
                        "number of remaining bytes");
            }

            // Can not receive into read-only buffers.
            Preconditions.checkArgument(!(buffer.isReadOnly() && !isSend), "buffer can not be "
                    + "read-only when receiving data");

            if (!buffer.isDirect()) {
                mTempBuffer = ByteBuffer.allocateDirect(mBuffer.remaining());

                if (isSend) {
                    // Copy buffer into temporary buffer
                    mBuffer.mark();
                    mTempBuffer.put(mBuffer);
                    mTempBuffer.flip();
                    mBuffer.reset();
                }

                // Send/Receive into the temp buffer instead
                buffer = mTempBuffer;
            }

            mIsUsingNewQueue = true;
            wasQueued = native_queue(buffer, buffer.position(), buffer.remaining());
        }
    }

    if (!wasQueued) {
        mIsUsingNewQueue = false;
        mTempBuffer = null;
        mBuffer = null;
    }

    return wasQueued;
}
 
Example 9
Source File: UsbPort.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** @hide */
public static void checkRoles(int powerRole, int dataRole) {
    Preconditions.checkArgumentInRange(powerRole, POWER_ROLE_NONE, POWER_ROLE_SINK,
            "powerRole");
    Preconditions.checkArgumentInRange(dataRole, DATA_ROLE_NONE, DATA_ROLE_DEVICE, "dataRole");
}
 
Example 10
Source File: UsbPort.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** @hide */
public static void checkDataRole(int mode) {
    Preconditions.checkArgumentInRange(mode, Constants.PortDataRole.NONE,
            Constants.PortDataRole.NUM_DATA_ROLES - 1, "powerRole");
}
 
Example 11
Source File: TextClassifier.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
static void checkTextLength(CharSequence text, int maxLength) {
    Preconditions.checkArgumentInRange(text.length(), 0, maxLength, "text.length()");
}
 
Example 12
Source File: UsbPort.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** @hide */
public static void checkMode(int powerRole) {
    Preconditions.checkArgumentInRange(powerRole, Constants.PortMode.NONE,
            Constants.PortMode.NUM_MODES - 1, "portMode");
}
 
Example 13
Source File: UserData.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void checkValidValue(@Nullable String value) {
    Preconditions.checkNotNull(value);
    final int length = value.length();
    Preconditions.checkArgumentInRange(length, getMinValueLength(),
            getMaxValueLength(), "value length (" + length + ")");
}
 
Example 14
Source File: PrecomputedText.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the paragraph end offset of the text.
 */
public @IntRange(from = 0) int getParagraphEnd(@IntRange(from = 0) int paraIndex) {
    Preconditions.checkArgumentInRange(paraIndex, 0, getParagraphCount(), "paraIndex");
    return mParagraphInfo[paraIndex].paragraphEnd;
}
 
Example 15
Source File: PrecomputedText.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the paragraph start offset of the text.
 */
public @IntRange(from = 0) int getParagraphStart(@IntRange(from = 0) int paraIndex) {
    Preconditions.checkArgumentInRange(paraIndex, 0, getParagraphCount(), "paraIndex");
    return paraIndex == 0 ? mStart : getParagraphEnd(paraIndex - 1);
}
 
Example 16
Source File: FillResponse.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Sets which fields are used for
 * <a href="AutofillService.html#FieldClassification">field classification</a>
 *
 * <p><b>Note:</b> This method automatically adds the
 * {@link FillResponse#FLAG_TRACK_CONTEXT_COMMITED} to the {@link #setFlags(int) flags}.

 * @throws IllegalArgumentException is length of {@code ids} args is more than
 * {@link UserData#getMaxFieldClassificationIdsSize()}.
 * @throws IllegalStateException if {@link #build()} or {@link #disableAutofill(long)} was
 * already called.
 * @throws NullPointerException if {@code ids} or any element on it is {@code null}.
 */
public Builder setFieldClassificationIds(@NonNull AutofillId... ids) {
    throwIfDestroyed();
    throwIfDisableAutofillCalled();
    Preconditions.checkArrayElementsNotNull(ids, "ids");
    Preconditions.checkArgumentInRange(ids.length, 1,
            UserData.getMaxFieldClassificationIdsSize(), "ids length");
    mFieldClassificationIds = ids;
    mFlags |= FLAG_TRACK_CONTEXT_COMMITED;
    return this;
}
 
Example 17
Source File: PrintJobInfo.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the progress of the print job.
 *
 * @param progress the progress of the job
 * @hide
 */
public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) {
    Preconditions.checkArgumentInRange(progress, 0, 1, "progress");

    mPrototype.mProgress = progress;
}
 
Example 18
Source File: PrintJobInfo.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the progress of the print job.
 *
 * @param progress the progress of the job
 *
 * @hide
 */
public void setProgress(@FloatRange(from=0.0, to=1.0) float progress) {
    Preconditions.checkArgumentInRange(progress, 0, 1, "progress");

    mProgress = progress;
}