1 /*
2  * Copyright (C) 2017 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.util;
18 
19 import static com.android.launcher3.LauncherConstants.ActivityCodes.REQUEST_HOME_ROLE;
20 import static com.android.launcher3.util.MainThreadInitializedObject.forOverride;
21 
22 import android.app.ActivityOptions;
23 import android.app.Person;
24 import android.app.role.RoleManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.LauncherActivityInfo;
28 import android.content.pm.ShortcutInfo;
29 import android.graphics.drawable.ColorDrawable;
30 import android.net.Uri;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.util.ArrayMap;
34 
35 import androidx.annotation.Nullable;
36 
37 import com.android.launcher3.BuildConfig;
38 import com.android.launcher3.Launcher;
39 import com.android.launcher3.R;
40 import com.android.launcher3.Utilities;
41 
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Map;
45 
46 /**
47  * A wrapper for the hidden API calls
48  */
49 public class ApiWrapper implements ResourceBasedOverride, SafeCloseable {
50 
51     public static final MainThreadInitializedObject<ApiWrapper> INSTANCE =
52             forOverride(ApiWrapper.class, R.string.api_wrapper_class);
53 
54     protected final Context mContext;
55 
ApiWrapper(Context context)56     public ApiWrapper(Context context) {
57         mContext = context;
58     }
59 
60     /**
61      * Returns the list of persons associated with the provided shortcut info
62      */
getPersons(ShortcutInfo si)63     public Person[] getPersons(ShortcutInfo si) {
64         return Utilities.EMPTY_PERSON_ARRAY;
65     }
66 
getActivityOverrides()67     public Map<String, LauncherActivityInfo> getActivityOverrides() {
68         return Collections.emptyMap();
69     }
70 
71     /**
72      * Creates an ActivityOptions to play fade-out animation on closing targets
73      */
createFadeOutAnimOptions()74     public ActivityOptions createFadeOutAnimOptions() {
75         return ActivityOptions.makeCustomAnimation(mContext, 0, android.R.anim.fade_out);
76     }
77 
78     /**
79      * Returns a map of all users on the device to their corresponding UI properties
80      */
queryAllUsers()81     public Map<UserHandle, UserIconInfo> queryAllUsers() {
82         UserManager um = mContext.getSystemService(UserManager.class);
83         Map<UserHandle, UserIconInfo> users = new ArrayMap<>();
84         List<UserHandle> usersActual = um.getUserProfiles();
85         if (usersActual != null) {
86             for (UserHandle user : usersActual) {
87                 long serial = um.getSerialNumberForUser(user);
88 
89                 // Simple check to check if the provided user is work profile
90                 // TODO: Migrate to a better platform API
91                 NoopDrawable d = new NoopDrawable();
92                 boolean isWork = (d != mContext.getPackageManager().getUserBadgedIcon(d, user));
93                 UserIconInfo info = new UserIconInfo(
94                         user,
95                         isWork ? UserIconInfo.TYPE_WORK : UserIconInfo.TYPE_MAIN,
96                         serial);
97                 users.put(user, info);
98             }
99         }
100         return users;
101     }
102 
103     /**
104      * Returns the list of the system packages that are installed at user creation.
105      * An empty list denotes that all system packages are installed for that user at creation.
106      */
getPreInstalledSystemPackages(UserHandle user)107     public List<String> getPreInstalledSystemPackages(UserHandle user) {
108         return Collections.emptyList();
109     }
110 
111     /**
112      * Returns an intent which can be used to start the App Market activity (Installer
113      * Activity).
114      */
getAppMarketActivityIntent(String packageName, UserHandle user)115     public Intent getAppMarketActivityIntent(String packageName, UserHandle user) {
116         return new Intent(Intent.ACTION_VIEW)
117                 .setData(new Uri.Builder()
118                         .scheme("market")
119                         .authority("details")
120                         .appendQueryParameter("id", packageName)
121                         .build())
122                 .putExtra(Intent.EXTRA_REFERRER, new Uri.Builder().scheme("android-app")
123                         .authority(BuildConfig.APPLICATION_ID).build());
124     }
125 
126     /**
127      * Returns an intent which can be used to open Private Space Settings.
128      */
129     @Nullable
getPrivateSpaceSettingsIntent()130     public Intent getPrivateSpaceSettingsIntent() {
131         return null;
132     }
133 
134     /**
135      * Checks if an activity is flagged as non-resizeable.
136      */
isNonResizeableActivity(LauncherActivityInfo lai)137     public boolean isNonResizeableActivity(LauncherActivityInfo lai) {
138         // Overridden in quickstep
139         return false;
140     }
141 
142     /**
143      * Starts an Activity which can be used to set this Launcher as the HOME app, via a consent
144      * screen. In case the consent screen cannot be shown, or the user does not set current Launcher
145      * as HOME app, a toast asking the user to do the latter is shown.
146      */
assignDefaultHomeRole(Context context)147     public void assignDefaultHomeRole(Context context) {
148         RoleManager roleManager = context.getSystemService(RoleManager.class);
149         assert roleManager != null;
150         if (roleManager.isRoleAvailable(RoleManager.ROLE_HOME)
151                 && !roleManager.isRoleHeld(RoleManager.ROLE_HOME)) {
152             Intent roleRequestIntent = roleManager.createRequestRoleIntent(
153                     RoleManager.ROLE_HOME);
154             Launcher launcher = Launcher.getLauncher(context);
155             launcher.startActivityForResult(roleRequestIntent, REQUEST_HOME_ROLE);
156         }
157     }
158 
159     @Override
close()160     public void close() { }
161 
162     private static class NoopDrawable extends ColorDrawable {
163         @Override
getIntrinsicHeight()164         public int getIntrinsicHeight() {
165             return 1;
166         }
167 
168         @Override
getIntrinsicWidth()169         public int getIntrinsicWidth() {
170             return 1;
171         }
172     }
173 }
174