1 /*
2  * Copyright (C) 2017 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.widget;
18 
19 import android.annotation.SuppressLint;
20 import android.appwidget.AppWidgetProviderInfo;
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.text.Layout;
25 import android.text.StaticLayout;
26 import android.text.TextPaint;
27 import android.text.TextUtils;
28 import android.util.TypedValue;
29 import android.view.View;
30 import android.widget.RemoteViews;
31 
32 import com.android.launcher3.R;
33 
34 import java.io.PrintWriter;
35 import java.io.StringWriter;
36 
37 /**
38  * A widget host views created while the host has not bind to the system service.
39  */
40 public class DeferredAppWidgetHostView extends LauncherAppWidgetHostView {
41 
42     private final TextPaint mPaint;
43     private Layout mSetupTextLayout;
44 
DeferredAppWidgetHostView(Context context)45     public DeferredAppWidgetHostView(Context context) {
46         super(context);
47         setWillNotDraw(false);
48 
49         mPaint = new TextPaint();
50         mPaint.setColor(Color.WHITE);
51         mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
52                 mLauncher.getDeviceProfile().iconTextSizePx,
53                 getResources().getDisplayMetrics()));
54         setBackgroundResource(R.drawable.bg_deferred_app_widget);
55     }
56 
57     @Override
updateAppWidget(RemoteViews remoteViews)58     public void updateAppWidget(RemoteViews remoteViews) {
59         // Not allowed
60     }
61 
62     @Override
addView(View child)63     public void addView(View child) {
64         // Not allowed
65     }
66 
67     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)68     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
69         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
70 
71         AppWidgetProviderInfo info = getAppWidgetInfo();
72         if (info == null || TextUtils.isEmpty(info.label)) {
73             return;
74         }
75 
76         // Use double padding so that there is extra space between background and text
77         int availableWidth = getMeasuredWidth() - 2 * (getPaddingLeft() + getPaddingRight());
78         if (mSetupTextLayout != null && mSetupTextLayout.getText().equals(info.label)
79                 && mSetupTextLayout.getWidth() == availableWidth) {
80             return;
81         }
82         try {
83             mSetupTextLayout = new StaticLayout(info.label, mPaint, availableWidth,
84                     Layout.Alignment.ALIGN_CENTER, 1, 0, true);
85         } catch (IllegalArgumentException e) {
86             @SuppressLint("DrawAllocation") StringWriter stringWriter = new StringWriter();
87             @SuppressLint("DrawAllocation") PrintWriter printWriter = new PrintWriter(stringWriter);
88             mActivity.getDeviceProfile().dump(/*prefix=*/"", printWriter);
89             printWriter.flush();
90             String message = "b/203530620 "
91                     + "- availableWidth: " + availableWidth
92                     + ", getMeasuredWidth: " + getMeasuredWidth()
93                     + ", getPaddingLeft: " + getPaddingLeft()
94                     + ", getPaddingRight: " + getPaddingRight()
95                     + ", deviceProfile: " + stringWriter.toString();
96             throw new IllegalArgumentException(message, e);
97         }
98     }
99 
100     @Override
onDraw(Canvas canvas)101     protected void onDraw(Canvas canvas) {
102         if (mSetupTextLayout != null) {
103             canvas.translate(getPaddingLeft() * 2,
104                     (getHeight() - mSetupTextLayout.getHeight()) / 2);
105             mSetupTextLayout.draw(canvas);
106         }
107     }
108 }
109