@Override
 protected void onPause() {
   super.onPause();
   // Clear transfer listeners to prevent memory leak, or
   // else this activity won't be garbage collected.
   if (observers != null && !observers.isEmpty()) {
     for (TransferObserver observer : observers) {
       observer.cleanTransferListener();
     }
   }
 }
  public TransferObserver processS3Service() {

    AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("123", "456"));

    TransferUtility transferManager = new TransferUtility(s3, context);
    // Starts a download
    String name = file.getName();

    final TransferObserver observer = transferManager.upload("testmaoninguo", this.hashId, file);

    observer.setTransferListener(
        new TransferListener() {

          @Override
          public void onStateChanged(int id, TransferState state) {
            Log.d("State:", "Id: " + hashId + ", " + "state: " + state.toString());
            String s3Location = "https://s3.amazonaws.com/testmaoninguo/" + hashId;
            if (state == TransferState.COMPLETED) {
              MessageDTO messagedto =
                  new MessageDTO(questionSetting.getMessageDTO().getTextMsg(), s3Location);
              questionSetting.setMessageDTO(messagedto);
              //  MessageDTO messageDTO = new MessageDTO("testText", s3Location);
              //  Question question = new Question("math", "testPerson", messageDTO);
              HttpService post =
                  new HttpService(
                      "https://easyace-api-staging.herokuapp.com/questions",
                      "POST",
                      questionSetting);
              post.start();
            }
          }

          @Override
          public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int) (bytesCurrent / bytesTotal * 100);
            Log.d("Id: ", +id + ", " + "percentage: " + percentage);
            // Display percentage transfered to user
          }

          @Override
          public void onError(int id, Exception ex) {
            Log.d("hehe", "error: ", ex);
          }
        });
    return observer;
  }
  /** Gets all relevant transfers from the Transfer Service for populating the UI */
  private void initData() {
    transferRecordMaps.clear();
    // Use TransferUtility to get all upload transfers.
    observers = transferUtility.getTransfersWithType(TransferType.UPLOAD);
    TransferListener listener = new UploadListener();
    for (TransferObserver observer : observers) {

      // For each transfer we will will create an entry in
      // transferRecordMaps which will display
      // as a single row in the UI
      HashMap<String, Object> map = new HashMap<String, Object>();
      Util.fillMap(map, observer, false);
      transferRecordMaps.add(map);

      // Sets listeners to in progress transfers
      if (TransferState.WAITING.equals(observer.getState())
          || TransferState.WAITING_FOR_NETWORK.equals(observer.getState())
          || TransferState.IN_PROGRESS.equals(observer.getState())) {
        observer.setTransferListener(listener);
      }
    }
    simpleAdapter.notifyDataSetChanged();
  }
Ejemplo n.º 4
0
 /*
  * Fills in the map with information in the observer so that it can be used
  * with a SimpleAdapter to populate the UI
  */
 public static void fillMap(
     Map<String, Object> map, TransferObserver observer, boolean isChecked) {
   int progress = (int) ((double) observer.getBytesTransferred() * 100 / observer.getBytesTotal());
   map.put("id", observer.getId());
   map.put("checked", isChecked);
   map.put("fileName", observer.getAbsoluteFilePath());
   map.put("progress", progress);
   map.put(
       "bytes",
       getBytesString(observer.getBytesTransferred())
           + "/"
           + getBytesString(observer.getBytesTotal()));
   map.put("state", observer.getState());
   map.put("percentage", progress + "%");
 }