Skip to content

Commit

Permalink
Merge pull request #6593 from brave/pr6591_fix_crash_for_1.13_1.15.x
Browse files Browse the repository at this point in the history
Fix crash for 1.13 (uplift to 1.15.x)
  • Loading branch information
kjozwiak authored Sep 9, 2020
2 parents 939e359 + d404724 commit f965668
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 54 deletions.
36 changes: 20 additions & 16 deletions android/java/org/chromium/chrome/browser/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,26 @@ public void checkForBraveStats() {
}

public void showOnboardingV2(boolean fromStats) {
OnboardingPrefManager.getInstance().setNewOnboardingShown(true);
FragmentManager fm = getSupportFragmentManager();
HighlightDialogFragment fragment = (HighlightDialogFragment) fm
.findFragmentByTag(HighlightDialogFragment.TAG_FRAGMENT);
FragmentTransaction transaction = fm.beginTransaction();

if (fragment != null) {
transaction.remove(fragment);
}

fragment = new HighlightDialogFragment();
Bundle fragmentBundle = new Bundle();
fragmentBundle.putBoolean(OnboardingPrefManager.FROM_STATS, fromStats);
fragment.setArguments(fragmentBundle);
transaction.add(fragment, HighlightDialogFragment.TAG_FRAGMENT);
transaction.commit();
try {
OnboardingPrefManager.getInstance().setNewOnboardingShown(true);
FragmentManager fm = getSupportFragmentManager();
HighlightDialogFragment fragment = (HighlightDialogFragment) fm
.findFragmentByTag(HighlightDialogFragment.TAG_FRAGMENT);
FragmentTransaction transaction = fm.beginTransaction();

if (fragment != null) {
transaction.remove(fragment);
}

fragment = new HighlightDialogFragment();
Bundle fragmentBundle = new Bundle();
fragmentBundle.putBoolean(OnboardingPrefManager.FROM_STATS, fromStats);
fragment.setArguments(fragmentBundle);
transaction.add(fragment, HighlightDialogFragment.TAG_FRAGMENT);
transaction.commitAllowingStateLoss();
} catch (IllegalStateException e) {
Log.e("HighlightDialogFragment", e.getMessage());
}
}

public void hideRewardsOnboardingIcon() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.fragment.app.FragmentTransaction;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
Expand All @@ -57,6 +59,7 @@
import java.util.List;

public class BraveStatsBottomSheetDialogFragment extends BottomSheetDialogFragment {
final public static String TAG_FRAGMENT = "BRAVESTATS_FRAG";
private DatabaseHelper mDatabaseHelper = DatabaseHelper.getInstance();

private static final int WEBSITES = 0;
Expand Down Expand Up @@ -98,6 +101,21 @@ public void onCreate(Bundle savedInstanceState) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

@Override
public void show(FragmentManager manager, String tag) {
try {
BraveStatsBottomSheetDialogFragment fragment = (BraveStatsBottomSheetDialogFragment) manager.findFragmentByTag(BraveStatsBottomSheetDialogFragment.TAG_FRAGMENT);
FragmentTransaction transaction = manager.beginTransaction();
if (fragment != null) {
transaction.remove(fragment);
}
transaction.add(this, tag);
transaction.commitAllowingStateLoss();
} catch (IllegalStateException e) {
Log.e("BraveStatsBottomSheetDialogFragment", e.getMessage());
}
}

@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
Expand Down Expand Up @@ -186,26 +204,26 @@ private void updateBraveStatsLayoutAsync() {
@Override
protected Void doInBackground() {
adsTrackersCount =
mDatabaseHelper
.getAllStatsWithDate(BraveStatsUtil.getCalculatedDate(
"yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0))
.size();
mDatabaseHelper
.getAllStatsWithDate(BraveStatsUtil.getCalculatedDate(
"yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0))
.size();
totalSavedBandwidth = mDatabaseHelper.getTotalSavedBandwidthWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0));
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0));
adsTrackersCountToCheckForMonth =
mDatabaseHelper
.getAllStatsWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_30),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_7))
.size();
mDatabaseHelper
.getAllStatsWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_30),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_7))
.size();
adsTrackersCountToCheckFor3Month =
mDatabaseHelper
.getAllStatsWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_90),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_30))
.size();
mDatabaseHelper
.getAllStatsWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_90),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", DAYS_30))
.size();
return null;
}

