@@ -0,0 +1,3407 @@
+/*%0A * Copyright 2020 Google LLC%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     https://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.google.android.gnd.system;%0A%0Aimport android.graphics.Bitmap;%0Aimport android.util.Log;%0Aimport com.google.firebase.storage.FirebaseStorage;%0Aimport com.google.firebase.storage.StorageReference;%0Aimport com.google.firebase.storage.UploadTask;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;%0Aimport java.util.Locale;%0Aimport javax.inject.Inject;%0Aimport javax.inject.Singleton;%0A%0A@Singleton%0Apublic class MediaUploadManager %7B%0A%0A  private static final String TAG = MediaUploadManager.class.getName();%0A  private static final String MEDIA_ROOT_DIR = %22uploaded_media%22;%0A  private final SimpleDateFormat DATE_FORMAT =%0A      new SimpleDateFormat(%22yyyyMMddHHmmss%22, Locale.getDefault());%0A%0A  @Inject%0A  MediaUploadManager() %7B%7D%0A%0A  /** Returns a reference to the default Storage bucket. */%0A  private FirebaseStorage getStorage() %7B%0A    return FirebaseStorage.getInstance();%0A  %7D%0A%0A  /** Returns a reference to the root media dir. */%0A  private StorageReference getRootMediaDir() %7B%0A    return getStorage().getReference().child(MEDIA_ROOT_DIR);%0A  %7D%0A%0A  /**%0A   * Returns a reference to a object under the root media dir.%0A   *%0A   * @param fileName Name of the uploaded media%0A   */%0A  private StorageReference createReference(String fileName) %7B%0A    return getRootMediaDir().child(fileName + '-' + getFilenameSuffix());%0A  %7D%0A%0A  /** Converts current timestamp to a string to be used a suffix for uploading media. */%0A  private String getFilenameSuffix() %7B%0A    return DATE_FORMAT.format(new Date());%0A  %7D%0A%0A  public void uploadMedia(Bitmap bitmap, String fileName) %7B%0A    ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);%0A    byte%5B%5D data = baos.toByteArray();%0A%0A    uploadTask(createReference(fileName).putBytes(data), fileName);%0A  %7D%0A%0A  private void uploadTask(UploadTask uploadTask, String fileName) %7B%0A    uploadTask%0A        .addOnCanceledListener(%0A            () -%3E %7B%0A              Log.d(TAG, String.format(%22Uploading canceled: %25s%22, fileName));%0A            %7D)%0A        .addOnCompleteListener(%0A            task -%3E %7B%0A              Log.d(TAG, String.format(%22Uploading completed: %25s%22, fileName));%0A            %7D)%0A        .addOnFailureListener(%0A            e -%3E %7B%0A              Log.d(TAG, String.format(%22Uploading failed: %25s%22, fileName), e);%0A            %7D)%0A        .addOnSuccessListener(%0A            taskSnapshot -%3E %7B%0A              Log.d(TAG, String.format(%22Uploading succeeded: %25s%22, fileName));%0A            %7D)%0A        .addOnProgressListener(%0A            taskSnapshot -%3E %7B%0A              double percentCompleted =%0A                  100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount();%0A              Log.d(TAG, String.format(%22Uploading in progress: %25s %25f%22, fileName, percentCompleted));%0A            %7D);%0A  %7D%0A%7D%0A

Add MediaUploadManager