SettingsRateFragment.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2019-2024 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.app.preference;
  22. import android.content.ActivityNotFoundException;
  23. import android.content.Intent;
  24. import android.net.Uri;
  25. import android.widget.Toast;
  26. import org.slf4j.Logger;
  27. import androidx.annotation.NonNull;
  28. import ch.threema.app.BuildConfig;
  29. import ch.threema.app.BuildFlavor;
  30. import ch.threema.app.R;
  31. import ch.threema.app.dialogs.GenericAlertDialog;
  32. import ch.threema.app.dialogs.RateDialog;
  33. import ch.threema.app.utils.ConfigUtils;
  34. import ch.threema.base.utils.LoggingUtil;
  35. import static ch.threema.app.ThreemaApplication.getAppContext;
  36. public class SettingsRateFragment extends ThreemaPreferenceFragment implements RateDialog.RateDialogClickListener, GenericAlertDialog.DialogClickListener {
  37. private static final Logger logger = LoggingUtil.getThreemaLogger("SettingsRateFragment");
  38. private static final String DIALOG_TAG_RATE = "rate";
  39. private static final String DIALOG_TAG_RATE_ON_GOOGLE_PLAY = "ratep";
  40. @Override
  41. protected void initializePreferences() {
  42. RateDialog rateDialog = RateDialog.newInstance(getString(R.string.rate_title));
  43. rateDialog.setTargetFragment(SettingsRateFragment.this, 0);
  44. rateDialog.show(getParentFragmentManager(), DIALOG_TAG_RATE);
  45. }
  46. private boolean startRating(Uri uri) throws ActivityNotFoundException {
  47. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  48. intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  49. try {
  50. startActivity(intent);
  51. } catch (Exception e) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. @Override
  57. public void onYes(String tag, final int rating, final String text) {
  58. if (rating >= 4 && shouldRedirectToGooglePlay(BuildFlavor.getLicenseType())) {
  59. GenericAlertDialog dialog = GenericAlertDialog.newInstance(R.string.rate_title,
  60. getString(R.string.rate_thank_you) + " " +
  61. getString(R.string.rate_forward_to_play_store) ,
  62. R.string.yes,
  63. R.string.no);
  64. dialog.setTargetFragment(this);
  65. dialog.show(getParentFragmentManager(), DIALOG_TAG_RATE_ON_GOOGLE_PLAY);
  66. } else {
  67. Toast.makeText(getAppContext(), getString(R.string.rate_thank_you), Toast.LENGTH_LONG).show();
  68. onBackPressed();
  69. }
  70. }
  71. @Override
  72. public void onYes(String tag, Object data) {
  73. if (DIALOG_TAG_RATE_ON_GOOGLE_PLAY.equals(tag)) {
  74. if (!startRating(Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID))) {
  75. startRating(Uri.parse("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID));
  76. }
  77. onBackPressed();
  78. }
  79. }
  80. @Override
  81. public void onNo(String tag, Object data) {
  82. if (!ConfigUtils.isTabletLayout()) {
  83. // We only need to navigate back on phones, because on tablets this would leave the
  84. // preferences entirely.
  85. onBackPressed();
  86. }
  87. }
  88. @Override
  89. public void onCancel(String tag) {
  90. if (!ConfigUtils.isTabletLayout()) {
  91. // We only need to navigate back on phones, because on tablets this would leave the
  92. // preferences entirely.
  93. onBackPressed();
  94. }
  95. }
  96. @Override
  97. public int getPreferenceTitleResource() {
  98. return R.string.rate_title;
  99. }
  100. @Override
  101. public int getPreferenceResource() {
  102. return R.xml.preference_rate;
  103. }
  104. private void onBackPressed() {
  105. if (getActivity() != null) {
  106. getActivity().onBackPressed();
  107. }
  108. }
  109. private boolean shouldRedirectToGooglePlay(@NonNull BuildFlavor.LicenseType licenseType) {
  110. switch (licenseType) {
  111. case GOOGLE:
  112. case NONE:
  113. return true;
  114. case GOOGLE_WORK:
  115. return ConfigUtils.isInstalledFromPlayStore(getAppContext());
  116. default:
  117. return false;
  118. }
  119. }
  120. }