Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Added ability to prevent auto downloading and opening #7279

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ brave_java_resources = [
"java/res/xml/appearance_preferences.xml",
"java/res/xml/background_images_preferences.xml",
"java/res/xml/background_video_playback_preference.xml",
"java/res/xml/brave_download_preferences.xml",
"java/res/xml/brave_license_preferences.xml",
"java/res/xml/brave_main_preferences.xml",
"java/res/xml/brave_privacy_preferences.xml",
Expand Down
2 changes: 2 additions & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/custom_layout/NonSwipeableViewPager.java",
"../../brave/android/java/org/chromium/chrome/browser/custom_layout/VerticalViewPager.java",
"../../brave/android/java/org/chromium/chrome/browser/document/BraveLauncherActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/download/BraveMimeUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/download/settings/BraveDownloadSettings.java",
"../../brave/android/java/org/chromium/chrome/browser/externalnav/BraveExternalNavigationHandler.java",
"../../brave/android/java/org/chromium/chrome/browser/feedback/BraveHelpAndFeedbackLauncherImpl.java",
"../../brave/android/java/org/chromium/chrome/browser/help/BraveHelpAndFeedback.java",
Expand Down
4 changes: 4 additions & 0 deletions android/java/apk_for_test.flags
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@
*** updateNewTabButtonVisibility(...);
*** shouldShowIncognitoToggle(...);
}

