[Android] Fix logging on Android 8+

Geoffrey Métais git at videolan.org
Wed Nov 14 15:26:55 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Nov 14 11:10:37 2018 +0100| [21ecf47730543f6488b9c9ef1385d19c2587325f] | committer: Geoffrey Métais

Fix logging on Android 8+

> https://code.videolan.org/videolan/vlc-android/commit/21ecf47730543f6488b9c9ef1385d19c2587325f
---

 .../src/org/videolan/vlc/DebugLogService.java      | 12 ++++---
 .../src/org/videolan/vlc/gui/DebugLogActivity.java | 37 ++++++++++++++--------
 .../vlc/gui/helpers/NotificationHelper.java        | 19 +++++++++--
 3 files changed, 48 insertions(+), 20 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/DebugLogService.java b/vlc-android/src/org/videolan/vlc/DebugLogService.java
index a669f04f3..ab2aaa06c 100644
--- a/vlc-android/src/org/videolan/vlc/DebugLogService.java
+++ b/vlc-android/src/org/videolan/vlc/DebugLogService.java
@@ -32,10 +32,11 @@ import android.os.IBinder;
 import android.os.Looper;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
-import androidx.core.app.NotificationCompat;
 import android.text.format.DateFormat;
 
+import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.gui.DebugLogActivity;
+import org.videolan.vlc.gui.helpers.NotificationHelper;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Logcat;
 import org.videolan.vlc.util.Util;
@@ -48,6 +49,8 @@ import java.io.OutputStreamWriter;
 import java.util.LinkedList;
 import java.util.List;
 
+import androidx.core.app.NotificationCompat;
+
 public class DebugLogService extends Service implements Logcat.Callback, Runnable {
 
     private static final int MSG_STARTED = 0;
@@ -129,8 +132,7 @@ public class DebugLogService extends Service implements Logcat.Callback, Runnabl
     }
 
     public synchronized void start() {
-        if (mLogcat != null)
-            return;
+        if (mLogcat != null) return;
         clear();
         mLogcat = new Logcat();
         mLogcat.start(this);
@@ -140,7 +142,7 @@ public class DebugLogService extends Service implements Logcat.Callback, Runnabl
         debugLogIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
         final PendingIntent pi = PendingIntent.getActivity(this, 0, debugLogIntent, 0);
 
-        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
+        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationHelper.VLC_DEBUG_CHANNEL);
         builder.setContentTitle(getResources().getString(R.string.log_service_title));
         builder.setContentText(getResources().getString(R.string.log_service_text));
         builder.setSmallIcon(R.drawable.ic_stat_vlc);
