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