Skip to content

Commit

Permalink
Merge pull request #5339 from brave/pr5299_android_anrs_1.8.x
Browse files Browse the repository at this point in the history
Eliminate ANRs (uplift to 1.8.x)
  • Loading branch information
kjozwiak authored Apr 26, 2020
2 parents 51b6425 + e31dd35 commit eb74ea5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 49 deletions.
21 changes: 14 additions & 7 deletions android/java/org/chromium/chrome/browser/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.task.PostTask;
import org.chromium.base.task.TaskTraits;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveHelper;
import org.chromium.chrome.browser.BraveSyncWorker;
Expand Down Expand Up @@ -242,13 +244,18 @@ private void createNotificationChannel() {
}

private void setupBraveSetDefaultBrowserNotification() {
Context context = ContextUtils.getApplicationContext();
if (BraveSetDefaultBrowserNotificationService.isBraveSetAsDefaultBrowser(this)) {
// Don't ask again
return;
}
Intent intent = new Intent(context, BraveSetDefaultBrowserNotificationService.class);
context.sendBroadcast(intent);
// Post task to IO thread because isBraveSetAsDefaultBrowser may cause
// sqlite file IO operation underneath
PostTask.postTask(TaskTraits.BEST_EFFORT_MAY_BLOCK, () ->
{
Context context = ContextUtils.getApplicationContext();
if (BraveSetDefaultBrowserNotificationService.isBraveSetAsDefaultBrowser(this)) {
// Don't ask again
return;
}
Intent intent = new Intent(context, BraveSetDefaultBrowserNotificationService.class);
context.sendBroadcast(intent);
});
}

private boolean isNoRestoreState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.chrome.R;
import org.chromium.base.task.PostTask;
import org.chromium.base.task.TaskTraits;
import org.chromium.chrome.browser.BraveActivity;
import org.chromium.chrome.browser.util.UrlConstants;

Expand Down Expand Up @@ -189,31 +191,39 @@ private void handleBraveSetDefaultBrowserDeepLink(Intent intent) {
}
}

private class OnReceiveRunnable implements Runnable {
@Override
public void run() {
boolean deepLinkIsHandled = false;
if (mIntent != null
&& mIntent.hasExtra(BraveSetDefaultBrowserNotificationService.DEEP_LINK)) {
handleBraveSetDefaultBrowserDeepLink(mIntent);
deepLinkIsHandled = true;
}
if (deepLinkIsHandled
|| (mIntent != null && mIntent.getAction() != null
&& mIntent.getAction().equals(CANCEL_NOTIFICATION))) {
int notification_id = mIntent.getIntExtra(NOTIFICATION_ID_EXTRA, 0);
NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
return;
}

if (shouldShowNotification()) {
showNotification();
} else if (!hasAskedAt1122()) {
setAlarmFor1122();
}
}
}

@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
boolean deepLinkIsHandled = false;
if (intent != null
&& intent.hasExtra(BraveSetDefaultBrowserNotificationService.DEEP_LINK)) {
handleBraveSetDefaultBrowserDeepLink(intent);
deepLinkIsHandled = true;
}

if (deepLinkIsHandled
|| (intent != null && intent.getAction() != null
&& intent.getAction().equals(CANCEL_NOTIFICATION))) {
int notification_id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, 0);
NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
return;
}

if (shouldShowNotification()) {
showNotification();
} else if (!hasAskedAt1122()) {
setAlarmFor1122();
}
// Work is done in IO thread because
// ApplicationPackageManager.resolveActivity may cause file IO operation
PostTask.postTask(TaskTraits.BEST_EFFORT_MAY_BLOCK, new OnReceiveRunnable());
}
}
75 changes: 55 additions & 20 deletions android/java/org/chromium/chrome/browser/util/BraveReferrer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.IOException;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.task.PostTask;
import org.chromium.base.task.TaskTraits;

public class BraveReferrer implements InstallReferrerStateListener {
private static final String TAG = "BraveReferrer";
Expand All @@ -40,22 +42,61 @@ public static BraveReferrer getInstance() {
return sInstance;
}

private class InitReferrerRunnable implements Runnable {
private Context mContext;
private BraveReferrer mBraveReferrer;
public InitReferrerRunnable(Context context, BraveReferrer braveReferrer) {
mContext = context;
mBraveReferrer = braveReferrer;
}

@Override
public void run() {
promoCodeFilePath = mContext.getApplicationInfo().dataDir +
File.separator + APP_CHROME_DIR + File.separator + PROMO_CODE_FILE_NAME;
SharedPreferences sharedPref = ContextUtils.getAppSharedPreferences();
if (!sharedPref.getBoolean(BRAVE_REFERRER_RECEIVED, false) &&
PackageUtils.isFirstInstall(mContext)) {
referrerClient = InstallReferrerClient.newBuilder(mContext).build();
// This seems to be known issue, for now just wrapping it into try/catch block
// https://issuetracker.google.com/issues/72926755
try {
referrerClient.startConnection(mBraveReferrer);
} catch (SecurityException e) {
Log.e(TAG, "Unable to start connection for referrer client: " + e);
}
}
}
}

public void initReferrer(Context context) {
promoCodeFilePath = context.getApplicationInfo().dataDir +
File.separator + APP_CHROME_DIR + File.separator + PROMO_CODE_FILE_NAME;
// On some devices InstallReferrerClient.startConnection causes file IO,
// so run it in IO task
PostTask.postTask(TaskTraits.BEST_EFFORT_MAY_BLOCK,
new InitReferrerRunnable(context, this));
}

SharedPreferences sharedPref = ContextUtils.getAppSharedPreferences();
if (!sharedPref.getBoolean(BRAVE_REFERRER_RECEIVED, false) &&
PackageUtils.isFirstInstall(context)) {
referrerClient = InstallReferrerClient.newBuilder(context).build();
// This seems to be known issue, for now just wrapping it into try/catch block
// https://issuetracker.google.com/issues/72926755
private class SaveReferrerRunnable implements Runnable {
private String mUrpc;
public SaveReferrerRunnable(String urpc) {
mUrpc = urpc;
}

@Override
public void run() {
FileOutputStream outputStreamWriter = null;
try {
File promoCodeFile = new File(promoCodeFilePath);
outputStreamWriter = new FileOutputStream(promoCodeFile);
outputStreamWriter.write(mUrpc.getBytes());
} catch (IOException e) {
Log.e(TAG, "Could not write to file (" + promoCodeFilePath + "): " + e.getMessage());
} finally {
try {
referrerClient.startConnection(this);
} catch (SecurityException e) {
Log.e(TAG, "Unable to start connection for referrer client: " + e);
}
if (outputStreamWriter != null) outputStreamWriter.close();
} catch (IOException exception) {}
}
}
}

@Override
Expand All @@ -69,14 +110,8 @@ public void onInstallReferrerSetupFinished(int responseCode) {
// Get and save user referal program code
String urpc = uri.getQueryParameter("urpc");
if (urpc != null && !urpc.isEmpty()) {
try {
File promoCodeFile = new File(promoCodeFilePath);
FileOutputStream outputStreamWriter = new FileOutputStream(promoCodeFile);
outputStreamWriter.write(urpc.getBytes());
outputStreamWriter.close();
} catch (IOException e) {
Log.e(TAG, "Could not write to file (" + promoCodeFilePath + "): " + e.getMessage());
}
PostTask.postTask(TaskTraits.BEST_EFFORT_MAY_BLOCK,
new SaveReferrerRunnable(urpc));
}
referrerClient.endConnection();
// Set flag to not repeat this procedure
Expand Down

0 comments on commit eb74ea5

Please sign in to comment.