mobile/android/thirdparty/com/googlecode/eyesfree/braille/selfbraille/WriteData.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /*
     2  * Copyright (C) 2012 Google Inc.
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     5  * use this file except in compliance with the License. You may obtain a copy of
     6  * the License at
     7  *
     8  * http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13  * License for the specific language governing permissions and limitations under
    14  * the License.
    15  */
    17 package com.googlecode.eyesfree.braille.selfbraille;
    19 import android.os.Bundle;
    20 import android.os.Parcel;
    21 import android.os.Parcelable;
    22 import android.view.View;
    23 import android.view.accessibility.AccessibilityNodeInfo;
    25 /**
    26  * Represents what should be shown on the braille display for a
    27  * part of the accessibility node tree.
    28  */
    29 public class WriteData implements Parcelable {
    31     private static final String PROP_SELECTION_START = "selectionStart";
    32     private static final String PROP_SELECTION_END = "selectionEnd";
    34     private AccessibilityNodeInfo mAccessibilityNodeInfo;
    35     private CharSequence mText;
    36     private Bundle mProperties = Bundle.EMPTY;
    38     /**
    39      * Returns a new {@link WriteData} instance for the given {@code view}.
    40      */
    41     public static WriteData forView(View view) {
    42         AccessibilityNodeInfo node = AccessibilityNodeInfo.obtain(view);
    43         WriteData writeData = new WriteData();
    44         writeData.mAccessibilityNodeInfo = node;
    45         return writeData;
    46     }
    48     public static WriteData forInfo(AccessibilityNodeInfo info){
    49         WriteData writeData = new WriteData();
    50         writeData.mAccessibilityNodeInfo = info;
    51         return writeData;
    52     }
    55     public AccessibilityNodeInfo getAccessibilityNodeInfo() {
    56         return mAccessibilityNodeInfo;
    57     }
    59     /**
    60      * Sets the text to be displayed when the accessibility node associated
    61      * with this instance has focus.  If this method is not called (or
    62      * {@code text} is {@code null}), this client relinquishes control over
    63      * this node.
    64      */
    65     public WriteData setText(CharSequence text) {
    66         mText = text;
    67         return this;
    68     }
    70     public CharSequence getText() {
    71         return mText;
    72     }
    74     /**
    75      * Sets the start position in the text of a text selection or cursor that
    76      * should be marked on the display.  A negative value (the default) means
    77      * no selection will be added.
    78      */
    79     public WriteData setSelectionStart(int v) {
    80         writableProperties().putInt(PROP_SELECTION_START, v);
    81         return this;
    82     }
    84     /**
    85      * @see {@link #setSelectionStart}.
    86      */
    87     public int getSelectionStart() {
    88         return mProperties.getInt(PROP_SELECTION_START, -1);
    89     }
    91     /**
    92      * Sets the end of the text selection to be marked on the display.  This
    93      * value should only be non-negative if the selection start is
    94      * non-negative.  If this value is <= the selection start, the selection
    95      * is a cursor.  Otherwise, the selection covers the range from
    96      * start(inclusive) to end (exclusive).
    97      *
    98      * @see {@link android.text.Selection}.
    99      */
   100     public WriteData setSelectionEnd(int v) {
   101         writableProperties().putInt(PROP_SELECTION_END, v);
   102         return this;
   103     }
   105     /**
   106      * @see {@link #setSelectionEnd}.
   107      */
   108     public int getSelectionEnd() {
   109         return mProperties.getInt(PROP_SELECTION_END, -1);
   110     }
   112     private Bundle writableProperties() {
   113         if (mProperties == Bundle.EMPTY) {
   114             mProperties = new Bundle();
   115         }
   116         return mProperties;
   117     }
   119     /**
   120      * Checks constraints on the fields that must be satisfied before sending
   121      * this instance to the self braille service.
   122      * @throws IllegalStateException
   123      */
   124     public void validate() throws IllegalStateException {
   125         if (mAccessibilityNodeInfo == null) {
   126             throw new IllegalStateException(
   127                 "Accessibility node info can't be null");
   128         }
   129         int selectionStart = getSelectionStart();
   130         int selectionEnd = getSelectionEnd();
   131         if (mText == null) {
   132             if (selectionStart > 0 || selectionEnd > 0) {
   133                 throw new IllegalStateException(
   134                     "Selection can't be set without text");
   135             }
   136         } else {
   137             if (selectionStart < 0 && selectionEnd >= 0) {
   138                 throw new IllegalStateException(
   139                     "Selection end without start");
   140             }
   141             int textLength = mText.length();
   142             if (selectionStart > textLength || selectionEnd > textLength) {
   143                 throw new IllegalStateException("Selection out of bounds");
   144             }
   145         }
   146     }
   148     // For Parcelable support.
   150     public static final Parcelable.Creator<WriteData> CREATOR =
   151         new Parcelable.Creator<WriteData>() {
   152             @Override
   153             public WriteData createFromParcel(Parcel in) {
   154                 return new WriteData(in);
   155             }
   157             @Override
   158             public WriteData[] newArray(int size) {
   159                 return new WriteData[size];
   160             }
   161         };
   163     @Override
   164     public int describeContents() {
   165         return 0;
   166     }
   168     /**
   169      * {@inheritDoc}
   170      * <strong>Note:</strong> The {@link AccessibilityNodeInfo} will be
   171      * recycled by this method, don't try to use this more than once.
   172      */
   173     @Override
   174     public void writeToParcel(Parcel out, int flags) {
   175         mAccessibilityNodeInfo.writeToParcel(out, flags);
   176         // The above call recycles the node, so make sure we don't use it
   177         // anymore.
   178         mAccessibilityNodeInfo = null;
   179         out.writeString(mText.toString());
   180         out.writeBundle(mProperties);
   181     }
   183     private WriteData() {
   184     }
   186     private WriteData(Parcel in) {
   187         mAccessibilityNodeInfo =
   188                 AccessibilityNodeInfo.CREATOR.createFromParcel(in);
   189         mText = in.readString();
   190         mProperties = in.readBundle();
   191     }
   192 }

mercurial