1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/googlecode/eyesfree/braille/selfbraille/WriteData.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 1.4 +/* 1.5 + * Copyright (C) 2012 Google Inc. 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not 1.8 + * use this file except in compliance with the License. You may obtain a copy of 1.9 + * the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 1.15 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 1.16 + * License for the specific language governing permissions and limitations under 1.17 + * the License. 1.18 + */ 1.19 + 1.20 +package com.googlecode.eyesfree.braille.selfbraille; 1.21 + 1.22 +import android.os.Bundle; 1.23 +import android.os.Parcel; 1.24 +import android.os.Parcelable; 1.25 +import android.view.View; 1.26 +import android.view.accessibility.AccessibilityNodeInfo; 1.27 + 1.28 +/** 1.29 + * Represents what should be shown on the braille display for a 1.30 + * part of the accessibility node tree. 1.31 + */ 1.32 +public class WriteData implements Parcelable { 1.33 + 1.34 + private static final String PROP_SELECTION_START = "selectionStart"; 1.35 + private static final String PROP_SELECTION_END = "selectionEnd"; 1.36 + 1.37 + private AccessibilityNodeInfo mAccessibilityNodeInfo; 1.38 + private CharSequence mText; 1.39 + private Bundle mProperties = Bundle.EMPTY; 1.40 + 1.41 + /** 1.42 + * Returns a new {@link WriteData} instance for the given {@code view}. 1.43 + */ 1.44 + public static WriteData forView(View view) { 1.45 + AccessibilityNodeInfo node = AccessibilityNodeInfo.obtain(view); 1.46 + WriteData writeData = new WriteData(); 1.47 + writeData.mAccessibilityNodeInfo = node; 1.48 + return writeData; 1.49 + } 1.50 + 1.51 + public static WriteData forInfo(AccessibilityNodeInfo info){ 1.52 + WriteData writeData = new WriteData(); 1.53 + writeData.mAccessibilityNodeInfo = info; 1.54 + return writeData; 1.55 + } 1.56 + 1.57 + 1.58 + public AccessibilityNodeInfo getAccessibilityNodeInfo() { 1.59 + return mAccessibilityNodeInfo; 1.60 + } 1.61 + 1.62 + /** 1.63 + * Sets the text to be displayed when the accessibility node associated 1.64 + * with this instance has focus. If this method is not called (or 1.65 + * {@code text} is {@code null}), this client relinquishes control over 1.66 + * this node. 1.67 + */ 1.68 + public WriteData setText(CharSequence text) { 1.69 + mText = text; 1.70 + return this; 1.71 + } 1.72 + 1.73 + public CharSequence getText() { 1.74 + return mText; 1.75 + } 1.76 + 1.77 + /** 1.78 + * Sets the start position in the text of a text selection or cursor that 1.79 + * should be marked on the display. A negative value (the default) means 1.80 + * no selection will be added. 1.81 + */ 1.82 + public WriteData setSelectionStart(int v) { 1.83 + writableProperties().putInt(PROP_SELECTION_START, v); 1.84 + return this; 1.85 + } 1.86 + 1.87 + /** 1.88 + * @see {@link #setSelectionStart}. 1.89 + */ 1.90 + public int getSelectionStart() { 1.91 + return mProperties.getInt(PROP_SELECTION_START, -1); 1.92 + } 1.93 + 1.94 + /** 1.95 + * Sets the end of the text selection to be marked on the display. This 1.96 + * value should only be non-negative if the selection start is 1.97 + * non-negative. If this value is <= the selection start, the selection 1.98 + * is a cursor. Otherwise, the selection covers the range from 1.99 + * start(inclusive) to end (exclusive). 1.100 + * 1.101 + * @see {@link android.text.Selection}. 1.102 + */ 1.103 + public WriteData setSelectionEnd(int v) { 1.104 + writableProperties().putInt(PROP_SELECTION_END, v); 1.105 + return this; 1.106 + } 1.107 + 1.108 + /** 1.109 + * @see {@link #setSelectionEnd}. 1.110 + */ 1.111 + public int getSelectionEnd() { 1.112 + return mProperties.getInt(PROP_SELECTION_END, -1); 1.113 + } 1.114 + 1.115 + private Bundle writableProperties() { 1.116 + if (mProperties == Bundle.EMPTY) { 1.117 + mProperties = new Bundle(); 1.118 + } 1.119 + return mProperties; 1.120 + } 1.121 + 1.122 + /** 1.123 + * Checks constraints on the fields that must be satisfied before sending 1.124 + * this instance to the self braille service. 1.125 + * @throws IllegalStateException 1.126 + */ 1.127 + public void validate() throws IllegalStateException { 1.128 + if (mAccessibilityNodeInfo == null) { 1.129 + throw new IllegalStateException( 1.130 + "Accessibility node info can't be null"); 1.131 + } 1.132 + int selectionStart = getSelectionStart(); 1.133 + int selectionEnd = getSelectionEnd(); 1.134 + if (mText == null) { 1.135 + if (selectionStart > 0 || selectionEnd > 0) { 1.136 + throw new IllegalStateException( 1.137 + "Selection can't be set without text"); 1.138 + } 1.139 + } else { 1.140 + if (selectionStart < 0 && selectionEnd >= 0) { 1.141 + throw new IllegalStateException( 1.142 + "Selection end without start"); 1.143 + } 1.144 + int textLength = mText.length(); 1.145 + if (selectionStart > textLength || selectionEnd > textLength) { 1.146 + throw new IllegalStateException("Selection out of bounds"); 1.147 + } 1.148 + } 1.149 + } 1.150 + 1.151 + // For Parcelable support. 1.152 + 1.153 + public static final Parcelable.Creator<WriteData> CREATOR = 1.154 + new Parcelable.Creator<WriteData>() { 1.155 + @Override 1.156 + public WriteData createFromParcel(Parcel in) { 1.157 + return new WriteData(in); 1.158 + } 1.159 + 1.160 + @Override 1.161 + public WriteData[] newArray(int size) { 1.162 + return new WriteData[size]; 1.163 + } 1.164 + }; 1.165 + 1.166 + @Override 1.167 + public int describeContents() { 1.168 + return 0; 1.169 + } 1.170 + 1.171 + /** 1.172 + * {@inheritDoc} 1.173 + * <strong>Note:</strong> The {@link AccessibilityNodeInfo} will be 1.174 + * recycled by this method, don't try to use this more than once. 1.175 + */ 1.176 + @Override 1.177 + public void writeToParcel(Parcel out, int flags) { 1.178 + mAccessibilityNodeInfo.writeToParcel(out, flags); 1.179 + // The above call recycles the node, so make sure we don't use it 1.180 + // anymore. 1.181 + mAccessibilityNodeInfo = null; 1.182 + out.writeString(mText.toString()); 1.183 + out.writeBundle(mProperties); 1.184 + } 1.185 + 1.186 + private WriteData() { 1.187 + } 1.188 + 1.189 + private WriteData(Parcel in) { 1.190 + mAccessibilityNodeInfo = 1.191 + AccessibilityNodeInfo.CREATOR.createFromParcel(in); 1.192 + mText = in.readString(); 1.193 + mProperties = in.readBundle(); 1.194 + } 1.195 +}