Merge pull request #8984 from brave/fix-share-stats-crash

Fix share stats crash - waiting for QA confirmation
This commit is contained in:
alexsafe
2021-06-11 10:37:09 -04:00
committed by GitHub
6 changed files with 48 additions and 22 deletions
@@ -43,7 +43,7 @@ public class BraveReflectionUtil {
Log.e(TAG, "Illegal access for method: " + e);
assert (false);
} catch (InvocationTargetException e) {
Log.e(TAG, "Method invocation error: " + e);
Log.e(TAG, "Method invocation error e: " + e);
assert (false);
}
} catch (NoSuchMethodException e) {
@@ -731,6 +731,9 @@ public abstract class BraveActivity<C extends ChromeActivityComponent>
if (! TextUtils.isEmpty(open_url)) {
openNewOrSelectExistingTab(open_url);
}
} else if (resultCode == RESULT_OK
&& requestCode == BraveStatsUtil.SHARE_STATS_REQUEST_CODE) {
BraveStatsUtil.removeShareStatsFile();
}
super.onActivityResult(requestCode, resultCode, data);
}
@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.brave_stats;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -53,7 +54,9 @@ import java.util.Locale;
public class BraveStatsUtil {
public static final short MILLISECONDS_PER_ITEM = 50;
public static final int SHARE_STATS_WRITE_EXTERNAL_STORAGE_PERM = 3867;
public static final int SHARE_STATS_REQUEST_CODE = 4367;
public static final String TAG = "BraveStatsUtil";
private static String shareStatsFile = "";
/*
* Gets string view of specific time in seconds for Brave stats
*/
@@ -172,16 +175,16 @@ public class BraveStatsUtil {
try {
Context context = ContextUtils.getApplicationContext();
Bitmap bmp = convertToBitmap(view);
String path = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
path = MediaStore.Images.Media.insertImage(
shareStatsFile = MediaStore.Images.Media.insertImage(
context.getContentResolver(), bmp, "tempimage", null);
} else {
storeImage(bmp);
path = getOutputMediaFile().getAbsolutePath();
shareStatsFile = getOutputMediaFile().getAbsolutePath();
}
Uri uri = Uri.parse(path);
Uri uri = Uri.parse(shareStatsFile);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
@@ -193,9 +196,26 @@ public class BraveStatsUtil {
Intent shareIntent = Intent.createChooser(sendIntent, " ");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(shareIntent);
if (BraveActivity.getBraveActivity() != null) {
BraveActivity.getBraveActivity().startActivityForResult(
shareIntent, SHARE_STATS_REQUEST_CODE);
}
} catch (Exception e) {
Log.e(TAG, "exception :" + e);
e.printStackTrace();
}
}
public static void removeShareStatsFile() {
Context context = ContextUtils.getApplicationContext();
if (shareStatsFile.startsWith("content://")) {
ContentResolver contentResolver = context.getContentResolver();
contentResolver.delete(Uri.parse(shareStatsFile), null, null);
} else {
File file = new File(shareStatsFile);
if (file.exists()) {
file.delete();
}
}
}
@@ -129,10 +129,8 @@ public abstract class BraveToolbarLayout extends ToolbarLayout
BraveRewardsNativeWorker.PublisherObserver {
public static final String PREF_HIDE_BRAVE_REWARDS_ICON = "hide_brave_rewards_icon";
private static final String JAPAN_COUNTRY_CODE = "JP";
private static final long MB_10 = 10000000;
private static final long MINUTES_10 = 10 * 60 * 1000;
private static final int URL_FOCUS_TOOLBAR_BUTTONS_TRANSLATION_X_DP = 10;
private DatabaseHelper mDatabaseHelper = DatabaseHelper.getInstance();
@@ -459,12 +457,22 @@ public abstract class BraveToolbarLayout extends ToolbarLayout
private void chooseStatsShareTier(Tab tab, int trackersPlusAdsBlocked) {
String countryCode = Locale.getDefault().getCountry();
// the tooltip for stats sharing is shown only for Japan
if (!countryCode.equals(JAPAN_COUNTRY_CODE)) {
return;
}
int totalBlocked = Integer.parseInt(BraveStatsUtil.getAdsTrackersBlocked().first);
// double check if the shields button is shown to prevent situations like showing the
// tooltip on new tabs
if ((mBraveShieldsButton == null && !mBraveShieldsButton.isShown())
|| UrlUtilities.isNTPUrl(
BraveActivity.getBraveActivity().getActivityTab().getUrlString())) {
return;
}
int totalBlocked =
Math.round(Float.parseFloat(BraveStatsUtil.getAdsTrackersBlocked().first.trim()));
// show after BraveShieldsUtils.BRAVE_BLOCKED_SHOW_DIFF (20) blocked stuff above the TIER
// threshold
if (!BraveShieldsUtils.hasShieldsTooltipShown(