From 42a1a9deeae71e2893b964a1c26d3f5dba90375c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20P=C3=A9chard?= Date: Sat, 29 Oct 2016 18:02:37 +0200 Subject: [PATCH] Fix SonarQube-detected issues --- .../main/java/fr/s13d/photobackup/Log.java | 63 +++++++++++++++++++ .../fr/s13d/photobackup/PBApplication.java | 11 ++-- .../photobackup/PBBootBroadcastReceiver.java | 8 ++- .../java/fr/s13d/photobackup/PBConstants.java | 4 ++ .../java/fr/s13d/photobackup/PBService.java | 56 +++++++++++++++-- .../photobackup/PBWifiBroadcastReceiver.java | 4 +- .../s13d/photobackup/media/PBMediaStore.java | 4 +- 7 files changed, 133 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/fr/s13d/photobackup/Log.java b/app/src/main/java/fr/s13d/photobackup/Log.java index 8aa294d..55ea872 100644 --- a/app/src/main/java/fr/s13d/photobackup/Log.java +++ b/app/src/main/java/fr/s13d/photobackup/Log.java @@ -19,66 +19,129 @@ package fr.s13d.photobackup; +/** + * Own logger class to avoid leaking logs in release mode. + */ public final class Log { private Log() throws InstantiationException { throw new InstantiationException("This class is not meant to be instantiated!"); } + + /** + * Log a debug message + * @param tag tag of the logged message + * @param message message logged + */ public static void d(final String tag, final String message) { if (BuildConfig.DEBUG) { android.util.Log.d(tag, message); } } + + /** + * Log a debug exception + * @param tag tag of the logged exception + * @param e exception logged + */ public static void d(final String tag, final Exception e) { if (BuildConfig.DEBUG) { android.util.Log.d(tag, e.toString()); } } + + /** + * Log an error message + * @param tag tag of the logged message + * @param message message logged + */ public static void e(final String tag, final String message) { if (BuildConfig.DEBUG) { android.util.Log.e(tag, message); } } + + /** + * Log an error exception + * @param tag tag of the logged exception + * @param e exception logged + */ public static void e(final String tag, final Exception e) { if (BuildConfig.DEBUG) { android.util.Log.e(tag, e.toString()); } } + + /** + * Log an info message + * @param tag tag of the logged message + * @param message message logged + */ public static void i(final String tag, final String message) { if (BuildConfig.DEBUG) { android.util.Log.i(tag, message); } } + + /** + * Log an info exception + * @param tag tag of the logged exception + * @param e exception logged + */ public static void i(final String tag, final Exception e) { if (BuildConfig.DEBUG) { android.util.Log.i(tag, e.toString()); } } + + /** + * Log a verbose message + * @param tag tag of the logged message + * @param message message logged + */ public static void v(final String tag, final String message) { if (BuildConfig.DEBUG) { android.util.Log.v(tag, message); } } + + /** + * Log a verbose exception + * @param tag tag of the logged exception + * @param e exception logged + */ public static void v(final String tag, final Exception e) { if (BuildConfig.DEBUG) { android.util.Log.v(tag, e.toString()); } } + + /** + * Log a warning message + * @param tag tag of the logged message + * @param message message logged + */ public static void w(final String tag, final String message) { if (BuildConfig.DEBUG) { android.util.Log.w(tag, message); } } + + /** + * Log a warning exception + * @param tag tag of the logged exception + * @param e exception logged + */ public static void w(final String tag, final Exception e) { if (BuildConfig.DEBUG) { android.util.Log.w(tag, e.toString()); diff --git a/app/src/main/java/fr/s13d/photobackup/PBApplication.java b/app/src/main/java/fr/s13d/photobackup/PBApplication.java index c28ba82..4d4ec8a 100644 --- a/app/src/main/java/fr/s13d/photobackup/PBApplication.java +++ b/app/src/main/java/fr/s13d/photobackup/PBApplication.java @@ -80,7 +80,7 @@ private void trimMemory() { Log.d(LOG_TAG, "trimMemory"); if (mediaStore != null) { mediaStore.close(); - setMediaStore(null); + nullifyMediaStore(); } } @@ -93,7 +93,7 @@ public static PBApplication getApp() { } - public static void setApp(PBApplication application) { + private static void setApp(PBApplication application) { app = application; } @@ -106,7 +106,10 @@ public static PBMediaStore getMediaStore() { return mediaStore; } - public static void setMediaStore(PBMediaStore store) { - mediaStore = store; + /** + * Nullify the media store when destroying it. + */ + public static void nullifyMediaStore() { + mediaStore = null; } } diff --git a/app/src/main/java/fr/s13d/photobackup/PBBootBroadcastReceiver.java b/app/src/main/java/fr/s13d/photobackup/PBBootBroadcastReceiver.java index 2784d80..affaa9f 100644 --- a/app/src/main/java/fr/s13d/photobackup/PBBootBroadcastReceiver.java +++ b/app/src/main/java/fr/s13d/photobackup/PBBootBroadcastReceiver.java @@ -25,6 +25,10 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; + +/** + * Receives boot broadcast intents and start PB service if required. + */ public class PBBootBroadcastReceiver extends BroadcastReceiver { @Override @@ -32,8 +36,8 @@ public void onReceive(final Context context, final Intent intent) { if (intent != null && "android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - final boolean running = preferences.getBoolean(PBConstants.PREF_SERVICE_RUNNING, false); - if (running) { + final boolean shouldRun = preferences.getBoolean(PBConstants.PREF_SERVICE_RUNNING, false); + if (shouldRun) { final Intent startServiceIntent = new Intent(context, PBService.class); context.startService(startServiceIntent); } diff --git a/app/src/main/java/fr/s13d/photobackup/PBConstants.java b/app/src/main/java/fr/s13d/photobackup/PBConstants.java index 5b0aa76..8b88c7b 100644 --- a/app/src/main/java/fr/s13d/photobackup/PBConstants.java +++ b/app/src/main/java/fr/s13d/photobackup/PBConstants.java @@ -1,5 +1,9 @@ package fr.s13d.photobackup; + +/** + * Delivers all public constants to the application. + */ public class PBConstants { // should correspond to what is in preferences.xml diff --git a/app/src/main/java/fr/s13d/photobackup/PBService.java b/app/src/main/java/fr/s13d/photobackup/PBService.java index ade0371..f5d8486 100644 --- a/app/src/main/java/fr/s13d/photobackup/PBService.java +++ b/app/src/main/java/fr/s13d/photobackup/PBService.java @@ -33,6 +33,10 @@ import fr.s13d.photobackup.media.PBMediaSender; +/** + * Main background service doing the whole job of listening + * to new media being saved and initiating media uploads. + */ public class PBService extends Service implements PBMediaStoreInterface, PBMediaSenderInterface { private static final String LOG_TAG = "PBService"; @@ -69,7 +73,7 @@ public void onDestroy() { setImagesContentObserver(null); setVideosContentObserver(null); PBApplication.getMediaStore().removeInterface(this); - PBApplication.setMediaStore(null); + PBApplication.nullifyMediaStore(); Log.i(LOG_TAG, "PhotoBackup service has stopped"); } @@ -89,6 +93,9 @@ public IBinder onBind(Intent intent) { } + /** + * Binder class + */ public class Binder extends android.os.Binder { public PBService getService() { return PBService.this; @@ -99,6 +106,9 @@ public PBService getService() { ///////////// // Methods // ///////////// + /** + * Sends next media in store. + */ public void sendNextMedia() { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(PBApplication.getApp()); final boolean isRunning = preferences.getBoolean(PBConstants.PREF_SERVICE_RUNNING, false); @@ -112,6 +122,12 @@ public void sendNextMedia() { } } + + /** + * Initiates a media media upload. + * @param media media to be sent + * @param manual indicates if upload is manually or automatically (through sendNextMedia()) initiated. + */ public void sendMedia(PBMedia media, boolean manual) { getMediaSender().send(media, manual); } @@ -120,6 +136,9 @@ public void sendMedia(PBMedia media, boolean manual) { //////////////////////////////////// // PBMediaStoreListener callbacks // //////////////////////////////////// + /** + * Sends next media on media store synchronisation. + */ public void onSyncMediaStoreTaskPostExecute() { sendNextMedia(); } @@ -128,16 +147,41 @@ public void onSyncMediaStoreTaskPostExecute() { ////////////////////////////////////// // PBMediaSenderInterface callbacks // ////////////////////////////////////// + /** + * Does nothing on receiving a message. + */ public void onMessage(final String message) { // Do nothing } - public void onSendSuccess() { sendNextMedia(); } + + + /** + * Send next media on sending with success. + */ + public void onSendSuccess() { + sendNextMedia(); + } + + + /** + * Does nothing on sending with failure. + */ public void onSendFailure() { // Do nothing } + + + /** + * Does nothing on test success. + */ public void onTestSuccess() { // Do nothing } + + + /** + * Does nothing on test failure. + */ public void onTestFailure() { // Do nothing } @@ -190,13 +234,13 @@ private PBMediaSender getMediaSender() { } - public static void setVideosContentObserver(MediaContentObserver videosContentObserver) { - PBService.videosContentObserver = videosContentObserver; + private static void setVideosContentObserver(MediaContentObserver contentObserver) { + videosContentObserver = contentObserver; } - public static void setImagesContentObserver(MediaContentObserver imagesContentObserver) { - PBService.imagesContentObserver = imagesContentObserver; + private static void setImagesContentObserver(MediaContentObserver contentObserver) { + imagesContentObserver = contentObserver; } diff --git a/app/src/main/java/fr/s13d/photobackup/PBWifiBroadcastReceiver.java b/app/src/main/java/fr/s13d/photobackup/PBWifiBroadcastReceiver.java index 4af365e..76691c0 100644 --- a/app/src/main/java/fr/s13d/photobackup/PBWifiBroadcastReceiver.java +++ b/app/src/main/java/fr/s13d/photobackup/PBWifiBroadcastReceiver.java @@ -85,11 +85,11 @@ private void handleWifi(final Context context, final long now) { ///////////////////// // getters/setters // ///////////////////// - public static long getLastFiredOn() { + private static long getLastFiredOn() { return lastFiredOn; } - public static void setLastFiredOn(long lastFiredOn) { + private static void setLastFiredOn(long lastFiredOn) { PBWifiBroadcastReceiver.lastFiredOn = lastFiredOn; } } diff --git a/app/src/main/java/fr/s13d/photobackup/media/PBMediaStore.java b/app/src/main/java/fr/s13d/photobackup/media/PBMediaStore.java index d4874e8..440e565 100644 --- a/app/src/main/java/fr/s13d/photobackup/media/PBMediaStore.java +++ b/app/src/main/java/fr/s13d/photobackup/media/PBMediaStore.java @@ -139,7 +139,7 @@ private boolean isBucketSelected(final String requestedBucketId) { } - private ArrayMap getBucketData(final ArrayMap bucketNamesList, final String buckedId, final String buckedName) { + private void getBucketData(final ArrayMap bucketNamesList, final String buckedId, final String buckedName) { // We want to group the images by bucket names. We abuse the // "WHERE" parameter to insert a "GROUP BY" clause into the SQL statement. @@ -170,8 +170,6 @@ private ArrayMap getBucketData(final ArrayMap bu closeCursor(cursor); Log.d(LOG_TAG, bucketNamesList.toString()); - - return bucketNamesList; }