Skip to content

Commit

Permalink
Fix SonarQube-detected issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanepechard committed Oct 29, 2016
1 parent 1b303e2 commit 42a1a9d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 17 deletions.
63 changes: 63 additions & 0 deletions app/src/main/java/fr/s13d/photobackup/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/fr/s13d/photobackup/PBApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void trimMemory() {
Log.d(LOG_TAG, "trimMemory");
if (mediaStore != null) {
mediaStore.close();
setMediaStore(null);
nullifyMediaStore();
}
}

Expand All @@ -93,7 +93,7 @@ public static PBApplication getApp() {
}


public static void setApp(PBApplication application) {
private static void setApp(PBApplication application) {
app = application;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


/**
* Receives boot broadcast intents and start PB service if required.
*/
public class PBBootBroadcastReceiver extends BroadcastReceiver {

@Override
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);
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/fr/s13d/photobackup/PBConstants.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
56 changes: 50 additions & 6 deletions app/src/main/java/fr/s13d/photobackup/PBService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
}
Expand All @@ -89,6 +93,9 @@ public IBinder onBind(Intent intent) {
}


/**
* Binder class
*/
public class Binder extends android.os.Binder {
public PBService getService() {
return PBService.this;
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -120,6 +136,9 @@ public void sendMedia(PBMedia media, boolean manual) {
////////////////////////////////////
// PBMediaStoreListener callbacks //
////////////////////////////////////
/**
* Sends next media on media store synchronisation.
*/
public void onSyncMediaStoreTaskPostExecute() {
sendNextMedia();
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private boolean isBucketSelected(final String requestedBucketId) {
}


private ArrayMap<String, String> getBucketData(final ArrayMap<String, String> bucketNamesList, final String buckedId, final String buckedName) {
private void getBucketData(final ArrayMap<String, String> 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.
Expand Down Expand Up @@ -170,8 +170,6 @@ private ArrayMap<String, String> getBucketData(final ArrayMap<String, String> bu
closeCursor(cursor);

Log.d(LOG_TAG, bucketNamesList.toString());

return bucketNamesList;
}


Expand Down

0 comments on commit 42a1a9d

Please sign in to comment.