package com.benoitletondor.easybudget.push;

import android.app.IntentService;
import android.content.Intent;

import com.batch.android.Batch;
import com.benoitletondor.easybudget.BuildConfig;
import com.benoitletondor.easybudget.helper.Logger;

/**
 * Service that handles Batch pushes
 *
 * @author Benoit LETONDOR
 */
public class PushService extends IntentService
{
    /**
     * Key to retrieve the new version for an update push
     */
    private final static String UPDATE_INTENT_VERSION_KEY = "newVersion";

// ----------------------------------->

    public PushService()
    {
        super("EasyBudgetPushService");
    }

// ----------------------------------->

    @Override
    protected void onHandleIntent(Intent intent)
    {
        try
        {
            if ( Batch.Push.shouldDisplayPush(this, intent) ) // Check that the push is valid
            {
                if( isUpdatePush(intent) && !shouldDisplayUpdatePush(intent) )
                {
                    Logger.debug("Not displaying update push cause app is already updated");
                    return;
                }

                // Display the notification
                Batch.Push.displayNotification(this, intent);
            }
        }
        finally
        {
            PushReceiver.completeWakefulIntent(intent);
        }
    }

    /**
     * Check if the given intent is an update push
     *
     * @param intent
     * @return true if intent contains the key, false otherwise
     */
    private boolean isUpdatePush(Intent intent)
    {
        return intent.hasExtra(UPDATE_INTENT_VERSION_KEY);
    }

    /**
     * Check if the version received in push is > to the current version
     *
     * @param intent
     * @return true if should display the push, false otherwise
     */
    private boolean shouldDisplayUpdatePush(Intent intent)
    {
        try
        {
            int newVersion = Integer.parseInt(intent.getStringExtra(UPDATE_INTENT_VERSION_KEY));

            return newVersion > BuildConfig.VERSION_CODE;
        }
        catch(Exception e)
        {
            Logger.error("Error while getting new version value from push", e);
            return false;
        }
    }
}

@@ -398,19 +398,19 @@
 eve the 
-new
+max
  version
@@ -415,24 +415,153 @@
 on for a
-n update
+ push%0A     */%0A    private final static String INTENT_MAX_VERSION_KEY = %22maxVersion%22;%0A    /**%0A     * Key to retrieve the max version for a
  push%0A  
@@ -598,30 +598,27 @@
  String 
-UPDATE_
 INTENT_
+MIN_
 VERSION_
@@ -624,19 +624,19 @@
 _KEY = %22
-new
+min
 Version%22
@@ -905,17 +905,16 @@
     if (
- 
 Batch.Pu
@@ -947,17 +947,16 @@
  intent)
- 
 ) // Che
@@ -1018,32 +1018,8 @@
  if(
- isUpdatePush(intent) &&
  !sh
@@ -1025,30 +1025,24 @@
 houldDisplay
-Update
 Push(intent)
@@ -1111,23 +1111,16 @@
 playing 
-update 
 push cau
@@ -1130,26 +1130,31 @@
 app 
-is already updated
+version is not matching
 %22);%0A
@@ -1461,38 +1461,64 @@
 the 
-given intent is an update push
+push should be displayed according to version constrains
 %0A   
@@ -1569,31 +1569,31 @@
  if 
-intent contains
+should display
  the 
-key
+push
 , fa
@@ -1638,16 +1638,21 @@
 ean 
-isUpdate
+shouldDisplay
 Push
@@ -1681,22 +1681,133 @@
         
-return
+try%0A        %7B%0A            int maxVersion = BuildConfig.VERSION_CODE;%0A            int minVersion = 1;%0A%0A            if(
  intent.
@@ -1811,38 +1811,35 @@
 nt.hasExtra(
-UPDATE_
 INTENT_
+MAX_
 VERSION_KEY)
@@ -1842,271 +1842,204 @@
 KEY)
-;
+ )
 %0A    
-%7D%0A%0A
     
-/**%0A     * Check if the version received in push is %3E to the current version%0A     *%0A     * @param intent%0A     * @return true if should display the push, false otherwise%0A     */%0A    private boolean shouldDisplayUpdatePush(Intent intent
+    %7B%0A                maxVersion = Integer.parseInt(intent.getStringExtra(INTENT_MAX_VERSION_KEY));%0A            %7D%0A%0A            if( intent.hasExtra(INTENT_MIN_VERSION_KEY) 
 )%0A    
-%7B%0A
         
 try%0A
@@ -2026,35 +2026,33 @@
 ) )%0A            
-try
+%7B
 %0A        %7B%0A     
@@ -2040,34 +2040,32 @@
   %7B%0A        
-%7B%0A
         
     int newV
@@ -2056,19 +2056,11 @@
     
-    int new
+min
 Vers
@@ -2108,22 +2108,19 @@
 tra(
-UPDATE_
 INTENT_
+MIN_
 VERS
@@ -2129,16 +2129,30 @@
 N_KEY));
+%0A            %7D
 %0A%0A      
@@ -2168,19 +2168,47 @@
 urn 
-new
+BuildConfig.VERSION_CODE %3C= max
 Version 
 %3E Bu
@@ -2203,17 +2203,18 @@
 Version 
-%3E
+&&
  BuildCo
@@ -2222,32 +2222,46 @@
 fig.VERSION_CODE
+ %3E= minVersion
 ;%0A        %7D%0A    
@@ -2335,19 +2335,20 @@
 ile 
-gett
+check
 ing 
-new
+app
  ver
@@ -2356,18 +2356,11 @@
 ion 
-value from
+for
  pus

Update batch push versioning filter