Commit 8325ce7b authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Split logic in ShareHelper when origin is Chrome vs other apps.

This is a refactoring to be able to register the SharedPrefs keys
declared in ShareHelper in ChromePreferenceKeys. Only the keys used for
the main SharedPrefs file need to be registered.

Bug: 1022108
Change-Id: Ia69e0a8c02f374662cdbed70010ad50bafe76b05
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1992501
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#731758}
parent 2d85689d
...@@ -243,7 +243,8 @@ public class ContextMenuHelper implements OnCreateContextMenuListener { ...@@ -243,7 +243,8 @@ public class ContextMenuHelper implements OnCreateContextMenuListener {
*/ */
private void shareImageWithLastShareComponent() { private void shareImageWithLastShareComponent() {
retrieveImage((Uri imageUri) -> { retrieveImage((Uri imageUri) -> {
ShareHelper.shareImage(mWindow, ShareHelper.getLastShareComponentName(null), imageUri); ShareHelper.shareImage(
mWindow, ShareHelper.getLastShareByChromeComponentName(), imageUri);
}); });
} }
......
...@@ -33,6 +33,7 @@ import android.view.View; ...@@ -33,6 +33,7 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -49,6 +50,8 @@ import org.chromium.base.StrictModeContext; ...@@ -49,6 +50,8 @@ import org.chromium.base.StrictModeContext;
import org.chromium.base.metrics.CachedMetrics; import org.chromium.base.metrics.CachedMetrics;
import org.chromium.base.task.AsyncTask; import org.chromium.base.task.AsyncTask;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.content_public.browser.RenderWidgetHostView; import org.chromium.content_public.browser.RenderWidgetHostView;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.UiUtils; import org.chromium.ui.UiUtils;
...@@ -90,8 +93,8 @@ public class ShareHelper { ...@@ -90,8 +93,8 @@ public class ShareHelper {
public static final String EXTRA_TASK_ID = "org.chromium.chrome.extra.TASK_ID"; 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_SUFFIX = "last_shared_package_name";
private static final String CLASS_NAME_KEY = "last_shared_class_name"; private static final String CLASS_NAME_KEY_SUFFIX = "last_shared_class_name";
private static final String EXTRA_SHARE_SCREENSHOT_AS_STREAM = "share_screenshot_as_stream"; private static final String EXTRA_SHARE_SCREENSHOT_AS_STREAM = "share_screenshot_as_stream";
/** /**
...@@ -666,13 +669,26 @@ public class ShareHelper { ...@@ -666,13 +669,26 @@ public class ShareHelper {
@VisibleForTesting @VisibleForTesting
public static void setLastShareComponentName( public static void setLastShareComponentName(
ComponentName component, @Nullable String sourcePackageName) { ComponentName component, @Nullable String sourcePackageName) {
SharedPreferences preferences = getSharePreferences(sourcePackageName); if (sourcePackageName == null) {
setLastShareComponentNameForChrome(component);
return;
}
SharedPreferences preferences = getExternalAppSharingSharedPreferences();
SharedPreferences.Editor editor = preferences.edit(); SharedPreferences.Editor editor = preferences.edit();
editor.putString(getPackageNameKey(sourcePackageName), component.getPackageName()); editor.putString(getPackageNameKey(sourcePackageName), component.getPackageName());
editor.putString(getClassNameKey(sourcePackageName), component.getClassName()); editor.putString(getClassNameKey(sourcePackageName), component.getClassName());
editor.apply(); editor.apply();
} }
private static void setLastShareComponentNameForChrome(ComponentName component) {
SharedPreferencesManager preferencesManager = SharedPreferencesManager.getInstance();
preferencesManager.writeString(
ChromePreferenceKeys.SHARING_LAST_SHARED_PACKAGE_NAME, component.getPackageName());
preferencesManager.writeString(
ChromePreferenceKeys.SHARING_LAST_SHARED_CLASS_NAME, component.getClassName());
}
@VisibleForTesting @VisibleForTesting
public static Intent getShareLinkIntent(ShareParams params) { public static Intent getShareLinkIntent(ShareParams params) {
final boolean isFileShare = (params.getFileUris() != null); final boolean isFileShare = (params.getFileUris() != null);
...@@ -756,25 +772,44 @@ public class ShareHelper { ...@@ -756,25 +772,44 @@ public class ShareHelper {
*/ */
@Nullable @Nullable
public static ComponentName getLastShareComponentName(@Nullable String sourcePackageName) { public static ComponentName getLastShareComponentName(@Nullable String sourcePackageName) {
SharedPreferences preferences = getSharePreferences(sourcePackageName); if (sourcePackageName == null) {
return getLastShareByChromeComponentName();
}
SharedPreferences preferences = getExternalAppSharingSharedPreferences();
String packageName = preferences.getString(getPackageNameKey(sourcePackageName), null); String packageName = preferences.getString(getPackageNameKey(sourcePackageName), null);
String className = preferences.getString(getClassNameKey(sourcePackageName), null); String className = preferences.getString(getClassNameKey(sourcePackageName), null);
return createComponentName(packageName, className);
}
/**
* Gets the {@link ComponentName} of the app that was used to last share by Chrome.
*/
@Nullable
public static ComponentName getLastShareByChromeComponentName() {
SharedPreferencesManager preferencesManager = SharedPreferencesManager.getInstance();
String packageName = preferencesManager.readString(
ChromePreferenceKeys.SHARING_LAST_SHARED_PACKAGE_NAME, null);
String className = preferencesManager.readString(
ChromePreferenceKeys.SHARING_LAST_SHARED_CLASS_NAME, null);
return createComponentName(packageName, className);
}
private static ComponentName createComponentName(String packageName, String className) {
if (packageName == null || className == null) return null; if (packageName == null || className == null) return null;
return new ComponentName(packageName, className); return new ComponentName(packageName, className);
} }
private static SharedPreferences getSharePreferences(@Nullable String sourcePackageName) { private static SharedPreferences getExternalAppSharingSharedPreferences() {
return sourcePackageName != null return ContextUtils.getApplicationContext().getSharedPreferences(
? ContextUtils.getApplicationContext().getSharedPreferences( EXTERNAL_APP_SHARING_PREF_FILE_NAME, Context.MODE_PRIVATE);
EXTERNAL_APP_SHARING_PREF_FILE_NAME, Context.MODE_PRIVATE)
: ContextUtils.getAppSharedPreferences();
} }
private static String getPackageNameKey(@Nullable String sourcePackageName) { private static String getPackageNameKey(@NonNull String sourcePackageName) {
return (TextUtils.isEmpty(sourcePackageName) ? "" : sourcePackageName) + PACKAGE_NAME_KEY; return sourcePackageName + PACKAGE_NAME_KEY_SUFFIX;
} }
private static String getClassNameKey(@Nullable String sourcePackageName) { private static String getClassNameKey(@NonNull String sourcePackageName) {
return (TextUtils.isEmpty(sourcePackageName) ? "" : sourcePackageName) + CLASS_NAME_KEY; return sourcePackageName + CLASS_NAME_KEY_SUFFIX;
} }
} }
...@@ -457,6 +457,9 @@ public final class ChromePreferenceKeys { ...@@ -457,6 +457,9 @@ public final class ChromePreferenceKeys {
public static final String SEARCH_ENGINE_CHOICE_REQUESTED_TIMESTAMP = public static final String SEARCH_ENGINE_CHOICE_REQUESTED_TIMESTAMP =
"search_engine_choice_requested_timestamp"; "search_engine_choice_requested_timestamp";
public static final String SHARING_LAST_SHARED_CLASS_NAME = "last_shared_class_name";
public static final String SHARING_LAST_SHARED_PACKAGE_NAME = "last_shared_package_name";
/** /**
* Generic signin and sync promo preferences. * Generic signin and sync promo preferences.
*/ */
...@@ -728,6 +731,8 @@ public final class ChromePreferenceKeys { ...@@ -728,6 +731,8 @@ public final class ChromePreferenceKeys {
SEARCH_ENGINE_CHOICE_DEFAULT_TYPE_BEFORE, SEARCH_ENGINE_CHOICE_DEFAULT_TYPE_BEFORE,
SEARCH_ENGINE_CHOICE_PRESENTED_VERSION, SEARCH_ENGINE_CHOICE_PRESENTED_VERSION,
SEARCH_ENGINE_CHOICE_REQUESTED_TIMESTAMP, SEARCH_ENGINE_CHOICE_REQUESTED_TIMESTAMP,
SHARING_LAST_SHARED_CLASS_NAME,
SHARING_LAST_SHARED_PACKAGE_NAME,
SIGNIN_AND_SYNC_PROMO_SHOW_COUNT, SIGNIN_AND_SYNC_PROMO_SHOW_COUNT,
SIGNIN_PROMO_IMPRESSIONS_COUNT_BOOKMARKS, SIGNIN_PROMO_IMPRESSIONS_COUNT_BOOKMARKS,
SIGNIN_PROMO_IMPRESSIONS_COUNT_SETTINGS, SIGNIN_PROMO_IMPRESSIONS_COUNT_SETTINGS,
......
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