1 /*
2  * Copyright (C) 2024 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.inputmethod;
18 
19 import android.annotation.NonNull;
20 import android.annotation.UserIdInt;
21 import android.content.pm.UserInfo;
22 import android.os.Handler;
23 import android.util.SparseArray;
24 
25 import com.android.internal.annotations.GuardedBy;
26 import com.android.server.pm.UserManagerInternal;
27 
28 import java.util.function.Consumer;
29 import java.util.function.IntFunction;
30 
31 final class UserDataRepository {
32 
33     @GuardedBy("ImfLock.class")
34     private final SparseArray<UserData> mUserData = new SparseArray<>();
35 
36     private final IntFunction<InputMethodBindingController> mBindingControllerFactory;
37 
38     @GuardedBy("ImfLock.class")
39     @NonNull
getOrCreate(@serIdInt int userId)40     UserData getOrCreate(@UserIdInt int userId) {
41         UserData userData = mUserData.get(userId);
42         if (userData == null) {
43             userData = new UserData(userId, mBindingControllerFactory.apply(userId));
44             mUserData.put(userId, userData);
45         }
46         return userData;
47     }
48 
49     @GuardedBy("ImfLock.class")
forAllUserData(Consumer<UserData> consumer)50     void forAllUserData(Consumer<UserData> consumer) {
51         for (int i = 0; i < mUserData.size(); i++) {
52             consumer.accept(mUserData.valueAt(i));
53         }
54     }
55 
UserDataRepository(@onNull Handler handler, @NonNull UserManagerInternal userManagerInternal, @NonNull IntFunction<InputMethodBindingController> bindingControllerFactory)56     UserDataRepository(@NonNull Handler handler, @NonNull UserManagerInternal userManagerInternal,
57             @NonNull IntFunction<InputMethodBindingController> bindingControllerFactory) {
58         mBindingControllerFactory = bindingControllerFactory;
59         userManagerInternal.addUserLifecycleListener(
60                 new UserManagerInternal.UserLifecycleListener() {
61                     @Override
62                     public void onUserRemoved(UserInfo user) {
63                         final int userId = user.id;
64                         handler.post(() -> {
65                             synchronized (ImfLock.class) {
66                                 mUserData.remove(userId);
67                             }
68                         });
69                     }
70 
71                     @Override
72                     public void onUserCreated(UserInfo user, Object unusedToken) {
73                         final int userId = user.id;
74                         handler.post(() -> {
75                             synchronized (ImfLock.class) {
76                                 getOrCreate(userId);
77                             }
78                         });
79                     }
80                 });
81     }
82 
83     /** Placeholder for all IMMS user specific fields */
84     static final class UserData {
85         @UserIdInt
86         final int mUserId;
87 
88         @NonNull
89         final InputMethodBindingController mBindingController;
90 
91         /**
92          * Intended to be instantiated only from this file.
93          */
UserData(@serIdInt int userId, @NonNull InputMethodBindingController bindingController)94         private UserData(@UserIdInt int userId,
95                 @NonNull InputMethodBindingController bindingController) {
96             mUserId = userId;
97             mBindingController = bindingController;
98         }
99 
100         @Override
toString()101         public String toString() {
102             return "UserData{" + "mUserId=" + mUserId + '}';
103         }
104     }
105 }
106