<commit_before>package com.ozodrukh.instant.eclass.assignments;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import com.ozodrukh.eclass.InhaEclassController;
import com.ozodrukh.eclass.InhaEclassWebService;
import com.ozodrukh.eclass.entity.SubjectReport;
import com.ozodrukh.instant.eclass.R;
import com.ozodrukh.instant.eclass.utils.IOUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class AttachmentDownloaderService extends Service {
  /**
   * if permission wasn't granted at download beginning then it will be requested
   * from activity, and service will await till it granting request
   */
  public final static int MSG_START_DOWNLOADING_SERVICE = 1;

  public final static int NOTIFICATION_ID = 123_000;

  public final static String EXTRA_ASSIGNMENT_SUBJECT = "arg:attachment_info";
  public final static String EXTRA_DESTINATION = "arg:destination";

  private Handler handler;
  private HandlerThread handlerThread;
  private Handler.Callback serviceHandlerCallback = new Handler.Callback() {
    @Override public boolean handleMessage(Message msg) {
      if (msg.what == MSG_START_DOWNLOADING_SERVICE && !requests.isEmpty()) {
        if (checkCallingOrSelfPermission(READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED
            && checkCallingOrSelfPermission(WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED) {
          startAttachmentDownloading(requests.pop());
          return true;
        }
      }
      return false;
    }
  };

  private ArrayDeque<AttachmentRequest> requests = new ArrayDeque<>();
  private AtomicInteger attachmentsDownloaded = new AtomicInteger(0);

  public AttachmentDownloaderService() {
  }

  @Override public void onCreate() {
    super.onCreate();
    handlerThread = new HandlerThread("AttachmentDownloader", Process.THREAD_PRIORITY_BACKGROUND);
    handlerThread.start();

    handler = new Handler(handlerThread.getLooper(), serviceHandlerCallback);
  }

  @Override public void onDestroy() {
    super.onDestroy();
    handlerThread.quit();
  }

  @Override public int onStartCommand(Intent intent, int flags, int startId) {
    requests.push(new AttachmentRequest(intent));

    handler.sendEmptyMessage(MSG_START_DOWNLOADING_SERVICE);
    return START_STICKY;
  }

  @Override public IBinder onBind(Intent intent) {
    return new Messenger(handler).getBinder();
  }

  protected void startAttachmentDownloading(AttachmentRequest attachmentRequest) {
    Notification foregroundNotification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)<commit_msg>avoid service null intent object crash<commit_after>package com.ozodrukh.instant.eclass.assignments;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import com.ozodrukh.eclass.InhaEclassController;
import com.ozodrukh.eclass.InhaEclassWebService;
import com.ozodrukh.eclass.entity.SubjectReport;
import com.ozodrukh.instant.eclass.R;
import com.ozodrukh.instant.eclass.utils.IOUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class AttachmentDownloaderService extends Service {
  /**
   * if permission wasn't granted at download beginning then it will be requested
   * from activity, and service will await till it granting request
   */
  public final static int MSG_START_DOWNLOADING_SERVICE = 1;

  public final static int NOTIFICATION_ID = 123_000;

  public final static String EXTRA_ASSIGNMENT_SUBJECT = "arg:attachment_info";
  public final static String EXTRA_DESTINATION = "arg:destination";

  private Handler handler;
  private HandlerThread handlerThread;
  private Handler.Callback serviceHandlerCallback = new Handler.Callback() {
    @Override public boolean handleMessage(Message msg) {
      if (msg.what == MSG_START_DOWNLOADING_SERVICE && !requests.isEmpty()) {
        if (checkCallingOrSelfPermission(READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED
            && checkCallingOrSelfPermission(WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED) {
          startAttachmentDownloading(requests.pop());
          return true;
        }
      }
      return false;
    }
  };

  private ArrayDeque<AttachmentRequest> requests = new ArrayDeque<>();
  private AtomicInteger attachmentsDownloaded = new AtomicInteger(0);

  public AttachmentDownloaderService() {
  }

  @Override public void onCreate() {
    super.onCreate();
    handlerThread = new HandlerThread("AttachmentDownloader", Process.THREAD_PRIORITY_BACKGROUND);
    handlerThread.start();

    handler = new Handler(handlerThread.getLooper(), serviceHandlerCallback);
  }

  @Override public void onDestroy() {
    super.onDestroy();
    handlerThread.quit();
  }

  @Override public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent == null){
      return START_NOT_STICKY;
    }

    requests.push(new AttachmentRequest(intent));

    handler.sendEmptyMessage(MSG_START_DOWNLOADING_SERVICE);
    return START_STICKY;
  }

  @Override public IBinder onBind(Intent intent) {
    return new Messenger(handler).getBinder();
  }

  protected void startAttachmentDownloading(AttachmentRequest attachmentRequest) {
    Notification foregroundNotification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
{'fraction_non_alphanumeric': 0.08489333913800609, 'fraction_numerical': 0.004643738209258453, 'mean_word_length': 5.1811659192825115, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '263892', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1966, 'n_words': 430}