1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.widget;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.MotionEvent;
21 import android.widget.FrameLayout;
22 
23 import androidx.annotation.Nullable;
24 
25 /**
26  * View group managing the widget preview: either using a {@link WidgetImageView} or an actual
27  * {@link LauncherAppWidgetHostView}.
28  */
29 public class WidgetCellPreview extends FrameLayout {
WidgetCellPreview(Context context)30     public WidgetCellPreview(Context context) {
31         this(context, null);
32     }
33 
WidgetCellPreview(Context context, AttributeSet attrs)34     public WidgetCellPreview(Context context, AttributeSet attrs) {
35         this(context, attrs, 0);
36     }
37 
WidgetCellPreview(Context context, AttributeSet attrs, int defStyle)38     public WidgetCellPreview(Context context, AttributeSet attrs, int defStyle) {
39         super(context, attrs, defStyle);
40     }
41 
42     @Override
onInterceptTouchEvent(MotionEvent ev)43     public boolean onInterceptTouchEvent(MotionEvent ev) {
44         super.onInterceptTouchEvent(ev);
45         return true;
46     }
47 
48     /** Returns {@code true} if this container has a preview layout. */
hasPreviewLayout()49     public boolean hasPreviewLayout() {
50         for (int i = 0; i < getChildCount(); i++) {
51             if (getChildAt(i) instanceof LauncherAppWidgetHostView) {
52                 return true;
53             }
54         }
55         return false;
56     }
57 
58     /**
59      * Returns {@link LauncherAppWidgetHostView} if this container has a preview layout. Otherwise,
60      * returns null.
61      */
62     @Nullable
getPreviewLayout()63     public LauncherAppWidgetHostView getPreviewLayout() {
64         for (int i = 0; i < getChildCount(); i++) {
65             if (getChildAt(i) instanceof LauncherAppWidgetHostView) {
66                 return (LauncherAppWidgetHostView) getChildAt(i);
67             }
68         }
69         return null;
70     }
71 }
72