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