Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

CAPAC-8 #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "pub.devrel.easypermissions.sample"
minSdkVersion 14
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
Expand All @@ -32,4 +32,6 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation "androidx.annotation:annotation:1.1.0"
implementation project(':easypermissions')
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
14 changes: 6 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="pub.devrel.easypermissions.sample">

<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:label="@string/app_name"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">

<activity
android:name=".ContactsActivity"
android:exported="false" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package pub.devrel.easypermissions.sample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class ContactsActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks,
EasyPermissions.RationaleCallbacks {

private static final int RC_CONTACTS_PERM = 126;
TextView message;
private static final String TAG = "ContactsActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
message = findViewById(R.id.textPermission);
contactsTask();
contactsMessage();
}


@Override
protected void onResume() {
super.onResume();
contactsMessage();


}

public void contactsMessage(){
if(hasContactsPermission()){
message.setText(getString(R.string.permission_accepted));
}else{
message.setText(getString(R.string.permission_denied));
}
}


private boolean hasContactsPermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.READ_CONTACTS);
}

@AfterPermissionGranted(RC_CONTACTS_PERM)
public void contactsTask() {
if (hasContactsPermission()) {
// Have permissions, do the thing!
Toast.makeText(this, "TODO: Contacts things", Toast.LENGTH_LONG).show();
} else {
// Ask for both permissions
EasyPermissions.requestPermissions(
this,
getString(R.string.rationale_contacts),
RC_CONTACTS_PERM,
Manifest.permission.READ_CONTACTS);
}
}

/*@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

// EasyPermissions handles the request result.
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}*/

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());
/* if(requestCode == RC_CONTACTS_PERM){
contactsMessage();
}*/
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());

// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
// This will display a dialog directing them to enable the permission in app settings.
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show();
}

/*if(requestCode == RC_CONTACTS_PERM){
contactsMessage(getString(R.string.permission_denied));
}*/
}


@Override
public void onRationaleAccepted(int requestCode) {
Log.d(TAG, "onRationaleAccepted:" + requestCode);
}

@Override
public void onRationaleDenied(int requestCode) {
Log.d(TAG, "onRationaleDenied:" + requestCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -33,6 +35,8 @@
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks,
EasyPermissions.RationaleCallbacks{

Button boton;

private static final String TAG = "MainActivity";
private static final String[] LOCATION_AND_CONTACTS =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS};
Expand All @@ -50,6 +54,15 @@ protected void onCreate(Bundle savedInstanceState) {

// Button click listener that will request two permissions.
findViewById(R.id.button_location_and_contacts).setOnClickListener(v -> locationAndContactsTask());
boton= (Button) findViewById(R.id.contacts_id);
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

startActivity(new Intent(MainActivity.this, ContactsActivity.class));

}
});
}

private boolean hasCameraPermission() {
Expand All @@ -68,6 +81,10 @@ private boolean hasStoragePermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}

private boolean hasContactsPermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.READ_CONTACTS);
}

@AfterPermissionGranted(RC_CAMERA_PERM)
public void cameraTask() {
if (hasCameraPermission()) {
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/res/layout/activity_contacts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ContactsActivity">

<TextView
android:id="@+id/textPermission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="24dp"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:gravity="center_horizontal" />
</LinearLayout>

6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
android:layout_height="wrap_content"
tools:layout="@layout/fragment_main" />

<Button
android:id="@+id/contacts_id"
android:layout_width="198dp"
android:layout_height="wrap_content"
android:text="@string/contacts" />

</LinearLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="rationale_camera">This app needs access to your camera so you can take pictures.</string>
<string name="rationale_location_contacts">This app needs access to your location and contacts to know where and who you are.</string>
<string name="rationale_sms">This app needs access to your sms to read all your great messages.</string>
<string name="rationale_contacts">This app needs access to your contacts.</string>
<string name="returned_from_app_settings_to_activity">
Returned from app settings to MainActivity with the following permissions:
\n\nCamera: %s
Expand All @@ -15,4 +16,7 @@
<string name="camera">Camera</string>
<string name="location_and_contacts">Location and Contacts</string>
<string name="sms">SMS</string>
<string name="contacts">CONTACTS</string>
<string name="permission_accepted">Se pudo obtener el permiso para acceder a los contactos.</string>
<string name="permission_denied">Se denego el permiso para acceder a los contactos.</string>
</resources>