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
17package com.android.inputmethod.compat;
18
19import android.annotation.TargetApi;
20import android.graphics.Outline;
21import android.inputmethodservice.InputMethodService;
22import android.os.Build;
23import android.view.View;
24import android.view.ViewOutlineProvider;
25
26import com.android.inputmethod.compat.ViewOutlineProviderCompatUtils.InsetsUpdater;
27
28@TargetApi(Build.VERSION_CODES.LOLLIPOP)
29class ViewOutlineProviderCompatUtilsLXX {
30    private ViewOutlineProviderCompatUtilsLXX() {
31        // This utility class is not publicly instantiable.
32    }
33
34    static InsetsUpdater setInsetsOutlineProvider(final View view) {
35        final InsetsOutlineProvider provider = new InsetsOutlineProvider(view);
36        view.setOutlineProvider(provider);
37        return provider;
38    }
39
40    private static class InsetsOutlineProvider extends ViewOutlineProvider
41            implements InsetsUpdater {
42        private final View mView;
43        private static final int NO_DATA = -1;
44        private int mLastVisibleTopInsets = NO_DATA;
45
46        public InsetsOutlineProvider(final View view) {
47            mView = view;
48            view.setOutlineProvider(this);
49        }
50
51        @Override
52        public void setInsets(final InputMethodService.Insets insets) {
53            final int visibleTopInsets = insets.visibleTopInsets;
54            if (mLastVisibleTopInsets != visibleTopInsets) {
55                mLastVisibleTopInsets = visibleTopInsets;
56                mView.invalidateOutline();
57            }
58        }
59
60        @Override
61        public void getOutline(final View view, final Outline outline) {
62            if (mLastVisibleTopInsets == NO_DATA) {
63                // Call default implementation.
64                ViewOutlineProvider.BACKGROUND.getOutline(view, outline);
65                return;
66            }
67            // TODO: Revisit this when floating/resize keyboard is supported.
68            outline.setRect(
69                    view.getLeft(), mLastVisibleTopInsets, view.getRight(), view.getBottom());
70        }
71    }
72}
73