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

Commit e3c27fff authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Synchronization service create notification for quota usage

parent d99ca938
Loading
Loading
Loading
Loading
Loading
+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"
+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 Double relativeQuota = (Double) 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
+6 −2
Original line number Diff line number Diff line
@@ -3,6 +3,10 @@
    <string name="eelo_account_type" translatable="false">e.foundation.webdav.eelo</string>
    <string name="notif_channel_name" translatable="false">eDrive\'s notification</string>
    <string name="notif_channel_description">eDrive\'s notification channel</string>
    <string name="notif_quota_execeeded_title">Drive Quota Exceeded</string>
    <string name="notif_channel_description">eDrive\'s notification channel</string>
    <string name="notif_quota_execeeded_title">/e/ account\'s quota reached</string>
    <string name="notif_quota_nearlyReached_title">/e/ account\'s quota nearly reached</string>
    <string name="notif_quota_execeeded_text">eDrive can\'t upload a file that is bigger than remaining quota at: </string>
    <string name="notif_quota_99Plus_text">You filled 99% of your /e/\'s account quota. check: </string>
    <string name="notif_quota_90Plus_text">You filled 90% of your /e/\'s account quota. check: </string>
    <string name="notif_quota_80Plus_text">You filled 80% of your /e/\'s account quota. check: </string>
</resources>