1 /*
2  * Copyright (C) 2023 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.providers.media.photopicker.sync;
18 
19 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_CLOUD_ONLY;
20 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_LOCAL_AND_CLOUD;
21 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_LOCAL_ONLY;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.VisibleForTesting;
25 
26 import java.util.UUID;
27 
28 /**
29  * This class stores all sync trackers.
30  */
31 public class SyncTrackerRegistry {
32     private static SyncTracker sLocalSyncTracker = new SyncTracker();
33     private static SyncTracker sLocalAlbumSyncTracker = new SyncTracker();
34     private static SyncTracker sCloudSyncTracker = new SyncTracker();
35     private static SyncTracker sCloudAlbumSyncTracker = new SyncTracker();
36 
getLocalSyncTracker()37     public static SyncTracker getLocalSyncTracker() {
38         return sLocalSyncTracker;
39     }
40 
41     /**
42      * This setter is required to inject mock data for tests. Do not use this anywhere else.
43      */
44     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
setLocalSyncTracker(SyncTracker syncTracker)45     public static void setLocalSyncTracker(SyncTracker syncTracker) {
46         sLocalSyncTracker = syncTracker;
47     }
48 
getLocalAlbumSyncTracker()49     public static SyncTracker getLocalAlbumSyncTracker() {
50         return sLocalAlbumSyncTracker;
51     }
52 
53     /**
54      * This setter is required to inject mock data for tests. Do not use this anywhere else.
55      */
56     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
setLocalAlbumSyncTracker( SyncTracker localAlbumSyncTracker)57     public static void setLocalAlbumSyncTracker(
58             SyncTracker localAlbumSyncTracker) {
59         sLocalAlbumSyncTracker = localAlbumSyncTracker;
60     }
61 
getCloudSyncTracker()62     public static SyncTracker getCloudSyncTracker() {
63         return sCloudSyncTracker;
64     }
65 
66     /**
67      * This setter is required to inject mock data for tests. Do not use this anywhere else.
68      */
69     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
setCloudSyncTracker( SyncTracker cloudSyncTracker)70     public static void setCloudSyncTracker(
71             SyncTracker cloudSyncTracker) {
72         sCloudSyncTracker = cloudSyncTracker;
73     }
74 
getCloudAlbumSyncTracker()75     public static SyncTracker getCloudAlbumSyncTracker() {
76         return sCloudAlbumSyncTracker;
77     }
78 
79     /**
80      * This setter is required to inject mock data for tests. Do not use this anywhere else.
81      */
82     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
setCloudAlbumSyncTracker( SyncTracker cloudAlbumSyncTracker)83     public static void setCloudAlbumSyncTracker(
84             SyncTracker cloudAlbumSyncTracker) {
85         sCloudAlbumSyncTracker = cloudAlbumSyncTracker;
86     }
87 
88     /**
89      * Return the appropriate sync tracker.
90      * @param isLocal is true when sync with local provider needs to be tracked. It is false for
91      *                sync with cloud provider.
92      * @return the appropriate {@link SyncTracker} object.
93      */
getSyncTracker(boolean isLocal)94     public static SyncTracker getSyncTracker(boolean isLocal) {
95         if (isLocal) {
96             return sLocalSyncTracker;
97         } else {
98             return sCloudSyncTracker;
99         }
100     }
101 
102     /**
103      * Return the appropriate album sync tracker.
104      * @param isLocal is true when sync with local provider needs to be tracked. It is false for
105      *                sync with cloud provider.
106      * @return the appropriate {@link SyncTracker} object.
107      */
getAlbumSyncTracker(boolean isLocal)108     public static SyncTracker getAlbumSyncTracker(boolean isLocal) {
109         if (isLocal) {
110             return sLocalAlbumSyncTracker;
111         } else {
112             return sCloudAlbumSyncTracker;
113         }
114     }
115 
116     /**
117      * Create the required completable futures for new media sync requests that need to be tracked.
118      */
trackNewSyncRequests( @ickerSyncManager.SyncSource int syncSource, @NonNull UUID syncRequestId)119     public static void trackNewSyncRequests(
120             @PickerSyncManager.SyncSource int syncSource,
121             @NonNull UUID syncRequestId) {
122         if (syncSource == SYNC_LOCAL_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
123             getLocalSyncTracker().createSyncFuture(syncRequestId);
124         }
125         if (syncSource == SYNC_CLOUD_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
126             getCloudSyncTracker().createSyncFuture(syncRequestId);
127         }
128     }
129 
130     /**
131      * Create the required completable futures for new album media sync requests that need to be
132      * tracked.
133      */
trackNewAlbumMediaSyncRequests( @ickerSyncManager.SyncSource int syncSource, @NonNull UUID syncRequestId)134     public static void trackNewAlbumMediaSyncRequests(
135             @PickerSyncManager.SyncSource int syncSource,
136             @NonNull UUID syncRequestId) {
137         if (syncSource == SYNC_LOCAL_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
138             getLocalAlbumSyncTracker().createSyncFuture(syncRequestId);
139         }
140         if (syncSource == SYNC_CLOUD_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
141             getCloudAlbumSyncTracker().createSyncFuture(syncRequestId);
142         }
143     }
144 
145     /**
146      * Mark the required futures as complete for existing media sync requests.
147      */
markSyncAsComplete( @ickerSyncManager.SyncSource int syncSource, @NonNull UUID syncRequestId)148     public static void markSyncAsComplete(
149             @PickerSyncManager.SyncSource int syncSource,
150             @NonNull UUID syncRequestId) {
151         if (syncSource == SYNC_LOCAL_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
152             getLocalSyncTracker().markSyncCompleted(syncRequestId);
153         }
154         if (syncSource == SYNC_CLOUD_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
155             getCloudSyncTracker().markSyncCompleted(syncRequestId);
156         }
157     }
158 
159     /**
160      * Mark the required futures as complete for existing album media sync requests.
161      */
markAlbumMediaSyncAsComplete( @ickerSyncManager.SyncSource int syncSource, @NonNull UUID syncRequestId)162     public static void markAlbumMediaSyncAsComplete(
163             @PickerSyncManager.SyncSource int syncSource,
164             @NonNull UUID syncRequestId) {
165         if (syncSource == SYNC_LOCAL_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
166             getLocalAlbumSyncTracker().markSyncCompleted(syncRequestId);
167         }
168         if (syncSource == SYNC_CLOUD_ONLY || syncSource == SYNC_LOCAL_AND_CLOUD) {
169             getCloudAlbumSyncTracker().markSyncCompleted(syncRequestId);
170         }
171     }
172 }
173