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

Commit 53522a86 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Merge branch '43-r-fixNotifQuotaFull' into '1-epic-refactoring-p1'

Display notification to inform user about quota usage

See merge request !103
parents 85cd5349 a18048cf
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ dependencies {
    api project(':NextcloudLib')
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    api 'androidx.annotation:annotation:1.3.0'
    implementation 'androidx.core:core:1.6.0'

    def work_version = "2.7.1"
    // (Java only)
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ http://www.gnu.org/licenses/gpl.html
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />


    <permission
        android:name="android.permission.FORCE_STOP_PACKAGES"
+4 −0
Original line number Diff line number Diff line
@@ -11,9 +11,12 @@ package foundation.e.drive;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

@@ -43,6 +46,7 @@ public class EdriveApplication extends Application {
        mFileObserver = new RecursiveFileObserver(getApplicationContext(), pathForObserver, fileEventListener);

        SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
        CommonUtils.createNotificationChannel(getApplicationContext());

        if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null) {
            Log.d(TAG, "Account already registered");
+7 −1
Original line number Diff line number Diff line
@@ -85,10 +85,13 @@ public class UploadFileOperation extends RemoteOperation {
            return new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
        }

        Float relativeQuotaBeforeFileUpload = 0.0f;
        if (this.availableQuota == -1) {
            RemoteOperationResult checkQuotaResult = checkAvailableSpace(client, file.length());
            if (checkQuotaResult.getCode() != ResultCode.OK) {
                return new RemoteOperationResult(checkQuotaResult.getCode());
            } else {
                relativeQuotaBeforeFileUpload = ((Double) checkQuotaResult.getSingleData()).floatValue();
            }
        }

@@ -155,6 +158,7 @@ public class UploadFileOperation extends RemoteOperation {

        ArrayList<Object> datas = new ArrayList<>();
        datas.add(syncedState.getSyncedFolderId());
        datas.add(relativeQuotaBeforeFileUpload);
        final RemoteOperationResult finalResult = new RemoteOperationResult(resultCode);
        finalResult.setData(datas);
        return finalResult;
@@ -194,7 +198,9 @@ public class UploadFileOperation extends RemoteOperation {
                return new RemoteOperationResult(ResultCode.QUOTA_EXCEEDED);
            } else {
                Log.d(TAG, "Quota Okay");
                return new RemoteOperationResult(ResultCode.OK);
                RemoteOperationResult result = new RemoteOperationResult(ResultCode.OK);
                result.setSingleData((Double) userInfo.getQuota().getRelative());
                return result;
            }
        } else {
            Log.w(TAG, "getRemoteUserInfoOperation failed: "+ocsResult.getHttpCode() );
+33 −13
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
@@ -35,6 +36,7 @@ import java.util.Collection;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentLinkedDeque;

import foundation.e.drive.R;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.DownloadRequest;
import foundation.e.drive.models.SyncRequest;
@@ -125,6 +127,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation
                Log.v(TAG, " an operation has been poll from queue");

                if (CommonUtils.isThisSyncAllowed(account, request.getSyncedFileState().isMediaType())) {
                    CommonUtils.createNotificationChannel(this);
                    startedOperations.put(operation, threadIndex);
                    threadPool[threadIndex] = operation.execute(client, this, handler);
                    threadWorkingState[threadIndex] = true;
@@ -149,6 +152,16 @@ public class SynchronizationService extends Service implements OnRemoteOperation
            }
        } else {
            String operationClassName = callerOperation.getClass().getSimpleName();
            if (callerOperation instanceof UploadFileOperation) {
                final Float relativeQuota = (Float) result.getData().get(1);
                if (relativeQuota >= 99.0) {
                    addNotification(getString(R.string.notif_quota_nearlyReached_title), getString(R.string.notif_quota_99Plus_text));
                } else if (relativeQuota >= 90.0) {
                    addNotification(getString(R.string.notif_quota_nearlyReached_title), getString(R.string.notif_quota_90Plus_text));
                } else if (relativeQuota >= 80.0) {
                    addNotification(getString(R.string.notif_quota_nearlyReached_title), getString(R.string.notif_quota_80Plus_text));
                }
            }
            switch (result.getCode()) {
                case OK:
                    Log.d(TAG, operationClassName + " Succeed");
@@ -187,19 +200,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation
                case QUOTA_EXCEEDED:
                    //Case specific to UploadFileOperation
                    Log.w(TAG, "Quota_EXCEEDED");

                    NotificationManager nM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

                    Notification notif = new Notification.Builder(this, AppConstants.notificationChannelID)
                            .setContentIntent(PendingIntent.getActivity(getApplicationContext(),
                                    0,
                                    new Intent(Intent.ACTION_VIEW, client.getBaseUri()),
                                    0))
                            .setContentText("Your drive lacks of space. Tap to check " + client.getBaseUri())
                            .setSmallIcon(android.R.drawable.stat_sys_warning)
                            .build();

                    nM.notify(1,notif );
                    addNotification(getString(R.string.notif_quota_execeeded_title), getString(R.string.notif_quota_execeeded_text));
                    break;
                case FILE_NOT_FOUND:
                    //Case specific to DownloadFileOperation
@@ -233,6 +234,25 @@ public class SynchronizationService extends Service implements OnRemoteOperation
        return operation;
    }

    /**
     *  send notification to inform user that he lacks space on ecloud
     * Improvement idea:
     * - add translatable message & title
     * - make message & title to be a parameter of the method, so we could reuse the function somewhere
     * - else with different notification. File conflict for example.
     **/
    private void addNotification(String title, String text){
        final NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, AppConstants.notificationChannelID)
                        .setSmallIcon(android.R.drawable.stat_sys_warning)
                        .setContentTitle(title)
                        .setContentText(text+ client.getBaseUri());
        // Add as notification
        final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }



    /**
     * Handler for the class
Loading