Browse Source

add notification channel support, with only one initially set-up

tobiasKaminsky 7 years ago
parent
commit
4ea1a907f4

+ 74 - 0
src/main/java/com/owncloud/android/MainApp.java

@@ -22,6 +22,8 @@ package com.owncloud.android;
 import android.Manifest;
 import android.accounts.Account;
 import android.app.Activity;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -57,6 +59,7 @@ import com.owncloud.android.ui.activity.ContactsPreferenceActivity;
 import com.owncloud.android.ui.activity.Preferences;
 import com.owncloud.android.ui.activity.SyncedFoldersActivity;
 import com.owncloud.android.ui.activity.WhatsNewActivity;
+import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.utils.AnalyticsUtils;
 import com.owncloud.android.utils.FilesSyncHelper;
 import com.owncloud.android.utils.PermissionUtil;
@@ -146,6 +149,7 @@ public class MainApp extends MultiDexApplication {
 
         initAutoUpload();
         initContactsBackup();
+        notificationChannels();
 
         // register global protection with pass code
         registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@@ -202,6 +206,7 @@ public class MainApp extends MultiDexApplication {
         }
 
     }
+
     public static void initAutoUpload() {
         updateToAutoUpload();
         cleanOldEntries();
@@ -231,6 +236,75 @@ public class MainApp extends MultiDexApplication {
         }
     }
 
+    public static void notificationChannels() {
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && getAppContext() != null) {
+            Context context = getAppContext();
+            NotificationManager notificationManager = (NotificationManager)
+                    context.getSystemService(Context.NOTIFICATION_SERVICE);
+
+            if (notificationManager != null) {
+                if (notificationManager.getNotificationChannel(
+                        NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD) == null) {
+                    CharSequence name = context.getString(R.string.notification_channel_download_name);
+                    String description = context.getString(R.string.notification_channel_download_description);
+                    NotificationChannel channel = new NotificationChannel(
+                            NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD, name,
+                            NotificationManager.IMPORTANCE_LOW);
+
+                    channel.setDescription(description);
+                    channel.enableLights(false);
+                    channel.enableVibration(false);
+                    notificationManager.createNotificationChannel(channel);
+                }
+
+                if (notificationManager.getNotificationChannel(
+                        NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD) == null) {
+                    CharSequence name = context.getString(R.string.notification_channel_upload_name);
+                    String description = context.getString(R.string.notification_channel_upload_description);
+                    NotificationChannel channel = new NotificationChannel(
+                            NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD, name,
+                            NotificationManager.IMPORTANCE_LOW);
+
+                    channel.setDescription(description);
+                    channel.enableLights(false);
+                    channel.enableVibration(false);
+                    notificationManager.createNotificationChannel(channel);
+                }
+
+                if (notificationManager.getNotificationChannel(
+                        NotificationUtils.NOTIFICATION_CHANNEL_MEDIA) == null) {
+                    CharSequence name = context.getString(R.string.notification_channel_media_name);
+                    String description = context.getString(R.string.notification_channel_media_description);
+                    NotificationChannel channel = new NotificationChannel(
+                            NotificationUtils.NOTIFICATION_CHANNEL_MEDIA, name,
+                            NotificationManager.IMPORTANCE_LOW);
+
+                    channel.setDescription(description);
+                    channel.enableLights(false);
+                    channel.enableVibration(false);
+                    notificationManager.createNotificationChannel(channel);
+                }
+
+                if (notificationManager.getNotificationChannel(
+                        NotificationUtils.NOTIFICATION_CHANNEL_FILE_SYNC) == null) {
+                    CharSequence name = context.getString(R.string.notification_channel_file_sync_name);
+                    String description = context.getString(R.string.notification_channel_file_sync_description);
+                    NotificationChannel channel = new NotificationChannel(
+                            NotificationUtils.NOTIFICATION_CHANNEL_FILE_SYNC, name,
+                            NotificationManager.IMPORTANCE_LOW);
+
+                    channel.setDescription(description);
+                    channel.enableLights(false);
+                    channel.enableVibration(false);
+                    notificationManager.createNotificationChannel(channel);
+                }
+            } else {
+                Log_OC.e(TAG, "Notification manager is null");
+            }
+        }
+    }
+    
+
     public static Context getAppContext() {
         return MainApp.mContext;
     }

+ 15 - 7
src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java

@@ -1,4 +1,4 @@
-/**
+/*
  * ownCloud Android client application
  *
  * @author David A. Velasco
@@ -48,12 +48,20 @@ public class BootupBroadcastReceiver extends BroadcastReceiver {
      */
     @Override
     public void onReceive(Context context, Intent intent) {
-        Log_OC.d(TAG, "Starting file observer service...");
-        Intent initObservers = FileObserverService.makeInitIntent(context);
-        context.startService(initObservers);
+        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
+            Log_OC.d(TAG, "Starting file observer service...");
+            Intent initObservers = FileObserverService.makeInitIntent(context);
 
-        MainApp.initAutoUpload();
-        MainApp.initContactsBackup();
-    }
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                context.startForegroundService(initObservers);
+            } else {
+                context.startService(initObservers);
+            }
 
