1282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/*
2282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Copyright (C) 2011 The Android Open Source Project
3282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
4282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
5282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * you may not use this file except in compliance with the License.
6282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * You may obtain a copy of the License at
7282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
8282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
9282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
10282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Unless required by applicable law or agreed to in writing, software
11282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
12282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * See the License for the specific language governing permissions and
14282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * limitations under the License.
15282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */
16282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
17282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipackage android.text;
18282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
1919acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Guptaimport com.android.ide.common.rendering.api.LayoutLog;
2019acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Guptaimport com.android.layoutlib.bridge.Bridge;
21282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
23d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Guptaimport android.icu.text.Bidi;
24282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
25282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/**
26282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Delegate used to provide new implementation for the native methods of {@link AndroidBidi}
27282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
28282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Through the layoutlib_create tool, the original  methods of AndroidBidi have been replaced
29282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * by calls to methods of the same name in this delegate class.
30282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
31282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */
32282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipublic class AndroidBidi_Delegate {
33282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
34282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    @LayoutlibDelegate
3519acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta    /*package*/ static int runBidi(int dir, char[] chars, byte[] charInfo, int count,
3619acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            boolean haveInfo) {
3719acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta
3819acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        switch (dir) {
3919acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        case 0: // Layout.DIR_REQUEST_LTR
4084d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta            dir = Bidi.LTR;
4184d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta            break;
4219acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        case 1: // Layout.DIR_REQUEST_RTL
4384d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta            dir = Bidi.RTL;
4419acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            break;
4584d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta        case -1: // Layout.DIR_REQUEST_DEFAULT_RTL
4619acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            dir = Bidi.LEVEL_DEFAULT_RTL;
4719acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            break;
4884d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta        case -2: // Layout.DIR_REQUEST_DEFAULT_LTR
4984d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta            dir = Bidi.LEVEL_DEFAULT_LTR;
5084d1d431cfe3e66029380fa038f8816b06da120aDeepanshu Gupta            break;
5119acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        default:
5219acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            // Invalid code. Log error, assume LEVEL_DEFAULT_LTR and continue.
5319acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Invalid direction flag", null);
5419acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            dir = Bidi.LEVEL_DEFAULT_LTR;
5519acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        }
5619acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        Bidi bidi = new Bidi(chars, 0, null, 0, count, dir);
5719acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        if (charInfo != null) {
5819acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            for (int i = 0; i < count; ++i)
5919acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta            charInfo[i] = bidi.getLevelAt(i);
6019acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        }
6119acfb8e873a04d42619d222c43eda95d259d0a5Deepanshu Gupta        return bidi.getParaLevel();
62282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
63282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski}
64