diff --git a/res/values/strings.xml b/res/values/strings.xml index 89e40ac4cdc4dabf23778e5bc8b10c8b9c6858da..72abecbb5499bbf426a99d9b131fd7aae4ec085d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -551,7 +551,7 @@ Allow %1$s to access files in %2$s? - This will let %1$s access current and future content stored in %2$s. + This will let "%1$s" access current and future content stored in %2$s. Can\u2019t use this folder diff --git a/src/com/android/documentsui/base/Shared.java b/src/com/android/documentsui/base/Shared.java index ac089999fdcdd1444bfc052ce0d301a0fe9e5a00..bb8a393934ef71ba8c2cb630e7c8b1d865b89e5f 100644 --- a/src/com/android/documentsui/base/Shared.java +++ b/src/com/android/documentsui/base/Shared.java @@ -16,6 +16,9 @@ package com.android.documentsui.base; +import static android.text.TextUtils.SAFE_STRING_FLAG_SINGLE_LINE; +import static android.text.TextUtils.SAFE_STRING_FLAG_TRIM; + import static com.android.documentsui.base.SharedMinimal.TAG; import static com.android.documentsui.ChangeIds.RESTRICT_STORAGE_ACCESS_FRAMEWORK; @@ -265,7 +268,7 @@ public final class Shared { * @return the calling app name or general anonymous name if not found */ @NonNull - public static String getCallingAppName(Activity activity) { + public static CharSequence getCallingAppName(Activity activity) { final String anonymous = activity.getString(R.string.anonymous_application); final String packageName = getCallingPackageName(activity); if (TextUtils.isEmpty(packageName)) { @@ -281,7 +284,12 @@ public final class Shared { } CharSequence result = pm.getApplicationLabel(ai); - return TextUtils.isEmpty(result) ? anonymous : result.toString(); + if (TextUtils.isEmpty(result)) { + return anonymous; + } + + return TextUtils.makeSafeForPresentation( + result.toString(), 500, 0, SAFE_STRING_FLAG_TRIM | SAFE_STRING_FLAG_SINGLE_LINE); } /** diff --git a/src/com/android/documentsui/dirlist/DirectoryFragment.java b/src/com/android/documentsui/dirlist/DirectoryFragment.java index e5ce226da6f10c90793c77a5e7fdbc6fcab34333..df4edf343c431d1b7b98f67fd3bdbd29be1fba7a 100644 --- a/src/com/android/documentsui/dirlist/DirectoryFragment.java +++ b/src/com/android/documentsui/dirlist/DirectoryFragment.java @@ -1569,10 +1569,5 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On public ActionHandler getActionHandler() { return mActions; } - - @Override - public String getCallingAppName() { - return Shared.getCallingAppName(mActivity); - } } } diff --git a/src/com/android/documentsui/dirlist/DocumentsAdapter.java b/src/com/android/documentsui/dirlist/DocumentsAdapter.java index 41ce73c8ca13207d4f5bc18787d57f032d092c8d..b32c15335bb65d021bb280fd443e7feb84d2c38f 100644 --- a/src/com/android/documentsui/dirlist/DocumentsAdapter.java +++ b/src/com/android/documentsui/dirlist/DocumentsAdapter.java @@ -90,7 +90,6 @@ public abstract class DocumentsAdapter extends RecyclerView.Adapter + + @Before + fun setUp() { + MockitoAnnotations.openMocks(this) + whenever(mockActivity.resources).thenReturn(resources) + whenever(mockActivity.packageManager).thenReturn(pm) + whenever(resources.getString(R.string.anonymous_application)).thenReturn(ANONYMOUS_PACKAGE) + whenever(mockActivity.callingPackage).thenReturn(PACKAGE_NAME) + } + + @Test + fun testNameIsSanitized() { + val info = ApplicationInfo() + whenever(pm.getApplicationInfo(PACKAGE_NAME, 0)).thenReturn(info) + + whenever(pm.getApplicationLabel(eq(info))).thenReturn(testData.first) + assertEquals(Shared.getCallingAppName(mockActivity), testData.second) + } +}