]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/Context.java
Update libsignal-service-java
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / Context.java
1 package org.asamk.signal.manager.helper;
2
3 import org.asamk.signal.manager.internal.JobExecutor;
4 import org.asamk.signal.manager.internal.SignalDependencies;
5 import org.asamk.signal.manager.storage.AttachmentStore;
6 import org.asamk.signal.manager.storage.AvatarStore;
7 import org.asamk.signal.manager.storage.SignalAccount;
8 import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
9
10 import java.util.function.Supplier;
11
12 public class Context implements AutoCloseable {
13
14 private final Object LOCK = new Object();
15
16 private final SignalAccount account;
17 private final AccountFileUpdater accountFileUpdater;
18 private final SignalDependencies dependencies;
19 private final AvatarStore avatarStore;
20 private final StickerPackStore stickerPackStore;
21 private final AttachmentStore attachmentStore;
22 private final JobExecutor jobExecutor;
23
24 private AccountHelper accountHelper;
25 private AttachmentHelper attachmentHelper;
26 private ContactHelper contactHelper;
27 private GroupHelper groupHelper;
28 private GroupV2Helper groupV2Helper;
29 private IdentityHelper identityHelper;
30 private IncomingMessageHandler incomingMessageHandler;
31 private PinHelper pinHelper;
32 private PreKeyHelper preKeyHelper;
33 private ProfileHelper profileHelper;
34 private ReceiveHelper receiveHelper;
35 private RecipientHelper recipientHelper;
36 private SendHelper sendHelper;
37 private StickerHelper stickerHelper;
38 private StorageHelper storageHelper;
39 private SyncHelper syncHelper;
40 private UnidentifiedAccessHelper unidentifiedAccessHelper;
41
42 public Context(
43 final SignalAccount account,
44 final AccountFileUpdater accountFileUpdater,
45 final SignalDependencies dependencies,
46 final AvatarStore avatarStore,
47 final AttachmentStore attachmentStore,
48 final StickerPackStore stickerPackStore
49 ) {
50 this.account = account;
51 this.accountFileUpdater = accountFileUpdater;
52 this.dependencies = dependencies;
53 this.avatarStore = avatarStore;
54 this.stickerPackStore = stickerPackStore;
55 this.attachmentStore = attachmentStore;
56 this.jobExecutor = new JobExecutor(this);
57 }
58
59 public SignalAccount getAccount() {
60 return account;
61 }
62
63 public AccountFileUpdater getAccountFileUpdater() {
64 return accountFileUpdater;
65 }
66
67 public SignalDependencies getDependencies() {
68 return dependencies;
69 }
70
71 public AvatarStore getAvatarStore() {
72 return avatarStore;
73 }
74
75 public StickerPackStore getStickerPackStore() {
76 return stickerPackStore;
77 }
78
79 AttachmentStore getAttachmentStore() {
80 return attachmentStore;
81 }
82
83 public JobExecutor getJobExecutor() {
84 return jobExecutor;
85 }
86
87 public AccountHelper getAccountHelper() {
88 return getOrCreate(() -> accountHelper, () -> accountHelper = new AccountHelper(this));
89 }
90
91 public AttachmentHelper getAttachmentHelper() {
92 return getOrCreate(() -> attachmentHelper, () -> attachmentHelper = new AttachmentHelper(this));
93 }
94
95 public ContactHelper getContactHelper() {
96 return getOrCreate(() -> contactHelper, () -> contactHelper = new ContactHelper(account));
97 }
98
99 GroupV2Helper getGroupV2Helper() {
100 return getOrCreate(() -> groupV2Helper, () -> groupV2Helper = new GroupV2Helper(this));
101 }
102
103 public GroupHelper getGroupHelper() {
104 return getOrCreate(() -> groupHelper, () -> groupHelper = new GroupHelper(this));
105 }
106
107 public IdentityHelper getIdentityHelper() {
108 return getOrCreate(() -> identityHelper, () -> identityHelper = new IdentityHelper(this));
109 }
110
111 public IncomingMessageHandler getIncomingMessageHandler() {
112 return getOrCreate(() -> incomingMessageHandler,
113 () -> this.incomingMessageHandler = new IncomingMessageHandler(this));
114 }
115
116 PinHelper getPinHelper() {
117 return getOrCreate(() -> pinHelper, () -> pinHelper = new PinHelper(dependencies.getSecureValueRecovery()));
118 }
119
120 public PreKeyHelper getPreKeyHelper() {
121 return getOrCreate(() -> preKeyHelper, () -> preKeyHelper = new PreKeyHelper(account, dependencies));
122 }
123
124 public ProfileHelper getProfileHelper() {
125 return getOrCreate(() -> profileHelper, () -> profileHelper = new ProfileHelper(this));
126 }
127
128 public ReceiveHelper getReceiveHelper() {
129 return getOrCreate(() -> receiveHelper, () -> receiveHelper = new ReceiveHelper(this));
130 }
131
132 public RecipientHelper getRecipientHelper() {
133 return getOrCreate(() -> recipientHelper, () -> recipientHelper = new RecipientHelper(this));
134 }
135
136 public SendHelper getSendHelper() {
137 return getOrCreate(() -> sendHelper, () -> sendHelper = new SendHelper(this));
138 }
139
140 public StickerHelper getStickerHelper() {
141 return getOrCreate(() -> stickerHelper, () -> stickerHelper = new StickerHelper(this));
142 }
143
144 public StorageHelper getStorageHelper() {
145 return getOrCreate(() -> storageHelper, () -> storageHelper = new StorageHelper(this));
146 }
147
148 public SyncHelper getSyncHelper() {
149 return getOrCreate(() -> syncHelper, () -> syncHelper = new SyncHelper(this));
150 }
151
152 UnidentifiedAccessHelper getUnidentifiedAccessHelper() {
153 return getOrCreate(() -> unidentifiedAccessHelper,
154 () -> unidentifiedAccessHelper = new UnidentifiedAccessHelper(this));
155 }
156
157 private <T> T getOrCreate(Supplier<T> supplier, Callable creator) {
158 var value = supplier.get();
159 if (value != null) {
160 return value;
161 }
162
163 synchronized (LOCK) {
164 value = supplier.get();
165 if (value != null) {
166 return value;
167 }
168 creator.call();
169 return supplier.get();
170 }
171 }
172
173 @Override
174 public void close() {
175 jobExecutor.close();
176 }
177
178 private interface Callable {
179
180 void call();
181 }
182 }