content: package com.afei.camera2getpreview;
  
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.afei.camera2getpreview.util.Permission;
  import com.afei.camera2getpreview.util.PermissionDialog;
  
  public class MainActivity extends AppCompatActivity {
  
      private CameraFragment mCameraFragment;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          Permission.checkPermission(this);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          if (Permission.isPermissionGranted(this)) {
              initCameraFragment();
          }
      }
  
      private void initCameraFragment() {
          if (mCameraFragment == null) {
              mCameraFragment = new CameraFragment();
              getSupportFragmentManager()
                      .beginTransaction()
                      .replace(R.id.fragment_container, mCameraFragment)
                      .commit();
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                             @NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          if (requestCode == Permission.REQUEST_CODE) {
              for (int grantResult : grantResults) {
                  if (grantResult != PackageManager.PERMISSION_GRANTED) {
                      showPermissionDenyDialog();
                      return;
                  }
              }
          }
      }
  
      private void showPermissionDenyDialog() {
          PermissionDialog dialog = new PermissionDialog();
          dialog.show(getSupportFragmentManager(), "PermissionDeny");
      }
  
  }
  