Skip to content

Commit

Permalink
Fix missing runtime contacts permission on newer Android versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sealor committed Jul 16, 2023
1 parent 9992053 commit e64dd47
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package de.ubuntix.android.birthdayreminder;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.text.DateFormatSymbols;
import java.util.Calendar;
Expand All @@ -34,6 +38,7 @@ public class BirthdayReminder extends ListActivity {
// TODO: call/write message on birthday
// TODO: hideNotificationPref

private static final int PERMISSIONS_REQUEST_WRITE_CONTACTS = 1;
private final DateFormatSymbols dateSymbols = new DateFormatSymbols();

private Database db;
Expand All @@ -53,13 +58,41 @@ public void onCreate(Bundle savedInstanceState) {
if (prefs.getActivateService()) {
BirthdayBroadcastReceiver.restart(getApplicationContext());
}

// request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!isContactsPermissionGranted()) {
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, PERMISSIONS_REQUEST_WRITE_CONTACTS);
}
}
}

private boolean isContactsPermissionGranted() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}

return checkSelfPermission(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED;
}

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

updateView();
if (isContactsPermissionGranted()) {
updateView();
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_WRITE_CONTACTS) {
if (isContactsPermissionGranted()) {
updateView();
} else {
Toast.makeText(this, R.string.no_contacts_permission, Toast.LENGTH_LONG).show();
}
}
}

private void updateView() {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<string name="ok">OK</string>
<string name="cancel">Abbrechen</string>
<string name="editor_phone">Telefon</string>
<string name="no_contacts_permission">Keine Berechtigung zum Lesen und Ändern von Kontakten</string>

</resources>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
<string name="add_date_of_birth">add date of birth</string>
<string name="new_birthday">+</string>
<string name="editor_phone">Phone</string>
<string name="no_contacts_permission">No permission to read and write contacts</string>

</resources>
</resources>

0 comments on commit e64dd47

Please sign in to comment.