From 41ef71b5417042603cf9e4e95c2c74deb21512d9 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 12 Dec 2019 14:30:19 +0100 Subject: [PATCH] Regrant permissions --- .../java/eu/faircode/email/ActivitySetup.java | 36 ++++--------------- .../eu/faircode/email/FragmentAccounts.java | 30 ++++++++++++++++ app/src/main/res/layout/fragment_accounts.xml | 15 +++++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 51 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index 104d503756..bcd1d29375 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -129,9 +129,8 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac static final int REQUEST_SOUND = 2; static final int REQUEST_EXPORT = 3; static final int REQUEST_IMPORT = 4; - static final int REQUEST_IMPORT_OAUTH = 5; - static final int REQUEST_CHOOSE_ACCOUNT = 6; - static final int REQUEST_DONE = 7; + static final int REQUEST_CHOOSE_ACCOUNT = 5; + static final int REQUEST_DONE = 6; static final int REQUEST_IMPORT_CERTIFICATE = 7; static final String ACTION_QUICK_GMAIL = BuildConfig.APPLICATION_ID + ".ACTION_QUICK_GMAIL"; @@ -381,9 +380,6 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac if (resultCode == RESULT_OK && data != null) handleImport(data, this.password); break; - case REQUEST_IMPORT_OAUTH: - ServiceSynchronize.eval(this, "import/oauth"); - break; case REQUEST_IMPORT_CERTIFICATE: if (resultCode == RESULT_OK && data != null) handleImportCertificate(data); @@ -660,14 +656,14 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac args.putParcelable("uri", data.getData()); args.putString("password", password); - new SimpleTask() { + new SimpleTask() { @Override protected void onPreExecute(Bundle args) { ToastEx.makeText(ActivitySetup.this, R.string.title_executing, Toast.LENGTH_LONG).show(); } @Override - protected Boolean onExecute(Context context, Bundle args) throws Throwable { + protected Void onExecute(Context context, Bundle args) throws Throwable { Uri uri = args.getParcelable("uri"); String password = args.getString("password"); @@ -676,8 +672,6 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac throw new IllegalArgumentException(context.getString(R.string.title_no_stream)); } - boolean oauth = false; - StringBuilder data = new StringBuilder(); Log.i("Reading URI=" + uri); ContentResolver resolver = context.getContentResolver(); @@ -744,9 +738,6 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac Long aid = account.id; account.id = null; - if (account.auth_type != MailService.AUTH_TYPE_PASSWORD) - oauth = true; - if (primary != null) account.primary = false; @@ -992,27 +983,12 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac Log.i("Imported data"); - return oauth; + return null; } @Override - protected void onExecuted(Bundle args, Boolean oauth) { + protected void onExecuted(Bundle args, Void data) { ToastEx.makeText(ActivitySetup.this, R.string.title_setup_imported, Toast.LENGTH_LONG).show(); - - if (oauth && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - List permissions = new ArrayList<>(); - permissions.add(Manifest.permission.READ_CONTACTS); // profile - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - permissions.add(Manifest.permission.GET_ACCOUNTS); - - for (String permission : permissions) - if (!hasPermission(permission)) { - // TODO: fix permissions request - requestPermissions(permissions.toArray(new String[0]), REQUEST_IMPORT_OAUTH); - //return; - } - } - ServiceSynchronize.eval(ActivitySetup.this, "import"); } diff --git a/app/src/main/java/eu/faircode/email/FragmentAccounts.java b/app/src/main/java/eu/faircode/email/FragmentAccounts.java index bc56a8e20e..379001e327 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccounts.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccounts.java @@ -32,6 +32,7 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -60,6 +61,7 @@ public class FragmentAccounts extends FragmentBase { private ViewGroup view; private SwipeRefreshLayout swipeRefresh; + private Button btnGrant; private RecyclerView rvAccount; private ContentLoadingProgressBar pbWait; private Group grpReady; @@ -70,6 +72,8 @@ public class FragmentAccounts extends FragmentBase { private String searching = null; private AdapterAccount adapter; + private static final int REQUEST_IMPORT_OAUTH = 1; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -91,6 +95,7 @@ public class FragmentAccounts extends FragmentBase { // Get controls swipeRefresh = view.findViewById(R.id.swipeRefresh); + btnGrant = view.findViewById(R.id.btnGrant); rvAccount = view.findViewById(R.id.rvAccount); pbWait = view.findViewById(R.id.pbWait); grpReady = view.findViewById(R.id.grpReady); @@ -110,6 +115,13 @@ public class FragmentAccounts extends FragmentBase { }); swipeRefresh.setEnabled(!settings); + btnGrant.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + requestPermissions(Helper.getOAuthPermissions(), REQUEST_IMPORT_OAUTH); + } + }); + rvAccount.setHasFixedSize(false); LinearLayoutManager llm = new LinearLayoutManager(getContext()); rvAccount.setLayoutManager(llm); @@ -226,6 +238,7 @@ public class FragmentAccounts extends FragmentBase { fabCompose.show(); } + btnGrant.setVisibility(View.GONE); grpReady.setVisibility(View.GONE); pbWait.setVisibility(View.VISIBLE); @@ -255,6 +268,14 @@ public class FragmentAccounts extends FragmentBase { if (accounts == null) accounts = new ArrayList<>(); + boolean authorized = true; + for (TupleAccountEx account : accounts) + if (account.auth_type != MailService.AUTH_TYPE_PASSWORD && + !Helper.hasPermissions(getContext(), Helper.getOAuthPermissions())) { + authorized = false; + } + btnGrant.setVisibility(authorized ? View.GONE : View.VISIBLE); + adapter.set(accounts); pbWait.setVisibility(View.GONE); @@ -298,6 +319,15 @@ public class FragmentAccounts extends FragmentBase { super.onPrepareOptionsMenu(menu); } + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == REQUEST_IMPORT_OAUTH) + if (Helper.hasPermissions(getContext(), permissions)) + ServiceSynchronize.reload(getContext(), null, "Permissions regranted"); + } + private void onSwipeRefresh() { Bundle args = new Bundle(); diff --git a/app/src/main/res/layout/fragment_accounts.xml b/app/src/main/res/layout/fragment_accounts.xml index 9e4e7f434c..43dd883114 100644 --- a/app/src/main/res/layout/fragment_accounts.xml +++ b/app/src/main/res/layout/fragment_accounts.xml @@ -15,6 +15,19 @@ android:layout_width="match_parent" android:layout_height="match_parent"> +