Skip to content

Commit 9701c10

Browse files
committed
request permissions for camera and sdcard together
1 parent 3dcab26 commit 9701c10

File tree

3 files changed

+20
-64
lines changed

3 files changed

+20
-64
lines changed

FaceTracker/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<uses-permission android:name="android.permission.CAMERA" />
1111
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
12-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
12+
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
1313

1414
<uses-sdk
1515
android:minSdkVersion="15"

FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/FaceTrackerActivity.java

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,19 @@
5454
* overlay graphics to indicate the position, size, and ID of each face.
5555
*/
5656
public final class FaceTrackerActivity extends AppCompatActivity {
57-
private static final String TAG = "FaceTracker";
57+
private static final String TAG = "FR";
5858

5959
private CameraSource mCameraSource = null;
6060

6161
private CameraSourcePreview mPreview;
6262
private GraphicOverlay mGraphicOverlay;
63-
// private Button mBtnDetect;
63+
//private Button mBtnDetect;
6464
private CustomDetector customDetector;
6565
//private FaceDetector mPictureDetector;
6666

6767
private static final int RC_HANDLE_GMS = 9001;
6868
// permission request codes need to be < 256
69-
private static final int RC_HANDLE_CAMERA_PERM = 2;
70-
private static final int REQUEST_WRITE_STORAGE = 112;
69+
private static final int RC_HANDLE_CAMERA_AND_SDCARD_PERM = 7;
7170

7271
private FaceRecognizer mFaceRecognizer;
7372

@@ -88,26 +87,14 @@ public void onCreate(Bundle icicle) {
8887
// mBtnDetect = (Button) findViewById(R.id.btnDetect);
8988
mFaceRecognizer = new FaceRecognizer();
9089

91-
// Check for the sdcard write permission. If the
92-
// permission is not granted yet, request permission.
93-
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
94-
if (rc == PackageManager.PERMISSION_GRANTED) {
95-
//createCameraSource();
96-
Log.w(TAG, "SDCard write permission is granted.");
97-
} else {
98-
//requestCameraPermission();
99-
requestSdCardPermission();
100-
Log.w(TAG, "SDCard write permission is NOT granted.");
101-
}
102-
103-
10490
// Check for the camera permission before accessing the camera. If the
10591
// permission is not granted yet, request permission.
106-
rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
107-
if (rc == PackageManager.PERMISSION_GRANTED) {
92+
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
93+
int rs = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
94+
if (rc == PackageManager.PERMISSION_GRANTED && rs == PackageManager.PERMISSION_GRANTED) {
10895
createCameraSource();
10996
} else {
110-
requestCameraPermission();
97+
requestCameraAndSdCardPermission();
11198
}
11299
}
113100

@@ -116,41 +103,15 @@ public void onCreate(Bundle icicle) {
116103
* showing a "Snackbar" message of why the permission is needed then
117104
* sending the request.
118105
*/
119-
private void requestCameraPermission() {
120-
Log.w(TAG, "Camera permission is not granted. Requesting permission");
106+
private void requestCameraAndSdCardPermission() {
107+
Log.w(TAG, "Camera and sdcard permissions are not granted. Requesting permission");
121108

122-
final String[] permissions = new String[]{Manifest.permission.CAMERA};
109+
final String[] permissions = new String[]{Manifest.permission.CAMERA,
110+
Manifest.permission.READ_EXTERNAL_STORAGE};
123111

124112
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
125113
Manifest.permission.CAMERA)) {
126-
ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
127-
return;
128-
}
129-
130-
final Activity thisActivity = this;
131-
132-
View.OnClickListener listener = new View.OnClickListener() {
133-
@Override
134-
public void onClick(View view) {
135-
ActivityCompat.requestPermissions(thisActivity, permissions,
136-
RC_HANDLE_CAMERA_PERM);
137-
}
138-
};
139-
140-
Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale,
141-
Snackbar.LENGTH_INDEFINITE)
142-
.setAction(R.string.ok, listener)
143-
.show();
144-
}
145-
146-
private void requestSdCardPermission() {
147-
Log.w(TAG, "SdCard permission is not granted. Requesting permission");
148-
149-
final String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
150-
151-
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
152-
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
153-
ActivityCompat.requestPermissions(this, permissions, REQUEST_WRITE_STORAGE);
114+
ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_AND_SDCARD_PERM);
154115
return;
155116
}
156117

@@ -160,11 +121,11 @@ private void requestSdCardPermission() {
160121
@Override
161122
public void onClick(View view) {
162123
ActivityCompat.requestPermissions(thisActivity, permissions,
163-
REQUEST_WRITE_STORAGE);
124+
RC_HANDLE_CAMERA_AND_SDCARD_PERM);
164125
}
165126
};
166127

167-
Snackbar.make(mGraphicOverlay, R.string.permission_sdcard_rationale,
128+
Snackbar.make(mGraphicOverlay, R.string.permission_camera_and_sdcard_rationale,
168129
Snackbar.LENGTH_INDEFINITE)
169130
.setAction(R.string.ok, listener)
170131
.show();
@@ -310,7 +271,7 @@ protected void onDestroy() {
310271
*/
311272
@Override
312273
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
313-
if (requestCode != RC_HANDLE_CAMERA_PERM) {
274+
if (requestCode != RC_HANDLE_CAMERA_AND_SDCARD_PERM) {
314275
Log.d(TAG, "Got unexpected permission result: " + requestCode);
315276
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
316277
return;
@@ -334,8 +295,8 @@ public void onClick(DialogInterface dialog, int id) {
334295
};
335296

336297
AlertDialog.Builder builder = new AlertDialog.Builder(this);
337-
builder.setTitle("Face Tracker sample")
338-
.setMessage(R.string.no_camera_permission)
298+
builder.setTitle("FR demo")
299+
.setMessage(R.string.no_camera_sdcard_permission)
339300
.setPositiveButton(R.string.ok, listener)
340301
.show();
341302
}
@@ -435,8 +396,4 @@ public void onDone() {
435396
mOverlay.remove(mFaceGraphic);
436397
}
437398
}
438-
439-
//public native int loadResources();
440-
//public native String recognizeFromImage(Bitmap bmp);
441-
//public native int nativeAdd(int a, int b);
442399
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<string name="ok">OK</string>
4-
<string name="permission_camera_rationale">Access to the camera is needed for detection</string>
5-
<string name="permission_sdcard_rationale">Access to the sdcard is needed for debug</string>
6-
<string name="no_camera_permission">This application cannot run because it does not have the camera permission. The application will now exit.</string>
4+
<string name="permission_camera_and_sdcard_rationale">Access to the camera and read sdcard is needed for detection</string>
5+
<string name="no_camera_sdcard_permission">This application cannot run because it does not have the camera or sdcard permissions. The application will now exit.</string>
76
<string name="low_storage_error">Face detector dependencies cannot be downloaded due to low device storage</string>
87
</resources>

0 commit comments

Comments
 (0)