@@ -186,6 +188,7 @@ public class DebugLogService extends Service implements Logcat.Callback, Runnabl
                 }
             }
         } catch (FileNotFoundException e) {
+
             saved = false;
         } catch (IOException ioe) {
             saved = false;
@@ -317,6 +320,7 @@ public class DebugLogService extends Service implements Logcat.Callback, Runnabl
             synchronized (this) {
                 if (mIDebugLogService != null) {
                     try {
+                        if (AndroidUtil.isOOrLater) NotificationHelper.createDebugServcieChannel(mContext.getApplicationContext());
                         mIDebugLogService.start();
                         return true;
                     } catch (RemoteException e) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/DebugLogActivity.java b/vlc-android/src/org/videolan/vlc/gui/DebugLogActivity.java
index 3d1de467f..f308d1224 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DebugLogActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DebugLogActivity.java
@@ -20,22 +20,27 @@
  *****************************************************************************/
 package org.videolan.vlc.gui;
 
-import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ListView;
 
+import com.google.android.material.snackbar.Snackbar;
+
+import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.DebugLogService;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.helpers.UiTools;
+import org.videolan.vlc.util.Permissions;
 
 import java.util.ArrayList;
 import java.util.List;
 
-public class DebugLogActivity extends Activity implements DebugLogService.Client.Callback {
+import androidx.fragment.app.FragmentActivity;
+
+public class DebugLogActivity extends FragmentActivity implements DebugLogService.Client.Callback {
     public final static String TAG = "VLC/DebugLogActivity";
     private DebugLogService.Client mClient = null;
     private Button mStartButton = null;
@@ -52,12 +57,12 @@ public class DebugLogActivity extends Activity implements DebugLogService.Client
         super.onCreate(savedInstanceState);
         setContentView(R.layout.debug_log);
 
-        mStartButton = (Button)findViewById(R.id.start_log);
-        mStopButton = (Button)findViewById(R.id.stop_log);
-        mLogView = (ListView) findViewById(R.id.log_list);
-        mCopyButton = (Button)findViewById(R.id.copy_to_clipboard);
-        mClearButton = (Button)findViewById(R.id.clear_log);
-        mSaveButton = (Button)findViewById(R.id.save_to_file);
+        mStartButton = findViewById(R.id.start_log);
+        mStopButton = findViewById(R.id.stop_log);
+        mLogView = findViewById(R.id.log_list);
+        mCopyButton = findViewById(R.id.copy_to_clipboard);
+        mClearButton = findViewById(R.id.clear_log);
+        mSaveButton = findViewById(R.id.save_to_file);
 
         mClient = new DebugLogService.Client(this, this);
 
@@ -118,7 +123,13 @@ public class DebugLogActivity extends Activity implements DebugLogService.Client
     private View.OnClickListener mSaveClickListener = new View.OnClickListener() {
         @Override
         public void onClick(View v) {
-            mClient.save();
+            if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage()) Permissions.askWriteStoragePermission(DebugLogActivity.this, false, new Runnable() {
+                @Override
+                public void run() {
+                    mClient.save();
+                }
+            });
+            else mClient.save();
         }
     };
 
@@ -143,8 +154,8 @@ public class DebugLogActivity extends Activity implements DebugLogService.Client
         mStopButton.setEnabled(true);
         if (logList.size() > 0)
             setOptionsButtonsEnabled(true);
-        mLogList = new ArrayList<String>(logList);
-        mLogAdapter = new ArrayAdapter<String>(this, R.layout.debug_log_item, mLogList);
+        mLogList = new ArrayList<>(logList);
+        mLogAdapter = new ArrayAdapter<>(this, R.layout.debug_log_item, mLogList);
         mLogView.setAdapter(mLogAdapter);
         mLogView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
         if (mLogList.size() > 0)
@@ -169,9 +180,9 @@ public class DebugLogActivity extends Activity implements DebugLogService.Client
     @Override
     public void onSaved(boolean success, String path) {
         if (success) {
-            UiTools.snacker(getWindow().getDecorView().findViewById(android.R.id.content), String.format(
+            Snackbar.make(mLogView, String.format(
                     VLCApplication.getAppResources().getString(R.string.dump_logcat_success),
-                    path));
+                    path), Snackbar.LENGTH_LONG).show();
         } else {
             UiTools.snacker(getWindow().getDecorView().findViewById(android.R.id.content), R.string.dump_logcat_failure);
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
index a76c0f9e0..de39c8768 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
@@ -28,9 +28,6 @@ import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.os.Build;
-import androidx.annotation.RequiresApi;
-import androidx.core.app.NotificationCompat;
-import androidx.media.session.MediaButtonReceiver;
 import android.support.v4.media.session.MediaSessionCompat;
 import android.support.v4.media.session.PlaybackStateCompat;
 
@@ -38,6 +35,10 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.StartActivity;
 import org.videolan.vlc.util.AndroidDevices;
 
+import androidx.annotation.RequiresApi;
+import androidx.core.app.NotificationCompat;
+import androidx.media.session.MediaButtonReceiver;
+
 import static org.videolan.vlc.util.Constants.ACTION_PAUSE_SCAN;
 import static org.videolan.vlc.util.Constants.ACTION_RESUME_SCAN;
 import static org.videolan.vlc.util.Util.getMediaDescription;
@@ -46,6 +47,7 @@ public class NotificationHelper {
     public final static String TAG = "VLC/NotificationHelper";
 
     private final static StringBuilder sb = new StringBuilder();
+    public static final String VLC_DEBUG_CHANNEL = "vlc_debug";
 
     public static Notification createPlaybackNotification(Context ctx, boolean video, String title, String artist,
                                                           String album, Bitmap cover, boolean playing,
@@ -150,4 +152,15 @@ public class NotificationHelper {
             notificationManager.createNotificationChannel(channel);
         }
     }
+
+    @RequiresApi(api = Build.VERSION_CODES.O)
+    public static void createDebugServcieChannel(Context appCtx) {
+        final NotificationManager notificationManager = (NotificationManager) appCtx.getSystemService(Context.NOTIFICATION_SERVICE);
+        if (notificationManager == null) return;
+        // Playback channel
+        final CharSequence name = appCtx.getString(R.string.debug_logs);
+        final NotificationChannel channel = new NotificationChannel(VLC_DEBUG_CHANNEL, name, NotificationManager.IMPORTANCE_LOW);
+        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
+        notificationManager.createNotificationChannel(channel);
+    }
 }



More information about the Android mailing list