mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	refactor camera launcher
Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
		
							parent
							
								
									6a98923a83
								
							
						
					
					
						commit
						77cd18c560
					
				
					 8 changed files with 50 additions and 39 deletions
				
			
		|  | @ -37,7 +37,9 @@ public class BookmarkLocationsFragment extends DaggerFragment { | ||||||
| 
 | 
 | ||||||
|     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), |     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), | ||||||
|         result -> { |         result -> { | ||||||
|             // TODO handle result from controller |             contributionController.handleActivityResultWithCallback(requireActivity(),callbacks -> { | ||||||
|  |                 contributionController.onPictureReturnedFromCamera(result,requireActivity(),callbacks); | ||||||
|  |             }); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|       private ActivityResultLauncher<Intent> galleryPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), |       private ActivityResultLauncher<Intent> galleryPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), | ||||||
|  | @ -99,7 +101,6 @@ public class BookmarkLocationsFragment extends DaggerFragment { | ||||||
|             commonPlaceClickActions, |             commonPlaceClickActions, | ||||||
|             inAppCameraLocationPermissionLauncher, |             inAppCameraLocationPermissionLauncher, | ||||||
|             galleryPickLauncherForResult, |             galleryPickLauncherForResult, | ||||||
|             //TODO[Parry] just a stub, make sure if this is actually the launcher we need!! |  | ||||||
|             cameraPickLauncherForResult |             cameraPickLauncherForResult | ||||||
| 
 | 
 | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|  | @ -113,7 +113,7 @@ public class ContributionController { | ||||||
|             public void onLocationPermissionGranted() { |             public void onLocationPermissionGranted() { | ||||||
|                 if (!locationPermissionsHelper.isLocationAccessToAppsTurnedOn()) { |                 if (!locationPermissionsHelper.isLocationAccessToAppsTurnedOn()) { | ||||||
|                     showLocationOffDialog(activity, R.string.in_app_camera_needs_location, |                     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 { |                 } else { | ||||||
|                     initiateCameraUpload(activity, resultLauncher); |                     initiateCameraUpload(activity, resultLauncher); | ||||||
|                 } |                 } | ||||||
|  | @ -138,9 +138,10 @@ public class ContributionController { | ||||||
|      * @param activity           Activity reference |      * @param activity           Activity reference | ||||||
|      * @param dialogTextResource Resource id of text to be shown in dialog |      * @param dialogTextResource Resource id of text to be shown in dialog | ||||||
|      * @param toastTextResource  Resource id of text to be shown in toast |      * @param toastTextResource  Resource id of text to be shown in toast | ||||||
|  |      * @param resultLauncher | ||||||
|      */ |      */ | ||||||
|     private void showLocationOffDialog(Activity activity, int dialogTextResource, |     private void showLocationOffDialog(Activity activity, int dialogTextResource, | ||||||
|         int toastTextResource) { |         int toastTextResource, ActivityResultLauncher<Intent> resultLauncher) { | ||||||
|         DialogUtil |         DialogUtil | ||||||
|             .showAlertDialog(activity, |             .showAlertDialog(activity, | ||||||
|                 activity.getString(R.string.ask_to_turn_location_on), |                 activity.getString(R.string.ask_to_turn_location_on), | ||||||
|  | @ -151,8 +152,7 @@ public class ContributionController { | ||||||
|                 () -> { |                 () -> { | ||||||
|                     Toast.makeText(activity, activity.getString(toastTextResource), |                     Toast.makeText(activity, activity.getString(toastTextResource), | ||||||
|                         Toast.LENGTH_LONG).show(); |                         Toast.LENGTH_LONG).show(); | ||||||
|                     //TODO [Parry] why do we need this call??? |                     initiateCameraUpload(activity,resultLauncher); | ||||||
| //                    initiateCameraUpload(activity); |  | ||||||
|                 } |                 } | ||||||
|             ); |             ); | ||||||
|     } |     } | ||||||
|  | @ -270,6 +270,10 @@ public class ContributionController { | ||||||
|         FilePicker.onPictureReturnedFromCustomSelector(result,activity,callbacks); |         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. |      * Attaches callback for file picker. | ||||||
|      */ |      */ | ||||||
|  |  | ||||||
|  | @ -114,7 +114,9 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl | ||||||
| 
 | 
 | ||||||
|     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), |     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), | ||||||
|         result -> { |         result -> { | ||||||
|             // TODO handle result from controller |             controller.handleActivityResultWithCallback(requireActivity(),callbacks -> { | ||||||
|  |                 controller.onPictureReturnedFromCamera(result,requireActivity(),callbacks); | ||||||
|  |             }); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult( |     private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult( | ||||||
|  |  | ||||||
|  | @ -135,7 +135,6 @@ public class FilePicker implements Constants { | ||||||
|      */ |      */ | ||||||
|     public static void openCameraForImage(Activity activity, ActivityResultLauncher<Intent> resultLauncher, int type) { |     public static void openCameraForImage(Activity activity, ActivityResultLauncher<Intent> resultLauncher, int type) { | ||||||
|         Intent intent = createCameraForImageIntent(activity, type); |         Intent intent = createCameraForImageIntent(activity, type); | ||||||
|         //TODO[Parry] we're not using the result anyways. |  | ||||||
| //        activity.startActivityForResult(intent, RequestCodes.TAKE_PICTURE); | //        activity.startActivityForResult(intent, RequestCodes.TAKE_PICTURE); | ||||||
|         resultLauncher.launch(intent); |         resultLauncher.launch(intent); | ||||||
|     } |     } | ||||||
|  | @ -179,8 +178,7 @@ public class FilePicker implements Constants { | ||||||
|                     } else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) { |                     } else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) { | ||||||
| //                        onPictureReturnedFromCustomSelector(data, activity, callbacks); | //                        onPictureReturnedFromCustomSelector(data, activity, callbacks); | ||||||
|                     } else if (requestCode == RequestCodes.TAKE_PICTURE) { |                     } 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 { |                 } else { | ||||||
|                     if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) { |                     if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) { | ||||||
|  | @ -337,36 +335,38 @@ public class FilePicker implements Constants { | ||||||
|         return files; |         return files; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static void onPictureReturnedFromCamera(Activity activity, @NonNull FilePicker.Callbacks callbacks) { |     public static void onPictureReturnedFromCamera(ActivityResult activityResult, Activity activity, @NonNull FilePicker.Callbacks callbacks) { | ||||||
|         try { |         if(activityResult.getResultCode() == Activity.RESULT_OK){ | ||||||
|             String lastImageUri = PreferenceManager.getDefaultSharedPreferences(activity).getString(KEY_PHOTO_URI, null); |             try { | ||||||
|             if (!TextUtils.isEmpty(lastImageUri)) { |                 String lastImageUri = PreferenceManager.getDefaultSharedPreferences(activity).getString(KEY_PHOTO_URI, null); | ||||||
|                 revokeWritePermission(activity, Uri.parse(lastImageUri)); |                 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)); |  | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 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() |                     .edit() | ||||||
|                     .remove(KEY_LAST_CAMERA_PHOTO) |                     .remove(KEY_LAST_CAMERA_PHOTO) | ||||||
|                     .remove(KEY_PHOTO_URI) |                     .remove(KEY_PHOTO_URI) | ||||||
|                     .apply(); |                     .apply(); | ||||||
|         } catch (Exception e) { |             } catch (Exception e) { | ||||||
|             e.printStackTrace(); |                 e.printStackTrace(); | ||||||
|             callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity)); |                 callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity)); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -29,7 +29,7 @@ fun placeAdapterDelegate( | ||||||
|     onDirectionsClicked: (Place) -> Unit, |     onDirectionsClicked: (Place) -> Unit, | ||||||
|     onDirectionsLongPressed: () -> Boolean, |     onDirectionsLongPressed: () -> Boolean, | ||||||
|     inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>, |     inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>, | ||||||
|     resultLauncher: ActivityResultLauncher<Intent>, |     cameraPickLauncherForResult: ActivityResultLauncher<Intent>, | ||||||
|     galleryPickLauncherForResult: ActivityResultLauncher<Intent> |     galleryPickLauncherForResult: ActivityResultLauncher<Intent> | ||||||
| ) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent -> | ) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent -> | ||||||
|     ItemPlaceBinding.inflate(layoutInflater, parent, false) |     ItemPlaceBinding.inflate(layoutInflater, parent, false) | ||||||
|  | @ -47,7 +47,7 @@ fun placeAdapterDelegate( | ||||||
|                 onItemClick?.invoke(item) |                 onItemClick?.invoke(item) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, resultLauncher) } |         nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, cameraPickLauncherForResult) } | ||||||
|         nearbyButtonLayout.cameraButton.setOnLongClickListener { onCameraLongPressed() } |         nearbyButtonLayout.cameraButton.setOnLongClickListener { onCameraLongPressed() } | ||||||
| 
 | 
 | ||||||
|         nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item,galleryPickLauncherForResult) } |         nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item,galleryPickLauncherForResult) } | ||||||
|  |  | ||||||
|  | @ -243,7 +243,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment | ||||||
| 
 | 
 | ||||||
|     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), |     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), | ||||||
|         result -> { |         result -> { | ||||||
|             // TODO handle result from controller |             controller.handleActivityResultWithCallback(requireActivity(),callbacks -> { | ||||||
|  |                 controller.onPictureReturnedFromCamera(result,requireActivity(),callbacks); | ||||||
|  |             }); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult( |     private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult( | ||||||
|  |  | ||||||
|  | @ -14,7 +14,7 @@ class PlaceAdapter( | ||||||
|     commonPlaceClickActions: CommonPlaceClickActions, |     commonPlaceClickActions: CommonPlaceClickActions, | ||||||
|     inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>, |     inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>, | ||||||
|     galleryPickLauncherForResult: ActivityResultLauncher<Intent>, |     galleryPickLauncherForResult: ActivityResultLauncher<Intent>, | ||||||
|     resultLauncherStub: ActivityResultLauncher<Intent> |     cameraPickLauncherForResult: ActivityResultLauncher<Intent> | ||||||
| ) : BaseDelegateAdapter<Place>( | ) : BaseDelegateAdapter<Place>( | ||||||
|         placeAdapterDelegate( |         placeAdapterDelegate( | ||||||
|             bookmarkLocationsDao, |             bookmarkLocationsDao, | ||||||
|  | @ -30,7 +30,7 @@ class PlaceAdapter( | ||||||
|             commonPlaceClickActions.onDirectionsClicked(), |             commonPlaceClickActions.onDirectionsClicked(), | ||||||
|             commonPlaceClickActions.onDirectionsLongPressed(), |             commonPlaceClickActions.onDirectionsLongPressed(), | ||||||
|             inAppCameraLocationPermissionLauncher, |             inAppCameraLocationPermissionLauncher, | ||||||
|             resultLauncherStub, |             cameraPickLauncherForResult, | ||||||
|             galleryPickLauncherForResult |             galleryPickLauncherForResult | ||||||
|         ), |         ), | ||||||
|         areItemsTheSame = { oldItem, newItem -> oldItem.wikiDataEntityId == newItem.wikiDataEntityId }, |         areItemsTheSame = { oldItem, newItem -> oldItem.wikiDataEntityId == newItem.wikiDataEntityId }, | ||||||
|  |  | ||||||
|  | @ -89,7 +89,9 @@ public class SettingsFragment extends PreferenceFragmentCompat { | ||||||
| 
 | 
 | ||||||
|     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), |     private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), | ||||||
|         result -> { |         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>>() { |     private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 parneet-guraya
						parneet-guraya