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.wallpaper.module;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 /**
22  * Notifies clients when the app has changed the wallpaper.
23  */
24 public class WallpaperChangedNotifier {
25 
26     private static final Object sInstanceLock = new Object();
27     private static WallpaperChangedNotifier sInstance;
28     private List<Listener> mListeners;
29 
30     /**
31      * Make the constructor private to prevent instantiation outside the singleton getInstance()
32      * method.
33      */
WallpaperChangedNotifier()34     private WallpaperChangedNotifier() {
35         mListeners = new ArrayList<>();
36     }
37 
getInstance()38     public static WallpaperChangedNotifier getInstance() {
39         synchronized (sInstanceLock) {
40             if (sInstance == null) {
41                 sInstance = new WallpaperChangedNotifier();
42             }
43             return sInstance;
44         }
45     }
46 
47     /**
48      * Notifies all listeners that the wallpaper was changed.
49      */
notifyWallpaperChanged()50     public void notifyWallpaperChanged() {
51         for (int i = 0; i < mListeners.size(); i++) {
52             mListeners.get(i).onWallpaperChanged();
53         }
54     }
55 
56     /**
57      * Registers a listener for wallpaper change events.
58      */
registerListener(Listener listener)59     public void registerListener(Listener listener) {
60         if (!mListeners.contains(listener)) {
61             mListeners.add(listener);
62         }
63     }
64 
65     /**
66      * Unregisters a listener for wallpaper change events.
67      */
unregisterListener(Listener listener)68     public void unregisterListener(Listener listener) {
69         mListeners.remove(listener);
70     }
71 
72     /**
73      * Listener for wallpaper changed notification.
74      */
75     public interface Listener {
onWallpaperChanged()76         void onWallpaperChanged();
77     }
78 }
79