gt:             if (BuildVars.DEBUG_VERSION) {
context: /*
   * This is the source code of Telegram for Android v. 3.x.x.
   * It is licensed under GNU GPL v. 2 or later.
   * You should have received a copy of the license in this archive (see LICENSE).
   *
   * Copyright Nikolai Kudashov, 2013-2016.
   */
  
  package org.telegram.messenger;
  
  import android.util.Log;
  
  import org.telegram.tgnet.ConnectionsManager;
  import org.telegram.tgnet.RequestDelegate;
  import org.telegram.tgnet.TLObject;
  import org.telegram.tgnet.TLRPC;
  
  import java.io.RandomAccessFile;
  import java.io.File;
  import java.nio.channels.FileChannel;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  public class FileLoadOperation {
  
      private static class RequestInfo {
          private int requestToken;
          private int offset;
          private TLRPC.TL_upload_file response;
      }
  
      private final static int stateIdle = 0;
      private final static int stateDownloading = 1;
      private final static int stateFailed = 2;
      private final static int stateFinished = 3;
  
      private final static int downloadChunkSize = 1024 * 32;
      private final static int downloadChunkSizeBig = 1024 * 128;
      private final static int maxDownloadRequests = 4;
      private final static int maxDownloadRequestsBig = 2;
      private final static int bigFileSizeFrom = 1024 * 1024;
  
      private int datacenter_id;
      private TLRPC.InputFileLocation location;
      private volatile int state = stateIdle;
      private int downloadedBytes;
      private int totalBytesCount;
      private FileLoadOperationDelegate delegate;
      private byte[] key;
      private byte[] iv;
      private int currentDownloadChunkSize;
      private int currentMaxDownloadRequests;
      private int requestsCount;
      private int renameRetryCount;
  
      private int nextDownloadOffset;
      private ArrayList<RequestInfo> requestInfos;
      private ArrayList<RequestInfo> delayedRequestInfos;
  
      private File cacheFileTemp;
      private File cacheFileFinal;
      private File cacheIvTemp;
  
      private String ext;
      private RandomAccessFile fileOutputStream;
      private RandomAccessFile fiv;
      private File storePath;
      private File tempPath;
      private boolean isForceRequest;
  
      private static String orgName = null;
  
      public interface FileLoadOperationDelegate {
          void didFinishLoadingFile(FileLoadOperation operation, File finalFile);
          void didFailedLoadingFile(FileLoadOperation operation, int state);
          void didChangedLoadProgress(FileLoadOperation operation, float progress);
      }
  
      public FileLoadOperation(TLRPC.FileLocation photoLocation, String extension, int size) {
          if (photoLocation instanceof TLRPC.TL_fileEncryptedLocation) {
              location = new TLRPC.TL_inputEncryptedFileLocation();
              location.id = photoLocation.volume_id;
              location.volume_id = photoLocation.volume_id;
              location.access_hash = photoLocation.secret;
              location.local_id = photoLocation.local_id;
              iv = new byte[32];
              System.arraycopy(photoLocation.iv, 0, iv, 0, iv.length);
              key = photoLocation.key;
              datacenter_id = photoLocation.dc_id;
          } else if (photoLocation instanceof TLRPC.TL_fileLocation) {
              location = new TLRPC.TL_inputFileLocation();
              location.volume_id = photoLocation.volume_id;
              location.secret = photoLocation.secret;
              location.local_id = photoLocation.local_id;
              datacenter_id = photoLocation.dc_id;
          }
          totalBytesCount = size;
          ext = extension != null ? extension : "jpg";
          orgName = null;
      }
  
      public FileLoadOperation(TLRPC.Document documentLocation) {
          try {
              if (documentLocation instanceof TLRPC.TL_documentEncrypted) {
                  location = new TLRPC.TL_inputEncryptedFileLocation();
                  location.id = documentLocation.id;
                  location.access_hash = documentLocation.access_hash;
                  datacenter_id = documentLocation.dc_id;
                  iv = new byte[32];
                  System.arraycopy(documentLocation.iv, 0, iv, 0, iv.length);
                  key = documentLocation.key;
              } else if (documentLocation instanceof TLRPC.TL_document) {
                  location = new TLRPC.TL_inputDocumentFileLocation();
                  location.id = documentLocation.id;
                  location.access_hash = documentLocation.access_hash;
                  datacenter_id = documentLocation.dc_id;
              }
              if (totalBytesCount <= 0) {
                  totalBytesCount = documentLocation.size;
              }
              ext = FileLoader.getDocumentFileName(documentLocation);
              int idx;
              if (ext == null || (idx = ext.lastIndexOf('.')) == -1) {
                  ext = "";
              } else {
                  ext = ext.substring(idx);
              }
              if (ext.length() <= 1) {
                  if (documentLocation.mime_type != null) {
                      switch (documentLocation.mime_type) {
                          case "video/mp4":
                              ext = ".mp4";
                              break;
                          case "audio/ogg":
                              ext = ".ogg";
                              break;
                          default:
                              ext = "";
                              break;
                      }
                  } else {
                      ext = "";
                  }
              }
  
              if(ext.length() > 1 && ApplicationLoader.KEEP_ORIGINAL_FILENAME && !ext.contains("webp") && !ext.contains(".mp4") && !ext.contains(".gif") && !ext.contains(".ogg")) {
                  orgName = FileLoader.getDocName(documentLocation);
              }
          } catch (Exception e) {
              FileLog.e("tmessages", e);
              state = stateFailed;
              cleanup();
              Utilities.stageQueue.postRunnable(new Runnable() {
                  @Override
                  public void run() {
                      delegate.didFailedLoadingFile(FileLoadOperation.this, 0);
                  }
              });
          }
      }
  
      public void setForceRequest(boolean forceRequest) {
          isForceRequest = forceRequest;
      }
  
      public boolean isForceRequest() {
          return isForceRequest;
      }
  
      public void setPaths(File store, File temp) {
          storePath = store;
          tempPath = temp;
      }
  
      public void start() {
          if (state != stateIdle) {
              return;
          }
          currentDownloadChunkSize = totalBytesCount >= bigFileSizeFrom ? downloadChunkSizeBig : downloadChunkSize;
          currentMaxDownloadRequests = totalBytesCount >= bigFileSizeFrom ? maxDownloadRequestsBig : maxDownloadRequests;
          requestInfos = new ArrayList<>(currentMaxDownloadRequests);
          delayedRequestInfos = new ArrayList<>(currentMaxDownloadRequests - 1);
          state = stateDownloading;
          if (location == null) {
              cleanup();
              Utilities.stageQueue.postRunnable(new Runnable() {
                  @Override
                  public void run() {
                      delegate.didFailedLoadingFile(FileLoadOperation.this, 0);
                  }
              });
              return;
          }
          String fileNameFinal;
          String fileNameTemp;
          String fileNameIv = null;
          if (location.volume_id != 0 && location.local_id != 0) {
              fileNameTemp = location.volume_id + "_" + location.local_id + ".temp";
              fileNameFinal = location.volume_id + "_" + location.local_id + "." + ext;
              if (key != null) {
                  fileNameIv = location.volume_id + "_" + location.local_id + ".iv";
              }
              if (datacenter_id == Integer.MIN_VALUE || location.volume_id == Integer.MIN_VALUE || datacenter_id == 0) {
                  cleanup();
                  Utilities.stageQueue.postRunnable(new Runnable() {
                      @Override
                      public void run() {
                          delegate.didFailedLoadingFile(FileLoadOperation.this, 0);
                      }
                  });
                  return;
              }
          } else {
              fileNameTemp = datacenter_id + "_" + location.id + ".temp";
              fileNameFinal = datacenter_id + "_" + location.id + ext;
              if (key != null) {
                  fileNameIv = datacenter_id + "_" + location.id + ".iv";
              }
              if (datacenter_id == 0 || location.id == 0) {
                  cleanup();
                  Utilities.stageQueue.postRunnable(new Runnable() {
                      @Override
                      public void run() {
                          delegate.didFailedLoadingFile(FileLoadOperation.this, 0);
                      }
                  });
                  return;
              }
          }
          //
          if(ApplicationLoader.KEEP_ORIGINAL_FILENAME && orgName != null){
              fileNameFinal = orgName;
          }
          //
          cacheFileFinal = new File(storePath, fileNameFinal);
          boolean exist = cacheFileFinal.exists();
          //
          if(exist && orgName != null && ApplicationLoader.KEEP_ORIGINAL_FILENAME){
              exist = false;
              cacheFileFinal.delete();
          }
          //
          if (exist && totalBytesCount != 0 && totalBytesCount != cacheFileFinal.length()) {
              cacheFileFinal.delete();
          }
  
          if (!cacheFileFinal.exists()) {
              cacheFileTemp = new File(tempPath, fileNameTemp);
              if (cacheFileTemp.exists()) {
                  downloadedBytes = (int) cacheFileTemp.length();
                  nextDownloadOffset = downloadedBytes = downloadedBytes / currentDownloadChunkSize * currentDownloadChunkSize;
              }
  
  