Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
Adds RuntimePermissionsBasic samples to permissions repo.
Browse files Browse the repository at this point in the history
Change-Id: I72ad2e937cd3bcab99d8c7df808a167bfdbe26e6
  • Loading branch information
Jeremy Walker committed Sep 25, 2019
1 parent afed9f4 commit b8beff3
Show file tree
Hide file tree
Showing 65 changed files with 2,302 additions and 0 deletions.
18 changes: 18 additions & 0 deletions RuntimePermissionsBasic/.google/packaging.yaml
@@ -0,0 +1,18 @@

# GOOGLE SAMPLE PACKAGING DATA
#
# This file is used by Google as part of our samples packaging process.
# End users may safely ignore this file. It has no relevance to other systems.
---
status: PUBLISHED
technologies: [Android]
categories: [System]
languages: [Java]
solutions: [Mobile]
github: android/permissions
level: BEGINNER
icon: screenshots/big_icon.png
apiRefs:
- android:android.app.Activity
- android:android.Manifest.permission
license: apache2
42 changes: 42 additions & 0 deletions RuntimePermissionsBasic/Application/build.gradle
@@ -0,0 +1,42 @@
apply plugin: 'com.android.application'

repositories {
google()
jcenter()
}

dependencies {
compile "com.android.support:support-v4:27.0.2"
compile "com.android.support:support-v13:27.0.2"
compile "com.android.support:cardview-v7:27.0.2"
compile "com.android.support:appcompat-v7:27.0.2"
compile 'com.android.support:design:27.0.2'
}

List<String> dirs = ['main']

android {
compileSdkVersion 27

defaultConfig {
minSdkVersion 15
targetSdkVersion 27
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']

}
}
48 changes: 48 additions & 0 deletions RuntimePermissionsBasic/Application/src/main/AndroidManifest.xml
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 The Android Open Source Project
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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.basicpermissions">

<!-- BEGIN_INCLUDE(manifest) -->

<!-- Note that all required permissions are declared here in the Android manifest.
On Android M and above, use of these permissions is only requested at run time. -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- END_INCLUDE(manifest) -->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<!-- Activity that only displays the camera preview. -->
<activity
android:name=".camera.CameraPreviewActivity"
android:exported="false"/>

</application>

</manifest>
@@ -0,0 +1,153 @@
/*
* Copyright 2015 The Android Open Source Project
*
* 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.
*/

This comment was marked as resolved.

Copy link
@Milton56
package com.example.android.basicpermissions;

import android.Manifest;
import android.app.Activity;

This comment has been minimized.

Copy link
@Milton56

Milton56 Jan 15, 2023

>

#63

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

This comment has been minimized.

Copy link
@Milton56

This comment has been minimized.

Copy link
@Milton56
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.android.basicpermissions.camera.CameraPreviewActivity;

/**
* Launcher Activity that demonstrates the use of runtime permissions for Android M.
* This Activity requests permissions to access the camera
* ({@link android.Manifest.permission#CAMERA})
* when the 'Show Camera Preview' button is clicked to start {@link CameraPreviewActivity} once
* the permission has been granted.
* <p>
* First, the status of the Camera permission is checked using {@link
* ActivityCompat#checkSelfPermission(Context, String)}
* If it has not been granted ({@link PackageManager#PERMISSION_GRANTED}), it is requested by
* calling
* {@link ActivityCompat#requestPermissions(Activity, String[], int)}. The result of the request is
* returned to the
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}, which starts
* {@link
* CameraPreviewActivity} if the permission has been granted.
* <p>
* Note that there is no need to check the API level, the support library
* already takes care of this. Similar helper methods for permissions are also available in
* ({@link ActivityCompat},
* {@link android.support.v4.content.ContextCompat} and {@link android.support.v4.app.Fragment}).
*/
public class MainActivity extends AppCompatActivity
implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final int PERMISSION_REQUEST_CAMERA = 0;

private View mLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.main_layout);

// Register a listener for the 'Show Camera Preview' button.
findViewById(R.id.button_open_camera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCameraPreview();
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
// BEGIN_INCLUDE(onRequestPermissionsResult)
if (requestCode == PERMISSION_REQUEST_CAMERA) {
// Request for camera permission.
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted. Start camera preview Activity.
Snackbar.make(mLayout, R.string.camera_permission_granted,
Snackbar.LENGTH_SHORT)
.show();
startCamera();
} else {
// Permission request was denied.
Snackbar.make(mLayout, R.string.camera_permission_denied,
Snackbar.LENGTH_SHORT)
.show();
}
}
// END_INCLUDE(onRequestPermissionsResult)
}

private void showCameraPreview() {
// BEGIN_INCLUDE(startCamera)
// Check if the Camera permission has been granted
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
// Permission is already available, start camera preview
Snackbar.make(mLayout,
R.string.camera_permission_available,
Snackbar.LENGTH_SHORT).show();
startCamera();
} else {
// Permission is missing and must be requested.
requestCameraPermission();
}
// END_INCLUDE(startCamera)
}

/**
* Requests the {@link android.Manifest.permission#CAMERA} permission.
* If an additional rationale should be displayed, the user has to launch the request from
* a SnackBar that includes additional information.
*/
private void requestCameraPermission() {
// Permission has not been granted and must be requested.
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// Display a SnackBar with cda button to request the missing permission.
Snackbar.make(mLayout, R.string.camera_access_required,
Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
// Request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CAMERA);
}
}).show();

} else {
Snackbar.make(mLayout, R.string.camera_unavailable, Snackbar.LENGTH_SHORT).show();
// Request the permission. The result will be received in onRequestPermissionResult().
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA);
}
}

private void startCamera() {
Intent intent = new Intent(this, CameraPreviewActivity.class);
startActivity(intent);
}

}

0 comments on commit b8beff3

Please sign in to comment.