Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

Added ability to intercept button clicks to let the user add custom code #50

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion AppRater/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
minSdkVersion 8
targetSdkVersion 18
targetSdkVersion 29
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
}
Expand Down
4 changes: 1 addition & 3 deletions AppRater/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.codechimp.apprater" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-sdk android:targetSdkVersion="17" />
</manifest>
93 changes: 73 additions & 20 deletions AppRater/src/main/java/org/codechimp/apprater/AppRater.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AppRater {
private final static String TAG = AppRater.class.getSimpleName();
// Preference Constants
private final static String PREF_NAME = "apprater";
private final static String PREF_LAUNCH_COUNT = "launch_count";
Expand All @@ -33,8 +38,12 @@ public class AppRater {
private static boolean isVersionNameCheckEnabled;
private static boolean isVersionCodeCheckEnabled;
private static boolean isCancelable = true;
private static Callable<Boolean> beforeRateAction;
private static Callable<Boolean> beforePostponeAction;
private static Callable<Boolean> beforeCancelAction;

private static Market market = new GoogleMarket();
private static ExecutorService executor = Executors.newSingleThreadExecutor();

/**
* Decides if the version name check is active or not
Expand Down Expand Up @@ -196,7 +205,7 @@ public static void rateNow(final Context context) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, market.getMarketURI(context)));
} catch (ActivityNotFoundException activityNotFoundException1) {
Log.e(AppRater.class.getSimpleName(), "Market Intent not found");
Log.e(TAG, "Market Intent not found");
}
}

Expand Down Expand Up @@ -257,10 +266,12 @@ private static void showRateAlertDialog(final Context context, final SharedPrefe
builder.setPositiveButton(context.getString(R.string.rate),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
rateNow(context);
if (editor != null) {
editor.putBoolean(PREF_DONT_SHOW_AGAIN, true);
commitOrApply(editor);
if (beforeRateAction == null || execute(beforeRateAction)) {
rateNow(context);
if (editor != null) {
editor.putBoolean(PREF_DONT_SHOW_AGAIN, true);
commitOrApply(editor);
}
}
dialog.dismiss();
}
Expand All @@ -269,13 +280,15 @@ public void onClick(DialogInterface dialog, int id) {
builder.setNeutralButton(context.getString(R.string.later),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (editor != null) {
Long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_LAUNCH_COUNT, 0);
editor.putBoolean(PREF_REMIND_LATER, true);
editor.putBoolean(PREF_DONT_SHOW_AGAIN, false);
commitOrApply(editor);
if (beforePostponeAction == null || execute(beforePostponeAction)) {
if (editor != null) {
Long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_LAUNCH_COUNT, 0);
editor.putBoolean(PREF_REMIND_LATER, true);
editor.putBoolean(PREF_DONT_SHOW_AGAIN, false);
commitOrApply(editor);
}
}
dialog.dismiss();
}
Expand All @@ -284,13 +297,15 @@ public void onClick(DialogInterface dialog, int id) {
builder.setNegativeButton(context.getString(R.string.no_thanks),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (editor != null) {
editor.putBoolean(PREF_DONT_SHOW_AGAIN, true);
editor.putBoolean(PREF_REMIND_LATER, false);
long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_LAUNCH_COUNT, 0);
commitOrApply(editor);
if (beforeCancelAction == null || execute(beforeCancelAction)) {
if (editor != null) {
editor.putBoolean(PREF_DONT_SHOW_AGAIN, true);
editor.putBoolean(PREF_REMIND_LATER, false);
long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_LAUNCH_COUNT, 0);
commitOrApply(editor);
}
}
dialog.dismiss();
}
Expand All @@ -299,6 +314,17 @@ public void onClick(DialogInterface dialog, int id) {
builder.show();
}

private static boolean execute(Callable<Boolean> action) {
try {
return executor.submit(action).get();
} catch (InterruptedException e) {
Log.e(TAG, "error in custom action", e);
} catch (ExecutionException e) {
Log.e(TAG, "error in custom action", e);
}
return false;
}

@SuppressLint("NewApi")
private static void commitOrApply(SharedPreferences.Editor editor) {
if (Build.VERSION.SDK_INT > 8) {
Expand All @@ -318,4 +344,31 @@ public static void resetData(Context context) {
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
commitOrApply(editor);
}

/**
* Register a callback to be invoked when 'Rate Now' button is clicked.
* Callback will be executed in background thread, so be sure to not touch UI directly.
* @param action custom action; returns true if market should be opened, false otherwise
*/
public static void onRateClick(Callable<Boolean> action) {
beforeRateAction = action;
}

/**
* Register a callback to be invoked when 'Later' button is clicked.
* Callback will be executed in background thread, so be sure to not touch UI directly.
* @param action custom action; returns true if user decision should be saved, false otherwise
*/
public static void onLaterClick(Callable<Boolean> action) {
beforePostponeAction = action;
}

/**
* Register a callback to be invoked when 'No, thanks' button is clicked.
* Callback will be executed in background thread, so be sure to not touch UI directly.
* @param action custom action; returns true if user decision should be saved, false otherwise
*/
public static void onNoClick(Callable<Boolean> action) {
beforeCancelAction = action;
}
}
2 changes: 1 addition & 1 deletion AppRaterDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
minSdkVersion 8
targetSdkVersion 18
targetSdkVersion 29
versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
}
Expand Down
2 changes: 1 addition & 1 deletion AppRaterDemo/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

# Project target.
target=android-18
android.library.reference.1=../AppRater
android.library.reference.1=../AppRaterLib
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import org.codechimp.apprater.GoogleMarket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.widget.Toast;

import java.util.concurrent.Callable;

public class MainActivity extends Activity {

Expand All @@ -33,7 +38,20 @@ public void onClick(View v) {
}
});


// You can intercept button clicks to add custom logic
// Return true if you'd like to process button click as usual or false if processing should be interrupted
AppRater.onRateClick(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Opening store...", Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
// Optionally you can set the Market you want to use prior to calling app_launched
// If setMarket not called it will default to Google Play
// Current implementations are Google Play and Amazon App Store, you can add your own by implementing Market
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ You can implement your own market, implementing the Market interface and parse y

If you want to have a "Rate Now" menu option to go straight to your play store listing call `AppRater.rateNow(this);` within your menu code.

You can intercept button clicks to add custom logic. Use `onRateClick`, `onLaterClick` and `onNoClick` to perform additional actions like analytics reporting.
Try out the demo within this repository.

## Gradle
Expand Down
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
classpath 'com.android.tools.build:gradle:4.0.0'
}
}

ext {
compileSdkVersion = 19
buildToolsVersion = "19.1.0"
compileSdkVersion = 29
buildToolsVersion = "30.0.2"
}

def isReleaseBuild() {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Dec 07 11:28:32 GMT 2014
#Mon Oct 05 15:24:08 CEST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
Empty file modified gradlew
100644 → 100755
Empty file.