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