-keep class org.chromium.chrome.browser.download.MimeUtils {
*** canAutoOpenMimeType(...);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.download;

import org.chromium.base.ContextUtils;
import org.chromium.chrome.browser.preferences.BravePreferenceKeys;

public class BraveMimeUtils {
public static boolean canAutoOpenMimeType(String mimeType) {
return ContextUtils.getAppSharedPreferences().getBoolean(
BravePreferenceKeys.BRAVE_DOWNLOADS_AUTOMATICALLY_OPEN_WHEN_POSSIBLE, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.download.settings;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.preference.Preference;

import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.BravePreferenceKeys;
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
import org.chromium.components.browser_ui.settings.SettingsUtils;

public class BraveDownloadSettings
extends DownloadSettings implements Preference.OnPreferenceChangeListener {
private static final String PREF_AUTOMATICALLY_OPEN_WHEN_POSSIBLE =
"automatically_open_when_possible";

private ChromeSwitchPreference mAutomaticallyOpenWhenPossiblePref;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SettingsUtils.addPreferencesFromResource(this, R.xml.brave_download_preferences);

mAutomaticallyOpenWhenPossiblePref =
(ChromeSwitchPreference) findPreference(PREF_AUTOMATICALLY_OPEN_WHEN_POSSIBLE);
mAutomaticallyOpenWhenPossiblePref.setOnPreferenceChangeListener(this);
}

@Override
public void onResume() {
super.onResume();
updateDownloadSettings();
}

private void updateDownloadSettings() {
boolean automaticallyOpenWhenPossible = ContextUtils.getAppSharedPreferences().getBoolean(
BravePreferenceKeys.BRAVE_DOWNLOADS_AUTOMATICALLY_OPEN_WHEN_POSSIBLE, true);
mAutomaticallyOpenWhenPossiblePref.setChecked(automaticallyOpenWhenPossible);
}

// Preference.OnPreferenceChangeListener implementation.
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (PREF_AUTOMATICALLY_OPEN_WHEN_POSSIBLE.equals(preference.getKey())) {
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(
BravePreferenceKeys.BRAVE_DOWNLOADS_AUTOMATICALLY_OPEN_WHEN_POSSIBLE,
(boolean) newValue)
.apply();
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.preferences;

public final class BravePreferenceKeys {
public static final String BRAVE_BOTTOM_TOOLBAR_ENABLED_KEY = "brave_bottom_toolbar_enabled_key";
package org.chromium.chrome.browser.preferences;

public final class BravePreferenceKeys {
public static final String BRAVE_BOTTOM_TOOLBAR_ENABLED_KEY =
"brave_bottom_toolbar_enabled_key";
public static final String BRAVE_BOTTOM_TOOLBAR_SET_KEY = "brave_bottom_toolbar_enabled";
public static final String BRAVE_USE_CUSTOM_TABS = "use_custom_tabs";
public static final String BRAVE_APP_OPEN_COUNT = "brave_app_open_count";
public static final String BRAVE_UPDATE_EXTRA_PARAM =
"org.chromium.chrome.browser.upgrade.UPDATE_NOTIFICATION_NEW";
"org.chromium.chrome.browser.upgrade.UPDATE_NOTIFICATION_NEW";
public static final String BRAVE_NOTIFICATION_PREF_NAME =
"org.chromium.chrome.browser.upgrade.NotificationUpdateTimeStampPreferences_New";
"org.chromium.chrome.browser.upgrade.NotificationUpdateTimeStampPreferences_New";
public static final String BRAVE_MILLISECONDS_NAME =
"org.chromium.chrome.browser.upgrade.Milliseconds_New";
}
"org.chromium.chrome.browser.upgrade.Milliseconds_New";
public static final String BRAVE_DOWNLOADS_AUTOMATICALLY_OPEN_WHEN_POSSIBLE =
"org.chromium.chrome.browser.downloads.Automatically_Open_When_Possible";
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class BraveMainPreferencesBase extends BravePreferenceFragment {
private static final String PREF_BRAVE_LANGUAGES = "brave_languages";
private static final String PREF_RATE_BRAVE = "rate_brave";
private static final String PREF_BRAVE_STATS = "brave_stats";
private static final String PREF_BRAVE_DOWNLOADS = "brave_downloads";

private final HashMap<String, Preference> mRemovedPreferences = new HashMap<>();

Expand Down Expand Up @@ -91,6 +92,7 @@ private void updateBravePreferences() {
removePreferenceIfPresent(MainSettings.PREF_SYNC_AND_SERVICES);
removePreferenceIfPresent(MainSettings.PREF_SEARCH_ENGINE);
removePreferenceIfPresent(MainSettings.PREF_UI_THEME);
removePreferenceIfPresent(MainSettings.PREF_DOWNLOADS);
removePreferenceIfPresent(PREF_LANGUAGES);

updateSearchEnginePreference();
Expand Down Expand Up @@ -143,7 +145,7 @@ private void rearrangePreferenceOrders() {
findPreference(PREF_CONTENT_SETTINGS).setOrder(++order);
findPreference(PREF_BRAVE_LANGUAGES).setOrder(++order);
findPreference(MainSettings.PREF_DATA_REDUCTION).setOrder(++order);
findPreference(MainSettings.PREF_DOWNLOADS).setOrder(++order);
findPreference(PREF_BRAVE_DOWNLOADS).setOrder(++order);
// This preference doesn't exist by default in Release mode
if (findPreference(MainSettings.PREF_DEVELOPER) != null) {
findPreference(MainSettings.PREF_DEVELOPER).setOrder(++order);
Expand Down
14 changes: 14 additions & 0 deletions android/java/res/xml/brave_download_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2020 The Brave Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<org.chromium.components.browser_ui.settings.ChromeSwitchPreference
android:key="automatically_open_when_possible"
android:title="@string/automatically_open_when_possible_title"
android:summaryOn="@string/text_on"
android:summaryOff="@string/text_off" />

</PreferenceScreen>
5 changes: 5 additions & 0 deletions android/java/res/xml/brave_main_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@
android:key="brave_languages"
android:order="23"
android:title="@string/language_settings"/>
<org.chromium.components.browser_ui.settings.ChromeBasePreference
android:fragment="org.chromium.chrome.browser.download.settings.BraveDownloadSettings"
android:key="brave_downloads"
android:order="24"
android:title="@string/menu_downloads"/>
</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void testClassesExist() throws Exception {
"org/chromium/chrome/browser/customtabs/features/toolbar/CustomTabToolbar"));
Assert.assertTrue(
classExists("org/chromium/chrome/browser/suggestions/tile/SuggestionsTileView"));
Assert.assertTrue(classExists("org/chromium/chrome/browser/download/MimeUtils"));
}

@Test
Expand Down Expand Up @@ -105,6 +106,8 @@ public void testMethodsExist() throws Exception {
Assert.assertTrue(
methodExists("org/chromium/chrome/browser/toolbar/top/TabSwitcherModeTTPhone",
"shouldShowIncognitoToggle"));
Assert.assertTrue(methodExists(
"org/chromium/chrome/browser/download/MimeUtils", "canAutoOpenMimeType"));
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,9 @@ until they verify, or until 90 days have passed.
<message name="IDS_PLEASE_CHECK_THE_CONNECTION" desc="Connection error string">
Please check your network connection.
</message>
<message name="IDS_AUTOMATICALLY_OPEN_WHEN_POSSIBLE_TITLE" desc="Title for preference that allows the user to open downloads automatically.">
Automatically open when possible
</message>
</messages>
</release>
</grit>
1 change: 1 addition & 0 deletions build/android/bytecode/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_binary("java_bytecode_rewriter") {
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveLaunchIntentDispatcherClassAdapter.java",
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveMainPreferenceBaseClassAdapter.java",
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveManageSyncSettingsClassAdapter.java",
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveMimeUtilsClassAdapter.java",
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveNewTabPageClassAdapter.java",
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveNewTabPageLayoutClassAdapter.java",
"//brave/build/android/bytecode/java/org/brave/bytecode/BraveSearchEngineAdapterClassAdapter.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static ClassVisitor createAdapter(ClassVisitor chain) {
chain = new BraveTabSwitcherModeTTPhoneClassAdapter(chain);
chain = new BraveToolbarLayoutClassAdapter(chain);
chain = new BraveTileViewClassAdapter(chain);
chain = new BraveMimeUtilsClassAdapter(chain);
return chain;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.brave.bytecode;

import org.objectweb.asm.ClassVisitor;

public class BraveMimeUtilsClassAdapter extends BraveClassVisitor {
static String sMimeUtilsClassName = "org/chromium/chrome/browser/download/MimeUtils";

static String sBraveMimeUtilsClassName = "org/chromium/chrome/browser/download/BraveMimeUtils";

public BraveMimeUtilsClassAdapter(ClassVisitor visitor) {
super(visitor);
changeMethodOwner(sMimeUtilsClassName, "canAutoOpenMimeType", sBraveMimeUtilsClassName);
}
}