1 /* 2 * Copyright (C) 2022 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.server.devicelock; 18 19 import android.annotation.NonNull; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.os.Process; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.util.Slog; 28 29 import com.android.server.SystemService; 30 31 import java.util.Objects; 32 33 /** 34 * Service implementing DeviceLock functionality. Delegates actual interface 35 * implementation to DeviceLockServiceImpl. 36 */ 37 public final class DeviceLockService extends SystemService { 38 39 private static final String TAG = "DeviceLockService"; 40 41 private final DeviceLockServiceImpl mImpl; 42 DeviceLockService(Context context)43 public DeviceLockService(Context context) { 44 super(context); 45 Slog.d(TAG, "DeviceLockService constructor"); 46 mImpl = new DeviceLockServiceImpl(context); 47 48 final IntentFilter userFilter = new IntentFilter(); 49 userFilter.addAction(Intent.ACTION_USER_ADDED); 50 context.registerReceiver(mUserReceiver, userFilter); 51 } 52 53 @Override onStart()54 public void onStart() { 55 Slog.i(TAG, "Registering " + Context.DEVICE_LOCK_SERVICE); 56 publishBinderService(Context.DEVICE_LOCK_SERVICE, mImpl); 57 } 58 59 @Override onBootPhase(int phase)60 public void onBootPhase(int phase) { 61 Slog.d(TAG, "onBootPhase: " + phase); 62 } 63 64 @NonNull getUserContext(@onNull Context context, @NonNull UserHandle user)65 private static Context getUserContext(@NonNull Context context, @NonNull UserHandle user) { 66 if (Process.myUserHandle().equals(user)) { 67 return context; 68 } else { 69 return context.createContextAsUser(user, 0 /* flags */); 70 } 71 } 72 73 @Override isUserSupported(@onNull TargetUser user)74 public boolean isUserSupported(@NonNull TargetUser user) { 75 return isUserSupported(user.getUserHandle()); 76 } 77 isUserSupported(UserHandle userHandle)78 private boolean isUserSupported(UserHandle userHandle) { 79 final UserManager userManager = 80 getUserContext(getContext(), userHandle).getSystemService(UserManager.class); 81 return !userManager.isProfile(); 82 } 83 84 @Override onUserSwitching(@onNull TargetUser from, @NonNull TargetUser to)85 public void onUserSwitching(@NonNull TargetUser from, @NonNull TargetUser to) { 86 Objects.requireNonNull(to); 87 Slog.d(TAG, "onUserSwitching from: " + from + " to: " + to); 88 final UserHandle userHandle = to.getUserHandle(); 89 mImpl.onUserSwitching(userHandle); 90 } 91 92 @Override onUserUnlocking(@onNull TargetUser user)93 public void onUserUnlocking(@NonNull TargetUser user) { 94 Slog.d(TAG, "onUserUnlocking: " + user); 95 } 96 97 @Override onUserUnlocked(@onNull TargetUser user)98 public void onUserUnlocked(@NonNull TargetUser user) { 99 Slog.d(TAG, "onUserUnlocked: " + user); 100 final UserHandle userHandle = user.getUserHandle(); 101 final Context userContext = getUserContext(getContext(), userHandle); 102 mImpl.onUserUnlocked(userContext, userHandle); 103 } 104 105 @Override onUserStopping(@onNull TargetUser user)106 public void onUserStopping(@NonNull TargetUser user) { 107 Slog.d(TAG, "onUserStopping: " + user); 108 } 109 110 private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { 111 @Override 112 public void onReceive(Context context, Intent intent) { 113 if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { 114 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); 115 UserHandle userHandle = UserHandle.of(userId); 116 if (!isUserSupported(userHandle)) { 117 return; 118 } 119 Slog.d(TAG, "onUserAdded: " + userHandle); 120 mImpl.onUserAdded(userHandle); 121 } 122 } 123 }; 124 } 125