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