1/*
2 * Copyright (C) 2011 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
17package android.text;
18
19import com.android.ide.common.rendering.api.LayoutLog;
20import com.android.layoutlib.bridge.Bridge;
21import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22import com.ibm.icu.text.Bidi;
23
24
25/**
26 * Delegate used to provide new implementation for the native methods of {@link AndroidBidi}
27 *
28 * Through the layoutlib_create tool, the original  methods of AndroidBidi have been replaced
29 * by calls to methods of the same name in this delegate class.
30 *
31 */
32public class AndroidBidi_Delegate {
33
34    @LayoutlibDelegate
35    /*package*/ static int runBidi(int dir, char[] chars, byte[] charInfo, int count,
36            boolean haveInfo) {
37
38        switch (dir) {
39        case 0: // Layout.DIR_REQUEST_LTR
40        case 1: // Layout.DIR_REQUEST_RTL
41            break;  // No change.
42        case -1:
43            dir = Bidi.LEVEL_DEFAULT_LTR;
44            break;
45        case -2:
46            dir = Bidi.LEVEL_DEFAULT_RTL;
47            break;
48        default:
49            // Invalid code. Log error, assume LEVEL_DEFAULT_LTR and continue.
50            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Invalid direction flag", null);
51            dir = Bidi.LEVEL_DEFAULT_LTR;
52        }
53        Bidi bidi = new Bidi(chars, 0, null, 0, count, dir);
54        if (charInfo != null) {
55            for (int i = 0; i < count; ++i)
56            charInfo[i] = bidi.getLevelAt(i);
57        }
58        return bidi.getParaLevel();
59    }
60}
61