1 /*
2  * Copyright (C) 2019 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.testcomponent;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ShortcutInfo;
22 import android.content.pm.ShortcutManager;
23 import android.graphics.drawable.Icon;
24 import android.os.Bundle;
25 
26 import com.android.launcher3.R;
27 
28 import java.util.UUID;
29 
30 /**
31  * A custom shortcut is a 1x1 widget that launches a specific intent when user tap on it.
32  * Custom shortcuts are replaced by deep shortcuts after api 25.
33  */
34 public class CustomShortcutConfigActivity extends BaseTestingActivity {
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39 
40         Intent launchIntent = new Intent(this, BaseTestingActivity.class)
41                 .setAction("com.android.launcher3.intent.action.test_shortcut");
42         Intent shortcutIntent = createShortcutResultIntent(
43                 this, UUID.randomUUID().toString(), "Shortcut",
44                 R.drawable.ic_widget, launchIntent);
45         setResult(RESULT_OK, shortcutIntent);
46         finish();
47     }
48 
createShortcutResultIntent( Context context, String uniqueId, String name, int iconId, Intent launchIntent)49     private static Intent createShortcutResultIntent(
50             Context context, String uniqueId, String name, int iconId, Intent launchIntent) {
51         ShortcutInfo shortcutInfo =
52                 createShortcutInfo(context, uniqueId, name, iconId, launchIntent);
53         ShortcutManager sm = context.getSystemService(ShortcutManager.class);
54         return sm.createShortcutResultIntent(shortcutInfo);
55     }
56 
createShortcutInfo( Context context, String uniqueId, String name, int iconId, Intent launchIntent)57     private static ShortcutInfo createShortcutInfo(
58             Context context, String uniqueId, String name, int iconId, Intent launchIntent) {
59         return new ShortcutInfo.Builder(context, uniqueId)
60                 .setShortLabel(name)
61                 .setLongLabel(name)
62                 .setIcon(Icon.createWithResource(context, iconId))
63                 .setIntent(launchIntent)
64                 .build();
65     }
66 }
67