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

Crash in Rating dialog (uplift to 1.48.x) #16931

Merged
merged 1 commit into from
Feb 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,8 @@ private void clearObservers() {
}

private void showBraveRateDialog() {
BraveRateDialogFragment mRateDialogFragment = new BraveRateDialogFragment();
mRateDialogFragment.show(getSupportFragmentManager(), "BraveRateDialogFragment");
BraveRateDialogFragment rateDialogFragment = BraveRateDialogFragment.newInstance(false);
rateDialogFragment.show(getSupportFragmentManager(), BraveRateDialogFragment.TAG_FRAGMENT);
}

private void showCrossPromotionalDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ private void showBraveNewsRatingUI(
view.setOnClickListener((v) -> { showBraveRateDialog(); });
linearLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
mLinearLayout.setLayoutParams(linearLayoutParams);
mLinearLayout.setBackground(makeRound(CARD_LAYOUT, R.color.card_background, 30));
linearLayout.addView(view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.chromium.chrome.browser.rate;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
Expand Down Expand Up @@ -33,9 +34,27 @@
public class BraveAskPlayStoreRatingDialog extends BottomSheetDialogFragment {
final public static String TAG_FRAGMENT = "brave_ask_play_store_rating_dialog_tag";
private static final String TAG = "AskPlayStoreRating";
private ReviewManager mReviewManager;
private ReviewInfo mReviewInfo;
private boolean mIsFromSettings;
private Context mContext;

public static BraveAskPlayStoreRatingDialog newInstance() {
return new BraveAskPlayStoreRatingDialog();
public static BraveAskPlayStoreRatingDialog newInstance(boolean isFromSettings) {
Bundle bundle = new Bundle();
bundle.putBoolean(RateUtils.FROM_SETTINGS, isFromSettings);
BraveAskPlayStoreRatingDialog fragment = new BraveAskPlayStoreRatingDialog();
fragment.setArguments(bundle);

return fragment;
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mContext = context;
if (getArguments() != null) {
mIsFromSettings = getArguments().getBoolean(RateUtils.FROM_SETTINGS);
}
}

@Override
Expand Down Expand Up @@ -65,6 +84,12 @@ public void show(@NonNull FragmentManager manager, @Nullable String tag) {
@Override
public void setupDialog(@NonNull Dialog dialog, int style) {
super.setupDialog(dialog, style);
mReviewManager = ReviewManagerFactory.create(mContext);
try {
requestReviewFlow();
} catch (NullPointerException e) {
Log.e(TAG, "In-App requestReviewFlow exception");
}

final View view = LayoutInflater.from(getContext())
.inflate(R.layout.brave_ask_play_store_rating_dialog, null);
Expand All @@ -76,7 +101,15 @@ public void setupDialog(@NonNull Dialog dialog, int style) {
private void clickRateNowButton(View view) {
Button rateNowButton = view.findViewById(R.id.rate_now_button);
rateNowButton.setOnClickListener((v) -> {
rating(getActivity());
try {
if (mIsFromSettings) {
RateUtils.getInstance().openPlaystore(mContext);
} else {
launchReviewFlow();
}
} catch (NullPointerException e) {
Log.e(TAG, "In-App launch Review exception");
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Expand All @@ -91,33 +124,33 @@ private void clickNotNowButton(View view) {
rateNotNowButton.setOnClickListener((v) -> dismiss());
}

public static void showBraveAskPlayStoreRatingDialog(AppCompatActivity activity) {
if (activity != null) {
BraveAskPlayStoreRatingDialog braveAskPlayStoreRatingDialog =
BraveAskPlayStoreRatingDialog.newInstance();
braveAskPlayStoreRatingDialog.show(activity.getSupportFragmentManager(), TAG_FRAGMENT);
private void requestReviewFlow() {
if (mReviewManager != null) {
Task<ReviewInfo> request = mReviewManager.requestReviewFlow();
request.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
mReviewInfo = task.getResult();
} else {
// There was some problem
Log.e(TAG, "In-App review error " + task.getException());
}
});
}
}

private void rating(Context context) {
ReviewManager manager = ReviewManagerFactory.create(context);
Task<ReviewInfo> request = manager.requestReviewFlow();
request.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
// We can get the ReviewInfo object
ReviewInfo reviewInfo = task.getResult();
Task<Void> flow = manager.launchReviewFlow(getActivity(), reviewInfo);
flow.addOnCompleteListener(task1
-> {
// The flow has finished. The API does not indicate whether the user
// reviewed or not, or even whether the review dialog was shown.
// Thus, no
// matter the result, we continue our app flow.
});
} else {
// There was some problem
Log.e(TAG, "In-App review error " + task.getException());
}
});
private void launchReviewFlow() {
if (mReviewManager != null && mReviewInfo != null && mContext instanceof Activity) {
// We can get the ReviewInfo object
Task<Void> flow = mReviewManager.launchReviewFlow((Activity) mContext, mReviewInfo);
flow.addOnCompleteListener(task1
-> {
// The flow has finished. The API does not indicate whether the user
// reviewed or not, or even whether the review dialog was shown.
// Thus, no matter the result, we continue our app flow.
});
} else {
// if case fails then open play store app page
RateUtils.getInstance().openPlaystore(mContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -26,9 +27,23 @@
public class BraveRateDialogFragment extends BottomSheetDialogFragment {
final public static String TAG_FRAGMENT = "brave_rating_dialog_tag";
private static final String TAG = "RateDialogFragment";
private boolean mIsFromSettings;

public static BraveRateDialogFragment newInstance() {
return new BraveRateDialogFragment();
public static BraveRateDialogFragment newInstance(boolean isFromSettings) {
Bundle bundle = new Bundle();
bundle.putBoolean(RateUtils.FROM_SETTINGS, isFromSettings);

BraveRateDialogFragment rateDialogFragment = new BraveRateDialogFragment();
rateDialogFragment.setArguments(bundle);
return rateDialogFragment;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (getArguments() != null) {
mIsFromSettings = getArguments().getBoolean(RateUtils.FROM_SETTINGS);
}
}

@Override
Expand Down Expand Up @@ -69,8 +84,10 @@ private void clickOnHappyImageView(View view) {
ImageView happyImageView = view.findViewById(R.id.happyImageView);
happyImageView.setOnClickListener((v) -> {
dismiss();
BraveAskPlayStoreRatingDialog.showBraveAskPlayStoreRatingDialog(
(AppCompatActivity) getActivity());
BraveAskPlayStoreRatingDialog fragment =
BraveAskPlayStoreRatingDialog.newInstance(mIsFromSettings);
fragment.show(((AppCompatActivity) getActivity()).getSupportFragmentManager(),
BraveAskPlayStoreRatingDialog.TAG_FRAGMENT);
});
}

Expand All @@ -82,11 +99,4 @@ private void clickOnSadImageView(View view) {
(AppCompatActivity) getActivity());
});
}

public static void showBraveRatingDialog(AppCompatActivity activity) {
if (activity != null) {
BraveRateDialogFragment braveRateDialogFragment = BraveRateDialogFragment.newInstance();
braveRateDialogFragment.show(activity.getSupportFragmentManager(), TAG_FRAGMENT);
}
}
}
22 changes: 22 additions & 0 deletions android/java/org/chromium/chrome/browser/rate/RateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package org.chromium.chrome.browser.rate;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
Expand Down Expand Up @@ -181,4 +183,24 @@ private long dayDifference(long date1, long date2) {
long difference = date1 - date2;
return (difference / (1000 * 60 * 60 * 24)) % 365;
}

/**
* This opens app page in playstore
* if it fails open app playstore page link in browser
* */
public void openPlaystore(Context context) {
final Uri marketUri = Uri.parse("market://details?id=" + context.getPackageName());
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
} catch (android.content.ActivityNotFoundException ex) {
openReviewLink(context);
}
}

private void openReviewLink(Context context) {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id="
+ context.getPackageName()));
context.startActivity(webIntent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,10 @@ private void initRateBrave() {
findPreference(PREF_RATE_BRAVE).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Bundle bundle = new Bundle();
bundle.putBoolean(RateUtils.FROM_SETTINGS, true);

BraveRateDialogFragment mRateDialogFragment = new BraveRateDialogFragment();
mRateDialogFragment.setArguments(bundle);
mRateDialogFragment.show(getParentFragmentManager(), "RateDialogFragment");
BraveRateDialogFragment rateDialogFragment =
BraveRateDialogFragment.newInstance(true);
rateDialogFragment.show(
getParentFragmentManager(), BraveRateDialogFragment.TAG_FRAGMENT);
return true;
}
});
Expand Down