/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package info.zamojski.soft.towercollector.tasks;

import org.acra.ACRA;

import info.zamojski.soft.towercollector.MyApplication;
import info.zamojski.soft.towercollector.io.network.NetworkHelper;
import info.zamojski.soft.towercollector.io.network.NetworkHelper.ResponseData;
import info.zamojski.soft.towercollector.model.UpdateInfo;
import info.zamojski.soft.towercollector.parsers.update.UpdateFeedParseException;
import info.zamojski.soft.towercollector.parsers.update.UpdateFeedParser;
import info.zamojski.soft.towercollector.updater.UpdaterNotificationHelper;
import info.zamojski.soft.towercollector.utils.StringUtils;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.AsyncTask;
import trikita.log.Log;

public class UpdateCheckAsyncTask extends AsyncTask<String, Void, UpdateInfo> {

    private static final String TAG = UpdateCheckAsyncTask.class.getSimpleName();

    public static final String TASK_FULL_NAME = UpdateCheckAsyncTask.class.getCanonicalName();
    public static final String INTENT_KEY_UPDATE_INFO = UpdateInfo.class.getCanonicalName();

    public static final int NOTIFICATION_ID = 'A';

    private UpdaterNotificationHelper notificationHelper;

    private Context context;
    private int currentVersion;
    private UpdateFeedParser responseParser;

    public UpdateCheckAsyncTask(Context context, int currentVersion) {
        this.context = context;
        this.currentVersion = currentVersion;
        responseParser = new UpdateFeedParser();
    }

    @Override
    protected UpdateInfo doInBackground(String... urls) {
        // get update feed url
        String updateFeedUrl = urls[0];
        // check for updates and download info
        ResponseData response = NetworkHelper.sendGet(updateFeedUrl);
        Log.d("doInBackground(): Server response: %s", response);
        if (response.getCode() == 200 && !StringUtils.isNullEmptyOrWhitespace(response.getContent())) {
            // parse response
            try {
                UpdateInfo updateInfo = responseParser.parse(response.getContent());
                return updateInfo;
            } catch (UpdateFeedParseException ex) {
                Log.w("doInBackground(): Cannot parse update feed response");
                MyApplication.getAnalytics().sendException(ex, Boolean.FALSE);
                ACRA.getErrorReporter().handleSilentException(ex);
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(UpdateInfo updateInfo) {
        if (updateInfo == null)
            return;
        // check if new version available
        if (currentVersion < updateInfo.getVersionCode()) {
            // display notification
            displayNewVersionAvailableNotification(updateInfo);
        }
    }

    private void displayNewVersionAvailableNotification(UpdateInfo updateInfo) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationHelper = new UpdaterNotificationHelper(context);
        Notification notification = notificationHelper.createNotification(updateInfo);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
}

@@ -961,16 +961,17 @@
 ncTask;%0A
+%0A
 import t
@@ -1953,16 +1953,34 @@
 ad info%0A
+        try %7B%0A    
         
@@ -2045,24 +2045,28 @@
 l);%0A        
+ 
+   
 Log.d(%22doInB
@@ -2111,16 +2111,20 @@
 ponse);%0A
+    
         
@@ -2231,16 +2231,20 @@
         
+    
 // parse
@@ -2257,32 +2257,36 @@
 nse%0A            
+    
 try %7B%0A          
@@ -2291,16 +2291,20 @@
         
+   
+ 
 UpdateIn
@@ -2376,24 +2376,28 @@
             
+    
 return updat
@@ -2403,16 +2403,20 @@
 teInfo;%0A
+    
         
@@ -2471,24 +2471,28 @@
             
+    
 Log.w(%22doInB
@@ -2541,16 +2541,20 @@
 onse%22);%0A
+    
         
@@ -2640,16 +2640,20 @@
         
+ 
+   
 ACRA.get
@@ -2699,33 +2699,267 @@
 x);%0A            
-%7D
+    %7D%0A            %7D%0A        %7D catch (SecurityException ex) %7B%0A            // for the rare case when user has custom rom with possibility to disable any permission%0A            Log.e(%22doInBackground(): internet permission is denied%22, ex);
 %0A        %7D%0A     

Add runtime permission check in UpdateCheckAsyncTask for internet permission on custom ROMs.