refactor camera launcher

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parneet-guraya 2024-10-17 19:30:56 +05:30
parent 6a98923a83
commit 77cd18c560
No known key found for this signature in database
GPG key ID: 63B807C4B2A9064B
8 changed files with 50 additions and 39 deletions

View file

@ -37,7 +37,9 @@ public class BookmarkLocationsFragment extends DaggerFragment {
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> {
// TODO handle result from controller
contributionController.handleActivityResultWithCallback(requireActivity(),callbacks -> {
contributionController.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
});
private ActivityResultLauncher<Intent> galleryPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
@ -99,7 +101,6 @@ public class BookmarkLocationsFragment extends DaggerFragment {
commonPlaceClickActions,
inAppCameraLocationPermissionLauncher,
galleryPickLauncherForResult,
//TODO[Parry] just a stub, make sure if this is actually the launcher we need!!
cameraPickLauncherForResult
);

View file

@ -113,7 +113,7 @@ public class ContributionController {
public void onLocationPermissionGranted() {
if (!locationPermissionsHelper.isLocationAccessToAppsTurnedOn()) {
showLocationOffDialog(activity, R.string.in_app_camera_needs_location,
R.string.in_app_camera_location_unavailable);
R.string.in_app_camera_location_unavailable, resultLauncher);
} else {
initiateCameraUpload(activity, resultLauncher);
}
@ -138,9 +138,10 @@ public class ContributionController {
* @param activity Activity reference
* @param dialogTextResource Resource id of text to be shown in dialog
* @param toastTextResource Resource id of text to be shown in toast
* @param resultLauncher
*/
private void showLocationOffDialog(Activity activity, int dialogTextResource,
int toastTextResource) {
int toastTextResource, ActivityResultLauncher<Intent> resultLauncher) {
DialogUtil
.showAlertDialog(activity,
activity.getString(R.string.ask_to_turn_location_on),
@ -151,8 +152,7 @@ public class ContributionController {
() -> {
Toast.makeText(activity, activity.getString(toastTextResource),
Toast.LENGTH_LONG).show();
//TODO [Parry] why do we need this call???
// initiateCameraUpload(activity);
initiateCameraUpload(activity,resultLauncher);
}
);
}
@ -270,6 +270,10 @@ public class ContributionController {
FilePicker.onPictureReturnedFromCustomSelector(result,activity,callbacks);
}
public void onPictureReturnedFromCamera(ActivityResult result, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
FilePicker.onPictureReturnedFromCamera(result,activity,callbacks);
}
/**
* Attaches callback for file picker.
*/

View file

@ -114,7 +114,9 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> {
// TODO handle result from controller
controller.handleActivityResultWithCallback(requireActivity(),callbacks -> {
controller.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
});
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(

View file

@ -135,7 +135,6 @@ public class FilePicker implements Constants {
*/
public static void openCameraForImage(Activity activity, ActivityResultLauncher<Intent> resultLauncher, int type) {
Intent intent = createCameraForImageIntent(activity, type);
//TODO[Parry] we're not using the result anyways.
// activity.startActivityForResult(intent, RequestCodes.TAKE_PICTURE);
resultLauncher.launch(intent);
}
@ -179,8 +178,7 @@ public class FilePicker implements Constants {
} else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) {
// onPictureReturnedFromCustomSelector(data, activity, callbacks);
} else if (requestCode == RequestCodes.TAKE_PICTURE) {
//TODO[Parry] why handle the result , when not using it in the first place???
onPictureReturnedFromCamera(activity, callbacks);
// onPictureReturnedFromCamera(activity, callbacks);
}
} else {
if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) {
@ -337,36 +335,38 @@ public class FilePicker implements Constants {
return files;
}
private static void onPictureReturnedFromCamera(Activity activity, @NonNull FilePicker.Callbacks callbacks) {
try {
String lastImageUri = PreferenceManager.getDefaultSharedPreferences(activity).getString(KEY_PHOTO_URI, null);
if (!TextUtils.isEmpty(lastImageUri)) {
revokeWritePermission(activity, Uri.parse(lastImageUri));
}
UploadableFile photoFile = FilePicker.takenCameraPicture(activity);
List<UploadableFile> files = new ArrayList<>();
files.add(photoFile);
if (photoFile == null) {
Exception e = new IllegalStateException("Unable to get the picture returned from camera");
callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
} else {
if (configuration(activity).shouldCopyTakenPhotosToPublicGalleryAppFolder()) {
PickedFiles.copyFilesInSeparateThread(activity, singleFileList(photoFile));
public static void onPictureReturnedFromCamera(ActivityResult activityResult, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
if(activityResult.getResultCode() == Activity.RESULT_OK){
try {
String lastImageUri = PreferenceManager.getDefaultSharedPreferences(activity).getString(KEY_PHOTO_URI, null);
if (!TextUtils.isEmpty(lastImageUri)) {
revokeWritePermission(activity, Uri.parse(lastImageUri));
}
callbacks.onImagesPicked(files, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
}
UploadableFile photoFile = FilePicker.takenCameraPicture(activity);
List<UploadableFile> files = new ArrayList<>();
files.add(photoFile);
PreferenceManager.getDefaultSharedPreferences(activity)
if (photoFile == null) {
Exception e = new IllegalStateException("Unable to get the picture returned from camera");
callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
} else {
if (configuration(activity).shouldCopyTakenPhotosToPublicGalleryAppFolder()) {
PickedFiles.copyFilesInSeparateThread(activity, singleFileList(photoFile));
}
callbacks.onImagesPicked(files, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
}
PreferenceManager.getDefaultSharedPreferences(activity)
.edit()
.remove(KEY_LAST_CAMERA_PHOTO)
.remove(KEY_PHOTO_URI)
.apply();
} catch (Exception e) {
e.printStackTrace();
callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
} catch (Exception e) {
e.printStackTrace();
callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
}
}
}

View file

@ -29,7 +29,7 @@ fun placeAdapterDelegate(
onDirectionsClicked: (Place) -> Unit,
onDirectionsLongPressed: () -> Boolean,
inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>,
resultLauncher: ActivityResultLauncher<Intent>,
cameraPickLauncherForResult: ActivityResultLauncher<Intent>,
galleryPickLauncherForResult: ActivityResultLauncher<Intent>
) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent ->
ItemPlaceBinding.inflate(layoutInflater, parent, false)
@ -47,7 +47,7 @@ fun placeAdapterDelegate(
onItemClick?.invoke(item)
}
}
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, resultLauncher) }
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, cameraPickLauncherForResult) }
nearbyButtonLayout.cameraButton.setOnLongClickListener { onCameraLongPressed() }
nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item,galleryPickLauncherForResult) }

View file

@ -243,7 +243,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> {
// TODO handle result from controller
controller.handleActivityResultWithCallback(requireActivity(),callbacks -> {
controller.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
});
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(

View file

@ -14,7 +14,7 @@ class PlaceAdapter(
commonPlaceClickActions: CommonPlaceClickActions,
inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>,
galleryPickLauncherForResult: ActivityResultLauncher<Intent>,
resultLauncherStub: ActivityResultLauncher<Intent>
cameraPickLauncherForResult: ActivityResultLauncher<Intent>
) : BaseDelegateAdapter<Place>(
placeAdapterDelegate(
bookmarkLocationsDao,
@ -30,7 +30,7 @@ class PlaceAdapter(
commonPlaceClickActions.onDirectionsClicked(),
commonPlaceClickActions.onDirectionsLongPressed(),
inAppCameraLocationPermissionLauncher,
resultLauncherStub,
cameraPickLauncherForResult,
galleryPickLauncherForResult
),
areItemsTheSame = { oldItem, newItem -> oldItem.wikiDataEntityId == newItem.wikiDataEntityId },

View file

@ -89,7 +89,9 @@ public class SettingsFragment extends PreferenceFragmentCompat {
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> {
// TODO handle result from controller
contributionController.handleActivityResultWithCallback(requireActivity(),callbacks -> {
contributionController.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
});
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {