1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.example.cannylive; 17 18 import android.Manifest; 19 import android.app.Activity; 20 import android.content.pm.ActivityInfo; 21 import android.content.pm.PackageManager; 22 import android.os.Bundle; 23 import androidx.core.app.ActivityCompat; 24 import androidx.core.content.ContextCompat; 25 import android.util.Log; 26 import android.view.View; 27 import android.widget.Button; 28 import android.widget.SeekBar; 29 import android.widget.TextView; 30 import android.widget.Toast; 31 import android.widget.ViewFlipper; 32 33 import com.android.example.cannylive.R; 34 35 import java.text.DecimalFormat; 36 import java.util.Timer; 37 import java.util.TimerTask; 38 39 /** 40 * Main Activity for this app 41 * It presents a ui for setting ISO, Shutter speed, and focus 42 */ 43 public class MainActivity extends Activity { 44 private static final String TAG = "MainActivity"; 45 private static final long ONE_SECOND = 1000000000; 46 private CameraView mPreviewView; 47 48 49 private Timer mTimer; 50 51 @Override onCreate(Bundle savedInstanceState)52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(new TextView(this)); 55 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 56 57 checkPermission(); 58 } onPermissionOK()59 protected void onPermissionOK() { 60 61 setContentView(R.layout.activity_main); 62 mPreviewView = (CameraView) findViewById(R.id.preview); 63 64 mTimer = new Timer(); 65 66 mTimer.scheduleAtFixedRate(new TimerTask() { 67 68 @Override 69 public void run() { 70 71 runOnUiThread(new Runnable() { 72 DecimalFormat df = new DecimalFormat("##.##"); 73 public void run() { 74 setTitle("RS Camera (" + df.format( mPreviewView.getFps()) + "fps)"); 75 } 76 }); 77 78 } 79 }, 250, 250); 80 } 81 82 public static final int PERMISSION_CHECK_ID = 22; checkPermission()83 private void checkPermission() { 84 String []permission = { 85 Manifest.permission.CAMERA, 86 Manifest.permission.RECORD_AUDIO, 87 Manifest.permission.WRITE_EXTERNAL_STORAGE, 88 Manifest.permission.READ_EXTERNAL_STORAGE 89 }; 90 boolean permission_ok = true; 91 boolean []permissionDenied = new boolean[permission.length]; 92 for (int i = 0; i < permission.length; i++) { 93 String per = permission[i]; 94 int permissionCheck = ContextCompat.checkSelfPermission(this, 95 permission[i] ); 96 permissionDenied[i] = permissionCheck != PackageManager.PERMISSION_GRANTED; 97 if (permissionDenied[i]) { 98 permission_ok = false; 99 } 100 } 101 Log.v(TAG, "Permission ok = " + permission_ok); 102 103 if (! permission_ok ) { 104 boolean request = false; 105 for (int i = 0; i < permissionDenied.length; i++) { 106 if ( permissionDenied[i]); 107 if (ActivityCompat.shouldShowRequestPermissionRationale(this, 108 permission[i])) { 109 request = true; 110 } 111 } 112 if (request) { 113 Toast.makeText(this, "We need this permission", Toast.LENGTH_LONG).show(); 114 finish(); 115 } else { 116 ActivityCompat.requestPermissions(this, 117 permission, 118 PERMISSION_CHECK_ID); 119 120 } 121 } else { 122 onPermissionOK(); 123 } 124 } onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)125 public void onRequestPermissionsResult(int requestCode, 126 String permissions[], int[] grantResults) { 127 switch (requestCode) { 128 case PERMISSION_CHECK_ID: { 129 // If request is cancelled, the result arrays are empty. 130 if (grantResults.length > 0 131 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 132 133 onPermissionOK(); 134 135 } else { 136 Toast.makeText(this, "We need permissions", Toast.LENGTH_LONG).show(); 137 138 finish(); 139 } 140 return; 141 } 142 143 // other 'case' lines to check for other 144 // permissions this app might request 145 } 146 } 147 @Override onResume()148 protected void onResume() { 149 super.onResume(); 150 if (mPreviewView != null) { 151 mPreviewView.resume(this); 152 } 153 } 154 changeEffectMode(View v)155 public void changeEffectMode(View v) { 156 mPreviewView.changeEffectMode(); 157 } 158 159 @Override onPause()160 protected void onPause() { 161 Log.v(TAG,">>>>>>>>>>>>>> onPause()"); 162 super.onPause(); 163 if (mPreviewView != null) { 164 mPreviewView.pause(); 165 } 166 } 167 168 @Override onStop()169 protected void onStop() { 170 Log.v(TAG,">>>>>>>>>>>>>> onStop()"); 171 super.onStop(); 172 } 173 174 @Override onDestroy()175 protected void onDestroy() { 176 Log.v(TAG,">>>>>>>>>>>>>> onDestroy()"); 177 178 super.onDestroy(); 179 } 180 capture(View v)181 public void capture(View v) { 182 mPreviewView.takePicture(mPreviewView.getEffect()); 183 } 184 185 } 186