/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gnd.system;

import android.Manifest.permission;
import android.content.Intent;
import android.util.Log;
import com.google.android.gnd.rx.RxCompletable;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class StorageManager {

  public static final String TAG = StorageManager.class.getName();

  private static final int PICKFILE_REQUEST_CODE = StorageManager.class.hashCode() & 0xffff;
  private final PermissionsManager permissionsManager;
  private final ActivityStreams activityStreams;

  @Inject
  public StorageManager(PermissionsManager permissionsManager, ActivityStreams activityStreams) {
    this.permissionsManager = permissionsManager;
    this.activityStreams = activityStreams;
  }

  public void imagePicker() {
    permissionsManager
        .obtainPermission(permission.READ_EXTERNAL_STORAGE)
        .andThen(RxCompletable.completeIf(this::sendIntent))
        .andThen(
            activityStreams
                .getNextActivityResult(PICKFILE_REQUEST_CODE)
                .doOnNext(
                    activityResult -> {
                      if (activityResult.isOk()) {
                        Intent intent = activityResult.getData();
                        if (intent != null) {
                          Log.d(TAG, activityResult.getData().getData() + " = uri");
                        }
                      } else if (activityResult.isCanceled()) {
                        Log.d(TAG, "file picker canceled");
                      }
                    }))
        .subscribe();
  }

  private boolean sendIntent() {
    Log.d(TAG, "Sending file picker intent");
    activityStreams.withActivity(
        activity -> {
          Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
          intent.setType("image/*");
          activity.startActivityForResult(intent, PICKFILE_REQUEST_CODE);
        });
    return true;
  }
}

@@ -732,36 +732,21 @@
 ort 
-com.google.android.gnd.rx.Rx
+io.reactivex.
 Comp
@@ -1463,49 +1463,30 @@
 hen(
-RxCompletable.completeIf(this::send
+sendImagePicker
 Intent
+(
 ))%0A 
@@ -2146,77 +2146,103 @@
 ate 
-boolean sendIntent() %7B%0A    Log.d(TAG, %22Sending file picker intent%22);%0A
+Completable sendImagePickerIntent() %7B%0A    return Completable.fromAction(%0A        () -%3E%0A        
     
@@ -2271,32 +2271,40 @@
 tivity(%0A        
+    
+    
 activity -%3E %7B%0A  
@@ -2297,24 +2297,32 @@
 tivity -%3E %7B%0A
+        
           In
@@ -2384,16 +2384,24 @@
         
+        
 intent.s
@@ -2421,32 +2421,40 @@
 /*%22);%0A          
+     
+   
 activity.startAc
@@ -2513,27 +2513,76 @@
     
-%7D);%0A    return true
+          Log.d(TAG, %22file picker intent sent%22);%0A                %7D))
 ;%0A  

Use Completable instead of completeIf