summaryrefslogtreecommitdiff
path: root/androidx/slice/widget/ShortcutView.java
blob: d200512758324866f9d55c3897633409a70ad561 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 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 androidx.slice.widget;

import static android.app.slice.Slice.HINT_LARGE;
import static android.app.slice.Slice.HINT_NO_TINT;
import static android.app.slice.Slice.HINT_TITLE;
import static android.app.slice.Slice.SUBTYPE_COLOR;
import static android.app.slice.SliceItem.FORMAT_ACTION;
import static android.app.slice.SliceItem.FORMAT_IMAGE;
import static android.app.slice.SliceItem.FORMAT_INT;
import static android.app.slice.SliceItem.FORMAT_SLICE;
import static android.app.slice.SliceItem.FORMAT_TEXT;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.RestrictTo;
import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.core.SliceQuery;
import androidx.slice.view.R;

/**
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
public class ShortcutView extends SliceChildView {

    private static final String TAG = "ShortcutView";

    private Slice mSlice;
    private Uri mUri;
    private SliceItem mActionItem;
    private SliceItem mLabel;
    private SliceItem mIcon;

    private int mLargeIconSize;
    private int mSmallIconSize;

    public ShortcutView(Context context) {
        super(context);
        final Resources res = getResources();
        mSmallIconSize = res.getDimensionPixelSize(R.dimen.abc_slice_icon_size);
        mLargeIconSize = res.getDimensionPixelSize(R.dimen.abc_slice_shortcut_size);
    }

    @SuppressLint("NewApi") // mIcon can only be non-null on API 23+
    @Override
    public void setSlice(Slice slice) {
        resetView();
        mSlice = slice;
        determineShortcutItems(getContext(), slice);
        SliceItem colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
        if (colorItem == null) {
            colorItem = SliceQuery.findSubtype(slice, FORMAT_INT, SUBTYPE_COLOR);
        }
        final int color = colorItem != null
                ? colorItem.getInt()
                : SliceViewUtil.getColorAccent(getContext());
        ShapeDrawable circle = new ShapeDrawable(new OvalShape());
        circle.setTint(color);
        ImageView iv = new ImageView(getContext());
        if (mIcon != null && !mIcon.hasHint(HINT_NO_TINT)) {
            // Only set the background if we're tintable
            iv.setBackground(circle);
        }
        addView(iv);
        if (mIcon != null) {
            boolean isImage = mIcon.hasHint(HINT_NO_TINT);
            final int iconSize = isImage ? mLargeIconSize : mSmallIconSize;
            SliceViewUtil.createCircledIcon(getContext(), iconSize, mIcon.getIcon(),
                    isImage, this /* parent */);
            mUri = slice.getUri();
            setClickable(true);
        } else {
            setClickable(false);
        }
    }

    @Override
    public @SliceView.SliceMode int getMode() {
        return SliceView.MODE_SHORTCUT;
    }

    @Override
    public boolean performClick() {
        if (!callOnClick()) {
            try {
                if (mActionItem != null) {
                    mActionItem.fireAction(null, null);
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW).setData(mUri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
                if (mObserver != null) {
                    EventInfo ei = new EventInfo(SliceView.MODE_SHORTCUT,
                            EventInfo.ACTION_TYPE_BUTTON,
                            EventInfo.ROW_TYPE_SHORTCUT, 0 /* rowIndex */);
                    SliceItem interactedItem = mActionItem != null
                            ? mActionItem
                            : new SliceItem(mSlice, FORMAT_SLICE, null /* subtype */,
                                    mSlice.getHints());
                    mObserver.onSliceAction(ei, interactedItem);
                }
            } catch (CanceledException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * Looks at the slice and determines which items are best to use to compose the shortcut.
     */
    private void determineShortcutItems(Context context, Slice slice) {
        ListContent lc = new ListContent(context, slice);
        SliceItem primaryAction = lc.getPrimaryAction();

        if (primaryAction != null) {
            // Preferred case: slice has a primary action
            mActionItem = primaryAction.getSlice().getItems().get(0);
            mIcon = SliceQuery.find(primaryAction.getSlice(), FORMAT_IMAGE, (String) null,
                    null);
            mLabel = SliceQuery.find(primaryAction.getSlice(), FORMAT_TEXT, (String) null,
                    null);
        } else {
            // No hinted action; just use the first one
            mActionItem = SliceQuery.find(slice, FORMAT_ACTION, (String) null, null);
        }
        // First fallback: any hinted image and text
        if (mIcon == null) {
            mIcon = SliceQuery.find(slice, FORMAT_IMAGE, HINT_TITLE,
                    null);
        }
        if (mLabel == null) {
            mLabel = SliceQuery.find(slice, FORMAT_TEXT, HINT_TITLE,
                    null);
        }
        // Second fallback: first image and text
        if (mIcon == null) {
            mIcon = SliceQuery.find(slice, FORMAT_IMAGE, (String) null,
                    null);
        }
        if (mLabel == null) {
            mLabel = SliceQuery.find(slice, FORMAT_TEXT, (String) null,
                    null);
        }
        // Final fallback: use app info
        if (mIcon == null || mLabel == null || mActionItem == null) {
            PackageManager pm = context.getPackageManager();
            ProviderInfo providerInfo = pm.resolveContentProvider(
                    slice.getUri().getAuthority(), 0);
            ApplicationInfo appInfo = providerInfo.applicationInfo;
            if (appInfo != null) {
                if (mIcon == null) {
                    Slice.Builder sb = new Slice.Builder(slice.getUri());
                    Drawable icon = pm.getApplicationIcon(appInfo);
                    sb.addIcon(SliceViewUtil.createIconFromDrawable(icon), HINT_LARGE);
                    mIcon = sb.build().getItems().get(0);
                }
                if (mLabel == null) {
                    Slice.Builder sb = new Slice.Builder(slice.getUri());
                    sb.addText(pm.getApplicationLabel(appInfo), null);
                    mLabel = sb.build().getItems().get(0);
                }
                if (mActionItem == null) {
                    mActionItem = new SliceItem(PendingIntent.getActivity(context, 0,
                            pm.getLaunchIntentForPackage(appInfo.packageName), 0),
                            new Slice.Builder(slice.getUri()).build(), FORMAT_SLICE,
                            null /* subtype */, null);
                }
            }
        }
    }

    @Override
    public void resetView() {
        mSlice = null;
        mUri = null;
        mActionItem = null;
        mLabel = null;
        mIcon = null;
        setBackground(null);
        removeAllViews();
    }
}