content: /**
   * Copyright 2018 Alibaba Group
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alibaba.android.bindingx.playground.weex;
  
  import android.Manifest;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.ContextCompat;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.widget.Toast;
  
  public class PGMainActivity extends WXActivity implements View.OnClickListener{
      private static final int CAMERA_PERMISSION_REQUEST_CODE = 0x1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setActionBarLeftIconDrawable(R.drawable.scan);
          Toolbar toolbar = getToolbar();
          if(toolbar != null) {
              toolbar.setNavigationOnClickListener(this);
          }
      }
  
      @Override
      public void onClick(View v) {
          performScan();
      }
  
      private void performScan() {
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
              if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
                  Toast.makeText(this, "We need camera permission to scan QRCode", Toast.LENGTH_LONG).show();
                  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
              } else {
                  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
              }
          } else {
              startActivity(new Intent(PGMainActivity.this, QRCodeCaptureActivity.class));
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              startActivity(new Intent(PGMainActivity.this, QRCodeCaptureActivity.class));
          } else {
              Toast.makeText(this, "permission denied", Toast.LENGTH_SHORT).show();
          }
      }
  }
  