• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 android.text;
18 
19 /**
20  * An AlteredCharSequence is a CharSequence that is largely mirrored from
21  * another CharSequence, except that a specified range of characters are
22  * mirrored from a different char array instead.
23  *
24  * @deprecated The functionality this class offers is easily implemented outside the framework.
25  */
26 @Deprecated
27 @android.ravenwood.annotation.RavenwoodKeepWholeClass
28 public class AlteredCharSequence
29 implements CharSequence, GetChars
30 {
31     /**
32      * Create an AlteredCharSequence whose text (and possibly spans)
33      * are mirrored from <code>source</code>, except that the range of
34      * offsets <code>substart</code> inclusive to <code>subend</code> exclusive
35      * are mirrored instead from <code>sub</code>, beginning at offset 0.
36      */
make(CharSequence source, char[] sub, int substart, int subend)37     public static AlteredCharSequence make(CharSequence source, char[] sub,
38                                            int substart, int subend) {
39         if (source instanceof Spanned)
40             return new AlteredSpanned(source, sub, substart, subend);
41         else
42             return new AlteredCharSequence(source, sub, substart, subend);
43     }
44 
AlteredCharSequence(CharSequence source, char[] sub, int substart, int subend)45     private AlteredCharSequence(CharSequence source, char[] sub,
46                                 int substart, int subend) {
47         mSource = source;
48         mChars = sub;
49         mStart = substart;
50         mEnd = subend;
51     }
52 
update(char[] sub, int substart, int subend)53     /* package */ void update(char[] sub, int substart, int subend) {
54         mChars = sub;
55         mStart = substart;
56         mEnd = subend;
57     }
58 
59     private static class AlteredSpanned
60     extends AlteredCharSequence
61     implements Spanned
62     {
AlteredSpanned(CharSequence source, char[] sub, int substart, int subend)63         private AlteredSpanned(CharSequence source, char[] sub,
64                                int substart, int subend) {
65             super(source, sub, substart, subend);
66             mSpanned = (Spanned) source;
67         }
68 
getSpans(int start, int end, Class<T> kind)69         public <T> T[] getSpans(int start, int end, Class<T> kind) {
70             return mSpanned.getSpans(start, end, kind);
71         }
72 
getSpanStart(Object span)73         public int getSpanStart(Object span) {
74             return mSpanned.getSpanStart(span);
75         }
76 
getSpanEnd(Object span)77         public int getSpanEnd(Object span) {
78             return mSpanned.getSpanEnd(span);
79         }
80 
getSpanFlags(Object span)81         public int getSpanFlags(Object span) {
82             return mSpanned.getSpanFlags(span);
83         }
84 
nextSpanTransition(int start, int end, Class kind)85         public int nextSpanTransition(int start, int end, Class kind) {
86             return mSpanned.nextSpanTransition(start, end, kind);
87         }
88 
89         private Spanned mSpanned;
90     }
91 
charAt(int off)92     public char charAt(int off) {
93         if (off >= mStart && off < mEnd)
94             return mChars[off - mStart];
95         else
96             return mSource.charAt(off);
97     }
98 
length()99     public int length() {
100         return mSource.length();
101     }
102 
subSequence(int start, int end)103     public CharSequence subSequence(int start, int end) {
104         return AlteredCharSequence.make(mSource.subSequence(start, end),
105                                         mChars, mStart - start, mEnd - start);
106     }
107 
getChars(int start, int end, char[] dest, int off)108     public void getChars(int start, int end, char[] dest, int off) {
109         TextUtils.getChars(mSource, start, end, dest, off);
110 
111         start = Math.max(mStart, start);
112         end = Math.min(mEnd, end);
113 
114         if (start > end)
115             System.arraycopy(mChars, start - mStart, dest, off, end - start);
116     }
117 
toString()118     public String toString() {
119         int len = length();
120 
121         char[] ret = new char[len];
122         getChars(0, len, ret, 0);
123         return String.valueOf(ret);
124     }
125 
126     private int mStart;
127     private int mEnd;
128     private char[] mChars;
129     private CharSequence mSource;
130 }
131