Skip to content

Commit

Permalink
For Google Play new in-app reviews launched by default
Browse files Browse the repository at this point in the history
  • Loading branch information
soenkegissel committed May 28, 2021
1 parent f0f8bfe commit 3a6924a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 deletions.
79 changes: 62 additions & 17 deletions ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@

import androidx.fragment.app.FragmentActivity;

import com.google.android.play.core.review.ReviewInfo;
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;
import com.google.android.play.core.review.testing.FakeReviewManager;
import com.google.android.play.core.tasks.Task;

import java.util.Date;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -67,15 +73,28 @@ public class RateThisApp implements Callback {
private Config sConfig;
private Callback sCallback;

private FragmentActivity fragmentActivity;
private FragmentActivity mFragmentActivity;
private Context mContext;

private Market mMarket;

private ReviewManager mReviewManager;
private ReviewInfo mReviewInfo;

//https://de.wikibooks.org/wiki/Muster:_Java:_Singleton
@SuppressLint("StaticFieldLeak")
private static volatile RateThisApp INSTANCE;

private RateThisApp(Context context, Config config, Market market) {
this.mContext = context;
this.sConfig = config;
this.mMarket = market;

this.mReviewManager = BuildConfig.DEBUG ? new FakeReviewManager(mContext) : ReviewManagerFactory.create(mContext);

setup();
}

public static RateThisApp initialize(Context context, Config config, Market market) {
Context applicationContext;
if (context.getApplicationContext() == null) {
Expand All @@ -95,18 +114,12 @@ public static RateThisApp getInstance(FragmentActivity fragmentActivity) {
if(INSTANCE == null) {
throw new NullPointerException("RateThisApp not initialized. Call RateThisApp.initalize(Context, Config) from your application class.");
}
INSTANCE.fragmentActivity = fragmentActivity;
INSTANCE.mFragmentActivity = fragmentActivity;

return RateThisApp.INSTANCE;
}

private RateThisApp(Context context, Config config, Market market) {
this.mContext = context;
this.sConfig = config;
this.mMarket = market;

setup();
}

/**
* Set callback INSTANCE.
Expand Down Expand Up @@ -163,10 +176,10 @@ public boolean showRateDialogIfNeeded(boolean forceDialog) {
*/
public boolean showRateDialogIfNeeded(int themeId, boolean forceDialog) {
if(forceDialog) {
showRateDialog(fragmentActivity, themeId);
showRateDialog(mFragmentActivity, themeId);
return true;
} else if (shouldShowRateDialog(sConfig.getmOperator())) {
showRateDialog(fragmentActivity, themeId);
showRateDialog(mFragmentActivity, themeId);
return true;
} else {
return false;
Expand Down Expand Up @@ -346,25 +359,57 @@ private void log(String message) {

@Override
public void onYesClicked() {
if (sCallback != null) {
sCallback.onYesClicked();
}
String appPackage = fragmentActivity.getPackageName();
String appPackage = mFragmentActivity.getPackageName();
String url = getMarketURL(mMarket, appPackage);
if (!TextUtils.isEmpty(sConfig.getmUrl())) {
url = sConfig.getmUrl();
}

if(mMarket.equals(Market.GOOGLE)) {
ReviewManager manager = ReviewManagerFactory.create(mContext);
Task<ReviewInfo> request = manager.requestReviewFlow();
String finalUrl = url;
request.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
// We can get the ReviewInfo object
mReviewInfo = task.getResult();
} else {
// There was some problem, log or handle the error code.
launchIntent(finalUrl, appPackage);
}
});
}
if(mReviewInfo != null) {
Task<Void> flow = mReviewManager.launchReviewFlow(mFragmentActivity, mReviewInfo);
flow.addOnCompleteListener(task -> {
// 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.
if (sCallback != null) {
sCallback.onYesClicked();
}
});
} else {
launchIntent(url, appPackage);
if (sCallback != null) {
sCallback.onYesClicked();
}
}
setOptOut(true);
}

private void launchIntent(String url, String appPackage) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
fragmentActivity.startActivity(intent);
mFragmentActivity.startActivity(intent);

} catch (android.content.ActivityNotFoundException anfe) {
url = getWebURL(mMarket, appPackage);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
fragmentActivity.startActivity(intent);
mFragmentActivity.startActivity(intent);
}
setOptOut(true);
}

private String getWebURL(Market mMarket, String appPackage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MainApplication extends Application {
public void onCreate() {
super.onCreate();

Config config = new Config(3, 5, Config.Operator.OR);
RateThisApp.initialize(this, config, Market.AMAZON);
Config config = new Config(1, 1, Config.Operator.OR);
RateThisApp.initialize(this, config, Market.GOOGLE);
}
}

0 comments on commit 3a6924a

Please sign in to comment.