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 package com.android.dreams.phototable;
17 
18 import android.content.SharedPreferences;
19 
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Set;
24 
25 /**
26  * Common utilities for album settings.
27  */
28 public class AlbumSettings {
29     public static final String ALBUM_SET = "Enabled Album Set V2";
30 
31     private static HashMap<SharedPreferences, AlbumSettings> singletons;
32 
33     private final SharedPreferences mSettings;
34     private final HashSet<String> mEnabledAlbums;
35 
getAlbumSettings(SharedPreferences settings)36     public static AlbumSettings getAlbumSettings(SharedPreferences settings) {
37         if (singletons == null) {
38             singletons = new HashMap<SharedPreferences, AlbumSettings>();
39         }
40         if (!singletons.containsKey(settings)) {
41             singletons.put(settings, new AlbumSettings(settings));
42         }
43         return singletons.get(settings);
44     }
45 
readEnabledAlbums()46     public void readEnabledAlbums() {
47         synchronized (mEnabledAlbums) {
48             readEnabledAlbumsLocked();
49         }
50     }
51 
isAlbumEnabled(String albumId)52     public boolean isAlbumEnabled(String albumId) {
53         synchronized (mEnabledAlbums) {
54             return mEnabledAlbums.contains(albumId);
55         }
56     }
57 
areAllEnabled(Collection<String> validAlbums)58     public boolean areAllEnabled(Collection<String> validAlbums) {
59         synchronized (mEnabledAlbums) {
60             return mEnabledAlbums.containsAll(validAlbums);
61         }
62     }
63 
setAlbumEnabled(String albumId, boolean enabled)64     public void setAlbumEnabled(String albumId, boolean enabled) {
65         if (isAlbumEnabled(albumId) != enabled) {
66             synchronized (mEnabledAlbums) {
67                 readEnabledAlbumsLocked();
68                 if (enabled) {
69                     mEnabledAlbums.add(albumId);
70                 } else {
71                     mEnabledAlbums.remove(albumId);
72                 }
73                 writeEnabledAlbumsLocked();
74             }
75         }
76     }
77 
disableAllAlbums()78     public void disableAllAlbums() {
79         synchronized (mEnabledAlbums) {
80             mEnabledAlbums.clear();
81             writeEnabledAlbumsLocked();
82         }
83     }
84 
enableAllAlbums(Collection<String> validAlbums)85     public void enableAllAlbums(Collection<String> validAlbums) {
86         synchronized (mEnabledAlbums) {
87             mEnabledAlbums.clear();
88             mEnabledAlbums.addAll(validAlbums);
89             writeEnabledAlbumsLocked();
90         }
91     }
92 
pruneObsoleteSettings(Collection<String> validAlbums)93     public void pruneObsoleteSettings(Collection<String> validAlbums) {
94         if (!validAlbums.containsAll(mEnabledAlbums)) {
95             synchronized (mEnabledAlbums) {
96                 readEnabledAlbumsLocked();
97                 mEnabledAlbums.retainAll(validAlbums);
98                 writeEnabledAlbumsLocked();
99             }
100         }
101     }
102 
isConfigured()103     public  boolean isConfigured() {
104         synchronized (mEnabledAlbums) {
105             return mEnabledAlbums.size() != 0;
106         }
107     }
108 
AlbumSettings(SharedPreferences settings)109     private AlbumSettings(SharedPreferences settings) {
110         mSettings = settings;
111         mEnabledAlbums = new HashSet<String>();
112         readEnabledAlbums();
113     }
114 
readEnabledAlbumsLocked()115     private void readEnabledAlbumsLocked() {
116         Set<String> enabledAlbums = mSettings.getStringSet(ALBUM_SET, null);
117         mEnabledAlbums.clear();
118         if (enabledAlbums != null) {
119             mEnabledAlbums.addAll(enabledAlbums);
120         }
121     }
122 
writeEnabledAlbumsLocked()123     private void writeEnabledAlbumsLocked() {
124         SharedPreferences.Editor editor = mSettings.edit();
125         // Give SharedSettings a copy, so that we are free to manipulate ours.
126         editor.putStringSet(ALBUM_SET, new HashSet<String>(mEnabledAlbums));
127         editor.commit();
128     }
129 }
130