• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.appfunctions;
18 
19 import android.annotation.Nullable;
20 import android.app.appsearch.AppSearchManager;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.os.UserHandle;
24 import android.util.SparseArray;
25 
26 import com.android.internal.annotations.GuardedBy;
27 
28 /** A Singleton class that manages per-user metadata sync adapters. */
29 public final class MetadataSyncPerUser {
30     private static final String TAG = MetadataSyncPerUser.class.getSimpleName();
31 
32     /** A map of per-user adapter for synchronizing appFunction metadata. */
33     @GuardedBy("sLock")
34     private static final SparseArray<MetadataSyncAdapter> sPerUserMetadataSyncAdapter =
35             new SparseArray<>();
36 
37     private static final Object sLock = new Object();
38 
39     /**
40      * Returns the per-user metadata sync adapter for the given user.
41      *
42      * @param user The user for which to get the metadata sync adapter.
43      * @param userContext The user context for the given user.
44      * @return The metadata sync adapter for the given user.
45      */
46     @Nullable
getPerUserMetadataSyncAdapter( UserHandle user, Context userContext)47     public static MetadataSyncAdapter getPerUserMetadataSyncAdapter(
48             UserHandle user, Context userContext) {
49         synchronized (sLock) {
50             MetadataSyncAdapter metadataSyncAdapter =
51                     sPerUserMetadataSyncAdapter.get(user.getIdentifier(), null);
52             if (metadataSyncAdapter == null) {
53                 AppSearchManager perUserAppSearchManager =
54                         userContext.getSystemService(AppSearchManager.class);
55                 PackageManager perUserPackageManager = userContext.getPackageManager();
56                 if (perUserAppSearchManager != null) {
57                     metadataSyncAdapter =
58                             new MetadataSyncAdapter(perUserPackageManager, perUserAppSearchManager);
59                     sPerUserMetadataSyncAdapter.put(user.getIdentifier(), metadataSyncAdapter);
60                     return metadataSyncAdapter;
61                 }
62             }
63             return metadataSyncAdapter;
64         }
65     }
66 
67     /**
68      * Removes the per-user metadata sync adapter for the given user.
69      *
70      * @param user The user for which to remove the metadata sync adapter.
71      */
removeUserSyncAdapter(UserHandle user)72     public static void removeUserSyncAdapter(UserHandle user) {
73         synchronized (sLock) {
74             MetadataSyncAdapter metadataSyncAdapter =
75                     sPerUserMetadataSyncAdapter.get(user.getIdentifier(), null);
76             if (metadataSyncAdapter != null) {
77                 metadataSyncAdapter.shutDown();
78                 sPerUserMetadataSyncAdapter.remove(user.getIdentifier());
79             }
80         }
81     }
82 }
83