1 /*
2  * Copyright (C) 2017 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 package com.android.server.pm;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.util.ArrayMap;
21 import android.util.ArraySet;
22 
23 import com.android.server.pm.ShortcutService.DumpFilter;
24 
25 import java.io.PrintWriter;
26 
27 /**
28  * This class holds per-user information for {@link ShortcutService} that doesn't have to be
29  * persisted and is kept in-memory.
30  *
31  * The access to it must be guarded with the shortcut manager lock.
32  */
33 public class ShortcutNonPersistentUser {
34 
35     private final int mUserId;
36 
37     /**
38      * Keep track of additional packages that other parts of the system have said are
39      * allowed to access shortcuts.  The key is the part of the system it came from,
40      * the value is the package name that has access.  We don't persist these because
41      * at boot all relevant system services will push this data back to us they do their
42      * normal evaluation of the state of the world.
43      */
44     private final ArrayMap<String, String> mHostPackages = new ArrayMap<>();
45 
46     /**
47      * Set of package name values from above.
48      */
49     private final ArraySet<String> mHostPackageSet = new ArraySet<>();
50 
ShortcutNonPersistentUser(int userId)51     public ShortcutNonPersistentUser(int userId) {
52         mUserId = userId;
53     }
54 
getUserId()55     public int getUserId() {
56         return mUserId;
57     }
58 
setShortcutHostPackage(@onNull String type, @Nullable String packageName)59     public void setShortcutHostPackage(@NonNull String type, @Nullable String packageName) {
60         if (packageName != null) {
61             mHostPackages.put(type, packageName);
62         } else {
63             mHostPackages.remove(type);
64         }
65 
66         mHostPackageSet.clear();
67         for (int i = 0; i < mHostPackages.size(); i++) {
68             mHostPackageSet.add(mHostPackages.valueAt(i));
69         }
70     }
71 
hasHostPackage(@onNull String packageName)72     public boolean hasHostPackage(@NonNull String packageName) {
73         return mHostPackageSet.contains(packageName);
74     }
75 
dump(@onNull PrintWriter pw, @NonNull String prefix, DumpFilter filter)76     public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
77         if (filter.shouldDumpDetails()) {
78             if (mHostPackages.size() > 0) {
79                 pw.print(prefix);
80                 pw.print("Non-persistent: user ID:");
81                 pw.println(mUserId);
82 
83                 pw.print(prefix);
84                 pw.println("  Host packages:");
85                 for (int i = 0; i < mHostPackages.size(); i++) {
86                     pw.print(prefix);
87                     pw.print("    ");
88                     pw.print(mHostPackages.keyAt(i));
89                     pw.print(": ");
90                     pw.println(mHostPackages.valueAt(i));
91                 }
92                 pw.println();
93             }
94         }
95     }
96 }
97