1782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal/*
2782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * Copyright (C) 2017 The Android Open Source Project
3782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal *
4782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * you may not use this file except in compliance with the License.
6782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * You may obtain a copy of the License at
7782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal *
8782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal *
10782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * Unless required by applicable law or agreed to in writing, software
11782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * See the License for the specific language governing permissions and
14782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * limitations under the License.
15782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal */
16782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
17782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalpackage com.android.launcher3.compat;
18782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
19782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.annotation.TargetApi;
20782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.app.Activity;
21782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.ActivityNotFoundException;
22782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.ComponentName;
23782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.Intent;
24782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.IntentSender;
25782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.pm.ActivityInfo;
26782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.pm.LauncherActivityInfo;
27782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.pm.LauncherApps;
28782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.content.pm.PackageManager;
29782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.graphics.drawable.Drawable;
30782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.os.Process;
31782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.os.UserHandle;
32782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.util.Log;
33782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport android.widget.Toast;
34782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
35782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport com.android.launcher3.IconCache;
36778634c81870ae2013907950a98bb7793802e6c7Sunny Goyalimport com.android.launcher3.LauncherSettings;
37782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport com.android.launcher3.R;
38dec3a908bfa395095e80e4a532cff98612b624deSunny Goyalimport com.android.launcher3.ShortcutInfo;
39782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
40782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal/**
41782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal * Wrapper class for representing a shortcut configure activity.
42782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal */
43782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalpublic abstract class ShortcutConfigActivityInfo {
44782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
45782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    private static final String TAG = "SCActivityInfo";
46782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
47782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    private final ComponentName mCn;
48782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    private final UserHandle mUser;
49782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
5065513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal    protected ShortcutConfigActivityInfo(ComponentName cn, UserHandle user) {
51782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        mCn = cn;
52782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        mUser = user;
53782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    }
54782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
55782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    public ComponentName getComponent() {
56782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        return mCn;
57782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    }
58782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
59782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    public UserHandle getUser() {
60782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        return mUser;
61782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    }
62782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
63778634c81870ae2013907950a98bb7793802e6c7Sunny Goyal    public int getItemType() {
64778634c81870ae2013907950a98bb7793802e6c7Sunny Goyal        return LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
65778634c81870ae2013907950a98bb7793802e6c7Sunny Goyal    }
66778634c81870ae2013907950a98bb7793802e6c7Sunny Goyal
67782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    public abstract CharSequence getLabel();
68782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
69782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    public abstract Drawable getFullResIcon(IconCache cache);
70782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
71dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    /**
72dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * Return a shortcut info, if it can be created directly on drop, without requiring any
73dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * {@link #startConfigActivity(Activity, int)}.
74dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     */
75dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    public ShortcutInfo createShortcutInfo() {
76dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal        return null;
77dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    }
78dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal
79dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    public boolean startConfigActivity(Activity activity, int requestCode) {
80782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT)
81782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                .setComponent(getComponent());
82782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        try {
83782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            activity.startActivityForResult(intent, requestCode);
84782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            return true;
85782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        } catch (ActivityNotFoundException e) {
86782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
87782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        } catch (SecurityException e) {
88782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
89782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
90782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
91782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                    "or use the exported attribute for this activity.", e);
92782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
93782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        return false;
94782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    }
9565513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal
9665513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal    /**
9765513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal     * Returns true if various properties ({@link #getLabel()}, {@link #getFullResIcon}) can
9865513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal     * be safely persisted.
9965513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal     */
10065513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal    public boolean isPersistable() {
10165513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal        return true;
10265513ba58c7d22bae2db82be6a2bfd243d844be9Sunny Goyal    }
103782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
104782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    static class ShortcutConfigActivityInfoVL extends ShortcutConfigActivityInfo {
105782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
106782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        private final ActivityInfo mInfo;
107782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        private final PackageManager mPm;
108782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
109782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
110782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        public ShortcutConfigActivityInfoVL(ActivityInfo info, PackageManager pm) {
111782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            super(new ComponentName(info.packageName, info.name), Process.myUserHandle());
112782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            mInfo = info;
113782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            mPm = pm;
114782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
115782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
116782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        @Override
117782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        public CharSequence getLabel() {
118782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            return mInfo.loadLabel(mPm);
119782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
120782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
121782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        @Override
122782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        public Drawable getFullResIcon(IconCache cache) {
123782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            return cache.getFullResIcon(mInfo);
124782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
125782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    }
126782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
127782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    @TargetApi(26)
128027fba3527fbafe1d0a24136e91de6796961b866Sunny Goyal    public static class ShortcutConfigActivityInfoVO extends ShortcutConfigActivityInfo {
129782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
130782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        private final LauncherActivityInfo mInfo;
131782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
132782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        public ShortcutConfigActivityInfoVO(LauncherActivityInfo info) {
133782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            super(info.getComponentName(), info.getUser());
134782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            mInfo = info;
135782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
136782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
137782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        @Override
138782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        public CharSequence getLabel() {
139782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            return mInfo.getLabel();
140782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
141782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
142782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        @Override
143782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        public Drawable getFullResIcon(IconCache cache) {
144782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            return cache.getFullResIcon(mInfo);
145782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
146782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal
147782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        @Override
148dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal        public boolean startConfigActivity(Activity activity, int requestCode) {
149782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            if (getUser().equals(Process.myUserHandle())) {
150782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                return super.startConfigActivity(activity, requestCode);
151782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            }
15222ca9ec137ec8eb70026f90591ae01af7669a3b7Sunny Goyal            IntentSender is = activity.getSystemService(LauncherApps.class)
15322ca9ec137ec8eb70026f90591ae01af7669a3b7Sunny Goyal                    .getShortcutConfigActivityIntent(mInfo);
154782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            try {
155782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                activity.startIntentSenderForResult(is, requestCode, null, 0, 0, 0);
156782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                return true;
15722ca9ec137ec8eb70026f90591ae01af7669a3b7Sunny Goyal            } catch (IntentSender.SendIntentException e) {
15822ca9ec137ec8eb70026f90591ae01af7669a3b7Sunny Goyal                Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
159782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                return false;
160782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal            }
161782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal        }
162782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    }
163782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal}
164