summaryrefslogtreecommitdiffstats
path: root/remoting/android
diff options
context:
space:
mode:
Diffstat (limited to 'remoting/android')
-rw-r--r--remoting/android/java/src/org/chromium/chromoting/Chromoting.java17
-rw-r--r--remoting/android/java/src/org/chromium/chromoting/accountswitcher/AccountSwitcherBasic.java36
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;