• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.systemui.util.service;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.PatternMatcher;
25 import android.util.Log;
26 
27 import com.google.android.collect.Lists;
28 
29 import java.lang.ref.WeakReference;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 
33 import javax.inject.Inject;
34 
35 /**
36  * {@link PackageObserver} allows for monitoring the system for changes relating to a particular
37  * package. This can be used by clients to detect when a related package has changed and reloading
38  * is necessary.
39  */
40 public class PackageObserver implements Observer {
41     private static final String TAG = "PackageObserver";
42     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
43 
44     private final ArrayList<WeakReference<Callback>> mCallbacks = Lists.newArrayList();
45 
46     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
47         @Override
48         public void onReceive(Context context, Intent intent) {
49             if (DEBUG) {
50                 Log.d(TAG, "package added receiver - onReceive");
51             }
52 
53             final Iterator<WeakReference<Callback>> iter = mCallbacks.iterator();
54             while (iter.hasNext()) {
55                 final Callback callback = iter.next().get();
56                 if (callback != null) {
57                     callback.onSourceChanged();
58                 } else {
59                     iter.remove();
60                 }
61             }
62         }
63     };
64 
65     private final String mPackageName;
66     private final Context mContext;
67 
68     @Inject
PackageObserver(Context context, ComponentName component)69     public PackageObserver(Context context, ComponentName component) {
70         mContext = context;
71         mPackageName = component.getPackageName();
72     }
73 
74     @Override
addCallback(Callback callback)75     public void addCallback(Callback callback) {
76         if (DEBUG) {
77             Log.d(TAG, "addCallback:" + callback);
78         }
79         mCallbacks.add(new WeakReference<>(callback));
80 
81         // Only register for listening to package additions on first callback.
82         if (mCallbacks.size() > 1) {
83             return;
84         }
85 
86         final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
87         filter.addDataScheme("package");
88         filter.addDataSchemeSpecificPart(mPackageName, PatternMatcher.PATTERN_LITERAL);
89         // Note that we directly register the receiver here as data schemes are not supported by
90         // BroadcastDispatcher.
91         mContext.registerReceiver(mReceiver, filter, Context.RECEIVER_EXPORTED);
92     }
93 
94     @Override
removeCallback(Callback callback)95     public void removeCallback(Callback callback) {
96         if (DEBUG) {
97             Log.d(TAG, "removeCallback:" + callback);
98         }
99         final boolean removed = mCallbacks.removeIf(el -> el.get() == callback);
100 
101         if (removed && mCallbacks.isEmpty()) {
102             mContext.unregisterReceiver(mReceiver);
103         }
104     }
105 }
106