1 /*
2  * Copyright (C) 2023 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.systemui.car.userpicker;
18 
19 import static android.os.UserHandle.USER_SYSTEM;
20 
21 import android.app.Activity;
22 import android.app.ActivityOptions;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.os.UserHandle;
27 import android.util.Log;
28 import android.util.Slog;
29 
30 final class ActivityHelper {
31     private static final String TAG = ActivityHelper.class.getSimpleName();
32     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
33 
startUserPickerAsUserSystem(Activity activity)34     static boolean startUserPickerAsUserSystem(Activity activity) {
35         int userId = activity.getUserId();
36         int displayId = activity.getDisplayId();
37 
38         // "Trampoline pattern": restarting itself as user 0 so the user picker can stay
39         // when the user that launched the user picker logs out of the display.
40         if (userId != USER_SYSTEM) {
41             if (DEBUG) {
42                 Slog.d(TAG, "Calling user is not system, so use trampoline to go to system user, "
43                         + "and stop this user(" + userId + ") for logout!");
44             }
45             Context context = activity.getApplicationContext();
46             activity.finish();
47 
48             Intent intent = new Intent(context, UserPickerActivity.class)
49                     .setData(Uri.parse("data://com.android.car/userpicker/display" + displayId));
50             ActivityOptions options = ActivityOptions.makeBasic()
51                     .setLaunchDisplayId(displayId);
52             context.startActivityAsUser(intent, options.toBundle(), UserHandle.SYSTEM);
53             return false;
54         }
55         return true;
56     }
57 }
58