diff --git a/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java b/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java index 8d0131580526a93d0ad23be89af6e6abb0ba1f94..339507d81ae07d4bdcd6a2bc126f94dbec0c0e60 100644 --- a/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java @@ -19,6 +19,7 @@ import android.content.Intent; import android.content.SharedPreferences; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.io.File; @@ -29,6 +30,7 @@ import foundation.e.drive.services.InitializerService; import foundation.e.drive.services.ObserverService; import foundation.e.drive.services.SynchronizationService; import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.ViewUtils; import timber.log.Timber; public class AccountRemoveCallbackReceiver extends BroadcastReceiver { @@ -36,6 +38,8 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { @SuppressLint("UnsafeProtectedBroadcastReceiver") @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { + Timber.d("Received account remove broadcast request"); + final Context applicationContext = context.getApplicationContext(); final SharedPreferences preferences = applicationContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); @@ -49,6 +53,8 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { deleteDatabase(applicationContext); cleanSharedPreferences(applicationContext, preferences); removeCachedFiles(applicationContext); + + ViewUtils.updateWidgetView(applicationContext); } private void deleteDatabase(@NonNull Context applicationContext) { @@ -63,7 +69,8 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { } private boolean shouldProceedWithRemoval(@NonNull Intent intent, @NonNull SharedPreferences preferences) { - if (isInvalidAction(intent)) { + if (isInvalidAction(intent) || intent.getExtras() == null) { + Timber.w("Invalid account removal request"); return false; } @@ -71,6 +78,7 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { String currentAccountType = preferences.getString(AccountManager.KEY_ACCOUNT_TYPE, ""); if (currentAccount.isEmpty() || currentAccountType.isEmpty()) { + Timber.d("No account set up, ignoring account removal"); return false; } @@ -113,21 +121,33 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { } private void removeCachedFiles(@NonNull Context applicationContext) { - final File[] cachedFiles = applicationContext.getExternalCacheDir().listFiles(); - if (cachedFiles == null) { - Timber.w("listFiles() returned null. preventing a NPE"); - return; + try { + deleteDir(applicationContext.getExternalCacheDir()); + } catch (SecurityException e) { + Timber.e(e, "failed to delete cached file on account removal call"); + } + } + + private boolean deleteDir(@Nullable File dir) throws SecurityException { + if (dir == null) { + Timber.w("cache file returned null. preventing a NPE"); + return false; } - for (File file : cachedFiles) { - try { - boolean removed = file.delete(); - if (!removed) { - Timber.w("Failed to remove the cached file: %s", file.getName()); + if (dir.isDirectory()) { + String[] children = dir.list(); + if (children == null) { + return dir.delete(); + } + + for (String child : children) { + boolean isSuccess = deleteDir(new File(dir, child)); + if (!isSuccess) { + Timber.w("Failed to remove the cached file: %s", child); } - } catch (SecurityException e) { - Timber.e(e, "failed to delete cached file on account removal call"); } } + + return dir.delete(); } } diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index 1c2bc934fada761d5c1554169a7469a6b1c363e5..92df659f3c3db046b0a6d30b931c709df7df88e2 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -104,7 +104,10 @@ public class SynchronizationService extends Service implements OnRemoteOperation @Override public void onDestroy() { - handlerThread.quitSafely(); + if (handlerThread != null) { + handlerThread.quitSafely(); + } + super.onDestroy(); } diff --git a/app/src/main/java/foundation/e/drive/utils/ViewUtils.java b/app/src/main/java/foundation/e/drive/utils/ViewUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..5b4f6493c626ad0a6370a9a9e9c7f72966c2f0f7 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/ViewUtils.java @@ -0,0 +1,26 @@ +/* + * Copyright © MURENA SAS 2023. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ + +package foundation.e.drive.utils; + +import android.appwidget.AppWidgetManager; +import android.content.Context; +import android.content.Intent; + +import androidx.annotation.NonNull; + +import foundation.e.drive.widgets.EDriveWidget; + +public class ViewUtils { + + public static void updateWidgetView(@NonNull Context context) { + final Intent updateIntent = new Intent(context, EDriveWidget.class); + updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); + context.sendBroadcast(updateIntent); + } +} diff --git a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java index 837bec2748ad633df41a6704cb1fc6f410129fb3..c6f8ce2b59bc842bd007cc615c575a30e4708365 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -22,6 +22,7 @@ import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.Network; import android.net.Uri; @@ -39,6 +40,7 @@ import java.util.Locale; import foundation.e.drive.R; import foundation.e.drive.utils.AccountUtils; +import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import timber.log.Timber; @@ -120,7 +122,7 @@ public class EDriveWidget extends AppWidgetProvider { accountManager); } - if (account == null) { + if (!isAccountPresentInApp(context) || account == null) { noAccountView(context); } else { onAccountAvailable(context, accountManager); @@ -128,6 +130,13 @@ public class EDriveWidget extends AppWidgetProvider { appWidgetManager.updateAppWidget(appWidgetId, views); } + private boolean isAccountPresentInApp(@NonNull Context context) { + final SharedPreferences preferences = context.getApplicationContext().getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + final String accountName = preferences.getString(AccountManager.KEY_ACCOUNT_NAME, ""); + + return !accountName.trim().isEmpty(); + } + @Override public void onEnabled(@NonNull Context context) { super.onEnabled(context); diff --git a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 2326515fa52a1dfea45cab07d6251bb7f75c2233..39b308c175dfccab88e7faeaec8a4356500aaa4f 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -19,9 +19,7 @@ import static foundation.e.drive.utils.AppConstants.ACCOUNT_USER_ID_KEY; import android.accounts.Account; import android.accounts.AccountManager; import android.app.NotificationManager; -import android.appwidget.AppWidgetManager; import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import androidx.annotation.NonNull; @@ -46,7 +44,7 @@ import foundation.e.drive.operations.GetAliasOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; -import foundation.e.drive.widgets.EDriveWidget; +import foundation.e.drive.utils.ViewUtils; import timber.log.Timber; /** @@ -87,7 +85,7 @@ public class AccountUserInfoWorker extends Worker { + client.getUserId() + "/" + 300) .diskCacheStrategy(DiskCacheStrategy.ALL) .preload(); - updateWidget(mContext); + ViewUtils.updateWidgetView(mContext); return Result.success(); } else { return Result.retry(); @@ -228,10 +226,4 @@ public class AccountUserInfoWorker extends Worker { Timber.d("fetchAliases(): success"); return true; } - - private void updateWidget(final Context context) { - final Intent updateIntent = new Intent(context, EDriveWidget.class); - updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - context.sendBroadcast(updateIntent); - } }