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.appwidget.AppWidgetProviderInfo; 20 import android.content.Context; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.text.Layout; 24 import android.text.StaticLayout; 25 import android.text.TextPaint; 26 import android.text.TextUtils; 27 import android.util.TypedValue; 28 import android.widget.RemoteViews; 29 30 import com.android.launcher3.R; 31 32 /** 33 * A widget host views created while the host has not bind to the system service. 34 */ 35 public class DeferredAppWidgetHostView extends LauncherAppWidgetHostView { 36 37 private final TextPaint mPaint; 38 private Layout mSetupTextLayout; 39 DeferredAppWidgetHostView(Context context)40 public DeferredAppWidgetHostView(Context context) { 41 super(context); 42 setWillNotDraw(false); 43 44 mPaint = new TextPaint(); 45 mPaint.setColor(Color.WHITE); 46 mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 47 mLauncher.getDeviceProfile().getFullScreenProfile().iconTextSizePx, 48 getResources().getDisplayMetrics())); 49 setBackgroundResource(R.drawable.bg_deferred_app_widget); 50 } 51 52 @Override updateAppWidget(RemoteViews remoteViews)53 public void updateAppWidget(RemoteViews remoteViews) { 54 // Not allowed 55 } 56 57 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)58 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 59 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 60 61 AppWidgetProviderInfo info = getAppWidgetInfo(); 62 if (info == null || TextUtils.isEmpty(info.label)) { 63 return; 64 } 65 66 // Use double padding so that there is extra space between background and text 67 int availableWidth = getMeasuredWidth() - 2 * (getPaddingLeft() + getPaddingRight()); 68 if (mSetupTextLayout != null && mSetupTextLayout.getText().equals(info.label) 69 && mSetupTextLayout.getWidth() == availableWidth) { 70 return; 71 } 72 mSetupTextLayout = new StaticLayout(info.label, mPaint, availableWidth, 73 Layout.Alignment.ALIGN_CENTER, 1, 0, true); 74 } 75 76 @Override onDraw(Canvas canvas)77 protected void onDraw(Canvas canvas) { 78 if (mSetupTextLayout != null) { 79 canvas.translate(getPaddingLeft() * 2, 80 (getHeight() - mSetupTextLayout.getHeight()) / 2); 81 mSetupTextLayout.draw(canvas); 82 } 83 } 84 } 85