├── Screenshot.png ├── .editorconfig ├── Source ├── AndroidManifest.xml ├── .gitignore ├── project.properties └── src │ └── im │ └── delight │ └── apprater │ └── AppRater.java ├── README.md └── LICENSE /Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/delight-im/AppRater/HEAD/Screenshot.png -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = tab 7 | trim_trailing_whitespace = true 8 | end_of_line = lf 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /Source/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Source/.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | .settings/ 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Intellij project files 27 | *.iml 28 | *.ipr 29 | *.iws 30 | .idea/ 31 | 32 | -------------------------------------------------------------------------------- /Source/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-16 15 | android.library=true 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AppRater 2 | 3 | * Android library that lets you prompt users to rate your application on their appstore (e.g. Google Play) 4 | * Prompt will only be shown to users after a specified number of days and app launches 5 | * Adapts to your application's styles and themes 6 | 7 |

8 | Screenshot 9 |

10 | 11 | ## Installation 12 | 13 | * Copy the Java package to your project's source folder 14 | * or 15 | * Create a new library project from this repository and reference it in your project 16 | 17 | ## Usage 18 | 19 | ### From `Activity` instances 20 | 21 | Decide which `Activity` you want to appear the rating prompt in (usually your ```MainActivity.java```). 22 | 23 | At the end of your `Activity`'s `onCreate(...)` or `onResume()`, add the following: 24 | 25 | `new AppRater(this).show();` 26 | 27 | ### From `Fragment` instances 28 | 29 | If you want to call the AppRater from a `Fragment` (e.g. at the end of `onCreateView()`), use the following line instead: 30 | 31 | `new AppRater(getActivity()).show();` 32 | 33 | ### Preview (usage during development) 34 | 35 | If you want to see a preview of the prompt (usually during development), replace the call to `show()` on the `AppRater` instance with a call to the `demo()` method. 36 | 37 | ## Customization 38 | 39 | You can customize the AppRater by using any of the following calls before ```show()``` (which are all optional): 40 | 41 | ```java 42 | AppRater appRater = new AppRater(this); 43 | appRater.setDaysBeforePrompt(3); 44 | appRater.setLaunchesBeforePrompt(7); 45 | 46 | appRater.show(); 47 | // or 48 | // AlertDialog myDialog = appRater.show(); 49 | ``` 50 | 51 | There are three additional methods which you won't need to call, usually: 52 | ```java 53 | appRater.setPhrases("Rate this app", "This is the explanation why you should rate our app.", "Rate now", "Later", "No, thanks"); 54 | // or 55 | appRater.setPhrases(R.string.rate_title, R.string.rate_explanation, R.string.rate_now, R.string.rate_later, R.string.rate_never); 56 | 57 | appRater.setTargetUri("https://play.google.com/store/apps/details?id=%1$s"); 58 | appRater.setPreferenceKeys("app_rater", "flag_dont_show", "launch_count", "first_launch_time"); 59 | ``` 60 | 61 | The first one lets you set the phrases as Strings directly, without referencing resources. The second one lets you enter an alternative target URI if you want to redirect the user to another appstore than Google Play (e.g. Amazon Appstore). The third method lets you change the name of the preferences, which you won't need to do, usually. 62 | 63 | Be sure to check the JavaDoc for all these methods when using them. Don't forget to call ```show()``` which is the most important part. 64 | 65 | In addition to that, please keep in mind that you should call `setDaysBeforePrompt(0)` and `setLaunchesBeforePrompt(0)` for debugging purposes, so that you can see the dialog right away. 66 | 67 | ## Dependencies 68 | 69 | * Android 2.2+ 70 | 71 | ## Contributing 72 | 73 | We welcome any contribution, no matter how small or large. Please fork this repository, apply your changes, and submit your contributions by sending a pull request. 74 | 75 | ## License 76 | 77 | ``` 78 | Copyright (c) delight.im 79 | 80 | Licensed under the Apache License, Version 2.0 (the "License"); 81 | you may not use this file except in compliance with the License. 82 | You may obtain a copy of the License at 83 | 84 | http://www.apache.org/licenses/LICENSE-2.0 85 | 86 | Unless required by applicable law or agreed to in writing, software 87 | distributed under the License is distributed on an "AS IS" BASIS, 88 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 89 | See the License for the specific language governing permissions and 90 | limitations under the License. 91 | ``` 92 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /Source/src/im/delight/apprater/AppRater.java: -------------------------------------------------------------------------------- 1 | package im.delight.apprater; 2 | 3 | /* 4 | * Copyright (c) delight.im 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import android.annotation.SuppressLint; 20 | import android.app.AlertDialog; 21 | import android.content.ActivityNotFoundException; 22 | import android.content.Context; 23 | import android.content.DialogInterface; 24 | import android.content.Intent; 25 | import android.content.SharedPreferences; 26 | import android.net.Uri; 27 | import android.os.Build; 28 | import android.text.format.DateUtils; 29 | 30 | /** 31 | * Android library that lets you prompt users to rate your application 32 | *

