properly handler Camera and Storage permissions
This commit is contained in:
parent
8cde453b9f
commit
d57ea88abc
|
@ -1,5 +1,6 @@
|
||||||
package com.simplemobiletools.camera;
|
package com.simplemobiletools.camera;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
|
@ -9,6 +10,7 @@ import android.hardware.SensorEventListener;
|
||||||
import android.hardware.SensorManager;
|
import android.hardware.SensorManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -21,6 +23,9 @@ import android.widget.TextView;
|
||||||
|
|
||||||
import com.simplemobiletools.camera.Preview.PreviewListener;
|
import com.simplemobiletools.camera.Preview.PreviewListener;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
|
@ -34,6 +39,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||||
@BindView(R.id.video_rec_curr_timer) TextView recCurrTimer;
|
@BindView(R.id.video_rec_curr_timer) TextView recCurrTimer;
|
||||||
|
|
||||||
public static int orientation;
|
public static int orientation;
|
||||||
|
private static final int CAMERA_STORAGE_PERMISSION = 1;
|
||||||
private static SensorManager sensorManager;
|
private static SensorManager sensorManager;
|
||||||
private Preview preview;
|
private Preview preview;
|
||||||
private int currCamera;
|
private int currCamera;
|
||||||
|
@ -47,9 +53,28 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
|
tryInitCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tryInitCamera() {
|
||||||
|
if (hasCameraAndStoragePermission()) {
|
||||||
|
initializeCamera();
|
||||||
|
} else {
|
||||||
|
final List<String> permissions = new ArrayList<>(2);
|
||||||
|
if (!Utils.hasCameraPermission(getApplicationContext())) {
|
||||||
|
permissions.add(Manifest.permission.CAMERA);
|
||||||
|
}
|
||||||
|
if (!Utils.hasStoragePermission(getApplicationContext())) {
|
||||||
|
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
|
}
|
||||||
|
ActivityCompat.requestPermissions(this, permissions.toArray(new String[permissions.size()]), CAMERA_STORAGE_PERMISSION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeCamera() {
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
currCamera = Camera.CameraInfo.CAMERA_FACING_BACK;
|
||||||
preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView), this);
|
preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView), this);
|
||||||
preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||||
|
@ -59,6 +84,24 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||||
timerHandler = new Handler();
|
timerHandler = new Handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasCameraAndStoragePermission() {
|
||||||
|
return Utils.hasCameraPermission(getApplicationContext()) && Utils.hasStoragePermission(getApplicationContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
|
||||||
|
if (requestCode == CAMERA_STORAGE_PERMISSION) {
|
||||||
|
if (hasCameraAndStoragePermission()) {
|
||||||
|
initializeCamera();
|
||||||
|
} else {
|
||||||
|
Utils.showToast(getApplicationContext(), R.string.no_permissions);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@OnClick(R.id.toggle_camera)
|
@OnClick(R.id.toggle_camera)
|
||||||
public void toggleCamera() {
|
public void toggleCamera() {
|
||||||
disableFlash();
|
disableFlash();
|
||||||
|
@ -180,11 +223,17 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
if (hasCameraAndStoragePermission()) {
|
||||||
|
resumeCameraItems();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resumeCameraItems() {
|
||||||
final int cnt = Camera.getNumberOfCameras();
|
final int cnt = Camera.getNumberOfCameras();
|
||||||
if (cnt == 1) {
|
if (cnt == 1) {
|
||||||
toggleCameraBtn.setVisibility(View.INVISIBLE);
|
toggleCameraBtn.setVisibility(View.INVISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
preview.setCamera(currCamera);
|
preview.setCamera(currCamera);
|
||||||
hideNavigationBarIcons();
|
hideNavigationBarIcons();
|
||||||
|
|
||||||
|
@ -202,6 +251,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
if (!hasCameraAndStoragePermission())
|
||||||
|
return;
|
||||||
|
|
||||||
hideTimer();
|
hideTimer();
|
||||||
if (preview != null) {
|
if (preview != null) {
|
||||||
preview.releaseCamera();
|
preview.releaseCamera();
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package com.simplemobiletools.camera;
|
package com.simplemobiletools.camera;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
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.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -93,4 +96,12 @@ public class Utils {
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean hasCameraPermission(Context cxt) {
|
||||||
|
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean hasStoragePermission(Context cxt) {
|
||||||
|
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<string name="video_creating_error">An error occurred at creating the video file</string>
|
<string name="video_creating_error">An error occurred at creating the video file</string>
|
||||||
<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>
|
||||||
|
|
||||||
<!-- About -->
|
<!-- About -->
|
||||||
<string name="about">About</string>
|
<string name="about">About</string>
|
||||||
|
|
Loading…
Reference in New Issue