1 /*
2  * Copyright (C) 2012 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.pm;
18 
19 import android.annotation.NonNull;
20 import android.content.IntentFilter;
21 
22 import com.android.server.utils.Snappable;
23 import com.android.server.utils.SnapshotCache;
24 
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 
28 public class PreferredIntentResolver
29         extends WatchedIntentResolver<PreferredActivity, PreferredActivity>
30         implements Snappable {
31     @Override
newArray(int size)32     protected PreferredActivity[] newArray(int size) {
33         return new PreferredActivity[size];
34     }
35 
36     @Override
isPackageForFilter(String packageName, PreferredActivity filter)37     protected boolean isPackageForFilter(String packageName, PreferredActivity filter) {
38         return packageName.equals(filter.mPref.mComponent.getPackageName());
39     }
40 
41     @Override
dumpFilter(PrintWriter out, String prefix, PreferredActivity filter)42     protected void dumpFilter(PrintWriter out, String prefix,
43             PreferredActivity filter) {
44         filter.mPref.dump(out, prefix, filter);
45     }
46 
47     @Override
getIntentFilter(@onNull PreferredActivity input)48     protected IntentFilter getIntentFilter(@NonNull PreferredActivity input) {
49         return input.getIntentFilter();
50     }
51 
shouldAddPreferredActivity(PreferredActivity pa)52     public boolean shouldAddPreferredActivity(PreferredActivity pa) {
53         ArrayList<PreferredActivity> pal = findFilters(pa);
54         if (pal == null || pal.isEmpty()) {
55             return true;
56         }
57         if (!pa.mPref.mAlways) {
58             return false;
59         }
60         final int activityCount = pal.size();
61         for (int i = 0; i < activityCount; i++) {
62             PreferredActivity cur = pal.get(i);
63             if (cur.mPref.mAlways
64                     && cur.mPref.mMatch == (pa.mPref.mMatch & IntentFilter.MATCH_CATEGORY_MASK)
65                     && cur.mPref.sameSet(pa.mPref)) {
66                 return false;
67             }
68         }
69         return true;
70     }
71 
PreferredIntentResolver()72     public PreferredIntentResolver() {
73         super();
74         mSnapshot = makeCache();
75     }
76 
77     // Take the snapshot of F
snapshot(PreferredActivity f)78     protected PreferredActivity snapshot(PreferredActivity f) {
79         return (f == null) ? null : f.snapshot();
80     }
81 
82     // Copy constructor used only to create a snapshot.
PreferredIntentResolver(PreferredIntentResolver f)83     private PreferredIntentResolver(PreferredIntentResolver f) {
84         copyFrom(f);
85         mSnapshot = new SnapshotCache.Sealed();
86     }
87 
88     // The cache for snapshots, so they are not rebuilt if the base object has not
89     // changed.
90     final SnapshotCache<PreferredIntentResolver> mSnapshot;
91 
makeCache()92     private SnapshotCache makeCache() {
93         return new SnapshotCache<PreferredIntentResolver>(this, this) {
94             @Override
95             public PreferredIntentResolver createSnapshot() {
96                 return new PreferredIntentResolver(mSource);
97             }};
98     }
99 
100     /**
101      * Return a snapshot of the current object.  The snapshot is a read-only copy suitable
102      * for read-only methods.
103      * @return A snapshot of the current object.
104      */
105     public PreferredIntentResolver snapshot() {
106         return mSnapshot.snapshot();
107     }
108 }
109