+            MainApp.initAutoUpload();
+            MainApp.initContactsBackup();
+        } else {
+            Log_OC.d(TAG, "Getting wrong intent: " + intent.getAction());
+        }
+    }
 }

+ 5 - 2
src/main/java/com/owncloud/android/files/services/FileDownloader.java

@@ -514,8 +514,7 @@ public class FileDownloader extends Service
     private void notifyDownloadStart(DownloadFileOperation download) {
         /// create status notification with a progress bar
         mLastPercent = 0;
-        mNotificationBuilder =
-                NotificationUtils.newNotificationBuilder(this);
+        mNotificationBuilder = NotificationUtils.newNotificationBuilder(this);
         mNotificationBuilder
                 .setSmallIcon(R.drawable.notification_icon)
                 .setTicker(getString(R.string.downloader_download_in_progress_ticker))
@@ -527,6 +526,10 @@ public class FileDownloader extends Service
                                 new File(download.getSavePath()).getName())
                 );
 
+        if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)) {
+            mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD);
+        }
+        
         /// includes a pending intent in the notification showing the details view of the file
         Intent showDetailsIntent = null;
         if (PreviewImageFragment.canBePreviewed(download.getFile())) {

+ 4 - 0
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -1118,6 +1118,10 @@ public class FileUploader extends Service
                         String.format(getString(R.string.uploader_upload_in_progress_content), 0, upload.getFileName())
                 );
 
+        if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)) {
+            mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD);
+        }
+
         /// includes a pending intent in the notification showing the details
         Intent showUploadListIntent = new Intent(this, UploadListActivity.class);
         showUploadListIntent.putExtra(FileActivity.EXTRA_FILE, upload.getFile());

+ 9 - 0
src/main/java/com/owncloud/android/media/MediaService.java

@@ -42,6 +42,7 @@ import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
+import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.utils.ThemeUtils;
 
 import java.io.IOException;
@@ -555,6 +556,10 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         mNotificationBuilder.setContentTitle(ticker);
         mNotificationBuilder.setContentText(content);
 
+        if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)) {
+            mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MEDIA);
+        }
+
         mNotificationManager.notify(R.string.media_notif_ticker, mNotificationBuilder.build());
     }
 
@@ -588,6 +593,10 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         mNotificationBuilder.setContentTitle(ticker);
         mNotificationBuilder.setContentText(content);
 
+        if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)) {
+            mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MEDIA);
+        }
+
         startForeground(R.string.media_notif_ticker, mNotificationBuilder.build());
     }
 

+ 9 - 2
src/main/java/com/owncloud/android/syncadapter/FileSyncAdapter.java

@@ -46,6 +46,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.RefreshFolderOperation;
 import com.owncloud.android.operations.UpdateOCVersionOperation;
 import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;
+import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.utils.DataHolderUtil;
 import com.owncloud.android.utils.ThemeUtils;
 
@@ -528,8 +529,14 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
      * @param builder
      */
     private void showNotification(int id, NotificationCompat.Builder builder) {
-        ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE))
-            .notify(id, builder.build());
+        NotificationManager notificationManager = ((NotificationManager) getContext().
+                getSystemService(Context.NOTIFICATION_SERVICE));
+
+        if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)) {
+            builder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_FILE_SYNC);
+        }
+
+        notificationManager.notify(id, builder.build());
     }
     /**
      * Shorthand translation

+ 5 - 0
src/main/java/com/owncloud/android/ui/notifications/NotificationUtils.java

@@ -34,6 +34,11 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 public class NotificationUtils {
 
+    public static final String NOTIFICATION_CHANNEL_DOWNLOAD = "NOTIFICATION_CHANNEL_DOWNLOAD";
+    public static final String NOTIFICATION_CHANNEL_UPLOAD = "NOTIFICATION_CHANNEL_UPLOAD";
+    public static final String NOTIFICATION_CHANNEL_MEDIA = "NOTIFICATION_CHANNEL_MEDIA";
+    public static final String NOTIFICATION_CHANNEL_FILE_SYNC = "NOTIFICATION_CHANNEL_FILE_SYNC";
+
     /**
      * Factory method for {@link android.support.v4.app.NotificationCompat.Builder} instances.
      *

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

@@ -727,6 +727,15 @@
     <string name="send">Send</string>
     <string name="share">Share</string>
     <string name="link">Link</string>
+
+    <string name="notification_channel_upload_name">Upload notification channel</string>
+    <string name="notification_channel_download_name">Download notification channel</string>
+    <string name="notification_channel_download_description">Shows download progress</string>
+    <string name="notification_channel_upload_description">Shows upload progress</string>
+    <string name="notification_channel_media_name">Media player</string>
+    <string name="notification_channel_media_description">Music player progress</string>
+    <string name="notification_channel_file_sync_name">File sync</string>
+    <string name="notification_channel_file_sync_description">Shows file sync progress and results</string>
   
     <string name="account_not_found">Account not found!</string>