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