mirror of
				https://github.com/SimpleMobileTools/Simple-Camera.git
				synced 2025-06-27 09:02:59 +02:00 
			
		
		
		
	ask for audio permission when switching to video
This commit is contained in:
		| @@ -2,6 +2,7 @@ package com.simplemobiletools.camera; | |||||||
|  |  | ||||||
| import android.Manifest; | import android.Manifest; | ||||||
| import android.content.Intent; | import android.content.Intent; | ||||||
|  | import android.content.pm.PackageManager; | ||||||
| import android.content.res.Resources; | import android.content.res.Resources; | ||||||
| import android.hardware.Camera; | import android.hardware.Camera; | ||||||
| import android.hardware.Sensor; | import android.hardware.Sensor; | ||||||
| @@ -40,11 +41,13 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen | |||||||
|  |  | ||||||
|     public static int orientation; |     public static int orientation; | ||||||
|     private static final int CAMERA_STORAGE_PERMISSION = 1; |     private static final int CAMERA_STORAGE_PERMISSION = 1; | ||||||
|  |     private static final int AUDIO_PERMISSION = 2; | ||||||
|     private static SensorManager sensorManager; |     private static SensorManager sensorManager; | ||||||
|     private Preview preview; |     private Preview preview; | ||||||
|     private int currCamera; |     private int currCamera; | ||||||
|     private boolean isFlashEnabled; |     private boolean isFlashEnabled; | ||||||
|     private boolean isPhoto; |     private boolean isPhoto; | ||||||
|  |     private boolean isAskingPermissions; | ||||||
|     private int currVideoRecTimer; |     private int currVideoRecTimer; | ||||||
|     private Handler timerHandler; |     private Handler timerHandler; | ||||||
|  |  | ||||||
| @@ -91,6 +94,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen | |||||||
|     @Override |     @Override | ||||||
|     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { | ||||||
|         super.onRequestPermissionsResult(requestCode, permissions, grantResults); |         super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||||||
|  |         isAskingPermissions = false; | ||||||
|  |  | ||||||
|         if (requestCode == CAMERA_STORAGE_PERMISSION) { |         if (requestCode == CAMERA_STORAGE_PERMISSION) { | ||||||
|             if (hasCameraAndStoragePermission()) { |             if (hasCameraAndStoragePermission()) { | ||||||
| @@ -99,6 +103,12 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen | |||||||
|                 Utils.showToast(getApplicationContext(), R.string.no_permissions); |                 Utils.showToast(getApplicationContext(), R.string.no_permissions); | ||||||
|                 finish(); |                 finish(); | ||||||
|             } |             } | ||||||
|  |         } else if (requestCode == AUDIO_PERMISSION) { | ||||||
|  |             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||||||
|  |                 toggleVideo(); | ||||||
|  |             } else { | ||||||
|  |                 Utils.showToast(getApplicationContext(), R.string.no_audio_permissions); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -167,6 +177,11 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen | |||||||
|  |  | ||||||
|     @OnClick(R.id.toggle_videocam) |     @OnClick(R.id.toggle_videocam) | ||||||
|     public void toggleVideo() { |     public void toggleVideo() { | ||||||
|  |         if (!Utils.hasAudioPermission(getApplicationContext())) { | ||||||
|  |             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, AUDIO_PERMISSION); | ||||||
|  |             isAskingPermissions = true; | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|         disableFlash(); |         disableFlash(); | ||||||
|         hideTimer(); |         hideTimer(); | ||||||
|         isPhoto = !isPhoto; |         isPhoto = !isPhoto; | ||||||
| @@ -251,7 +266,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen | |||||||
|     @Override |     @Override | ||||||
|     protected void onPause() { |     protected void onPause() { | ||||||
|         super.onPause(); |         super.onPause(); | ||||||
|         if (!hasCameraAndStoragePermission()) |         if (!hasCameraAndStoragePermission() || isAskingPermissions) | ||||||
|             return; |             return; | ||||||
|  |  | ||||||
|         hideTimer(); |         hideTimer(); | ||||||
|   | |||||||
| @@ -104,4 +104,8 @@ public class Utils { | |||||||
|     public static boolean hasStoragePermission(Context cxt) { |     public static boolean hasStoragePermission(Context cxt) { | ||||||
|         return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; |         return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static boolean hasAudioPermission(Context cxt) { | ||||||
|  |         return ContextCompat.checkSelfPermission(cxt, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ | |||||||
|     <string name="video_directory">Simple Videos</string> |     <string name="video_directory">Simple Videos</string> | ||||||
|     <string name="photo_directory">Simple Photos</string> |     <string name="photo_directory">Simple Photos</string> | ||||||
|     <string name="no_permissions">Not much to do without accessing your camera and storage</string> |     <string name="no_permissions">Not much to do without accessing your camera and storage</string> | ||||||
|  |     <string name="no_audio_permissions">We need the audio permission for recording videos</string> | ||||||
|  |  | ||||||
|     <!-- About --> |     <!-- About --> | ||||||
|     <string name="about">About</string> |     <string name="about">About</string> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user