summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/testcomponent/RequestPinItemActivity.java
blob: 85809922e1c8d9f65be7b6f5e6a0a3c8d002a4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.android.launcher3.testcomponent;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.IntentSender;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.widget.RemoteViews;

/**
 * Sample activity to request pinning an item.
 */
@TargetApi(26)
public class RequestPinItemActivity extends BaseTestingActivity {

    private PendingIntent mCallback = null;
    private String mShortcutId = "test-id";
    private int mRemoteViewColor = Color.TRANSPARENT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addButton("Pin Shortcut", "pinShortcut");
        addButton("Pin Widget without config ", "pinWidgetNoConfig");
        addButton("Pin Widget with config", "pinWidgetWithConfig");
    }

    public void setCallback(PendingIntent callback) {
        mCallback = callback;
    }

    public void setRemoteViewColor(int color) {
        mRemoteViewColor = color;
    }

    public void setShortcutId(String id) {
        mShortcutId = id;
    }

    public void pinShortcut() {
        ShortcutManager sm = getSystemService(ShortcutManager.class);

        // Generate icon
        int r = sm.getIconMaxWidth() / 2;
        Bitmap icon = Bitmap.createBitmap(r * 2, r * 2, Bitmap.Config.ARGB_8888);
        Paint p = new Paint();
        p.setColor(Color.RED);
        new Canvas(icon).drawCircle(r, r, r, p);

        ShortcutInfo info = new ShortcutInfo.Builder(this, mShortcutId)
                .setIntent(getPackageManager().getLaunchIntentForPackage(getPackageName()))
                .setIcon(Icon.createWithBitmap(icon))
                .setShortLabel("Test shortcut")
                .build();

        IntentSender callback = mCallback == null ? null : mCallback.getIntentSender();
        sm.requestPinShortcut(info, callback);
    }

    public void pinWidgetNoConfig() {
        requestWidget(new ComponentName(this, AppWidgetNoConfig.class));
    }

    public void pinWidgetWithConfig() {
        requestWidget(new ComponentName(this, AppWidgetWithConfig.class));
    }

    private void requestWidget(ComponentName cn) {
        Bundle extras = null;
        if (mRemoteViewColor != Color.TRANSPARENT) {
            int layoutId = getResources().getIdentifier(
                    "test_layout_appwidget_view", "layout", getPackageName());
            RemoteViews views = new RemoteViews(getPackageName(), layoutId);
            views.setInt(android.R.id.icon, "setBackgroundColor", mRemoteViewColor);
            extras = new Bundle();
            extras.putParcelable(AppWidgetManager.EXTRA_APPWIDGET_PREVIEW, views);
        }

        AppWidgetManager.getInstance(this).requestPinAppWidget(cn, extras, mCallback);
    }
}