1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.compat; 18 19 import android.annotation.TargetApi; 20 import android.graphics.Matrix; 21 import android.graphics.RectF; 22 import android.os.Build; 23 import android.view.inputmethod.CursorAnchorInfo; 24 25 import javax.annotation.Nonnull; 26 import javax.annotation.Nullable; 27 28 /** 29 * A wrapper for {@link CursorAnchorInfo}, which has been introduced in API Level 21. You can use 30 * this wrapper to avoid direct dependency on newly introduced types. 31 */ 32 public class CursorAnchorInfoCompatWrapper { 33 34 /** 35 * The insertion marker or character bounds have at least one visible region. 36 */ 37 public static final int FLAG_HAS_VISIBLE_REGION = 0x01; 38 39 /** 40 * The insertion marker or character bounds have at least one invisible (clipped) region. 41 */ 42 public static final int FLAG_HAS_INVISIBLE_REGION = 0x02; 43 44 /** 45 * The insertion marker or character bounds is placed at right-to-left (RTL) character. 46 */ 47 public static final int FLAG_IS_RTL = 0x04; 48 49 CursorAnchorInfoCompatWrapper() { 50 // This class is not publicly instantiable. 51 } 52 53 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 54 @Nullable 55 public static CursorAnchorInfoCompatWrapper wrap(@Nullable final CursorAnchorInfo instance) { 56 if (BuildCompatUtils.EFFECTIVE_SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 57 return null; 58 } 59 if (instance == null) { 60 return null; 61 } 62 return new RealWrapper(instance); 63 } 64 65 public int getSelectionStart() { 66 throw new UnsupportedOperationException("not supported."); 67 } 68 69 public int getSelectionEnd() { 70 throw new UnsupportedOperationException("not supported."); 71 } 72 73 public CharSequence getComposingText() { 74 throw new UnsupportedOperationException("not supported."); 75 } 76 77 public int getComposingTextStart() { 78 throw new UnsupportedOperationException("not supported."); 79 } 80 81 public Matrix getMatrix() { 82 throw new UnsupportedOperationException("not supported."); 83 } 84 85 @SuppressWarnings("unused") 86 public RectF getCharacterBounds(final int index) { 87 throw new UnsupportedOperationException("not supported."); 88 } 89 90 @SuppressWarnings("unused") 91 public int getCharacterBoundsFlags(final int index) { 92 throw new UnsupportedOperationException("not supported."); 93 } 94 95 public float getInsertionMarkerBaseline() { 96 throw new UnsupportedOperationException("not supported."); 97 } 98 99 public float getInsertionMarkerBottom() { 100 throw new UnsupportedOperationException("not supported."); 101 } 102 103 public float getInsertionMarkerHorizontal() { 104 throw new UnsupportedOperationException("not supported."); 105 } 106 107 public float getInsertionMarkerTop() { 108 throw new UnsupportedOperationException("not supported."); 109 } 110 111 public int getInsertionMarkerFlags() { 112 throw new UnsupportedOperationException("not supported."); 113 } 114 115 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 116 private static final class RealWrapper extends CursorAnchorInfoCompatWrapper { 117 118 @Nonnull 119 private final CursorAnchorInfo mInstance; 120 121 public RealWrapper(@Nonnull final CursorAnchorInfo info) { 122 mInstance = info; 123 } 124 125 @Override 126 public int getSelectionStart() { 127 return mInstance.getSelectionStart(); 128 } 129 130 @Override 131 public int getSelectionEnd() { 132 return mInstance.getSelectionEnd(); 133 } 134 135 @Override 136 public CharSequence getComposingText() { 137 return mInstance.getComposingText(); 138 } 139 140 @Override 141 public int getComposingTextStart() { 142 return mInstance.getComposingTextStart(); 143 } 144 145 @Override 146 public Matrix getMatrix() { 147 return mInstance.getMatrix(); 148 } 149 150 @Override 151 public RectF getCharacterBounds(final int index) { 152 return mInstance.getCharacterBounds(index); 153 } 154 155 @Override 156 public int getCharacterBoundsFlags(final int index) { 157 return mInstance.getCharacterBoundsFlags(index); 158 } 159 160 @Override 161 public float getInsertionMarkerBaseline() { 162 return mInstance.getInsertionMarkerBaseline(); 163 } 164 165 @Override 166 public float getInsertionMarkerBottom() { 167 return mInstance.getInsertionMarkerBottom(); 168 } 169 170 @Override 171 public float getInsertionMarkerHorizontal() { 172 return mInstance.getInsertionMarkerHorizontal(); 173 } 174 175 @Override 176 public float getInsertionMarkerTop() { 177 return mInstance.getInsertionMarkerTop(); 178 } 179 180 @Override 181 public int getInsertionMarkerFlags() { 182 return mInstance.getInsertionMarkerFlags(); 183 } 184 } 185 } 186