1 /*
2  * Copyright (C) 2020 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.internal.widget;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.RemoteViews;
24 import android.widget.TextView;
25 
26 import java.util.function.Consumer;
27 
28 /**
29  * A text view whose visibility can be observed.
30  */
31 @RemoteViews.RemoteView
32 public class ObservableTextView extends TextView {
33 
34     private Consumer<Integer> mOnVisibilityChangedListener;
35 
ObservableTextView(Context context)36     public ObservableTextView(Context context) {
37         super(context);
38     }
39 
ObservableTextView(Context context, @Nullable AttributeSet attrs)40     public ObservableTextView(Context context, @Nullable AttributeSet attrs) {
41         super(context, attrs);
42     }
43 
ObservableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)44     public ObservableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
45         super(context, attrs, defStyleAttr);
46     }
47 
ObservableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)48     public ObservableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
49             int defStyleRes) {
50         super(context, attrs, defStyleAttr, defStyleRes);
51     }
52 
53     @Override
onVisibilityChanged(View changedView, int visibility)54     protected void onVisibilityChanged(View changedView, int visibility) {
55         super.onVisibilityChanged(changedView, visibility);
56         if (changedView == this) {
57             if (mOnVisibilityChangedListener != null) {
58                 mOnVisibilityChangedListener.accept(visibility);
59             }
60         }
61     }
62 
setOnVisibilityChangedListener(Consumer<Integer> listener)63     public void setOnVisibilityChangedListener(Consumer<Integer> listener) {
64         mOnVisibilityChangedListener = listener;
65     }
66 }
67