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     private final ShortcutService mService;
35 
36     private final int mUserId;
37 
38     /**
39      * Keep track of additional packages that other parts of the system have said are
40      * allowed to access shortcuts.  The key is the part of the system it came from,
41      * the value is the package name that has access.  We don't persist these because
42      * at boot all relevant system services will push this data back to us they do their
43      * normal evaluation of the state of the world.
44      */
45     private final ArrayMap<String, String> mHostPackages = new ArrayMap<>();
46 
47     /**
48      * Set of package name values from above.
49      */
50     private final ArraySet<String> mHostPackageSet = new ArraySet<>();
51 
ShortcutNonPersistentUser(ShortcutService service, int userId)52     public ShortcutNonPersistentUser(ShortcutService service, int userId) {
53         mService = service;
54         mUserId = userId;
55     }
56 
getUserId()57     public int getUserId() {
58         return mUserId;
59     }
60 
setShortcutHostPackage(@onNull String type, @Nullable String packageName)61     public void setShortcutHostPackage(@NonNull String type, @Nullable String packageName) {
62         if (packageName != null) {
63             mHostPackages.put(type, packageName);
64         } else {
65             mHostPackages.remove(type);
66         }
67 
68         mHostPackageSet.clear();
69         for (int i = 0; i < mHostPackages.size(); i++) {
70             mHostPackageSet.add(mHostPackages.valueAt(i));
71         }
72     }
73 
hasHostPackage(@onNull String packageName)74     public boolean hasHostPackage(@NonNull String packageName) {
75         return mHostPackageSet.contains(packageName);
76     }
77 
dump(@onNull PrintWriter pw, @NonNull String prefix, DumpFilter filter)78     public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
79         if (filter.shouldDumpDetails()) {
80             if (mHostPackages.size() > 0) {
81                 pw.print(prefix);
82                 pw.print("Non-persistent: user ID:");
83                 pw.println(mUserId);
84 
85                 pw.print(prefix);
86                 pw.println("  Host packages:");
87                 for (int i = 0; i < mHostPackages.size(); i++) {
88                     pw.print(prefix);
89                     pw.print("    ");
90                     pw.print(mHostPackages.keyAt(i));
91                     pw.print(": ");
92                     pw.println(mHostPackages.valueAt(i));
93                 }
94                 pw.println();
95             }
96         }
97     }
98 }
99