-
Notifications
You must be signed in to change notification settings - Fork 822
/
BraveSetDefaultBrowserUtils.java
228 lines (189 loc) · 9.92 KB
/
BraveSetDefaultBrowserUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright (c) 2022 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.set_default_browser;
import android.app.Activity;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.preferences.BravePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.ui.widget.Toast;
public class BraveSetDefaultBrowserUtils {
public static final int DEFAULT_BROWSER_ROLE_REQUEST_CODE = 36;
public static final String ANDROID_SETUPWIZARD_PACKAGE_NAME = "com.google.android.setupwizard";
public static final String ANDROID_PACKAGE_NAME = "android";
public static final String BRAVE_BLOG_URL = "https://brave.com/privacy-features/";
public static boolean isBottomSheetVisible;
public static boolean isBraveSetAsDefaultBrowser(Context context) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse(UrlConstants.HTTP_URL_PREFIX));
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(
browserIntent, supportsDefault() ? PackageManager.MATCH_DEFAULT_ONLY : 0);
if (resolveInfo == null || resolveInfo.activityInfo == null
|| resolveInfo.activityInfo.packageName == null
|| ContextUtils.getApplicationContext() == null) {
return false;
}
return resolveInfo.activityInfo.packageName.equals(
BraveActivity.BRAVE_PRODUCTION_PACKAGE_NAME)
|| resolveInfo.activityInfo.packageName.equals(
BraveActivity.BRAVE_BETA_PACKAGE_NAME)
|| resolveInfo.activityInfo.packageName.equals(
BraveActivity.BRAVE_NIGHTLY_PACKAGE_NAME);
}
public static void checkSetDefaultBrowserModal(AppCompatActivity activity) {
if (!isBraveSetAsDefaultBrowser(activity) && !isBraveDefaultDontAsk()) {
boolean shouldShowDefaultBrowserModalAfterP3A =
OnboardingPrefManager.getInstance().shouldShowDefaultBrowserModalAfterP3A();
if (shouldShowDefaultBrowserModalAfterP3A) {
Intent setDefaultBrowserIntent =
new Intent(activity, SetDefaultBrowserActivity.class);
setDefaultBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(setDefaultBrowserIntent);
OnboardingPrefManager.getInstance().setShowDefaultBrowserModalAfterP3A(false);
} else if (SharedPreferencesManager.getInstance().readInt(
BravePreferenceKeys.BRAVE_APP_OPEN_COUNT)
== 5) {
showBraveSetDefaultBrowserDialog(activity, false);
} else if (shouldShowBraveWasDefaultDialog()) {
int braveWasDefaultCount = SharedPreferencesManager.getInstance().readInt(
BravePreferenceKeys.BRAVE_WAS_DEFAULT_ASK_COUNT);
SharedPreferencesManager.getInstance().writeInt(
BravePreferenceKeys.BRAVE_WAS_DEFAULT_ASK_COUNT, braveWasDefaultCount + 1);
showBraveSetDefaultBrowserDialog(activity, false);
}
} else if (isBraveSetAsDefaultBrowser(activity) && !wasBraveDefaultBefore()) {
setBraveDefaultSuccess();
}
}
public static void showBraveSetDefaultBrowserDialog(
AppCompatActivity activity, boolean isFromMenu) {
/* (Albert Wang): Default app settings didn't get added until API 24
* https://developer.android.com/reference/android/provider/Settings#ACTION_MANAGE_DEFAULT_APPS_SETTINGS
*/
if (isBraveSetAsDefaultBrowser(activity)) {
Toast toast = Toast.makeText(
activity, R.string.brave_already_set_as_default_browser, Toast.LENGTH_LONG);
toast.show();
return;
}
if (!isBottomSheetVisible) {
isBottomSheetVisible = true;
if (!isFromMenu) {
int braveDefaultModalCount = SharedPreferencesManager.getInstance().readInt(
BravePreferenceKeys.BRAVE_SET_DEFAULT_BOTTOM_SHEET_COUNT);
SharedPreferencesManager.getInstance().writeInt(
BravePreferenceKeys.BRAVE_SET_DEFAULT_BOTTOM_SHEET_COUNT,
braveDefaultModalCount + 1);
}
SetDefaultBrowserBottomSheetFragment bottomSheetDialog =
SetDefaultBrowserBottomSheetFragment.newInstance(isFromMenu);
bottomSheetDialog.show(
activity.getSupportFragmentManager(), "SetDefaultBrowserBottomSheetFragment");
}
}
public static void setDefaultBrowser(Activity activity) {
int roleManagerOpenCount = SharedPreferencesManager.getInstance().readInt(
BravePreferenceKeys.BRAVE_ROLE_MANAGER_DIALOG_COUNT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && roleManagerOpenCount < 2) {
RoleManager roleManager = activity.getSystemService(RoleManager.class);
if (roleManager.isRoleAvailable(RoleManager.ROLE_BROWSER)) {
if (!roleManager.isRoleHeld(RoleManager.ROLE_BROWSER)) {
// save role manager open count as the second times onwards the dialog is shown,
// the system allows the user to click on "don't show again".
SharedPreferencesManager.getInstance().writeInt(
BravePreferenceKeys.BRAVE_ROLE_MANAGER_DIALOG_COUNT,
roleManagerOpenCount + 1);
activity.startActivityForResult(
roleManager.createRequestRoleIntent(RoleManager.ROLE_BROWSER),
DEFAULT_BROWSER_ROLE_REQUEST_CODE);
}
} else {
openDefaultAppsSettings(activity);
}
} else if (supportsDefault()) {
openDefaultAppsSettings(activity);
} else {
ResolveInfo resolveInfo = getResolveInfo(activity);
if (resolveInfo.activityInfo.packageName.equals(ANDROID_SETUPWIZARD_PACKAGE_NAME)
|| resolveInfo.activityInfo.packageName.equals(ANDROID_PACKAGE_NAME)) {
openBraveBlog(activity);
} else {
Toast toast = Toast.makeText(
activity, R.string.brave_default_browser_go_to_settings, Toast.LENGTH_LONG);
toast.show();
}
}
}
private static boolean supportsDefault() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
}
private static ResolveInfo getResolveInfo(Activity activity) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse(UrlConstants.HTTP_URL_PREFIX));
return activity.getPackageManager().resolveActivity(
browserIntent, supportsDefault() ? PackageManager.MATCH_DEFAULT_ONLY : 0);
}
private static void openDefaultAppsSettings(Activity activity) {
if (activity instanceof OnBraveSetDefaultBrowserListener) {
((OnBraveSetDefaultBrowserListener) activity).OnCheckDefaultResume();
}
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
activity.startActivity(intent);
}
private static void openBraveBlog(Activity activity) {
if (activity instanceof OnBraveSetDefaultBrowserListener) {
((OnBraveSetDefaultBrowserListener) activity).OnCheckDefaultResume();
}
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.brave_set_default_browser_dialog,
(ViewGroup) activity.findViewById(R.id.brave_set_default_browser_toast_container));
Toast toast = new Toast(activity, layout);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 40);
toast.show();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(BRAVE_BLOG_URL));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
private static boolean wasBraveDefaultBefore() {
return SharedPreferencesManager.getInstance().readBoolean(
BravePreferenceKeys.BRAVE_IS_DEFAULT, false);
}
private static boolean shouldShowBraveWasDefaultDialog() {
int braveWasDefaultCount = SharedPreferencesManager.getInstance().readInt(
BravePreferenceKeys.BRAVE_WAS_DEFAULT_ASK_COUNT);
return braveWasDefaultCount < 2 && wasBraveDefaultBefore();
}
public static void setBraveDefaultSuccess() {
SharedPreferencesManager.getInstance().writeBoolean(
BravePreferenceKeys.BRAVE_IS_DEFAULT, true);
}
public static void setBraveDefaultDontAsk() {
SharedPreferencesManager.getInstance().writeBoolean(
BravePreferenceKeys.BRAVE_DEFAULT_DONT_ASK, true);
}
public static boolean isBraveDefaultDontAsk() {
return SharedPreferencesManager.getInstance().readBoolean(
BravePreferenceKeys.BRAVE_DEFAULT_DONT_ASK, false);
}
}