• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.launcher3.qsb;
18 
19 import android.content.Context;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewDebug;
23 import android.view.ViewGroup;
24 import android.widget.RemoteViews;
25 
26 import com.android.launcher3.Launcher;
27 import com.android.launcher3.R;
28 import com.android.launcher3.widget.NavigableAppWidgetHostView;
29 
30 /**
31  * Appwidget host view with QSB specific logic.
32  */
33 public class QsbWidgetHostView extends NavigableAppWidgetHostView {
34 
35     @ViewDebug.ExportedProperty(category = "launcher")
36     private int mPreviousOrientation;
37 
QsbWidgetHostView(Context context)38     public QsbWidgetHostView(Context context) {
39         super(context);
40         setFocusable(true);
41         setBackgroundResource(R.drawable.qsb_host_view_focus_bg);
42     }
43 
44     @Override
updateAppWidget(RemoteViews remoteViews)45     public void updateAppWidget(RemoteViews remoteViews) {
46         // Store the orientation in which the widget was inflated
47         mPreviousOrientation = getResources().getConfiguration().orientation;
48         super.updateAppWidget(remoteViews);
49     }
50 
51 
isReinflateRequired(int orientation)52     public boolean isReinflateRequired(int orientation) {
53         // Re-inflate is required if the orientation has changed since last inflation.
54         return mPreviousOrientation != orientation;
55     }
56 
57     @Override
setPadding(int left, int top, int right, int bottom)58     public void setPadding(int left, int top, int right, int bottom) {
59         // Prevent the base class from applying the default widget padding.
60         super.setPadding(0, 0, 0, 0);
61     }
62 
63     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)64     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
65         try {
66             super.onLayout(changed, left, top, right, bottom);
67         } catch (final RuntimeException e) {
68             // Update the widget with 0 Layout id, to reset the view to error view.
69             post(() -> updateAppWidget(
70                     new RemoteViews(getAppWidgetInfo().provider.getPackageName(), 0)));
71         }
72     }
73 
74     @Override
getErrorView()75     protected View getErrorView() {
76         return getDefaultView(this);
77     }
78 
79     @Override
getDefaultView()80     protected View getDefaultView() {
81         View v = super.getDefaultView();
82         v.setOnClickListener((v2) ->
83                 Launcher.getLauncher(getContext()).startSearch("", false, null, true));
84         return v;
85     }
86 
getDefaultView(ViewGroup parent)87     public static View getDefaultView(ViewGroup parent) {
88         View v = LayoutInflater.from(parent.getContext())
89                 .inflate(R.layout.qsb_default_view, parent, false);
90         v.findViewById(R.id.btn_qsb_search).setOnClickListener((v2) ->
91                 Launcher.getLauncher(v2.getContext()).startSearch("", false, null, true));
92         return v;
93     }
94 
95     @Override
shouldAllowDirectClick()96     protected boolean shouldAllowDirectClick() {
97         return true;
98     }
99 }
100