33 | * Users will be redirected to Google Play by default, but any other app store can be used by providing a custom URI (via setTargetUri(...)) 34 | *

35 | * This library uses English default phrases but you may provide custom phrases either as Strings or resource IDs (via setPhrases(...)) 36 | *

37 | * Users will be prompted to rate your app after at least 2 days and 5 app launches (customizable via setDaysBeforePrompt(...) and setLaunchesBeforePrompt(...)) 38 | *

39 | * You may overwrite the default preference group name and key names by calling setPreferenceKeys(...) (usually not necessary) 40 | *

41 | * The prompting dialog will be displayed (if adequate) as soon as you call show() on your AppRater instance 42 | *

43 | * The dialog will only be shown if at least one application is available on the user's phone to handle the Intent that is defined by the target URI 44 | *

45 | * AlertDialog is used so that the prompt window adapts to your application's styles and themes 46 | *

47 | * Minimum API level: 8 (Android 2.2) 48 | */ 49 | public class AppRater { 50 | 51 | private static final String DEFAULT_PREF_GROUP = "app_rater"; 52 | private static final String DEFAULT_PREFERENCE_DONT_SHOW = "flag_dont_show"; 53 | private static final String DEFAULT_PREFERENCE_LAUNCH_COUNT = "launch_count"; 54 | private static final String DEFAULT_PREFERENCE_FIRST_LAUNCH = "first_launch_time"; 55 | private static final String DEFAULT_TARGET_URI = "market://details?id=%s"; 56 | private static final String DEFAULT_TEXT_TITLE = "Rate this app"; 57 | private static final String DEFAULT_TEXT_EXPLANATION = "Has this app convinced you? We would be very happy if you could rate our application on Google Play. Thanks for your support!"; 58 | private static final String DEFAULT_TEXT_NOW = "Rate now"; 59 | private static final String DEFAULT_TEXT_LATER = "Later"; 60 | private static final String DEFAULT_TEXT_NEVER = "No, thanks"; 61 | private static final int DEFAULT_DAYS_BEFORE_PROMPT = 2; 62 | private static final int DEFAULT_LAUNCHES_BEFORE_PROMPT = 5; 63 | private final Context mContext; 64 | private final String mPackageName; 65 | private int mDaysBeforePrompt; 66 | private int mLaunchesBeforePrompt; 67 | private String mTargetUri; 68 | private String mText_title; 69 | private String mText_explanation; 70 | private String mText_buttonNow; 71 | private String mText_buttonLater; 72 | private String mText_buttonNever; 73 | private String mPrefGroup; 74 | private String mPreference_dontShow; 75 | private String mPreference_launchCount; 76 | private String mPreference_firstLaunch; 77 | private Intent mTargetIntent; 78 | 79 | /** 80 | * Creates a new AppRater instance 81 | * @param context the Activity reference to use for this instance (usually the Activity you called this from) 82 | */ 83 | public AppRater(final Context context) { 84 | this(context, context.getPackageName()); 85 | } 86 | 87 | /** 88 | * Creates a new AppRater instance 89 | * @param context the Activity reference to use for this instance (usually the Activity you called this from) 90 | * @param packageName your application's package name that will be used to open the ratings page 91 | */ 92 | public AppRater(final Context context, final String packageName) { 93 | if (context == null) { 94 | throw new RuntimeException("context may not be null"); 95 | } 96 | mContext = context; 97 | mPackageName = packageName; 98 | mDaysBeforePrompt = DEFAULT_DAYS_BEFORE_PROMPT; 99 | mLaunchesBeforePrompt = DEFAULT_LAUNCHES_BEFORE_PROMPT; 100 | mTargetUri = DEFAULT_TARGET_URI; 101 | mText_title = DEFAULT_TEXT_TITLE; 102 | mText_explanation = DEFAULT_TEXT_EXPLANATION; 103 | mText_buttonNow = DEFAULT_TEXT_NOW; 104 | mText_buttonLater = DEFAULT_TEXT_LATER; 105 | mText_buttonNever = DEFAULT_TEXT_NEVER; 106 | mPrefGroup = DEFAULT_PREF_GROUP; 107 | mPreference_dontShow = DEFAULT_PREFERENCE_DONT_SHOW; 108 | mPreference_launchCount = DEFAULT_PREFERENCE_LAUNCH_COUNT; 109 | mPreference_firstLaunch = DEFAULT_PREFERENCE_FIRST_LAUNCH; 110 | } 111 | 112 | /** 113 | * Sets how many days to wait before users may be prompted 114 | * 115 | * @param days the number of days to wait before showing the prompt 116 | */ 117 | public void setDaysBeforePrompt(final int days) { 118 | mDaysBeforePrompt = days; 119 | } 120 | 121 | /** 122 | * Sets how often users must have launched the app (i.e. called AppRater.show()) before they may be prompted 123 | * 124 | * @param launches the number of launches to wait before showing the prompt 125 | */ 126 | public void setLaunchesBeforePrompt(final int launches) { 127 | mLaunchesBeforePrompt = launches; 128 | } 129 | 130 | /** 131 | * Sets the target URI string that must contain exactly one placeholder (`%s`) for the package name 132 | * 133 | * @param uri the target URI to open when the user wants to rate the app (must include `%s`) 134 | */ 135 | public void setTargetUri(final String uri) { 136 | mTargetUri = uri; 137 | } 138 | 139 | /** 140 | * Sets the given Strings to use for the prompt 141 | * 142 | * @param title the title for the prompt window (as a string) 143 | * @param explanation the explanatory text that will be shown inside the prompt window (as a string) 144 | * @param buttonNow the caption for the `Rate now` button (as a string) 145 | * @param buttonLater the caption for the `Maybe later` button (as a string) 146 | * @param buttonNever the caption for the `Never` button (as a string) 147 | */ 148 | public void setPhrases(final String title, final String explanation, final String buttonNow, final String buttonLater, final String buttonNever) { 149 | mText_title = title; 150 | mText_explanation = explanation; 151 | mText_buttonNow = buttonNow; 152 | mText_buttonLater = buttonLater; 153 | mText_buttonNever = buttonNever; 154 | } 155 | 156 | /** 157 | * Sets the Strings referenced by the given resource IDs to use for the prompt 158 | * 159 | * @param title the title for the prompt window (as a resource ID) 160 | * @param explanation the explanatory text that will be shown inside the prompt window (as a resource ID) 161 | * @param buttonNow the caption for the `Rate now` button (as a resource ID) 162 | * @param buttonLater the caption for the `Maybe later` button (as a resource ID) 163 | * @param buttonNever the caption for the `Never` button (as a resource ID) 164 | */ 165 | public void setPhrases(final int title, final int explanation, final int buttonNow, final int buttonLater, final int buttonNever) { 166 | try { 167 | mText_title = mContext.getString(title); 168 | } 169 | catch (Exception e) { 170 | mText_title = DEFAULT_TEXT_TITLE; 171 | } 172 | try { 173 | mText_explanation = mContext.getString(explanation); 174 | } 175 | catch (Exception e) { 176 | mText_explanation = DEFAULT_TEXT_EXPLANATION; 177 | } 178 | try { 179 | mText_buttonNow = mContext.getString(buttonNow); 180 | } 181 | catch (Exception e) { 182 | mText_buttonNow = DEFAULT_TEXT_NOW; 183 | } 184 | try { 185 | mText_buttonLater = mContext.getString(buttonLater); 186 | } 187 | catch (Exception e) { 188 | mText_buttonLater = DEFAULT_TEXT_LATER; 189 | } 190 | try { 191 | mText_buttonNever = mContext.getString(buttonNever); 192 | } 193 | catch (Exception e) { 194 | mText_buttonNever = DEFAULT_TEXT_NEVER; 195 | } 196 | } 197 | 198 | /** 199 | * Sets the given keys for the preferences that will be used 200 | * @param group the preference group file that all values of this class go in 201 | * @param dontShow the preference name for the dont-show flag 202 | * @param launchCount the preference name for the launch counter 203 | * @param firstLaunchTime the preference name for the first launch time value 204 | */ 205 | public void setPreferenceKeys(String group, String dontShow, String launchCount, String firstLaunchTime) { 206 | mPrefGroup = group; 207 | mPreference_dontShow = dontShow; 208 | mPreference_launchCount = launchCount; 209 | mPreference_firstLaunch = firstLaunchTime; 210 | } 211 | 212 | private void createTargetIntent() { 213 | mTargetIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(mTargetUri, mPackageName))); 214 | } 215 | 216 | /** 217 | * Checks if the rating dialog should be shown to the user and displays it if needed 218 | * 219 | * @return the AlertDialog that has been shown or null 220 | */ 221 | @SuppressLint("CommitPrefEdits") 222 | public AlertDialog show() { 223 | createTargetIntent(); 224 | 225 | final SharedPreferences prefs = mContext.getSharedPreferences(mPrefGroup, 0); 226 | final SharedPreferences.Editor editor = prefs.edit(); 227 | 228 | if (prefs.getBoolean(mPreference_dontShow, false)) { // if user opted not to rate the app 229 | return null; // do not show anything 230 | } 231 | 232 | long launch_count = prefs.getLong(mPreference_launchCount, 0); // get launch counter 233 | launch_count++; // increase number of launches by one 234 | editor.putLong(mPreference_launchCount, launch_count); // write new counter back to preference 235 | 236 | long firstLaunchTime = prefs.getLong(mPreference_firstLaunch, 0); // get date of first launch 237 | if (firstLaunchTime == 0) { // if not set yet 238 | firstLaunchTime = System.currentTimeMillis(); // set to current time 239 | editor.putLong(mPreference_firstLaunch, firstLaunchTime); 240 | } 241 | 242 | savePreferences(editor); 243 | 244 | if (launch_count >= mLaunchesBeforePrompt) { // wait at least x app launches 245 | if (System.currentTimeMillis() >= (firstLaunchTime + (mDaysBeforePrompt * DateUtils.DAY_IN_MILLIS))) { // wait at least x days 246 | try { 247 | return showDialog(mContext, editor); 248 | } 249 | catch (Exception e) { 250 | return null; 251 | } 252 | } 253 | else { 254 | return null; 255 | } 256 | } 257 | else { 258 | return null; 259 | } 260 | } 261 | 262 | /** 263 | * Displays a preview of the prompt as it will be shown to users later (use as a replacement for `show()`) 264 | * 265 | * @return the AlertDialog that has been shown or null 266 | * @deprecated you should remove any reference to this method before deploying a production version 267 | */ 268 | @Deprecated 269 | public AlertDialog demo() { 270 | createTargetIntent(); 271 | return showDialog(mContext, mContext.getSharedPreferences(mPrefGroup, 0).edit()); 272 | } 273 | 274 | @SuppressLint("NewApi") 275 | private static void savePreferences(SharedPreferences.Editor editor) { 276 | if (editor != null) { 277 | if (Build.VERSION.SDK_INT < 9) { 278 | editor.commit(); 279 | } 280 | else { 281 | editor.apply(); 282 | } 283 | } 284 | } 285 | 286 | private static void closeDialog(DialogInterface dialog) { 287 | if (dialog != null) { 288 | dialog.dismiss(); 289 | } 290 | } 291 | 292 | private void setDontShow(SharedPreferences.Editor editor) { 293 | if (editor != null) { 294 | editor.putBoolean(mPreference_dontShow, true); 295 | savePreferences(editor); 296 | } 297 | } 298 | 299 | private void setFirstLaunchTime(SharedPreferences.Editor editor, long time) { 300 | if (editor != null) { 301 | editor.putLong(mPreference_firstLaunch, time); 302 | savePreferences(editor); 303 | } 304 | } 305 | 306 | private void buttonNowClick(SharedPreferences.Editor editor, DialogInterface dialog, Context context) { 307 | setDontShow(editor); 308 | closeDialog(dialog); 309 | try { 310 | context.startActivity(mTargetIntent); 311 | } 312 | catch (ActivityNotFoundException ignored) {} 313 | } 314 | 315 | private void buttonLaterClick(SharedPreferences.Editor editor, DialogInterface dialog) { 316 | setFirstLaunchTime(editor, System.currentTimeMillis()); 317 | closeDialog(dialog); 318 | } 319 | 320 | private void buttonNeverClick(SharedPreferences.Editor editor, DialogInterface dialog) { 321 | setDontShow(editor); 322 | closeDialog(dialog); 323 | } 324 | 325 | private AlertDialog showDialog(final Context context, final SharedPreferences.Editor editor) { 326 | final AlertDialog.Builder rateDialog = new AlertDialog.Builder(context); 327 | rateDialog.setTitle(mText_title); 328 | rateDialog.setMessage(mText_explanation); 329 | rateDialog.setNeutralButton(mText_buttonLater, new DialogInterface.OnClickListener() { 330 | @Override 331 | public void onClick(DialogInterface dialog, int which) { 332 | buttonLaterClick(editor, dialog); 333 | } 334 | }); 335 | rateDialog.setPositiveButton(mText_buttonNow, new DialogInterface.OnClickListener() { 336 | @Override 337 | public void onClick(DialogInterface dialog, int which) { 338 | buttonNowClick(editor, dialog, context); 339 | } 340 | }); 341 | rateDialog.setNegativeButton(mText_buttonNever, new DialogInterface.OnClickListener() { 342 | @Override 343 | public void onClick(DialogInterface dialog, int which) { 344 | buttonNeverClick(editor, dialog); 345 | } 346 | }); 347 | return rateDialog.show(); 348 | } 349 | 350 | } 351 | --------------------------------------------------------------------------------