diff --git a/app/src/main/java/com/polar/mirror/MainActivity.java b/app/src/main/java/com/polar/mirror/MainActivity.java index 5ff1ddf..8e8ebbc 100644 --- a/app/src/main/java/com/polar/mirror/MainActivity.java +++ b/app/src/main/java/com/polar/mirror/MainActivity.java @@ -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; @@ -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); @@ -50,8 +58,6 @@ protected void onCreate(Bundle savedInstanceState) { Toast.makeText(this, toastText, Toast.LENGTH_LONG).show(); e.printStackTrace(); } - - } @Override @@ -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(); + } + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 44a6b98..7be5860 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,4 +5,5 @@ Image displaying stop-frame Tap anywhere to show actions again Can not access camera + Camera permissions was not granted. Use Settings app to grant it. \ No newline at end of file