Expand All @@ -214,35 +232,35 @@ protected void onPostExecute(Void result) {
assert ThreadUtils.runningOnUiThread();
if (isCancelled()) return;
Pair<String, String> adsTrackersPair =
BraveStatsUtil.getBraveStatsStringFormNumberPair(adsTrackersCount, false);
BraveStatsUtil.getBraveStatsStringFormNumberPair(adsTrackersCount, false);
adsTrackersCountText.setText(
String.format(getResources().getString(R.string.ntp_stat_text),
adsTrackersPair.first, adsTrackersPair.second));
String.format(getResources().getString(R.string.ntp_stat_text),
adsTrackersPair.first, adsTrackersPair.second));

Pair<String, String> dataSavedPair =
BraveStatsUtil.getBraveStatsStringFormNumberPair(totalSavedBandwidth, true);
BraveStatsUtil.getBraveStatsStringFormNumberPair(totalSavedBandwidth, true);
dataSavedCountText.setText(dataSavedPair.first);
boolean isTablet = DeviceFormFactor.isNonMultiDisplayContextOnTablet(getActivity());
if (isTablet) {
adsTrackersText.setText(
String.format(mContext.getResources().getString(R.string.trackers_and_ads),
dataSavedPair.second));
dataSavedPair.second));
dataSavedText.setText(
String.format(mContext.getResources().getString(R.string.data_saved_tablet_text),
dataSavedPair.second));
dataSavedPair.second));
} else {
adsTrackersText.setText(
String.format(mContext.getResources().getString(R.string.ads_trackers_text),
dataSavedPair.second));
dataSavedPair.second));
dataSavedText.setText(
String.format(mContext.getResources().getString(R.string.data_saved_text),
dataSavedPair.second));
dataSavedPair.second));
}

long timeSavedCount =
adsTrackersCount * BraveNewTabPageLayout.MILLISECONDS_PER_ITEM;
adsTrackersCount * BraveNewTabPageLayout.MILLISECONDS_PER_ITEM;
timeSavedCountText.setText(
BraveStatsUtil.getBraveStatsStringFromTime(timeSavedCount / 1000));
BraveStatsUtil.getBraveStatsStringFromTime(timeSavedCount / 1000));
timeSavedText.setText(mContext.getResources().getString(R.string.time_saved_text));

if (adsTrackersCount > 0) {
Expand Down Expand Up @@ -270,7 +288,7 @@ protected void onPostExecute(Void result) {
}
showWebsitesTrackers();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

private void showWebsitesTrackers() {
Expand All @@ -280,12 +298,12 @@ private void showWebsitesTrackers() {
protected Void doInBackground() {
if (selectedType == WEBSITES) {
websiteTrackers = mDatabaseHelper.getStatsWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0));
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0));
} else {
websiteTrackers = mDatabaseHelper.getSitesWithDate(
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0));
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", selectedDuration),
BraveStatsUtil.getCalculatedDate("yyyy-MM-dd", 0));
}
return null;
}
Expand All @@ -311,18 +329,18 @@ protected void onPostExecute(Void result) {
for (Pair<String, Integer> statPair : websiteTrackers) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout =
(ViewGroup) inflater.inflate(R.layout.tracker_item_layout, null);
(ViewGroup) inflater.inflate(R.layout.tracker_item_layout, null);

TextView mTrackerCountText =
(TextView) layout.findViewById(R.id.tracker_count_text);
(TextView) layout.findViewById(R.id.tracker_count_text);
TextView mSiteText = (TextView) layout.findViewById(R.id.site_text);

mTrackerCountText.setText(String.valueOf(statPair.second));
mTrackerCountText.setTextColor(
getResources().getColor(R.color.brave_stats_text_color));
getResources().getColor(R.color.brave_stats_text_color));
mSiteText.setText(statPair.first);
mSiteText.setTextColor(
getResources().getColor(R.color.brave_stats_text_color));
getResources().getColor(R.color.brave_stats_text_color));

rootView.addView(layout);
}
Expand All @@ -333,6 +351,6 @@ protected void onPostExecute(Void result) {
braveStatsSubSectionText.setVisibility(View.GONE);
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static Pair<String, String> getBraveStatsStringFormNumberPair(long number
public static void showBraveStats() {
if (BraveActivity.getBraveActivity() != null) {
BraveStatsBottomSheetDialogFragment braveStatsBottomSheetDialogFragment = BraveStatsBottomSheetDialogFragment.newInstance();
braveStatsBottomSheetDialogFragment.show(BraveActivity.getBraveActivity().getSupportFragmentManager(), "brave_stats_bottom_sheet_dialog_fragment");
braveStatsBottomSheetDialogFragment.show(BraveActivity.getBraveActivity().getSupportFragmentManager(), BraveStatsBottomSheetDialogFragment.TAG_FRAGMENT);
}
}

Expand Down

0 comments on commit f965668

Please sign in to comment.