Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3776f4dd authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

Merge branch '1128-improve_functionality_on_account_remove_call' into 'main'

1128-improve_functionality_on_account_remove_call

See merge request !224
parents bc96a33c 88401fd6
Loading
Loading
Loading
Loading
Loading
+32 −12
Original line number Diff line number Diff line
@@ -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;
        }

        for (File file : cachedFiles) {
        try {
                boolean removed = file.delete();
                if (!removed) {
                    Timber.w("Failed to remove the cached file: %s", file.getName());
                }
            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;
        }

        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);
                }
            }
        }

        return dir.delete();
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -104,7 +104,10 @@ public class SynchronizationService extends Service implements OnRemoteOperation

    @Override
    public void onDestroy() {
        if (handlerThread != null) {
            handlerThread.quitSafely();
        }

        super.onDestroy();
    }

+26 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+10 −1
Original line number Diff line number Diff line
@@ -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);
+2 −10
Original line number Diff line number Diff line
@@ -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);
    }
}