Move common permissions code to K9Activity

This commit is contained in:
cketti 2018-11-28 04:21:24 +01:00
parent 9cc5b0f179
commit 4a79664139
3 changed files with 34 additions and 27 deletions

View file

@ -1,7 +1,10 @@
package com.fsck.k9.activity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.Toast;
@ -9,6 +12,7 @@ import android.widget.Toast;
import com.fsck.k9.activity.K9ActivityCommon.K9ActivityMagic;
import com.fsck.k9.activity.misc.SwipeGestureDetector.OnSwipeGestureListener;
import com.fsck.k9.ui.R;
import timber.log.Timber;
public abstract class K9Activity extends AppCompatActivity implements K9ActivityMagic {
@ -50,4 +54,28 @@ public abstract class K9Activity extends AppCompatActivity implements K9Activity
}
}
}
public boolean hasPermission(Permission permission) {
return ContextCompat.checkSelfPermission(this, permission.permission) == PackageManager.PERMISSION_GRANTED;
}
public void requestPermission(Permission permission) {
Timber.i("Requesting permission: " + permission.permission);
ActivityCompat.requestPermissions(this, new String[] { permission.permission }, permission.requestCode);
}
public enum Permission {
READ_CONTACTS(Manifest.permission.READ_CONTACTS, PERMISSIONS_REQUEST_READ_CONTACTS),
WRITE_CONTACTS(Manifest.permission.WRITE_CONTACTS, PERMISSIONS_REQUEST_WRITE_CONTACTS);
final String permission;
final int requestCode;
Permission(String permission, int requestCode) {
this.permission = permission;
this.requestCode = requestCode;
}
}
}

View file

@ -8,7 +8,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
@ -20,7 +19,6 @@ import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
@ -28,8 +26,6 @@ import android.os.Handler;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.text.TextWatcher;
@ -52,8 +48,6 @@ import com.fsck.k9.Account.MessageFormat;
import com.fsck.k9.Identity;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.ui.R;
import com.fsck.k9.activity.MessageLoaderHelper.MessageLoaderCallbacks;
import com.fsck.k9.activity.compose.AttachmentPresenter;
import com.fsck.k9.activity.compose.AttachmentPresenter.AttachmentMvpView;
@ -69,6 +63,7 @@ import com.fsck.k9.activity.compose.RecipientMvpView;
import com.fsck.k9.activity.compose.RecipientPresenter;
import com.fsck.k9.activity.compose.SaveMessageTask;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.controller.SimpleMessagingListener;
@ -102,6 +97,7 @@ import com.fsck.k9.message.SimpleMessageBuilder;
import com.fsck.k9.message.SimpleMessageFormat;
import com.fsck.k9.search.LocalSearch;
import com.fsck.k9.ui.EolConvertingEditText;
import com.fsck.k9.ui.R;
import com.fsck.k9.ui.compose.QuotedMessageMvpView;
import com.fsck.k9.ui.compose.QuotedMessagePresenter;
import org.openintents.openpgp.OpenPgpApiManager;
@ -648,14 +644,8 @@ public class MessageCompose extends K9Activity implements OnClickListener,
}
private void checkAndRequestPermissions() {
boolean hasContactPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
if (!hasContactPermission) {
Timber.i("requesting contact permission");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
K9Activity.PERMISSIONS_REQUEST_READ_CONTACTS);
if (!hasPermission(Permission.READ_CONTACTS)) {
requestPermission(Permission.READ_CONTACTS);
}
}

View file

@ -4,7 +4,6 @@ package com.fsck.k9.activity;
import java.util.Collection;
import java.util.List;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.arch.lifecycle.Observer;
@ -14,18 +13,15 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
@ -77,7 +73,6 @@ import com.fsck.k9.view.MessageTitleView;
import com.fsck.k9.view.ViewSwitcher;
import com.fsck.k9.view.ViewSwitcher.OnSwitchCompleteListener;
import com.mikepenz.materialdrawer.Drawer.OnDrawerListener;
import de.cketti.library.changelog.ChangeLog;
import timber.log.Timber;
@ -517,14 +512,8 @@ public class MessageList extends K9Activity implements MessageListFragmentListen
private void checkAndRequestPermissions() {
boolean hasContactPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
if (!hasContactPermission) {
Timber.i("requesting contact permission");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
K9Activity.PERMISSIONS_REQUEST_READ_CONTACTS);
if (!hasPermission(Permission.READ_CONTACTS)) {
requestPermission(Permission.READ_CONTACTS);
}
}