Skip to content

Commit

Permalink
Coverity fixes for MultiCameraApplication
Browse files Browse the repository at this point in the history
Coverity Issues identified due to resource leakages and use of
undeprecated apis

Made use of correct apis to fix security issues and also resolved other
coverity issues

Tracked-On:OAM-129876
Signed-off-by: NaveenVenturi1203 <[email protected]>
  • Loading branch information
NaveenVenturi1203 committed Feb 18, 2025
1 parent 4e3a887 commit 844ae59
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 151 deletions.
1 change: 1 addition & 0 deletions camera/MultiCameraApplication/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.BROADCAST_STCIKY" />

<uses-feature android:name="android.hardware.camera2.full"/>
<uses-feature android:name="android.hardware.usb.host" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -85,6 +86,10 @@ public class CameraBase {
private SharedPreferences settings;
private SurfaceTexture mSurfaceTexture;
private String Capture_Key, Video_key, SettingsKey;
private Uri mImageUri;
private String mPictureFileName;
private ContentValues mCurrentPictureValues;
private ImageReader mCaptureImageReader;

private CameraBase mCameraBase;
private static final String SIZE_HD = "HD 720p";
Expand Down Expand Up @@ -137,6 +142,7 @@ public CameraBase(Activity activity, AutoFitTextureView mtextureView, ImageButto
RecordingTimeView, SettingsKey);

mCameraBase = this;
mCaptureImageReader = null;
}

private void ClickListeners(ImageButton PictureButton, ImageButton RecordButton,
Expand Down Expand Up @@ -436,12 +442,13 @@ private Size getSelectedDimension(String Key) {
}

public void createCameraPreview() {
Surface surface = null;
try {
mRecord.closePreviewSession();
SurfaceTexture texture = textureView.getSurfaceTexture();
if (texture == null) return;

Surface surface = new Surface(texture);
surface = new Surface(texture);

String Key = GetChnagedPrefKey();
if (Key == null)
Expand Down Expand Up @@ -496,6 +503,8 @@ public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
} finally {
surface.release();
}
}

Expand Down Expand Up @@ -607,7 +616,6 @@ public void takePicture() {
Log.e(TAG, "cameraDevice is null");
return;
}

try {
final Size imageDimension = getSelectedDimension(Capture_Key);
if (imageDimension == null) {
Expand All @@ -618,15 +626,15 @@ public void takePicture() {
Log.i(TAG, "Still Capture imageDimension " + imageDimension.getWidth() + " x " +
imageDimension.getHeight());

ImageReader reader = ImageReader.newInstance(
mCaptureImageReader = ImageReader.newInstance(
imageDimension.getWidth(), imageDimension.getHeight(), ImageFormat.JPEG, 1);
List<Surface> outputSurfaces = new ArrayList<>(2);

outputSurfaces.add(reader.getSurface());
outputSurfaces.add(mCaptureImageReader.getSurface());

captureRequestBuilder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureRequestBuilder.addTarget(reader.getSurface());
captureRequestBuilder.addTarget(mCaptureImageReader.getSurface());
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE,
CameraMetadata.CONTROL_MODE_AUTO);
// Orientation
Expand All @@ -639,9 +647,17 @@ public void takePicture() {
Log.e(TAG, "takePicture Invalid file details");
return;
}
String mPictureFilename = ImageFileDetails[3];

final File ImageFile = new File(mPictureFilename);
Context mContext = mActivity.getApplicationContext();
ContentResolver resolver = mContext.getContentResolver();
Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
mCurrentPictureValues = Utils.getContentValues(Utils.MEDIA_TYPE_IMAGE, ImageFileDetails,imageDimension.getWidth(),
imageDimension.getHeight(),0,0);

mImageUri = resolver.insert(collection,mCurrentPictureValues);
mPictureFileName = Utils.getRealPathFromURI(mContext,mImageUri);

final File ImageFile = new File(mPictureFileName);

ImageReader.OnImageAvailableListener readerListener =
new ImageReader.OnImageAvailableListener() {
Expand Down Expand Up @@ -669,16 +685,18 @@ public void onImageAvailable(ImageReader reader) {
private void save(byte[] bytes) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(ImageFile);
output = resolver.openOutputStream(mImageUri);
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != output) {
output.close();
}
}
}
};
reader.setOnImageAvailableListener(readerListener, null);
mCaptureImageReader.setOnImageAvailableListener(readerListener, null);
final CameraCaptureSession.CaptureCallback captureListener =
new CameraCaptureSession.CaptureCallback() {
@Override
Expand Down Expand Up @@ -716,21 +734,26 @@ public void onConfigureFailed(CameraCaptureSession session) {
}

private void saveImage(Size imageDimension, File ImageFile) {
ContentValues mCurrentPictureValues;
Uri uri;
Context mContext = mActivity.getApplicationContext();
ContentResolver resolver = mContext.getContentResolver();
File imageFile = new File(mPictureFileName);
if(!imageFile.exists() || imageFile.length() == 0)
{
Log.e(TAG,"Image File Does not exist "+mPictureFileName);
return;
}
mCurrentPictureValues.put(MediaStore.Video.Media.SIZE,imageFile.length());

mCurrentPictureValues = Utils.getContentValues(
Utils.MEDIA_TYPE_IMAGE, ImageFileDetails, imageDimension.getWidth(),
imageDimension.getHeight(), 0, ImageFile.length());
resolver.update(mImageUri,mCurrentPictureValues,null,null);

uri = Utils.broadcastNewPicture(mActivity.getApplicationContext(), mCurrentPictureValues);
Utils.broadcastNewPicture(mContext, mImageUri);

ic_camera.setCurrentUri(uri);
ic_camera.setImagePath(ImageFile.getAbsolutePath());
ic_camera.setCurrentUri(mImageUri);
ic_camera.setImagePath(mPictureFileName);

ic_camera.setCurrentFileInfo(mCurrentPictureValues);

Log.i(TAG, "Image saved @ " + ImageFile.getAbsolutePath());
Log.i(TAG, "Image saved @ " + mPictureFileName);
}

private void showDetailsDialog(ContentValues info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private int getOrientation(int rotation) {


public void showVideoThumbnail() {
final Uri videoUri = Uri.fromFile(new File(ic_camera.getImagePath()));
final Uri videoUri = ic_camera.getCurrentUri();
mRoundedThumbnailView.startRevealThumbnailAnimation("Video taken");

final Optional<Bitmap> bitmap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ protected final void onResume() {

mMainHandler.removeCallbacks(mOnResumeTasks);
if (mSkippedFirstOnResume == false) {
long delay = mSkippedFirstOnResume ? ON_RESUME_DELAY_SECURE_MILLIS
: ON_RESUME_DELAY_NON_SECURE_MILLIS;
long delay = ON_RESUME_DELAY_NON_SECURE_MILLIS;
// Skipping onResumeTasks; set to true.
mSkippedFirstOnResume = true;
Log.v(TAG, "onResume() --> postDelayed(mOnResumeTasks," + delay + ")");
Expand Down
Loading

0 comments on commit 844ae59

Please sign in to comment.