Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5c8c6e77 authored by Saumya Pathak's avatar Saumya Pathak
Browse files

API for launcher User Awareness

Launcher today has no way of knowing a user's type, just the user-id.
To handle UI customisations like correct badging, it needs to know the
type. We add a new hideen API which system launchers (only, for now) can use to
get user type for a user.

Bug: 295173222
Test: atest LauncherAppsTest

Change-Id: I5774167c06530b651318cc8ba56c10994b92616a
parent 8f23adcc
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentSender;
import android.content.LocusId;
import android.content.pm.LauncherUserInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IOnAppsChangedListener;
import android.content.pm.LauncherActivityInfoInternal;
@@ -62,6 +63,7 @@ interface ILauncherApps {
            in Bundle opts, in UserHandle user);
    PendingIntent getActivityLaunchIntent(String callingPackage, in ComponentName component,
            in UserHandle user);
    LauncherUserInfo getLauncherUserInfo(in UserHandle user);
    void showAppDetailsAsUser(in IApplicationThread caller, String callingPackage,
            String callingFeatureId, in ComponentName component, in Rect sourceBounds,
            in Bundle opts, in UserHandle user);
+24 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.Manifest.permission;
import static android.Manifest.permission.READ_FRAME_BUFFER;

import android.annotation.CallbackExecutor;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -55,6 +56,7 @@ import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Flags;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@@ -775,6 +777,28 @@ public class LauncherApps {
        }
    }

    /**
     * Returns information related to a user which is useful for displaying UI elements
     * to distinguish it from other users (eg, badges). Only system launchers should
     * call this API.
     *
     * @param userHandle user handle of the user for which LauncherUserInfo is requested
     * @return the LauncherUserInfo object related to the user specified.
     * @hide
     */
    @Nullable
    @FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
    public final LauncherUserInfo getLauncherUserInfo(@NonNull UserHandle userHandle) {
        if (DEBUG) {
            Log.i(TAG, "getLauncherUserInfo " + userHandle);
        }
        try {
            return mService.getLauncherUserInfo(userHandle);
        } catch (RemoteException re) {
            throw re.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the activity info for a given intent and user handle, if it resolves. Otherwise it
     * returns null.
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 android.content.pm;

parcelable LauncherUserInfo;
+126 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 android.content.pm;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.os.Flags;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.os.UserManager;

/**
 * The LauncherUserInfo object holds information about an Android user that is required to display
 * the Launcher related UI elements specific to the user (like badges).
 *
 * @hide
 */
@FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
public final class LauncherUserInfo implements Parcelable {

    private final String mUserType;

    // Serial number for the user, should be same as in the {@link UserInfo} object.
    private final int mUserSerialNumber;

    /**
     * Returns type of the user as defined in {@link UserManager}. e.g.,
     * {@link UserManager.USER_TYPE_PROFILE_MANAGED} or {@link UserManager.USER_TYPE_PROFILE_ClONE}
     * TODO(b/303812736): Make the return type public and update javadoc here once the linked bug
     * is resolved.
     *
     * @return the userType for the user whose LauncherUserInfo this is
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
    @NonNull
    public String getUserType() {
        return mUserType;
    }

    /**
     * Returns serial number of user as returned by
     * {@link UserManager#getSerialNumberForUser(UserHandle)}
     *
     * @return the serial number associated with the user
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
    public int getUserSerialNumber() {
        return mUserSerialNumber;
    }

    private LauncherUserInfo(@NonNull Parcel in) {
        mUserType = in.readString16NoHelper();
        mUserSerialNumber = in.readInt();
    }

    @Override
    @FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString16NoHelper(mUserType);
        dest.writeInt(mUserSerialNumber);
    }

    @Override
    @FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
    public int describeContents() {
        return 0;
    }

    @FlaggedApi(Flags.FLAG_ALLOW_PRIVATE_PROFILE)
    public static final @android.annotation.NonNull Creator<LauncherUserInfo> CREATOR =
            new Creator<LauncherUserInfo>() {
                @Override
                public LauncherUserInfo createFromParcel(Parcel in) {
                    return new LauncherUserInfo(in);
                }

                @Override
                public LauncherUserInfo[] newArray(int size) {
                    return new LauncherUserInfo[size];
                }
            };

    /**
     * @hide
     */
    public static final class Builder {
        private final String mUserType;

        private final int mUserSerialNumber;

        public Builder(@NonNull String userType, int userSerialNumber) {
            this.mUserType = userType;
            this.mUserSerialNumber = userSerialNumber;
        }

        /**
         * Builds the LauncherUserInfo object
         */
        @NonNull public LauncherUserInfo build() {
            return new LauncherUserInfo(this.mUserType, this.mUserSerialNumber);
        }

    } // End builder

    private LauncherUserInfo(@NonNull  String userType, int userSerialNumber) {
        this.mUserType = userType;
        this.mUserSerialNumber = userSerialNumber;
    }
}
+20 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import android.content.pm.IncrementalStatesInfo;
import android.content.pm.LauncherActivityInfoInternal;
import android.content.pm.LauncherApps;
import android.content.pm.LauncherApps.ShortcutQuery;
import android.content.pm.LauncherUserInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInstaller.SessionInfo;
import android.content.pm.PackageManager;
@@ -1370,6 +1371,25 @@ public class LauncherAppsService extends SystemService {
            }
        }

        @Override
        public @Nullable LauncherUserInfo getLauncherUserInfo(@NonNull UserHandle user) {
            // Only system launchers, which have access to recents should have access to this API.
            // TODO(b/303803157): Add the new permission check if we decide to have one.
            if (!mActivityTaskManagerInternal.isCallerRecents(Binder.getCallingUid())) {
                throw new SecurityException("Caller is not the recents app");
            }
            if (!canAccessProfile(user.getIdentifier(),
                    "Can't access LauncherUserInfo for another user")) {
                return null;
            }
            long ident = injectClearCallingIdentity();
            try {
                return mUserManagerInternal.getLauncherUserInfo(user.getIdentifier());
            } finally {
                injectRestoreCallingIdentity(ident);
            }
        }

        @Override
        public void startActivityAsUser(IApplicationThread caller, String callingPackage,
                String callingFeatureId, ComponentName component, Rect sourceBounds,
Loading