diff options
author | lambroslambrou <lambroslambrou@chromium.org> | 2015-12-16 11:58:29 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-16 19:59:17 +0000 |
commit | bab30384a7d5cf9dfd77f6e9269b6d492e9aa0af (patch) | |
tree | 20bff6e736dd6dffe64a2570cefe9de35e39060f /remoting/android | |
parent | 65a833af01fe7588e76e18cd28a86aec8980389d (diff) | |
download | chromium_src-bab30384a7d5cf9dfd77f6e9269b6d492e9aa0af.zip chromium_src-bab30384a7d5cf9dfd77f6e9269b6d492e9aa0af.tar.gz chromium_src-bab30384a7d5cf9dfd77f6e9269b6d492e9aa0af.tar.bz2 |
Android Chromoting: Request GET_ACCOUNTS permission if needed.
Now that the app is targeted at API level 23, it needs to request any
permissions that it needs at runtime and cannot assume that all
permissions are granted. This CL requests the GET_ACCOUNTS permission
in AccountSwitcherBasic, if it isn't already granted.
The offical Google account-switcher doesn't seem to require this
permission. So this CL only updates the basic account-switcher with
minimal changes needed for correct functioning. No new translatable
strings are added, for example, which would normally be used to
explain to the user why the permissions are required.
BUG=546378
Review URL: https://codereview.chromium.org/1528783002
Cr-Commit-Position: refs/heads/master@{#365595}
Diffstat (limited to 'remoting/android')
-rw-r--r-- | remoting/android/java/src/org/chromium/chromoting/Chromoting.java | 17 | ||||
-rw-r--r-- | remoting/android/java/src/org/chromium/chromoting/accountswitcher/AccountSwitcherBasic.java | 36 |
2 files changed, 44 insertions, 9 deletions
diff --git a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java index 5dce18e..b4356e0 100644 --- a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java +++ b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java @@ -10,6 +10,7 @@ import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -355,6 +356,22 @@ public class Chromoting extends AppCompatActivity implements JniInterface.Connec } } + /** Called when a permissions request has returned. */ + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, + int[] grantResults) { + // This is currently only used by AccountSwitcherBasic. + // Check that the user has granted the needed permission, and reload the accounts. + // Otherwise, assume something unexpected occurred, or the user cancelled the request. + if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + mAccountSwitcher.reloadAccounts(); + } else if (permissions.length == 0) { + Log.e(TAG, "User cancelled the permission request."); + } else { + Log.e(TAG, "Permission %s was not granted.", permissions[0]); + } + } + /** Called when the display is rotated (as registered in the manifest). */ @Override public void onConfigurationChanged(Configuration newConfig) { diff --git a/remoting/android/java/src/org/chromium/chromoting/accountswitcher/AccountSwitcherBasic.java b/remoting/android/java/src/org/chromium/chromoting/accountswitcher/AccountSwitcherBasic.java index a3f439d..72875d8 100644 --- a/remoting/android/java/src/org/chromium/chromoting/accountswitcher/AccountSwitcherBasic.java +++ b/remoting/android/java/src/org/chromium/chromoting/accountswitcher/AccountSwitcherBasic.java @@ -4,10 +4,14 @@ package org.chromium.chromoting.accountswitcher; +import android.Manifest; import android.accounts.Account; import android.accounts.AccountManager; -import android.content.Context; +import android.app.Activity; import android.content.Intent; +import android.content.pm.PackageManager; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; @@ -27,26 +31,29 @@ public class AccountSwitcherBasic extends AccountSwitcherBase { private Spinner mAccountsSpinner; private LinearLayout mContainer; + private Activity mActivity; + /** * The registered callback instance. */ private Callback mCallback; /** - * Constructs an account-switcher, using the given Context to create any Views. Called from + * Constructs an account-switcher, using the given Activity to create any Views. Called from * the activity's onCreate() method. - * @param context Context used for creating Views and performing UI operations. + * @param activity Activity used for creating Views and performing UI operations. * @param callback Callback for receiving notifications from the account-switcher. */ - public AccountSwitcherBasic(Context context, Callback callback) { + public AccountSwitcherBasic(Activity activity, Callback callback) { + mActivity = activity; mCallback = callback; - mAccountsSpinner = new Spinner(context); + mAccountsSpinner = new Spinner(activity); mAccountsSpinner.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); - int padding = (int) (context.getResources().getDisplayMetrics().density * 16f); + int padding = (int) (activity.getResources().getDisplayMetrics().density * 16f); mAccountsSpinner.setPadding(padding, padding, padding, padding); - mContainer = new LinearLayout(context); + mContainer = new LinearLayout(activity); mContainer.setOrientation(LinearLayout.VERTICAL); mContainer.addView(mAccountsSpinner); } @@ -80,8 +87,19 @@ public class AccountSwitcherBasic extends AccountSwitcherBase { @Override public void reloadAccounts() { - Context context = getView().getContext(); - Account[] accounts = AccountManager.get(context).getAccountsByType(ACCOUNT_TYPE); + // AccountManager.getAccountsByType() requires the GET_ACCOUNTS permission, which is + // classed as a dangerous permission. If the permission is not granted, an exception might + // be thrown or the account-list might wrongly appear to be empty. Check if the permission + // has been granted, and request it if not, so the user is aware of the cause of this + // problem. + if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.GET_ACCOUNTS) + != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(mActivity, + new String[] {Manifest.permission.GET_ACCOUNTS}, 0); + return; + } + + Account[] accounts = AccountManager.get(mActivity).getAccountsByType(ACCOUNT_TYPE); if (accounts.length == 0) { mCallback.onAccountsListEmpty(); return; |