Skip to content

Commit

Permalink
Request camera permission
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Nov 20, 2023
1 parent ce55275 commit fd0d8c3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
36 changes: 34 additions & 2 deletions app/src/main/java/com/polar/mirror/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import androidx.camera.core.Preview;
import androidx.camera.view.PreviewView;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.Manifest;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

Expand All @@ -23,12 +28,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private FreezeController mFreezeController;
private ActionPanelController mActionPanelController;
private final static String TAG = "MainActivity";
private static final int CAMERA_PERMISSION_CODE = 858;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

requestCameraPermissionIfNeeded();

View panelView = findViewById(R.id.action_panel_layout);
View overlayView = findViewById(R.id.overlay_view);
mActionPanelController = new ActionPanelController(this, panelView, overlayView);
Expand All @@ -50,8 +58,6 @@ protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
e.printStackTrace();
}


}

@Override
Expand Down Expand Up @@ -149,5 +155,31 @@ public void onClick(@NonNull View v) {
Log.w(TAG, "Unknown id of view: " + viewId);
}
}

public void requestCameraPermissionIfNeeded() {
if(Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Camera permission is granted.");
return;
}
Log.d(TAG, "Camera permission is not granted yet, so will request it now");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
if (!(grantResults.length > 0) || grantResults[0] != PackageManager.PERMISSION_GRANTED){
final String toastText = getString(R.string.no_camera_permissions);
Log.d(TAG, "User denied camera permission");
Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
}
}
}
}

1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<string name="stop_view_desc">Image displaying stop-frame</string>
<string name="tap_to_show_actions">Tap anywhere to show actions again</string>
<string name="can_not_start_camera">Can not access camera</string>
<string name="no_camera_permissions">Camera permissions was not granted. Use Settings app to grant it.</string>
</resources>

0 comments on commit fd0d8c3

Please sign in to comment.