package fr.free.nrw.commons.nearby;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionController;
import timber.log.Timber;

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

class DirectUpload {

    private String title;
    private String desc;
    private ContributionController controller;
    private Fragment fragment;

    DirectUpload(String title, String desc, Fragment fragment, ContributionController controller) {
        this.title = title;
        this.desc = desc;
        this.fragment = fragment;
        this.controller = controller;
    }

    void storeSharedPrefs() {
        SharedPreferences sharedPref = fragment.getActivity().getSharedPreferences("Direct Upload",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();

        editor.putString("Title", title);
        editor.putString("Desc", desc);
        editor.apply();
    }

    void initiateGalleryUpload() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(fragment.getActivity(), READ_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
                if (fragment.getActivity().shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)) {
                    new AlertDialog.Builder(fragment.getActivity())
                            .setMessage(fragment.getActivity().getString(R.string.read_storage_permission_rationale))
                            .setPositiveButton("OK", (dialog, which) -> {
                                fragment.getActivity().requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, 1);
                                dialog.dismiss();
                            })
                            .setNegativeButton("Cancel", null)
                            .create()
                            .show();
                } else {
                    fragment.getActivity().requestPermissions(new String[]{READ_EXTERNAL_STORAGE},
                            1);
                }
            } else {
                controller.startGalleryPick();
            }
        }
        else {
            controller.startGalleryPick();
        }
    }
}

@@ -407,16 +407,72 @@
 ialog;%0A%0A
+import javax.inject.Inject;%0Aimport javax.inject.Named;%0A%0A
 import f
@@ -493,16 +493,16 @@
 mons.R;%0A
-
 import f
@@ -650,16 +650,82 @@
 TORAGE;%0A
+import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;%0A
 import s
@@ -937,16 +937,144 @@
 gment;%0A%0A
+    @Inject @Named(%22prefs%22) SharedPreferences prefs;%0A    @Inject @Named(%22default_preferences%22) SharedPreferences defaultPrefs;%0A%0A
     Dire
@@ -1594,16 +1594,16 @@
  desc);%0A
-
         
@@ -1617,32 +1617,1381 @@
 apply();%0A    %7D%0A%0A
+    void initiateCameraUpload() %7B%0A        boolean useExtStorage = defaultPrefs.getBoolean(%22useExternalStorage%22, true);%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M && useExtStorage) %7B%0A            if (ContextCompat.checkSelfPermission(fragment.getActivity(), WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) %7B%0A                if (fragment.getActivity().shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) %7B%0A                    new AlertDialog.Builder(fragment.getActivity())%0A                            .setMessage(fragment.getActivity().getString(R.string.write_storage_permission_rationale))%0A                            .setPositiveButton(%22OK%22, (dialog, which) -%3E %7B%0A                                fragment.getActivity().requestPermissions(new String%5B%5D%7BWRITE_EXTERNAL_STORAGE%7D, 3);%0A                                dialog.dismiss();%0A                            %7D)%0A                            .setNegativeButton(%22Cancel%22, null)%0A                            .create()%0A                            .show();%0A                %7D else %7B%0A                    fragment.getActivity().requestPermissions(new String%5B%5D%7BWRITE_EXTERNAL_STORAGE%7D, 3);%0A                %7D%0A            %7D else %7B%0A                controller.startCameraCapture();%0A            %7D%0A        %7D else %7B%0A            controller.startCameraCapture();%0A        %7D%0A    %7D%0A%0A
     void initiat

Copy camera capture code to DirectUpload.java