From bca90e8c579f82345c471a475d7dbf54ba7a922d Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 1 May 2023 17:54:27 +0600 Subject: [PATCH 1/3] 1128-improve_functionality_on_account_remove_call issue: https://gitlab.e.foundation/e/os/backlog/-/issues/1128 --- .../AccountRemoveCallbackReceiver.java | 44 ++++++++++++++----- .../services/SynchronizationService.java | 5 ++- .../foundation/e/drive/utils/ViewUtils.java | 18 ++++++++ .../e/drive/widgets/EDriveWidget.java | 10 ++++- .../e/drive/work/AccountUserInfoWorker.java | 12 +---- 5 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/utils/ViewUtils.java 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 8d013158..8a3791d4 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 success = deleteDir(new File(dir, child)); + if (!success) { + 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 1c2bc934..92df659f 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 00000000..7782d5f1 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/ViewUtils.java @@ -0,0 +1,18 @@ +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 837bec27..1355b0c9 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,12 @@ 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); + String accountName = preferences.getString(AccountManager.KEY_ACCOUNT_NAME, null); + return accountName != null && !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 2326515f..39b308c1 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); - } } -- GitLab From e6ec6c8228d47db9f0edf48ac940b5441cf3dbfb Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 2 May 2023 16:03:32 +0600 Subject: [PATCH 2/3] add missing copyright --- app/src/main/java/foundation/e/drive/utils/ViewUtils.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/main/java/foundation/e/drive/utils/ViewUtils.java b/app/src/main/java/foundation/e/drive/utils/ViewUtils.java index 7782d5f1..5b4f6493 100644 --- a/app/src/main/java/foundation/e/drive/utils/ViewUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/ViewUtils.java @@ -1,3 +1,11 @@ +/* + * 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; -- GitLab From 4bb2477be003c89d804d6673313de3fc2f2abb50 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 2 May 2023 16:40:53 +0600 Subject: [PATCH 3/3] refactor according to review --- .../e/drive/receivers/AccountRemoveCallbackReceiver.java | 4 ++-- .../main/java/foundation/e/drive/widgets/EDriveWidget.java | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) 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 8a3791d4..339507d8 100644 --- a/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java @@ -141,8 +141,8 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { } for (String child : children) { - boolean success = deleteDir(new File(dir, child)); - if (!success) { + boolean isSuccess = deleteDir(new File(dir, child)); + if (!isSuccess) { Timber.w("Failed to remove the cached file: %s", child); } } 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 1355b0c9..c6f8ce2b 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -132,8 +132,9 @@ public class EDriveWidget extends AppWidgetProvider { private boolean isAccountPresentInApp(@NonNull Context context) { final SharedPreferences preferences = context.getApplicationContext().getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - String accountName = preferences.getString(AccountManager.KEY_ACCOUNT_NAME, null); - return accountName != null && !accountName.trim().isEmpty(); + final String accountName = preferences.getString(AccountManager.KEY_ACCOUNT_NAME, ""); + + return !accountName.trim().isEmpty(); } @Override -- GitLab