Эх сурвалжийг харах

Resolve #3080 adding notification option to not show notifications for new media folders

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 6 жил өмнө
parent
commit
08fc1cbfd2

+ 1 - 0
src/main/AndroidManifest.xml

@@ -112,6 +112,7 @@
         <activity android:name=".ui.activities.ActivitiesActivity"
             android:configChanges="orientation|screenSize|keyboardHidden" />
         <activity android:name=".ui.activity.SyncedFoldersActivity"/>
+        <receiver android:name="com.owncloud.android.jobs.MediaFoldersDetectionJob$NotificationReceiver" />
         <activity android:name=".ui.activity.UploadFilesActivity" />
         <activity android:name=".ui.activity.ExternalSiteWebView"
                   android:configChanges="orientation|screenSize|keyboardHidden" />

+ 9 - 0
src/main/java/com/owncloud/android/db/PreferenceManager.java

@@ -67,6 +67,7 @@ public final class PreferenceManager {
     private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
     private static final String PREF__FOLDER_LAYOUT = "folder_layout";
     public static final String PREF__LOCK_TIMESTAMP = "lock_timestamp";
+    private static final String PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS = "show_media_scan_notifications";
 
     private PreferenceManager() {
     }
@@ -550,6 +551,14 @@ public final class PreferenceManager {
         saveBooleanPreference(context, AUTO_PREF__SHOW_DETAILED_TIMESTAMP, showDetailedTimestamp);
     }
 
+    public static boolean isShowMediaScanNotifications(Context context) {
+        return getDefaultSharedPreferences(context).getBoolean(PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS, true);
+    }
+
+    public static void setShowMediaScanNotifications(Context context, boolean value) {
+        saveBooleanPreference(context, PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS, value);
+    }
+
     private static void saveBooleanPreference(Context context, String key, boolean value) {
         SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
         appPreferences.putBoolean(key, value).apply();

+ 107 - 38
src/main/java/com/owncloud/android/jobs/MediaFoldersDetectionJob.java

@@ -23,8 +23,10 @@ package com.owncloud.android.jobs;
 
 
 import android.accounts.Account;
+import android.app.Activity;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
@@ -41,6 +43,8 @@ import com.owncloud.android.datamodel.MediaFolder;
 import com.owncloud.android.datamodel.MediaFoldersModel;
 import com.owncloud.android.datamodel.MediaProvider;
 import com.owncloud.android.datamodel.SyncedFolderProvider;
+import com.owncloud.android.db.PreferenceManager;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.ManageAccountsActivity;
 import com.owncloud.android.ui.activity.SyncedFoldersActivity;
 import com.owncloud.android.ui.notifications.NotificationUtils;
@@ -48,9 +52,11 @@ import com.owncloud.android.utils.ThemeUtils;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Random;
 
 import androidx.annotation.NonNull;
 import androidx.core.app.NotificationCompat;
+import lombok.NoArgsConstructor;
 
 public class MediaFoldersDetectionJob extends Job {
     public static final String TAG = "MediaFoldersDetectionJob";
@@ -60,6 +66,9 @@ public class MediaFoldersDetectionJob extends Job {
 
     private static final String ACCOUNT_NAME_GLOBAL = "global";
     private static final String KEY_MEDIA_FOLDERS = "media_folders";
+    public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
+
+    private static String DISABLE_DETECTION_CLICK = "DISABLE_DETECTION_CLICK";
 
     @NonNull
     @Override
@@ -78,11 +87,11 @@ public class MediaFoldersDetectionJob extends Job {
         List<String> imageMediaFolderPaths = new ArrayList<>();
         List<String> videoMediaFolderPaths = new ArrayList<>();
 
-        for (MediaFolder imageMediaFolder: imageMediaFolders) {
+        for (MediaFolder imageMediaFolder : imageMediaFolders) {
             imageMediaFolderPaths.add(imageMediaFolder.absolutePath);
         }
 
-        for (MediaFolder videoMediaFolder: videoMediaFolders) {
+        for (MediaFolder videoMediaFolder : videoMediaFolders) {
             imageMediaFolderPaths.add(videoMediaFolder.absolutePath);
         }
 
@@ -92,38 +101,41 @@ public class MediaFoldersDetectionJob extends Job {
 
             // Store updated values
             arbitraryDataProvider.storeOrUpdateKeyValue(ACCOUNT_NAME_GLOBAL, KEY_MEDIA_FOLDERS, gson.toJson(new
-                    MediaFoldersModel(imageMediaFolderPaths, videoMediaFolderPaths)));
+                MediaFoldersModel(imageMediaFolderPaths, videoMediaFolderPaths)));
+
 
-            imageMediaFolderPaths.removeAll(mediaFoldersModel.getImageMediaFolders());
-            videoMediaFolderPaths.removeAll(mediaFoldersModel.getVideoMediaFolders());
+            if (PreferenceManager.isShowMediaScanNotifications(context)) {
+                imageMediaFolderPaths.removeAll(mediaFoldersModel.getImageMediaFolders());
+                videoMediaFolderPaths.removeAll(mediaFoldersModel.getVideoMediaFolders());
 
-            if (!imageMediaFolderPaths.isEmpty() || !videoMediaFolderPaths.isEmpty()) {
-                Account[] accounts = AccountUtils.getAccounts(getContext());
-                List<Account> accountList = new ArrayList<>();
-                for (Account account : accounts) {
-                    if (!arbitraryDataProvider.getBooleanValue(account, ManageAccountsActivity.PENDING_FOR_REMOVAL)) {
-                        accountList.add(account);
+                if (!imageMediaFolderPaths.isEmpty() || !videoMediaFolderPaths.isEmpty()) {
+                    Account[] accounts = AccountUtils.getAccounts(getContext());
+                    List<Account> accountList = new ArrayList<>();
+                    for (Account account : accounts) {
+                        if (!arbitraryDataProvider.getBooleanValue(account, ManageAccountsActivity.PENDING_FOR_REMOVAL)) {
+                            accountList.add(account);
+                        }
                     }
-                }
 
-                for (Account account : accountList) {
-                    for (String imageMediaFolder : imageMediaFolderPaths) {
-                        if (syncedFolderProvider.findByLocalPathAndAccount(imageMediaFolder, account) == null) {
-                            sendNotification(String.format(context.getString(R.string.new_media_folder_detected),
+                    for (Account account : accountList) {
+                        for (String imageMediaFolder : imageMediaFolderPaths) {
+                            if (syncedFolderProvider.findByLocalPathAndAccount(imageMediaFolder, account) == null) {
+                                sendNotification(String.format(context.getString(R.string.new_media_folder_detected),
                                     context.getString(R.string.new_media_folder_photos)),
-                                             imageMediaFolder.substring(imageMediaFolder.lastIndexOf('/') + 1
-                                             ),
-                                             account, imageMediaFolder, 1);
+                                    imageMediaFolder.substring(imageMediaFolder.lastIndexOf('/') + 1
+                                    ),
+                                    account, imageMediaFolder, 1);
+                            }
                         }
-                    }
 
-                    for (String videoMediaFolder : videoMediaFolderPaths) {
-                        if (syncedFolderProvider.findByLocalPathAndAccount(videoMediaFolder, account) == null) {
-                            sendNotification(String.format(context.getString(R.string.new_media_folder_detected),
+                        for (String videoMediaFolder : videoMediaFolderPaths) {
+                            if (syncedFolderProvider.findByLocalPathAndAccount(videoMediaFolder, account) == null) {
+                                sendNotification(String.format(context.getString(R.string.new_media_folder_detected),
                                     context.getString(R.string.new_media_folder_videos)),
-                                             videoMediaFolder.substring(videoMediaFolder.lastIndexOf('/') + 1
-                                             ),
-                                             account, videoMediaFolder, 2);
+                                    videoMediaFolder.substring(videoMediaFolder.lastIndexOf('/') + 1
+                                    ),
+                                    account, videoMediaFolder, 2);
+                            }
                         }
                     }
                 }
@@ -139,8 +151,11 @@ public class MediaFoldersDetectionJob extends Job {
     }
 
     private void sendNotification(String contentTitle, String subtitle, Account account, String path, int type) {
+        int notificationId = new Random().nextInt();
+
         Context context = getContext();
         Intent intent = new Intent(getContext(), SyncedFoldersActivity.class);
+        intent.putExtra(NOTIFICATION_ID, notificationId);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.putExtra(NotificationJob.KEY_NOTIFICATION_ACCOUNT, account.name);
         intent.putExtra(KEY_MEDIA_FOLDER_PATH, path);
@@ -149,22 +164,76 @@ public class MediaFoldersDetectionJob extends Job {
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
 
         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
-                context, NotificationUtils.NOTIFICATION_CHANNEL_GENERAL)
-                .setSmallIcon(R.drawable.notification_icon)
-                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.notification_icon))
-                .setColor(ThemeUtils.primaryColor(getContext()))
-                .setSubText(account.name)
-                .setContentTitle(contentTitle)
-                .setContentText(subtitle)
-                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
-                .setAutoCancel(true)
-                .setContentIntent(pendingIntent);
+            context, NotificationUtils.NOTIFICATION_CHANNEL_GENERAL)
+            .setSmallIcon(R.drawable.notification_icon)
+            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.notification_icon))
+            .setColor(ThemeUtils.primaryColor(getContext()))
+            .setSubText(account.name)
+            .setContentTitle(contentTitle)
+            .setContentText(subtitle)
+            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
+            .setAutoCancel(true)
+            .setContentIntent(pendingIntent);
+
+        Intent disableDetection = new Intent(context, NotificationReceiver.class);
+        disableDetection.putExtra(NOTIFICATION_ID, notificationId);
+        disableDetection.setAction(DISABLE_DETECTION_CLICK);
+
+        PendingIntent disableIntent = PendingIntent.getBroadcast(
+            context,
+            notificationId,
+            disableDetection,
+            PendingIntent.FLAG_CANCEL_CURRENT
+        );
+        notificationBuilder.addAction(
+            new NotificationCompat.Action(
+                R.drawable.ic_close,
+                context.getString(R.string.disable_new_media_folder_detection_notifications),
+                disableIntent
+            )
+        );
+
+        PendingIntent configureIntent = PendingIntent.getActivity(
+            context,
+            notificationId,
+            intent,
+            PendingIntent.FLAG_CANCEL_CURRENT
+        );
+        notificationBuilder.addAction(
+            new NotificationCompat.Action(
+                R.drawable.ic_settings,
+                context.getString(R.string.configure_new_media_folder_detection_notifications),
+                configureIntent
+            )
+        );
 
         NotificationManager notificationManager = (NotificationManager)
-                context.getSystemService(Context.NOTIFICATION_SERVICE);
+            context.getSystemService(Context.NOTIFICATION_SERVICE);
 
         if (notificationManager != null) {
-            notificationManager.notify(0, notificationBuilder.build());
+            notificationManager.notify(notificationId, notificationBuilder.build());
+        }
+    }
+
+
+    public static class NotificationReceiver extends BroadcastReceiver {
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            String action = intent.getAction();
+            int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
+
+            if (DISABLE_DETECTION_CLICK.equals(action)) {
+                Log_OC.d(this, "Disable media scan notifications");
+                PreferenceManager.setShowMediaScanNotifications(context, false);
+                cancel(context, notificationId);
+            }
+        }
+
+        private void cancel(Context context, int notificationId) {
+            NotificationManager notificationManager =
+                (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
+            notificationManager.cancel(notificationId);
         }
     }
 }

+ 30 - 1
src/main/java/com/owncloud/android/ui/activity/Preferences.java

@@ -96,6 +96,7 @@ public class Preferences extends PreferenceActivity
     public static final String LOCK_DEVICE_CREDENTIALS = "device_credentials";
 
     public final static String PREFERENCE_USE_FINGERPRINT = "use_fingerprint";
+    public static final String PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS = "show_media_scan_notifications";
 
     private static final int ACTION_REQUEST_PASSCODE = 5;
     private static final int ACTION_CONFIRM_PASSCODE = 6;
@@ -114,6 +115,7 @@ public class Preferences extends PreferenceActivity
 
     private ListPreference mLock;
     private SwitchPreference mShowHiddenFiles;
+    private SwitchPreference mShowMediaScanNotifications;
     private AppCompatDelegate mDelegate;
 
     private ListPreference mPrefStoragePath;
@@ -506,16 +508,43 @@ public class Preferences extends PreferenceActivity
         boolean fDeviceCredentialsEnabled = getResources().getBoolean(R.bool.device_credentials_enabled);
         boolean fShowHiddenFilesEnabled = getResources().getBoolean(R.bool.show_hidden_files_enabled);
         boolean fSyncedFolderLightEnabled = getResources().getBoolean(R.bool.syncedFolder_light);
+        boolean fShowMediaScanNotifications = com.owncloud.android.db.PreferenceManager
+            .isShowMediaScanNotifications(this);
 
         setupLockPreference(preferenceCategoryDetails, fPassCodeEnabled, fDeviceCredentialsEnabled);
 
         setupHiddenFilesPreference(preferenceCategoryDetails, fShowHiddenFilesEnabled);
 
-        if (!fPassCodeEnabled && !fDeviceCredentialsEnabled && !fShowHiddenFilesEnabled && fSyncedFolderLightEnabled) {
+        setupShowMediaScanNotifications(preferenceCategoryDetails, fShowMediaScanNotifications);
+
+        if (!fPassCodeEnabled && !fDeviceCredentialsEnabled && !fShowHiddenFilesEnabled && fSyncedFolderLightEnabled
+            && fShowMediaScanNotifications) {
             preferenceScreen.removePreference(preferenceCategoryDetails);
         }
     }
 
+    private void setupShowMediaScanNotifications(PreferenceCategory preferenceCategoryDetails,
+                                                 boolean fShowMediaScanNotifications) {
+        mShowMediaScanNotifications = (SwitchPreference) findPreference(PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS);
+
+        if (fShowMediaScanNotifications) {
+            preferenceCategoryDetails.removePreference(mShowMediaScanNotifications);
+        } else {
+            mShowMediaScanNotifications = (SwitchPreference) findPreference(PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS);
+
+            mShowMediaScanNotifications.setOnPreferenceClickListener(preference -> {
+                com.owncloud.android.db.PreferenceManager.setShowMediaScanNotifications(
+                    this,
+                    mShowMediaScanNotifications.isChecked()
+                );
+                preferenceCategoryDetails.removePreference(mShowMediaScanNotifications);
+
+                return true;
+            });
+        }
+
+    }
+
     private void setupHiddenFilesPreference(PreferenceCategory preferenceCategoryDetails,
                                             boolean fShowHiddenFilesEnabled) {
         mShowHiddenFiles = (SwitchPreference) findPreference("show_hidden_files");

+ 8 - 0
src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.java

@@ -23,6 +23,8 @@ package com.owncloud.android.ui.activity;
 
 import android.accounts.Account;
 import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.app.NotificationManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -131,6 +133,12 @@ public class SyncedFoldersActivity extends FileActivity implements SyncedFolderA
 
             path = getIntent().getStringExtra(MediaFoldersDetectionJob.KEY_MEDIA_FOLDER_PATH);
             type = getIntent().getIntExtra(MediaFoldersDetectionJob.KEY_MEDIA_FOLDER_TYPE, -1);
+
+            // Cancel notification
+            int notificationId = getIntent().getIntExtra(MediaFoldersDetectionJob.NOTIFICATION_ID, 0);
+            NotificationManager notificationManager =
+                (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
+            notificationManager.cancel(notificationId);
         }
 
         // setup toolbar

+ 4 - 0
src/main/res/values/strings.xml

@@ -45,6 +45,8 @@
     <string name="prefs_lock_device_credentials_enabled">Device credentials enabled</string>
     <string name="prefs_lock_device_credentials_not_setup">No device credentials have been set up.</string>
     <string name="prefs_show_hidden_files">Show hidden files</string>
+    <string name="prefs_enable_media_scan_notifications">Show media scan notifications</string>
+    <string name="prefs_enable_media_scan_notifications_summary">Notify about newly found media folders</string>
     <string name="prefs_log_delete_history_button">Delete history</string>
     <string name="prefs_calendar_contacts">Sync calendar &amp; contacts</string>
     <string name="prefs_calendar_contacts_summary">Set up DAVdroid (v1.3.0+) for current account</string>
@@ -788,6 +790,8 @@
     <string name="notification_channel_general_name">General notifications</string>
     <string name="notification_channel_general_description">Show notifications for new media folders and similar</string>
     <string name="new_media_folder_detected">New %1$s media folder detected.</string>
+    <string name="configure_new_media_folder_detection_notifications">Configure</string>
+    <string name="disable_new_media_folder_detection_notifications">Disable</string>
     <string name="new_media_folder_photos">photo</string>
     <string name="new_media_folder_videos">video</string>
     <string name="outdated_server">The server has reached end of life, please upgrade!</string>

+ 4 - 0
src/main/res/xml/preferences.xml

@@ -47,6 +47,10 @@
 		<com.owncloud.android.ui.ThemeableSwitchPreference
 			android:title="@string/prefs_show_hidden_files"
 			android:key="show_hidden_files"/>
+        <com.owncloud.android.ui.ThemeableSwitchPreference
+            android:title="@string/prefs_enable_media_scan_notifications"
+            android:summary="@string/prefs_enable_media_scan_notifications_summary"
+            android:key="show_media_scan_notifications"/>
 	</PreferenceCategory>
 
 	<PreferenceCategory android:title="@string/prefs_category_more" android:key="more">