Commit 710a8ec6 authored by tedchoc's avatar tedchoc Committed by Commit bot

Remove the print option from the menu and make it a share destination.

BUG=629642

Review-Url: https://codereview.chromium.org/2180293002
Cr-Commit-Position: refs/heads/master@{#408280}
parent 1ed89c41
...@@ -535,6 +535,24 @@ by a child template that "extends" this file. ...@@ -535,6 +535,24 @@ by a child template that "extends" this file.
android:process=":browser_restart_process"> android:process=":browser_restart_process">
</activity> </activity>
<!-- This activity is to expose the print option via the generic Android share action. -->
<activity
android:name="org.chromium.chrome.browser.printing.PrintShareActivity"
android:icon="@drawable/print"
android:label="@string/print_share_activity_title"
android:enabled="false"
android:excludeFromRecents="true"
android:exported="true"
android:noHistory="true"
android:theme="@android:style/Theme.NoDisplay"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|mcc|mnc|screenLayout|smallestScreenSize" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<!-- Activity to list Physical Web Urls --> <!-- Activity to list Physical Web Urls -->
<activity android:name="org.chromium.chrome.browser.physicalweb.ListUrlsActivity" <activity android:name="org.chromium.chrome.browser.physicalweb.ListUrlsActivity"
android:label="@string/physical_web_list_urls_activity_title" android:label="@string/physical_web_list_urls_activity_title"
......
...@@ -50,8 +50,6 @@ ...@@ -50,8 +50,6 @@
android:title="@null" /> android:title="@null" />
</menu> </menu>
</item> </item>
<item android:id="@+id/print_id"
android:title="@string/menu_print" />
<item android:id="@+id/find_in_page_id" <item android:id="@+id/find_in_page_id"
android:title="@string/menu_find_in_page" /> android:title="@string/menu_find_in_page" />
<item android:id="@+id/add_to_homescreen_id" <item android:id="@+id/add_to_homescreen_id"
......
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
<item type="id" name="passphrase_type_list" /> <item type="id" name="passphrase_type_list" />
<!-- Keyboard shortcuts --> <!-- Keyboard shortcuts -->
<item type="id" name="print_id" />
<item type="id" name="focus_url_bar" /> <item type="id" name="focus_url_bar" />
<item type="id" name="show_menu" /> <item type="id" name="show_menu" />
<item type="id" name="open_recently_closed_tab" /> <item type="id" name="open_recently_closed_tab" />
......
...@@ -92,6 +92,7 @@ import org.chromium.chrome.browser.partnercustomizations.PartnerBrowserCustomiza ...@@ -92,6 +92,7 @@ import org.chromium.chrome.browser.partnercustomizations.PartnerBrowserCustomiza
import org.chromium.chrome.browser.preferences.ChromePreferenceManager; import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
import org.chromium.chrome.browser.preferences.PrefServiceBridge; import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.preferences.PreferencesLauncher; import org.chromium.chrome.browser.preferences.PreferencesLauncher;
import org.chromium.chrome.browser.printing.PrintShareActivity;
import org.chromium.chrome.browser.printing.TabPrinter; import org.chromium.chrome.browser.printing.TabPrinter;
import org.chromium.chrome.browser.share.ShareHelper; import org.chromium.chrome.browser.share.ShareHelper;
import org.chromium.chrome.browser.snackbar.DataUseSnackbarController; import org.chromium.chrome.browser.snackbar.DataUseSnackbarController;
...@@ -958,6 +959,12 @@ public abstract class ChromeActivity extends AsyncInitializationActivity ...@@ -958,6 +959,12 @@ public abstract class ChromeActivity extends AsyncInitializationActivity
final Tab currentTab = getActivityTab(); final Tab currentTab = getActivityTab();
if (currentTab == null) return; if (currentTab == null) return;
PrintingController printingController = getChromeApplication().getPrintingController();
if (printingController != null && !currentTab.isNativePage() && !printingController.isBusy()
&& PrefServiceBridge.getInstance().isPrintingEnabled()) {
PrintShareActivity.enablePrintShareOption(this);
}
final Activity mainActivity = this; final Activity mainActivity = this;
ContentBitmapCallback callback = new ContentBitmapCallback() { ContentBitmapCallback callback = new ContentBitmapCallback() {
@Override @Override
......
...@@ -23,7 +23,6 @@ import org.chromium.chrome.browser.share.ShareHelper; ...@@ -23,7 +23,6 @@ import org.chromium.chrome.browser.share.ShareHelper;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.util.FeatureUtilities; import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils;
import org.chromium.printing.PrintingController;
import org.chromium.ui.base.DeviceFormFactor; import org.chromium.ui.base.DeviceFormFactor;
/** /**
...@@ -154,14 +153,6 @@ public class AppMenuPropertiesDelegate { ...@@ -154,14 +153,6 @@ public class AppMenuPropertiesDelegate {
? mActivity.getString(R.string.menu_request_desktop_site_on) ? mActivity.getString(R.string.menu_request_desktop_site_on)
: mActivity.getString(R.string.menu_request_desktop_site_off)); : mActivity.getString(R.string.menu_request_desktop_site_off));
PrintingController printingController =
mActivity.getChromeApplication().getPrintingController();
disableEnableMenuItem(menu, R.id.print_id,
printingController != null && !currentTab.isNativePage(),
printingController != null && !printingController.isBusy()
&& PrefServiceBridge.getInstance().isPrintingEnabled(),
PrefServiceBridge.getInstance().isPrintingManaged());
// Only display reader mode settings menu option if the current page is in reader mode. // Only display reader mode settings menu option if the current page is in reader mode.
menu.findItem(R.id.reader_mode_prefs_id) menu.findItem(R.id.reader_mode_prefs_id)
.setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab.getUrl())); .setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab.getUrl()));
......
...@@ -630,9 +630,9 @@ public class CustomTabActivity extends ChromeActivity { ...@@ -630,9 +630,9 @@ public class CustomTabActivity extends ChromeActivity {
public boolean onMenuOrKeyboardAction(int id, boolean fromMenu) { public boolean onMenuOrKeyboardAction(int id, boolean fromMenu) {
// Disable creating new tabs, bookmark, history, print, help, focus_url, etc. // Disable creating new tabs, bookmark, history, print, help, focus_url, etc.
if (id == R.id.focus_url_bar || id == R.id.all_bookmarks_menu_id if (id == R.id.focus_url_bar || id == R.id.all_bookmarks_menu_id
|| id == R.id.print_id || id == R.id.help_id || id == R.id.help_id || id == R.id.recent_tabs_menu_id
|| id == R.id.recent_tabs_menu_id || id == R.id.new_incognito_tab_menu_id || id == R.id.new_incognito_tab_menu_id || id == R.id.new_tab_menu_id
|| id == R.id.new_tab_menu_id || id == R.id.open_history_menu_id) { || id == R.id.open_history_menu_id) {
return true; return true;
} else if (id == R.id.open_in_browser_id) { } else if (id == R.id.open_in_browser_id) {
openCurrentUrlInBrowser(false, true); openCurrentUrlInBrowser(false, true);
......
// Copyright 2016 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.printing;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.ApplicationStatus.ActivityStateListener;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.share.ShareHelper;
import org.chromium.chrome.browser.util.IntentUtils;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* A simple activity that allows Chrome to expose print as an option in the share menu.
*/
public class PrintShareActivity extends AppCompatActivity {
private static Set<Activity> sPendingShareActivities = new HashSet<>();
private static ActivityStateListener sStateListener;
/**
* Enable the print sharing option.
*
* @param activity The activity that will be triggering the share action. The activitiy's
* state will be tracked to disable the print option when the share operation
* has been completed.
*/
public static void enablePrintShareOption(Activity activity) {
ThreadUtils.assertOnUiThread();
if (sStateListener == null) {
sStateListener = new ActivityStateListener() {
@Override
public void onActivityStateChange(Activity activity, int newState) {
if (newState == ActivityState.PAUSED) return;
unregisterActivity(activity);
}
};
}
ApplicationStatus.registerStateListenerForAllActivities(sStateListener);
boolean wasEmpty = sPendingShareActivities.isEmpty();
sPendingShareActivities.add(activity);
if (wasEmpty) {
activity.getPackageManager().setComponentEnabledSetting(
new ComponentName(activity, PrintShareActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
}
private static void unregisterActivity(Activity activity) {
sPendingShareActivities.remove(activity);
if (!sPendingShareActivities.isEmpty()) return;
ApplicationStatus.unregisterActivityStateListener(sStateListener);
Context context = ContextUtils.getApplicationContext();
context.getPackageManager().setComponentEnabledSetting(
new ComponentName(context, PrintShareActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Intent intent = getIntent();
if (intent == null) return;
if (!Intent.ACTION_SEND.equals(intent.getAction())) return;
if (!IntentUtils.safeHasExtra(getIntent(), ShareHelper.EXTRA_TASK_ID)) return;
handlePrintAction();
} finally {
finish();
}
}
private void handlePrintAction() {
int triggeringTaskId =
IntentUtils.safeGetIntExtra(getIntent(), ShareHelper.EXTRA_TASK_ID, 0);
List<WeakReference<Activity>> activities = ApplicationStatus.getRunningActivities();
ChromeActivity triggeringActivity = null;
for (int i = 0; i < activities.size(); i++) {
Activity activity = activities.get(i).get();
if (activity == null) continue;
// Since the share intent is triggered without NEW_TASK or NEW_DOCUMENT, the task ID
// of this activity will match that of the triggering activity.
if (activity.getTaskId() == triggeringTaskId
&& activity instanceof ChromeActivity) {
triggeringActivity = (ChromeActivity) activity;
break;
}
}
if (triggeringActivity == null) return;
unregisterActivity(triggeringActivity);
triggeringActivity.onMenuOrKeyboardAction(R.id.print_id, true);
}
}
...@@ -59,6 +59,9 @@ public class ShareHelper { ...@@ -59,6 +59,9 @@ public class ShareHelper {
private static final String TAG = "share"; private static final String TAG = "share";
/** The task ID of the activity that triggered the share action. */
public static final String EXTRA_TASK_ID = "org.chromium.chrome.extra.TASK_ID";
private static final String JPEG_EXTENSION = ".jpg"; private static final String JPEG_EXTENSION = ".jpg";
private static final String PACKAGE_NAME_KEY = "last_shared_package_name"; private static final String PACKAGE_NAME_KEY = "last_shared_package_name";
private static final String CLASS_NAME_KEY = "last_shared_class_name"; private static final String CLASS_NAME_KEY = "last_shared_class_name";
...@@ -140,7 +143,7 @@ public class ShareHelper { ...@@ -140,7 +143,7 @@ public class ShareHelper {
ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT); ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT);
if (target != null) { if (target != null) {
setLastShareComponentName(context, target); setLastShareComponentName(target);
} }
} }
} }
...@@ -259,7 +262,7 @@ public class ShareHelper { ...@@ -259,7 +262,7 @@ public class ShareHelper {
*/ */
private static void showShareDialog(final Activity activity, final String title, private static void showShareDialog(final Activity activity, final String title,
final String url, final Bitmap screenshot) { final String url, final Bitmap screenshot) {
Intent intent = getShareIntent(title, url, null); Intent intent = getShareIntent(activity, title, url, null);
PackageManager manager = activity.getPackageManager(); PackageManager manager = activity.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0); List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
assert resolveInfoList.size() > 0; assert resolveInfoList.size() > 0;
...@@ -281,7 +284,7 @@ public class ShareHelper { ...@@ -281,7 +284,7 @@ public class ShareHelper {
ActivityInfo ai = info.activityInfo; ActivityInfo ai = info.activityInfo;
ComponentName component = ComponentName component =
new ComponentName(ai.applicationInfo.packageName, ai.name); new ComponentName(ai.applicationInfo.packageName, ai.name);
setLastShareComponentName(activity, component); setLastShareComponentName(component);
makeIntentAndShare(activity, title, url, screenshot, component); makeIntentAndShare(activity, title, url, screenshot, component);
dialog.dismiss(); dialog.dismiss();
} }
...@@ -298,7 +301,7 @@ public class ShareHelper { ...@@ -298,7 +301,7 @@ public class ShareHelper {
*/ */
private static void shareWithLastUsed( private static void shareWithLastUsed(
Activity activity, String title, String url, Bitmap screenshot) { Activity activity, String title, String url, Bitmap screenshot) {
ComponentName component = getLastShareComponentName(activity); ComponentName component = getLastShareComponentName();
if (component == null) return; if (component == null) return;
makeIntentAndShare(activity, title, url, screenshot, component); makeIntentAndShare(activity, title, url, screenshot, component);
} }
...@@ -315,7 +318,8 @@ public class ShareHelper { ...@@ -315,7 +318,8 @@ public class ShareHelper {
private static void makeIntentAndShare(final Activity activity, final String title, private static void makeIntentAndShare(final Activity activity, final String title,
final String url, final Bitmap screenshot, final ComponentName component) { final String url, final Bitmap screenshot, final ComponentName component) {
if (screenshot == null) { if (screenshot == null) {
shareIntent(activity, getDirectShareIntentForComponent(title, url, null, component)); shareIntent(activity,
getDirectShareIntentForComponent(activity, title, url, null, component));
} else { } else {
new AsyncTask<Void, Void, File>() { new AsyncTask<Void, Void, File>() {
@Override @Override
...@@ -355,7 +359,7 @@ public class ShareHelper { ...@@ -355,7 +359,7 @@ public class ShareHelper {
Uri screenshotUri = saveFile == null Uri screenshotUri = saveFile == null
? null : UiUtils.getUriForImageCaptureFile(activity, saveFile); ? null : UiUtils.getUriForImageCaptureFile(activity, saveFile);
shareIntent(activity, getDirectShareIntentForComponent( shareIntent(activity, getDirectShareIntentForComponent(
title, url, screenshotUri, component)); activity, title, url, screenshotUri, component));
} }
} }
}.execute(); }.execute();
...@@ -372,10 +376,10 @@ public class ShareHelper { ...@@ -372,10 +376,10 @@ public class ShareHelper {
Drawable directShareIcon = null; Drawable directShareIcon = null;
CharSequence directShareTitle = null; CharSequence directShareTitle = null;
final ComponentName component = getLastShareComponentName(activity); final ComponentName component = getLastShareComponentName();
boolean isComponentValid = false; boolean isComponentValid = false;
if (component != null) { if (component != null) {
Intent intent = getShareIntent("", "", null); Intent intent = getShareIntent(activity, "", "", null);
intent.setPackage(component.getPackageName()); intent.setPackage(component.getPackageName());
PackageManager manager = activity.getPackageManager(); PackageManager manager = activity.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0); List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
...@@ -434,13 +438,16 @@ public class ShareHelper { ...@@ -434,13 +438,16 @@ public class ShareHelper {
} }
@VisibleForTesting @VisibleForTesting
public static Intent getShareIntent(String title, String url, Uri screenshotUri) { protected static Intent getShareIntent(
Activity activity, String title, String url, Uri screenshotUri) {
url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url); url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag());
intent.setType("text/plain"); intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, title); intent.putExtra(Intent.EXTRA_SUBJECT, title);
intent.putExtra(Intent.EXTRA_TEXT, url); intent.putExtra(Intent.EXTRA_TEXT, url);
intent.putExtra(EXTRA_TASK_ID, activity.getTaskId());
if (screenshotUri != null) { if (screenshotUri != null) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// To give read access to an Intent target, we need to put |screenshotUri| in clipData // To give read access to an Intent target, we need to put |screenshotUri| in clipData
...@@ -462,15 +469,16 @@ public class ShareHelper { ...@@ -462,15 +469,16 @@ public class ShareHelper {
} }
private static Intent getDirectShareIntentForComponent( private static Intent getDirectShareIntentForComponent(
String title, String url, Uri screenshotUri, ComponentName component) { Activity activity, String title, String url,
Intent intent = getShareIntent(title, url, screenshotUri); Uri screenshotUri, ComponentName component) {
Intent intent = getShareIntent(activity, title, url, screenshotUri);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
| Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setComponent(component); intent.setComponent(component);
return intent; return intent;
} }
private static ComponentName getLastShareComponentName(Context context) { private static ComponentName getLastShareComponentName() {
SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
String packageName = preferences.getString(PACKAGE_NAME_KEY, null); String packageName = preferences.getString(PACKAGE_NAME_KEY, null);
String className = preferences.getString(CLASS_NAME_KEY, null); String className = preferences.getString(CLASS_NAME_KEY, null);
...@@ -478,7 +486,7 @@ public class ShareHelper { ...@@ -478,7 +486,7 @@ public class ShareHelper {
return new ComponentName(packageName, className); return new ComponentName(packageName, className);
} }
private static void setLastShareComponentName(Context context, ComponentName component) { private static void setLastShareComponentName(ComponentName component) {
SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = preferences.edit(); SharedPreferences.Editor editor = preferences.edit();
editor.putString(PACKAGE_NAME_KEY, component.getPackageName()); editor.putString(PACKAGE_NAME_KEY, component.getPackageName());
......
...@@ -2460,6 +2460,11 @@ However, you aren’t invisible. Going incognito doesn’t hide your browsing fr ...@@ -2460,6 +2460,11 @@ However, you aren’t invisible. Going incognito doesn’t hide your browsing fr
Web App Web App
</message> </message>
<!-- Share targets -->
<message name="IDS_PRINT_SHARE_ACTIVITY_TITLE" desc="Title of the Print Share activity that will appear in the Android share dialog.">
Print
</message>
<!-- Omnibox Autocomplete --> <!-- Omnibox Autocomplete -->
<message name="IDS_OMNIBOX_CONFIRM_DELETE" desc="A message within a modal dialog that confirms the user wants to delete a suggestion from the list dropdown in the omnibox"> <message name="IDS_OMNIBOX_CONFIRM_DELETE" desc="A message within a modal dialog that confirms the user wants to delete a suggestion from the list dropdown in the omnibox">
Remove suggestion from history? Remove suggestion from history?
......
...@@ -760,6 +760,7 @@ chrome_java_sources = [ ...@@ -760,6 +760,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/prerender/ChromePrerenderService.java", "java/src/org/chromium/chrome/browser/prerender/ChromePrerenderService.java",
"java/src/org/chromium/chrome/browser/prerender/ExternalPrerenderHandler.java", "java/src/org/chromium/chrome/browser/prerender/ExternalPrerenderHandler.java",
"java/src/org/chromium/chrome/browser/printing/PrintingControllerFactory.java", "java/src/org/chromium/chrome/browser/printing/PrintingControllerFactory.java",
"java/src/org/chromium/chrome/browser/printing/PrintShareActivity.java",
"java/src/org/chromium/chrome/browser/printing/TabPrinter.java", "java/src/org/chromium/chrome/browser/printing/TabPrinter.java",
"java/src/org/chromium/chrome/browser/profiles/MostVisitedSites.java", "java/src/org/chromium/chrome/browser/profiles/MostVisitedSites.java",
"java/src/org/chromium/chrome/browser/profiles/Profile.java", "java/src/org/chromium/chrome/browser/profiles/Profile.java",
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.share; package org.chromium.chrome.browser.share;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
...@@ -24,7 +25,7 @@ public class ShareUrlTest extends NativeLibraryTestBase { ...@@ -24,7 +25,7 @@ public class ShareUrlTest extends NativeLibraryTestBase {
} }
private void assertCorrectUrl(String originalUrl, String sharedUrl) { private void assertCorrectUrl(String originalUrl, String sharedUrl) {
Intent intent = ShareHelper.getShareIntent("", sharedUrl, null); Intent intent = ShareHelper.getShareIntent(new Activity(), "", sharedUrl, null);
assert (intent.hasExtra(Intent.EXTRA_TEXT)); assert (intent.hasExtra(Intent.EXTRA_TEXT));
String url = intent.getStringExtra(Intent.EXTRA_TEXT); String url = intent.getStringExtra(Intent.EXTRA_TEXT);
assertEquals(originalUrl, url); assertEquals(originalUrl, url);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment