mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| be7351d683 | |||
| 79bdc73444 | |||
| e919a8614e |
@@ -58,9 +58,12 @@ import chat.delta.rpc.RpcException;
|
||||
public class ApplicationContext extends MultiDexApplication {
|
||||
private static final String TAG = ApplicationContext.class.getSimpleName();
|
||||
|
||||
public static DcAccounts dcAccounts;
|
||||
public Rpc rpc;
|
||||
public DcContext dcContext;
|
||||
private static final Object initLock = new Object();
|
||||
private static volatile boolean isInitialized = false;
|
||||
|
||||
private static DcAccounts dcAccounts;
|
||||
private Rpc rpc;
|
||||
private DcContext dcContext;
|
||||
public DcLocationManager dcLocationManager;
|
||||
public DcEventCenter eventCenter;
|
||||
public NotificationCenter notificationCenter;
|
||||
@@ -75,6 +78,71 @@ public class ApplicationContext extends MultiDexApplication {
|
||||
return (ApplicationContext)context.getApplicationContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get DcAccounts instance, waiting for initialization if necessary.
|
||||
* This method is thread-safe and will block until initialization is complete.
|
||||
*/
|
||||
public static DcAccounts getDcAccounts() {
|
||||
synchronized (initLock) {
|
||||
while (!isInitialized) {
|
||||
try {
|
||||
initLock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while waiting for DcAccounts initialization", e);
|
||||
}
|
||||
}
|
||||
return dcAccounts;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Rpc instance, waiting for initialization if necessary.
|
||||
* This method is thread-safe and will block until initialization is complete.
|
||||
*/
|
||||
public Rpc getRpc() {
|
||||
synchronized (initLock) {
|
||||
while (!isInitialized) {
|
||||
try {
|
||||
initLock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while waiting for Rpc initialization", e);
|
||||
}
|
||||
}
|
||||
return rpc;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get DcContext instance, waiting for initialization if necessary.
|
||||
* This method is thread-safe and will block until initialization is complete.
|
||||
*/
|
||||
public DcContext getDcContext() {
|
||||
synchronized (initLock) {
|
||||
while (!isInitialized) {
|
||||
try {
|
||||
initLock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while waiting for DcContext initialization", e);
|
||||
}
|
||||
}
|
||||
return dcContext;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set DcContext instance. This should only be called by AccountManager when switching accounts,
|
||||
* which only happens after initial initialization is complete.
|
||||
* This method is thread-safe but does NOT trigger initialization or notify waiting threads.
|
||||
*/
|
||||
public void setDcContext(DcContext context) {
|
||||
synchronized (initLock) {
|
||||
this.dcContext = context;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
@@ -110,66 +178,87 @@ public class ApplicationContext extends MultiDexApplication {
|
||||
|
||||
System.loadLibrary("native-utils");
|
||||
|
||||
dcAccounts = new DcAccounts(new File(getFilesDir(), "accounts").getAbsolutePath());
|
||||
Log.i(TAG, "DcAccounts created");
|
||||
rpc = new Rpc(new FFITransport(dcAccounts.getJsonrpcInstance()));
|
||||
Log.i(TAG, "Rpc created");
|
||||
AccountManager.getInstance().migrateToDcAccounts(this);
|
||||
|
||||
// October-2025 migration: delete deprecated "permanent channel" id
|
||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
||||
notificationManager.deleteNotificationChannel("dc_foreground_notification_ch");
|
||||
// end October-2025 migration
|
||||
|
||||
int[] allAccounts = dcAccounts.getAll();
|
||||
Log.i(TAG, "Number of profiles: " + allAccounts.length);
|
||||
for (int accountId : allAccounts) {
|
||||
DcContext ac = dcAccounts.getAccount(accountId);
|
||||
if (!ac.isOpen()) {
|
||||
// Initialize DcAccounts in background to avoid ANR during SQL migrations
|
||||
Util.runOnBackground(() -> {
|
||||
synchronized (initLock) {
|
||||
try {
|
||||
DatabaseSecret secret = DatabaseSecretProvider.getOrCreateDatabaseSecret(this, accountId);
|
||||
boolean res = ac.open(secret.asString());
|
||||
if (res) Log.i(TAG, "Successfully opened account " + accountId + ", path: " + ac.getBlobdir());
|
||||
else Log.e(TAG, "Error opening account " + accountId + ", path: " + ac.getBlobdir());
|
||||
Log.i(TAG, "Starting DcAccounts initialization in background");
|
||||
dcAccounts = new DcAccounts(new File(getFilesDir(), "accounts").getAbsolutePath());
|
||||
Log.i(TAG, "DcAccounts created");
|
||||
rpc = new Rpc(new FFITransport(dcAccounts.getJsonrpcInstance()));
|
||||
Log.i(TAG, "Rpc created");
|
||||
AccountManager.getInstance().migrateToDcAccounts(ApplicationContext.this);
|
||||
|
||||
// October-2025 migration: delete deprecated "permanent channel" id
|
||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(ApplicationContext.this);
|
||||
notificationManager.deleteNotificationChannel("dc_foreground_notification_ch");
|
||||
// end October-2025 migration
|
||||
|
||||
int[] allAccounts = dcAccounts.getAll();
|
||||
Log.i(TAG, "Number of profiles: " + allAccounts.length);
|
||||
for (int accountId : allAccounts) {
|
||||
DcContext ac = dcAccounts.getAccount(accountId);
|
||||
if (!ac.isOpen()) {
|
||||
try {
|
||||
DatabaseSecret secret = DatabaseSecretProvider.getOrCreateDatabaseSecret(ApplicationContext.this, accountId);
|
||||
boolean res = ac.open(secret.asString());
|
||||
if (res) Log.i(TAG, "Successfully opened account " + accountId + ", path: " + ac.getBlobdir());
|
||||
else Log.e(TAG, "Error opening account " + accountId + ", path: " + ac.getBlobdir());
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to open account " + accountId + ", path: " + ac.getBlobdir() + ": " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 2025.11.12: this is needed until core starts ignoring "delete_server_after" for chatmail
|
||||
if (ac.isChatmail()) {
|
||||
ac.setConfig("delete_server_after", null); // reset
|
||||
}
|
||||
}
|
||||
if (allAccounts.length == 0) {
|
||||
try {
|
||||
rpc.addAccount();
|
||||
} catch (RpcException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
dcContext = dcAccounts.getSelectedAccount();
|
||||
notificationCenter = new NotificationCenter(ApplicationContext.this);
|
||||
eventCenter = new DcEventCenter(ApplicationContext.this);
|
||||
|
||||
// Mark as initialized before starting threads that depend on it
|
||||
isInitialized = true;
|
||||
initLock.notifyAll();
|
||||
Log.i(TAG, "DcAccounts initialization complete");
|
||||
|
||||
// Start event loop thread
|
||||
new Thread(() -> {
|
||||
Log.i(TAG, "Starting event loop");
|
||||
DcEventEmitter emitter = dcAccounts.getEventEmitter();
|
||||
Log.i(TAG, "DcEventEmitter obtained");
|
||||
while (true) {
|
||||
DcEvent event = emitter.getNextEvent();
|
||||
if (event==null) {
|
||||
break;
|
||||
}
|
||||
eventCenter.handleEvent(event);
|
||||
}
|
||||
Log.i("DeltaChat", "shutting down event handler");
|
||||
}, "eventThread").start();
|
||||
|
||||
// set translations before starting I/O to avoid sending untranslated MDNs (issue #2288)
|
||||
DcHelper.setStockTranslations(ApplicationContext.this);
|
||||
|
||||
dcAccounts.startIo();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to open account " + accountId + ", path: " + ac.getBlobdir() + ": " + e);
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "Fatal error during DcAccounts initialization", e);
|
||||
// Mark as initialized even on error to avoid deadlock
|
||||
isInitialized = true;
|
||||
initLock.notifyAll();
|
||||
throw new RuntimeException("Failed to initialize DcAccounts", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 2025.11.12: this is needed until core starts ignoring "delete_server_after" for chatmail
|
||||
if (ac.isChatmail()) {
|
||||
ac.setConfig("delete_server_after", null); // reset
|
||||
}
|
||||
}
|
||||
if (allAccounts.length == 0) {
|
||||
try {
|
||||
rpc.addAccount();
|
||||
} catch (RpcException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
dcContext = dcAccounts.getSelectedAccount();
|
||||
notificationCenter = new NotificationCenter(this);
|
||||
eventCenter = new DcEventCenter(this);
|
||||
new Thread(() -> {
|
||||
Log.i(TAG, "Starting event loop");
|
||||
DcEventEmitter emitter = dcAccounts.getEventEmitter();
|
||||
Log.i(TAG, "DcEventEmitter obtained");
|
||||
while (true) {
|
||||
DcEvent event = emitter.getNextEvent();
|
||||
if (event==null) {
|
||||
break;
|
||||
}
|
||||
eventCenter.handleEvent(event);
|
||||
}
|
||||
Log.i("DeltaChat", "shutting down event handler");
|
||||
}, "eventThread").start();
|
||||
|
||||
// set translations before starting I/O to avoid sending untranslated MDNs (issue #2288)
|
||||
DcHelper.setStockTranslations(this);
|
||||
|
||||
dcAccounts.startIo();
|
||||
});
|
||||
|
||||
new ForegroundDetector(ApplicationContext.getInstance(this));
|
||||
|
||||
@@ -180,7 +269,9 @@ public class ApplicationContext extends MultiDexApplication {
|
||||
@Override
|
||||
public void onAvailable(@NonNull android.net.Network network) {
|
||||
Log.i("DeltaChat", "++++++++++++++++++ NetworkCallback.onAvailable() #" + debugOnAvailableCount++);
|
||||
dcAccounts.maybeNetwork();
|
||||
// getDcAccounts() will wait for initialization to complete before calling maybeNetwork()
|
||||
// This is intentional - early network events should wait for proper initialization
|
||||
getDcAccounts().maybeNetwork();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -110,7 +110,7 @@ public class DozeReminder {
|
||||
}
|
||||
|
||||
private static boolean isPushAvailableAndSufficient() {
|
||||
return ApplicationContext.dcAccounts.isAllChatmail()
|
||||
return ApplicationContext.getDcAccounts().isAllChatmail()
|
||||
&& FcmReceiveService.getToken() != null;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public class AccountManager {
|
||||
|
||||
private void resetDcContext(Context context) {
|
||||
ApplicationContext appContext = (ApplicationContext)context.getApplicationContext();
|
||||
appContext.dcContext = ApplicationContext.dcAccounts.getSelectedAccount();
|
||||
appContext.setDcContext(ApplicationContext.getDcAccounts().getSelectedAccount());
|
||||
DcHelper.setStockTranslations(context);
|
||||
DirectShareUtil.resetAllShortcuts(appContext);
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public class AccountManager {
|
||||
for (File file : files) {
|
||||
// old accounts have the pattern "messenger*.db"
|
||||
if (!file.isDirectory() && file.getName().startsWith("messenger") && file.getName().endsWith(".db")) {
|
||||
int accountId = ApplicationContext.dcAccounts.migrateAccount(file.getAbsolutePath());
|
||||
int accountId = ApplicationContext.getDcAccounts().migrateAccount(file.getAbsolutePath());
|
||||
if (accountId != 0) {
|
||||
String selName = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.getString("curr_account_db_name", "messenger.db");
|
||||
@@ -70,7 +70,7 @@ public class AccountManager {
|
||||
}
|
||||
|
||||
if (selectAccountId != 0) {
|
||||
ApplicationContext.dcAccounts.selectAccount(selectAccountId);
|
||||
ApplicationContext.getDcAccounts().selectAccount(selectAccountId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error in migrateToDcAccounts()", e);
|
||||
|
||||
@@ -64,15 +64,15 @@ public class DcHelper {
|
||||
public static final String CONFIG_PRIVATE_TAG = "private_tag";
|
||||
|
||||
public static DcContext getContext(@NonNull Context context) {
|
||||
return ApplicationContext.getInstance(context).dcContext;
|
||||
return ApplicationContext.getInstance(context).getDcContext();
|
||||
}
|
||||
|
||||
public static Rpc getRpc(@NonNull Context context) {
|
||||
return ApplicationContext.getInstance(context).rpc;
|
||||
return ApplicationContext.getInstance(context).getRpc();
|
||||
}
|
||||
|
||||
public static DcAccounts getAccounts(@NonNull Context context) {
|
||||
return ApplicationContext.getInstance(context).dcAccounts;
|
||||
return ApplicationContext.getDcAccounts();
|
||||
}
|
||||
|
||||
public static DcEventCenter getEventCenter(@NonNull Context context) {
|
||||
|
||||
@@ -45,7 +45,7 @@ public final class FetchForegroundService extends Service {
|
||||
// we need to handle the message within 20s, and the time window may be even shorter than 20s,
|
||||
// so, use 10s to be safe.
|
||||
fetchingSynchronously = true;
|
||||
if (ApplicationContext.dcAccounts.backgroundFetch(10)) {
|
||||
if (ApplicationContext.getDcAccounts().backgroundFetch(10)) {
|
||||
// The background fetch was successful, but we need to wait until all events were processed.
|
||||
// After all events were processed, we will get DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE,
|
||||
// and stop() will be called.
|
||||
@@ -92,7 +92,7 @@ public final class FetchForegroundService extends Service {
|
||||
|
||||
Util.runOnAnyBackgroundThread(() -> {
|
||||
Log.i(TAG, "Starting fetch");
|
||||
if (!ApplicationContext.dcAccounts.backgroundFetch(300)) { // as startForeground() was called, there is time
|
||||
if (!ApplicationContext.getDcAccounts().backgroundFetch(300)) { // as startForeground() was called, there is time
|
||||
FetchForegroundService.stop(this);
|
||||
} // else we stop FetchForegroundService on DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE
|
||||
});
|
||||
@@ -111,7 +111,7 @@ public final class FetchForegroundService extends Service {
|
||||
|
||||
@Override
|
||||
public void onTimeout(int startId, int fgsType) {
|
||||
ApplicationContext.dcAccounts.stopBackgroundFetch();
|
||||
ApplicationContext.getDcAccounts().stopBackgroundFetch();
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user