From fc6e380ab86217ebc6259455880102f3cc1f206e Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 16 Mar 2017 16:46:59 -0500 Subject: [PATCH] Use verifyPermissions for all permission results --- .../todoroo/astrid/gtasks/GtasksPreferences.java | 5 +++-- .../todoroo/astrid/core/DefaultsPreferences.java | 5 +++-- .../todoroo/astrid/files/AACRecordingActivity.java | 12 ++---------- .../astrid/reminders/ReminderPreferences.java | 9 +++------ .../com/todoroo/astrid/ui/ReminderControlSet.java | 4 ++-- src/main/java/org/tasks/PermissionUtil.java | 3 ++- .../tasks/activities/CalendarSelectionDialog.java | 5 +---- src/main/java/org/tasks/files/FileExplore.java | 13 ++++++------- .../tasks/preferences/MiscellaneousPreferences.java | 5 +++-- src/main/java/org/tasks/ui/CalendarControlSet.java | 6 +++--- 10 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/googleplay/java/com/todoroo/astrid/gtasks/GtasksPreferences.java b/src/googleplay/java/com/todoroo/astrid/gtasks/GtasksPreferences.java index 3fea143b1..ac7f4fdf3 100644 --- a/src/googleplay/java/com/todoroo/astrid/gtasks/GtasksPreferences.java +++ b/src/googleplay/java/com/todoroo/astrid/gtasks/GtasksPreferences.java @@ -6,7 +6,6 @@ package com.todoroo.astrid.gtasks; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.support.annotation.NonNull; @@ -31,6 +30,8 @@ import org.tasks.preferences.PermissionRequestor; import javax.inject.Inject; +import static org.tasks.PermissionUtil.verifyPermissions; + public class GtasksPreferences extends InjectingPreferenceActivity implements GoogleTaskListSelectionHandler { private static final String FRAG_TAG_GOOGLE_TASK_LIST_SELECTION = "frag_tag_google_task_list_selection"; @@ -121,7 +122,7 @@ public class GtasksPreferences extends InjectingPreferenceActivity implements Go @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_ACCOUNTS) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { requestLogin(); } } else { diff --git a/src/main/java/com/todoroo/astrid/core/DefaultsPreferences.java b/src/main/java/com/todoroo/astrid/core/DefaultsPreferences.java index 2a1f234e6..10f40ce72 100644 --- a/src/main/java/com/todoroo/astrid/core/DefaultsPreferences.java +++ b/src/main/java/com/todoroo/astrid/core/DefaultsPreferences.java @@ -6,7 +6,6 @@ package com.todoroo.astrid.core; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Bundle; import android.preference.Preference; import android.support.annotation.NonNull; @@ -23,6 +22,8 @@ import org.tasks.preferences.Preferences; import javax.inject.Inject; +import static org.tasks.PermissionUtil.verifyPermissions; + /** * Displays the preference screen for users to edit their preferences * @@ -70,7 +71,7 @@ public class DefaultsPreferences extends InjectingPreferenceActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_CALENDAR) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { startCalendarSelectionActivity(); } } else { diff --git a/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java b/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java index 9554682df..97fae5dd2 100644 --- a/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java +++ b/src/main/java/com/todoroo/astrid/files/AACRecordingActivity.java @@ -6,25 +6,20 @@ package com.todoroo.astrid.files; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.FragmentManager; -import android.widget.Chronometer; -import org.tasks.R; import org.tasks.dialogs.RecordAudioDialog; import org.tasks.injection.ActivityComponent; import org.tasks.injection.InjectingAppCompatActivity; import org.tasks.preferences.ActivityPermissionRequestor; import org.tasks.preferences.PermissionRequestor; -import org.tasks.preferences.Preferences; import org.tasks.themes.Theme; import javax.inject.Inject; -import butterknife.BindView; - +import static org.tasks.PermissionUtil.verifyPermissions; import static org.tasks.dialogs.RecordAudioDialog.newRecordAudioDialog; public class AACRecordingActivity extends InjectingAppCompatActivity implements RecordAudioDialog.RecordAudioDialogCallback { @@ -33,12 +28,9 @@ public class AACRecordingActivity extends InjectingAppCompatActivity implements public static final String RESULT_OUTFILE = "outfile"; //$NON-NLS-1$ - @Inject Preferences preferences; @Inject ActivityPermissionRequestor permissionRequestor; @Inject Theme theme; - @BindView(R.id.timer) Chronometer timer; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -66,7 +58,7 @@ public class AACRecordingActivity extends InjectingAppCompatActivity implements @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_MIC) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { showDialog(); } else { finish(); diff --git a/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java b/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java index e8fc27ef9..f58aadaa6 100644 --- a/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java +++ b/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java @@ -6,7 +6,6 @@ package com.todoroo.astrid.reminders; import android.content.Intent; -import android.content.pm.PackageManager; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; @@ -40,6 +39,7 @@ import javax.inject.Inject; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybean; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow; +import static org.tasks.PermissionUtil.verifyPermissions; public class ReminderPreferences extends InjectingPreferenceActivity { @@ -117,12 +117,9 @@ public class ReminderPreferences extends InjectingPreferenceActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_CONTACTS) { - for (int grantResult : grantResults) { - if (grantResult != PackageManager.PERMISSION_GRANTED) { - return; - } + if (verifyPermissions(grantResults)) { + fieldMissedCalls.setChecked(true); } - fieldMissedCalls.setChecked(true); } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } diff --git a/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.java b/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.java index b75174907..395018189 100644 --- a/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.java +++ b/src/main/java/com/todoroo/astrid/ui/ReminderControlSet.java @@ -8,7 +8,6 @@ package com.todoroo.astrid.ui; import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.NonNull; @@ -58,6 +57,7 @@ import butterknife.OnItemSelected; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Sets.newHashSet; import static com.todoroo.andlib.utility.DateUtilities.getLongDateStringWithTime; +import static org.tasks.PermissionUtil.verifyPermissions; import static org.tasks.date.DateTimeUtils.newDateTime; /** @@ -269,7 +269,7 @@ public class ReminderControlSet extends TaskEditControlFragment { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_LOCATION) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { pickLocation(); } } else { diff --git a/src/main/java/org/tasks/PermissionUtil.java b/src/main/java/org/tasks/PermissionUtil.java index 71e54e244..ab5e779f1 100644 --- a/src/main/java/org/tasks/PermissionUtil.java +++ b/src/main/java/org/tasks/PermissionUtil.java @@ -4,7 +4,8 @@ import android.content.pm.PackageManager; public abstract class PermissionUtil { public static boolean verifyPermissions(int[] grantResults) { - if(grantResults.length < 1){ + if(grantResults.length == 0) { + // request canceled return false; } diff --git a/src/main/java/org/tasks/activities/CalendarSelectionDialog.java b/src/main/java/org/tasks/activities/CalendarSelectionDialog.java index 002ec93fe..f622d795e 100644 --- a/src/main/java/org/tasks/activities/CalendarSelectionDialog.java +++ b/src/main/java/org/tasks/activities/CalendarSelectionDialog.java @@ -1,7 +1,6 @@ package org.tasks.activities; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; @@ -14,7 +13,6 @@ import org.tasks.calendars.CalendarProvider; import org.tasks.dialogs.AlertDialogBuilder; import org.tasks.dialogs.DialogBuilder; import org.tasks.injection.DialogFragmentComponent; -import org.tasks.injection.ForActivity; import org.tasks.injection.InjectingDialogFragment; import org.tasks.preferences.FragmentPermissionRequestor; import org.tasks.preferences.PermissionChecker; @@ -47,7 +45,6 @@ public class CalendarSelectionDialog extends InjectingDialogFragment { @Inject DialogBuilder dialogBuilder; @Inject CalendarProvider calendarProvider; - @Inject @ForActivity Context context; @Inject FragmentPermissionRequestor fragmentPermissionRequestor; @Inject PermissionChecker permissionChecker; @Inject Theme theme; @@ -114,7 +111,7 @@ public class CalendarSelectionDialog extends InjectingDialogFragment { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_CALENDAR) { - if (grantResults.length > 0 && !verifyPermissions(grantResults)) { + if (!verifyPermissions(grantResults)) { handler.cancel(); } } else { diff --git a/src/main/java/org/tasks/files/FileExplore.java b/src/main/java/org/tasks/files/FileExplore.java index aa3c7cf2d..b317acf54 100644 --- a/src/main/java/org/tasks/files/FileExplore.java +++ b/src/main/java/org/tasks/files/FileExplore.java @@ -2,7 +2,6 @@ package org.tasks.files; import android.app.Activity; import android.content.Intent; -import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Environment; @@ -20,6 +19,8 @@ import java.io.File; import javax.inject.Inject; +import static org.tasks.PermissionUtil.verifyPermissions; + public class FileExplore extends InjectingAppCompatActivity { private static final int REQUEST_PICKER = 1000; @@ -79,12 +80,10 @@ public class FileExplore extends InjectingAppCompatActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_FILE_WRITE) { - if (grantResults.length > 0) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { - launchPicker(); - } else { - finish(); - } + if (verifyPermissions(grantResults)) { + launchPicker(); + } else { + finish(); } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); diff --git a/src/main/java/org/tasks/preferences/MiscellaneousPreferences.java b/src/main/java/org/tasks/preferences/MiscellaneousPreferences.java index eaa68d5d2..eff8a3ac2 100644 --- a/src/main/java/org/tasks/preferences/MiscellaneousPreferences.java +++ b/src/main/java/org/tasks/preferences/MiscellaneousPreferences.java @@ -1,7 +1,6 @@ package org.tasks.preferences; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.speech.tts.TextToSpeech; @@ -21,6 +20,8 @@ import javax.inject.Inject; import timber.log.Timber; +import static org.tasks.PermissionUtil.verifyPermissions; + public class MiscellaneousPreferences extends InjectingPreferenceActivity { private static final int REQUEST_CODE_FILES_DIR = 2; @@ -143,7 +144,7 @@ public class MiscellaneousPreferences extends InjectingPreferenceActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PermissionRequestor.REQUEST_CALENDAR) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { calendarReminderPreference.setChecked(true); } } else { diff --git a/src/main/java/org/tasks/ui/CalendarControlSet.java b/src/main/java/org/tasks/ui/CalendarControlSet.java index b6d42d814..498c18fd7 100644 --- a/src/main/java/org/tasks/ui/CalendarControlSet.java +++ b/src/main/java/org/tasks/ui/CalendarControlSet.java @@ -5,7 +5,6 @@ import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -44,6 +43,7 @@ import timber.log.Timber; import static android.support.v4.content.ContextCompat.getColor; import static com.google.common.base.Strings.isNullOrEmpty; +import static org.tasks.PermissionUtil.verifyPermissions; public class CalendarControlSet extends TaskEditControlFragment { @@ -289,11 +289,11 @@ public class CalendarControlSet extends TaskEditControlFragment { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CODE_OPEN_EVENT) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { openCalendarEvent(); } } else if (requestCode == REQUEST_CODE_CLEAR_EVENT) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (verifyPermissions(grantResults)) { clear(); } } else {