forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Add a transparent Activity in Chrome to handle Browser Acti…
…on Intent Add a transparent Activity in Chrome to handle the Browser Action Intent. This CL also deals with parsing the information of Browser Action request from the Intent. Later, this Activity will open a context menu from Chrome with the request information. BUG=706696 Review-Url: https://codereview.chromium.org/2786283002 Cr-Commit-Position: refs/heads/master@{#467837}
- Loading branch information
1 parent
ca74aee
commit 3e1e45d
Showing
9 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...me/android/java/src/org/chromium/chrome/browser/browseractions/BrowserActionActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.chrome.browser.browseractions; | ||
|
||
import android.app.PendingIntent; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.support.customtabs.browseractions.BrowserActionItem; | ||
import android.support.customtabs.browseractions.BrowserActionsIntent; | ||
|
||
import org.chromium.base.Log; | ||
import org.chromium.base.annotations.SuppressFBWarnings; | ||
import org.chromium.chrome.browser.IntentHandler; | ||
import org.chromium.chrome.browser.UrlConstants; | ||
import org.chromium.chrome.browser.init.AsyncInitializationActivity; | ||
import org.chromium.chrome.browser.util.IntentUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* A transparent {@link AsyncInitializationActivity} that displays the browser action context menu. | ||
*/ | ||
public class BrowserActionActivity extends AsyncInitializationActivity { | ||
private static final String TAG = "BrowserActions"; | ||
|
||
private int mType; | ||
private Uri mUri; | ||
private String mCreatorPackageName; | ||
private List<BrowserActionItem> mActions = new ArrayList<>(); | ||
|
||
@Override | ||
protected void setContentView() { | ||
openContextMenu(); | ||
} | ||
|
||
@Override | ||
@SuppressFBWarnings("URF_UNREAD_FIELD") | ||
protected boolean isStartedUpCorrectly(Intent intent) { | ||
if (intent == null | ||
|| !BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN.equals(intent.getAction())) { | ||
return false; | ||
} | ||
mUri = Uri.parse(IntentHandler.getUrlFromIntent(intent)); | ||
mType = IntentUtils.safeGetIntExtra( | ||
intent, BrowserActionsIntent.EXTRA_TYPE, BrowserActionsIntent.URL_TYPE_NONE); | ||
mCreatorPackageName = BrowserActionsIntent.getCreatorPackageName(intent); | ||
ArrayList<Bundle> bundles = IntentUtils.getParcelableArrayListExtra( | ||
intent, BrowserActionsIntent.EXTRA_MENU_ITEMS); | ||
if (bundles != null) { | ||
parseBrowserActionItems(bundles); | ||
} | ||
if (mUri == null) { | ||
Log.e(TAG, "Missing url"); | ||
return false; | ||
} else if (!UrlConstants.HTTP_SCHEME.equals(mUri.getScheme()) | ||
&& !UrlConstants.HTTPS_SCHEME.equals(mUri.getScheme())) { | ||
Log.e(TAG, "Url should only be HTTP or HTTPS scheme"); | ||
return false; | ||
} else if (mCreatorPackageName == null) { | ||
Log.e(TAG, "Missing creator's pacakge name"); | ||
return false; | ||
} else if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { | ||
Log.e(TAG, "Intent should not be started with FLAG_ACTIVITY_NEW_TASK"); | ||
return false; | ||
} else if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0) { | ||
Log.e(TAG, "Intent should not be started with FLAG_ACTIVITY_NEW_DOCUMENT"); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Opens a Browser Actions context menu based on the parsed data. | ||
*/ | ||
public void openContextMenu() { | ||
return; | ||
} | ||
|
||
@Override | ||
protected boolean shouldDelayBrowserStartup() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean shouldStartGpuProcess() { | ||
return true; | ||
} | ||
|
||
/** | ||
* Gets custom item list for browser action menu. | ||
* @param bundles Data for custom items from {@link BrowserActionsIntent}. | ||
*/ | ||
private void parseBrowserActionItems(ArrayList<Bundle> bundles) { | ||
for (int i = 0; i < bundles.size(); i++) { | ||
Bundle bundle = bundles.get(i); | ||
String title = IntentUtils.safeGetString(bundle, BrowserActionsIntent.KEY_TITLE); | ||
PendingIntent action = | ||
IntentUtils.safeGetParcelable(bundle, BrowserActionsIntent.KEY_ACTION); | ||
Bitmap icon = IntentUtils.safeGetParcelable(bundle, BrowserActionsIntent.KEY_ICON); | ||
if (title != null && action != null) { | ||
BrowserActionItem item = new BrowserActionItem(title, action); | ||
if (icon != null) { | ||
item.setIcon(icon); | ||
} | ||
mActions.add(item); | ||
} else if (title != null) { | ||
Log.e(TAG, "Missing action for item: " + i); | ||
} else if (action != null) { | ||
Log.e(TAG, "Missing title for item: " + i); | ||
} else { | ||
Log.e(TAG, "Missing title and action for item: " + i); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Callback when Browser Actions menu dialog is shown. | ||
*/ | ||
private void onMenuShown() { | ||
beginLoadingLibrary(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...droid/junit/src/org/chromium/chrome/browser/browseractions/BrowserActionActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.chrome.browser.browseractions; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.doAnswer; | ||
|
||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.support.customtabs.browseractions.BrowserActionsIntent; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
|
||
import org.chromium.base.test.util.Feature; | ||
import org.chromium.testing.local.LocalRobolectricTestRunner; | ||
|
||
/** | ||
* Unit tests for BrowserActionActivity. | ||
*/ | ||
@RunWith(LocalRobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class BrowserActionActivityTest { | ||
private static final String HTTP_SCHEME_TEST_URL = "http://www.example.com"; | ||
private static final String HTTPS_SCHEME_TEST_URL = "https://www.example.com"; | ||
private static final String CHROME_SCHEME_TEST_URL = "chrome://example"; | ||
private static final String CONTENT_SCHEME_TEST_URL = "content://example"; | ||
|
||
private BrowserActionActivity mActivity = new BrowserActionActivity(); | ||
private Context mContext; | ||
|
||
@Mock | ||
private PendingIntent mPendingIntent; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
mContext = RuntimeEnvironment.application; | ||
Answer<String> answer = new Answer<String>() { | ||
@Override | ||
public String answer(InvocationOnMock invocation) { | ||
return "some.other.app.package.name"; | ||
} | ||
}; | ||
doAnswer(answer).when(mPendingIntent).getCreatorPackage(); | ||
} | ||
|
||
@Test | ||
@Feature({"BrowserActions"}) | ||
public void testStartedUpCorrectly() { | ||
assertFalse(mActivity.isStartedUpCorrectly(null)); | ||
assertFalse(mActivity.isStartedUpCorrectly(new Intent())); | ||
|
||
Intent mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); | ||
assertTrue(mActivity.isStartedUpCorrectly(mIntent)); | ||
|
||
mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); | ||
mIntent.removeExtra(BrowserActionsIntent.EXTRA_APP_ID); | ||
assertFalse(mActivity.isStartedUpCorrectly(mIntent)); | ||
|
||
mIntent = createBaseBrowserActionsIntent(HTTPS_SCHEME_TEST_URL); | ||
assertTrue(mActivity.isStartedUpCorrectly(mIntent)); | ||
|
||
mIntent = createBaseBrowserActionsIntent(CHROME_SCHEME_TEST_URL); | ||
assertFalse(mActivity.isStartedUpCorrectly(mIntent)); | ||
|
||
mIntent = createBaseBrowserActionsIntent(CONTENT_SCHEME_TEST_URL); | ||
assertFalse(mActivity.isStartedUpCorrectly(mIntent)); | ||
|
||
mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); | ||
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
assertFalse(mActivity.isStartedUpCorrectly(mIntent)); | ||
|
||
mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); | ||
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); | ||
assertFalse(mActivity.isStartedUpCorrectly(mIntent)); | ||
} | ||
|
||
/** | ||
* Creates a simple Intent for Browser Actions which contains a url, the {@link | ||
* BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN} action and source package name. | ||
* @param url The url for the data of the Intent. | ||
* @return The simple Intent for Browser Actions. | ||
*/ | ||
private Intent createBaseBrowserActionsIntent(String url) { | ||
return new BrowserActionsIntent.Builder(mContext, Uri.parse(url)) | ||
.build() | ||
.getIntent() | ||
.putExtra(BrowserActionsIntent.EXTRA_APP_ID, mPendingIntent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters