1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.launcher3.testcomponent; 17 18 import android.annotation.TargetApi; 19 import android.app.PendingIntent; 20 import android.appwidget.AppWidgetManager; 21 import android.content.ComponentName; 22 import android.content.IntentSender; 23 import android.content.pm.ShortcutInfo; 24 import android.content.pm.ShortcutManager; 25 import android.graphics.Bitmap; 26 import android.graphics.Canvas; 27 import android.graphics.Color; 28 import android.graphics.Paint; 29 import android.graphics.drawable.Icon; 30 import android.os.Bundle; 31 import android.widget.RemoteViews; 32 33 /** 34 * Sample activity to request pinning an item. 35 */ 36 @TargetApi(26) 37 public class RequestPinItemActivity extends BaseTestingActivity { 38 39 private PendingIntent mCallback = null; 40 private String mShortcutId = "test-id"; 41 private int mRemoteViewColor = Color.TRANSPARENT; 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 47 addButton("Pin Shortcut", "pinShortcut"); 48 addButton("Pin Widget without config ", "pinWidgetNoConfig"); 49 addButton("Pin Widget with config", "pinWidgetWithConfig"); 50 } 51 setCallback(PendingIntent callback)52 public void setCallback(PendingIntent callback) { 53 mCallback = callback; 54 } 55 setRemoteViewColor(int color)56 public void setRemoteViewColor(int color) { 57 mRemoteViewColor = color; 58 } 59 setShortcutId(String id)60 public void setShortcutId(String id) { 61 mShortcutId = id; 62 } 63 pinShortcut()64 public void pinShortcut() { 65 ShortcutManager sm = getSystemService(ShortcutManager.class); 66 67 // Generate icon 68 int r = sm.getIconMaxWidth() / 2; 69 Bitmap icon = Bitmap.createBitmap(r * 2, r * 2, Bitmap.Config.ARGB_8888); 70 Paint p = new Paint(); 71 p.setColor(Color.RED); 72 new Canvas(icon).drawCircle(r, r, r, p); 73 74 ShortcutInfo info = new ShortcutInfo.Builder(this, mShortcutId) 75 .setIntent(getPackageManager().getLaunchIntentForPackage(getPackageName())) 76 .setIcon(Icon.createWithBitmap(icon)) 77 .setShortLabel("Test shortcut") 78 .build(); 79 80 IntentSender callback = mCallback == null ? null : mCallback.getIntentSender(); 81 sm.requestPinShortcut(info, callback); 82 } 83 pinWidgetNoConfig()84 public void pinWidgetNoConfig() { 85 requestWidget(new ComponentName(this, AppWidgetNoConfig.class)); 86 } 87 pinWidgetWithConfig()88 public void pinWidgetWithConfig() { 89 requestWidget(new ComponentName(this, AppWidgetWithConfig.class)); 90 } 91 requestWidget(ComponentName cn)92 private void requestWidget(ComponentName cn) { 93 Bundle extras = null; 94 if (mRemoteViewColor != Color.TRANSPARENT) { 95 int layoutId = getResources().getIdentifier( 96 "test_layout_appwidget_view", "layout", getPackageName()); 97 RemoteViews views = new RemoteViews(getPackageName(), layoutId); 98 views.setInt(android.R.id.icon, "setBackgroundColor", mRemoteViewColor); 99 extras = new Bundle(); 100 extras.putParcelable(AppWidgetManager.EXTRA_APPWIDGET_PREVIEW, views); 101 } 102 103 AppWidgetManager.getInstance(this).requestPinAppWidget(cn, extras, mCallback); 104 } 105 } 106