Skip to content

Commit

Permalink
List of included changes:
Browse files Browse the repository at this point in the history
  - Only request runtime permissions in EntryChoiceActivity

PiperOrigin-RevId: 427835347
Change-Id: I4c224d607bd6f4a7657adb7e18cba89677007c15
  • Loading branch information
Google ML Kit authored and Lei YU committed Feb 10, 2022
1 parent 94f4c90 commit 1d3b433
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 670 deletions.
20 changes: 10 additions & 10 deletions android/vision-quickstart/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,59 @@

<activity
android:name=".java.ChooserActivity"
android:exported="true">
android:exported="false">
</activity>

<activity
android:name=".java.LivePreviewActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".java.CameraXLivePreviewActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".java.CameraXSourceDemoActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".java.StillImageActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".kotlin.ChooserActivity"
android:exported="true">
android:exported="false">
</activity>

<activity
android:name=".kotlin.LivePreviewActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".kotlin.CameraXLivePreviewActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".kotlin.CameraXSourceDemoActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

<activity
android:name=".kotlin.StillImageActivity"
android:exported="true"
android:exported="false"
android:theme="@style/AppTheme">
</activity>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

package com.google.mlkit.vision.demo

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.mlkit.vision.demo.java.ChooserActivity
import java.util.ArrayList

class EntryChoiceActivity : AppCompatActivity() {
class EntryChoiceActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -41,5 +48,61 @@ class EntryChoiceActivity : AppCompatActivity() {
)
startActivity(intent)
}

if (!allRuntimePermissionsGranted()) {
getRuntimePermissions()
}
}

private fun allRuntimePermissionsGranted(): Boolean {
for (permission in REQUIRED_RUNTIME_PERMISSIONS) {
permission?.let {
if (!isPermissionGranted(this, it)) {
return false
}
}
}
return true
}

private fun getRuntimePermissions() {
val permissionsToRequest = ArrayList<String>()
for (permission in REQUIRED_RUNTIME_PERMISSIONS) {
permission?.let {
if (!isPermissionGranted(this, it)) {
permissionsToRequest.add(permission)
}
}
}

if (permissionsToRequest.isNotEmpty()) {
ActivityCompat.requestPermissions(
this,
permissionsToRequest.toTypedArray(),
PERMISSION_REQUESTS
)
}
}

private fun isPermissionGranted(context: Context, permission: String): Boolean {
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
) {
Log.i(TAG, "Permission granted: $permission")
return true
}
Log.i(TAG, "Permission NOT granted: $permission")
return false
}

companion object {
private const val TAG = "EntryChoiceActivity"
private const val PERMISSION_REQUESTS = 1

private val REQUIRED_RUNTIME_PERMISSIONS =
arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.google.mlkit.vision.demo.java;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.annotation.Nullable;
Expand All @@ -43,8 +40,6 @@
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory;
Expand Down Expand Up @@ -81,11 +76,8 @@
@KeepName
@RequiresApi(VERSION_CODES.LOLLIPOP)
public final class CameraXLivePreviewActivity extends AppCompatActivity
implements OnRequestPermissionsResultCallback,
OnItemSelectedListener,
CompoundButton.OnCheckedChangeListener {
implements OnItemSelectedListener, CompoundButton.OnCheckedChangeListener {
private static final String TAG = "CameraXLivePreview";
private static final int PERMISSION_REQUESTS = 1;

private static final String OBJECT_DETECTION = "Object Detection";
private static final String OBJECT_DETECTION_CUSTOM = "Custom Object Detection";
Expand Down Expand Up @@ -175,9 +167,7 @@ protected void onCreate(Bundle savedInstanceState) {
this,
provider -> {
cameraProvider = provider;
if (allPermissionsGranted()) {
bindAllCameraUseCases();
}
bindAllCameraUseCases();
});

ImageView settingsButton = findViewById(R.id.settings_button);
Expand All @@ -189,10 +179,6 @@ protected void onCreate(Bundle savedInstanceState) {
SettingsActivity.LaunchSource.CAMERAX_LIVE_PREVIEW);
startActivity(intent);
});

if (!allPermissionsGranted()) {
getRuntimePermissions();
}
}

@Override
Expand Down Expand Up @@ -464,63 +450,4 @@ private void bindAnalysisUseCase() {

cameraProvider.bindToLifecycle(/* lifecycleOwner= */ this, cameraSelector, analysisUseCase);
}

