From 91699af8c6dae2a3ca703abc410fc00c0ab5de59 Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Thu, 28 Jul 2016 15:16:55 -0700 Subject: [PATCH] Always use chooser for Share Bug Report intent. When using startActivity(intent), the platform allows the user to pick the same activity for the intent, which can be a problem for sharing bug reports when the device has personal and work profiles. Change-Id: I1fd66905feb5d894307bbe5656c2aec705a59ab7 Fixes: 22115530 --- packages/Shell/res/values/strings.xml | 3 ++ .../shell/BugreportProgressService.java | 7 ++- .../shell/BugreportWarningActivity.java | 3 +- .../android/shell/BugreportReceiverTest.java | 2 + .../tests/src/com/android/shell/UiBot.java | 53 ++++--------------- 5 files changed, 23 insertions(+), 45 deletions(-) diff --git a/packages/Shell/res/values/strings.xml b/packages/Shell/res/values/strings.xml index 5dfac95e35274..933d650773741 100644 --- a/packages/Shell/res/values/strings.xml +++ b/packages/Shell/res/values/strings.xml @@ -76,4 +76,7 @@ Save + + + Share Bug report diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java index 235bfcc06a4d8..4211369a27cbb 100644 --- a/packages/Shell/src/com/android/shell/BugreportProgressService.java +++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java @@ -1000,12 +1000,17 @@ public class BugreportProgressService extends Service { notifIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Send the share intent... - mContext.startActivity(notifIntent); + sendShareIntent(mContext, notifIntent); // ... and stop watching this process. stopProgress(id); } + static void sendShareIntent(Context context, Intent intent) { + context.startActivity(Intent.createChooser(intent, + context.getResources().getText(R.string.bugreport_intent_chooser_title))); + } + /** * Sends a notification indicating the bugreport has finished so use can share it. */ diff --git a/packages/Shell/src/com/android/shell/BugreportWarningActivity.java b/packages/Shell/src/com/android/shell/BugreportWarningActivity.java index 2426ba099dce6..bdf41714d26c8 100644 --- a/packages/Shell/src/com/android/shell/BugreportWarningActivity.java +++ b/packages/Shell/src/com/android/shell/BugreportWarningActivity.java @@ -21,6 +21,7 @@ import static com.android.shell.BugreportPrefs.STATE_SHOW; import static com.android.shell.BugreportPrefs.STATE_UNKNOWN; import static com.android.shell.BugreportPrefs.getWarningState; import static com.android.shell.BugreportPrefs.setWarningState; +import static com.android.shell.BugreportProgressService.sendShareIntent; import android.app.AlertDialog; import android.content.DialogInterface; @@ -78,7 +79,7 @@ public class BugreportWarningActivity extends AlertActivity if (which == AlertDialog.BUTTON_POSITIVE) { // Remember confirm state, and launch target setWarningState(this, mConfirmRepeat.isChecked() ? STATE_HIDE : STATE_SHOW); - startActivity(mSendIntent); + sendShareIntent(this, mSendIntent); } finish(); diff --git a/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java b/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java index ad66dfc992d00..8eed6a27d942e 100644 --- a/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java +++ b/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java @@ -175,6 +175,8 @@ public class BugreportReceiverTest extends InstrumentationTestCase { mDescription = sb.toString(); setWarningState(mContext, STATE_HIDE); + + mUiBot.turnScreenOn(); } public void testProgress() throws Exception { diff --git a/packages/Shell/tests/src/com/android/shell/UiBot.java b/packages/Shell/tests/src/com/android/shell/UiBot.java index ef24791aac728..d9f5b7a0415c8 100644 --- a/packages/Shell/tests/src/com/android/shell/UiBot.java +++ b/packages/Shell/tests/src/com/android/shell/UiBot.java @@ -22,7 +22,6 @@ import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObjectNotFoundException; -import android.support.test.uiautomator.UiScrollable; import android.support.test.uiautomator.UiSelector; import android.support.test.uiautomator.Until; import android.util.Log; @@ -156,56 +155,24 @@ final class UiBot { } /** - * Chooses a given activity to handle an Intent, using the "Just Once" button. + * Chooses a given activity to handle an Intent. * * @param name name of the activity as displayed in the UI (typically the value set by * {@code android:label} in the manifest). */ - // TODO: UI Automator should provide such logic. public void chooseActivity(String name) { - // First check if the activity is the default option. - String shareText = "Share with " + name; - Log.v(TAG, "Waiting for ActivityChooser text: '" + shareText + "'"); - boolean gotIt = mDevice.wait(Until.hasObject(By.text(shareText)), mTimeout); - boolean justOnceHack = false; - - if (gotIt) { - Log.v(TAG, "Found activity " + name + ", it's the default action"); - clickJustOnce(); - } else { - // Since it's not, need to find it in the scrollable list... - Log.v(TAG, "Activity " + name + " is not default action"); - UiScrollable activitiesList = new UiScrollable(new UiSelector().scrollable(true)); - try { - activitiesList.scrollForward(); - } catch (UiObjectNotFoundException e) { - // TODO: for some paranormal issue, the first time a test is run the scrollable - // activity list is displayed but calling scrollForwad() (or even isScrollable()) - // throws a "UiObjectNotFoundException: UiSelector[SCROLLABLE=true]" exception - justOnceHack = true; - Log.d(TAG, "could not scroll forward", e); - } - UiObject activity = getVisibleObject(name); - // ... then select it. - click(activity, name); - if (justOnceHack) { - clickJustOnce(); - } - } - } - - private void clickJustOnce() { - boolean gotIt = mDevice.wait(Until.hasObject(By.res("android", "button_once")), mTimeout); - assertTrue("'Just Once' button not visible yet", gotIt); - - UiObject justOnce = mDevice - .findObject(new UiSelector().resourceId("android:id/button_once")); - assertTrue("'Just Once' button not found", justOnce.exists()); - - click(justOnce, "Just Once"); + // It uses an intent chooser now, so just getting the activity by text is enough... + UiObject activity = getVisibleObject(name); + click(activity, name); } public void pressBack() { mDevice.pressBack(); } + + public void turnScreenOn() throws Exception { + mDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP"); + mDevice.executeShellCommand("wm dismiss-keyguard"); + } + }