private String[] getRequiredPermissions() {
try {
PackageInfo info =
this.getPackageManager()
.getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] ps = info.requestedPermissions;
if (ps != null && ps.length > 0) {
return ps;
} else {
return new String[0];
}
} catch (Exception e) {
return new String[0];
}
}

private boolean allPermissionsGranted() {
for (String permission : getRequiredPermissions()) {
if (!isPermissionGranted(this, permission)) {
return false;
}
}
return true;
}

private void getRuntimePermissions() {
List<String> allNeededPermissions = new ArrayList<>();
for (String permission : getRequiredPermissions()) {
if (!isPermissionGranted(this, permission)) {
allNeededPermissions.add(permission);
}
}

if (!allNeededPermissions.isEmpty()) {
ActivityCompat.requestPermissions(
this, allNeededPermissions.toArray(new String[0]), PERMISSION_REQUESTS);
}
}

@Override
public void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
Log.i(TAG, "Permission granted!");
if (allPermissionsGranted()) {
bindAllCameraUseCases();
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

private static boolean isPermissionGranted(Context context, String permission) {
if (ContextCompat.checkSelfPermission(context, permission)
== PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission granted: " + permission);
return true;
}
Log.i(TAG, "Permission NOT granted: " + permission);
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.google.mlkit.vision.demo.java;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
Expand All @@ -32,9 +29,6 @@
import android.widget.ToggleButton;
import androidx.annotation.RequiresApi;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback;
import androidx.core.content.ContextCompat;
import com.google.android.gms.common.annotation.KeepName;
import com.google.mlkit.common.model.LocalModel;
import com.google.mlkit.vision.camera.CameraSourceConfig;
Expand All @@ -50,17 +44,15 @@
import com.google.mlkit.vision.objects.ObjectDetection;
import com.google.mlkit.vision.objects.ObjectDetector;
import com.google.mlkit.vision.objects.custom.CustomObjectDetectorOptions;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/** Live preview demo app for ML Kit APIs using CameraXSource API. */
@KeepName
@RequiresApi(VERSION_CODES.LOLLIPOP)
public final class CameraXSourceDemoActivity extends AppCompatActivity
implements OnRequestPermissionsResultCallback, CompoundButton.OnCheckedChangeListener {
implements CompoundButton.OnCheckedChangeListener {
private static final String TAG = "CameraXSourceDemo";
private static final int PERMISSION_REQUESTS = 1;

private static final LocalModel localModel =
new LocalModel.Builder().setAssetFilePath("custom_models/object_labeler.tflite").build();
Expand Down Expand Up @@ -108,9 +100,6 @@ protected void onCreate(Bundle savedInstanceState) {
detectionTask
.addOnSuccessListener(this::onDetectionTaskSuccess)
.addOnFailureListener(this::onDetectionTaskFailure);
if (!allPermissionsGranted()) {
getRuntimePermissions();
}
}

@Override
Expand Down Expand Up @@ -155,14 +144,6 @@ public void onDestroy() {
}
}

@Override
public void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
Log.i(TAG, "Permission granted!");
createThenStartCameraXSource();
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

private void createThenStartCameraXSource() {
if (cameraXSource != null) {
cameraXSource.close();
Expand Down Expand Up @@ -228,53 +209,4 @@ private boolean isPortraitMode() {
return getApplicationContext().getResources().getConfiguration().orientation
!= Configuration.ORIENTATION_LANDSCAPE;
}

private boolean allPermissionsGranted() {
for (String permission : getRequiredPermissions()) {
if (!isPermissionGranted(this, permission)) {
return false;
}
}
return true;
}

private String[] getRequiredPermissions() {
try {
PackageInfo info =
this.getPackageManager()
.getPackageInfo(this.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] ps = info.requestedPermissions;
if (ps != null && ps.length > 0) {
return ps;
} else {
return new String[0];
}
} catch (Exception e) {
return new String[0];
}
}

private void getRuntimePermissions() {
List<String> allNeededPermissions = new ArrayList<>();
for (String permission : getRequiredPermissions()) {
if (!isPermissionGranted(this, permission)) {
allNeededPermissions.add(permission);
}
}

if (!allNeededPermissions.isEmpty()) {
ActivityCompat.requestPermissions(
this, allNeededPermissions.toArray(new String[0]), PERMISSION_REQUESTS);
}
}

private static boolean isPermissionGranted(Context context, String permission) {
if (ContextCompat.checkSelfPermission(context, permission)
== PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission granted: " + permission);
return true;
}
Log.i(TAG, "Permission NOT granted: " + permission);
return false;
}
}
Loading

0 comments on commit 1d3b433

Please sign in to comment.