Skip to content

Commit

Permalink
Version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-4444 committed Mar 5, 2019
1 parent 94e692d commit c46932f
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 218 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile project(path: ':smartrate')
}
219 changes: 4 additions & 215 deletions app/src/main/java/guy/rateapplication/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.widget.TextView;
import android.widget.Toast;

import guy4444.smartrate.SmartRate;

public class MainActivity extends AppCompatActivity {

@Override
Expand All @@ -41,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rate(MainActivity.this
SmartRate.Rate(MainActivity.this
, "Rate Us"
, "Tell others what you think about this app"
, "Continue"
Expand All @@ -54,221 +56,8 @@ public void onClick(View view) {
}
});

saveLastAskTime(this, 0);
//saveLastAskTime(this, 0);
}

private static final long DONT_ASK_AGAIN_VALUE = -1;
private static final long TIME_BETWEEN_DIALOG_MS = 1000l * 20 * 1;
private static final String SP_LIBRARY_NAME = "SP_RATE_LIBRARY";
private static final String SP_KEY_LAST_ASK_TIME = "SP_KEY_LAST_ASK_TIME";
private static int selectedStar = 1;
private static String DEFAULT_TEXT_TITLE = "Rate Us";
private static String DEFAULT_TEXT_CONTENT = "Tell others what you think about this app";
private static String DEFAULT_TEXT_OK = "Continue";
private static String DEFAULT_TEXT_LATER = "Ask me later";
private static String DEFAULT_TEXT_STOP = "Never ask again";
private static String DEFAULT_TEXT_THANKS = "Thanks for the feedback";

private static String shadeColor(String color, int percent) {

int R = Integer.parseInt(color.substring(1,3),16);
int G = Integer.parseInt(color.substring(3,5),16);
int B = Integer.parseInt(color.substring(5,7),16);

R = R * (100 + percent) / 100;
G = G * (100 + percent) / 100;
B = B * (100 + percent) / 100;

R = (R<255)?R:255;
G = (G<255)?G:255;
B = (B<255)?B:255;

String RR = (Integer.toString(R, 16).length()==1) ? "0" + Integer.toString(R, 16) : Integer.toString(R, 16);
String GG = (Integer.toString(G, 16).length()==1) ? "0" + Integer.toString(G, 16) : Integer.toString(G, 16);
String BB = (Integer.toString(B, 16).length()==1) ? "0" + Integer.toString(B, 16) : Integer.toString(B, 16);

return "#"+RR+GG+BB;
}

public static void Rate(
final Activity activity
, final String _title
, final String _content
, final String _ok_text
, final String _later_text
, final String _stop_text
, final String _thanksForFeedback
, final int mainColor
, final int openStoreFromXStars
) {


final String title = (_title!=null && !_title.equals("")) ? _title : DEFAULT_TEXT_TITLE;
final String content = (_content!=null && !_content.equals("")) ? _content : DEFAULT_TEXT_CONTENT;
final String ok_text = (_ok_text!=null && !_ok_text.equals("")) ? _ok_text : DEFAULT_TEXT_OK;
final String later_text = (_later_text!=null && !_later_text.equals("")) ? _later_text : DEFAULT_TEXT_LATER;
final String stop_text = (_stop_text!=null && !_stop_text.equals("")) ? _stop_text : DEFAULT_TEXT_STOP;
final String thanksForFeedback = (_thanksForFeedback!=null && !_thanksForFeedback.equals("")) ? _thanksForFeedback : DEFAULT_TEXT_THANKS;

if (getLastAskTime(activity) == DONT_ASK_AGAIN_VALUE) {
// user already rate or click on never ask button
return;
}

if (System.currentTimeMillis() < getLastAskTime(activity) + TIME_BETWEEN_DIALOG_MS) {
return;
}

saveLastAskTime(activity, System.currentTimeMillis());

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
LayoutInflater inflater = activity.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_rate, null);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setCancelable(true);
alertDialog.setCanceledOnTouchOutside(true);

final RelativeLayout alert_LAY_back = (RelativeLayout) dialogView.findViewById(R.id.alert_LAY_back);
final AppCompatButton alert_BTN_ok = (AppCompatButton) dialogView.findViewById(R.id.alert_BTN_ok);
final Button alert_BTN_later = (Button) dialogView.findViewById(R.id.alert_BTN_later);
final Button alert_BTN_stop = (Button) dialogView.findViewById(R.id.alert_BTN_stop);
final TextView alert_LBL_title = (TextView) dialogView.findViewById(R.id.alert_LBL_title);
final TextView alert_LBL_content = (TextView) dialogView.findViewById(R.id.alert_LBL_content);
final ImageButton alert_BTN_star_1 = (ImageButton) dialogView.findViewById(R.id.alert_BTN_star_1);
final ImageButton alert_BTN_star_2 = (ImageButton) dialogView.findViewById(R.id.alert_BTN_star_2);
final ImageButton alert_BTN_star_3 = (ImageButton) dialogView.findViewById(R.id.alert_BTN_star_3);
final ImageButton alert_BTN_star_4 = (ImageButton) dialogView.findViewById(R.id.alert_BTN_star_4);
final ImageButton alert_BTN_star_5 = (ImageButton) dialogView.findViewById(R.id.alert_BTN_star_5);
final ImageButton[] stars = new ImageButton[]{alert_BTN_star_1, alert_BTN_star_2, alert_BTN_star_3, alert_BTN_star_4, alert_BTN_star_5};


alert_LAY_back.setBackgroundColor(mainColor);
alert_BTN_ok.getBackground().setColorFilter(mainColor, PorterDuff.Mode.MULTIPLY);
alert_LBL_title.setTextColor(mainColor);
alert_LBL_content.setTextColor(mainColor);
alert_BTN_later.setTextColor(Color.parseColor(shadeColor(String.format("#%06X", 0xFFFFFF & mainColor), -33)));
alert_BTN_stop.setTextColor(Color.parseColor(shadeColor(String.format("#%06X", 0xFFFFFF & mainColor), -33)));


final int drawable_active = R.drawable.ic_star_active;
final int drawable_deactive = R.drawable.ic_star_deactive;

View.OnClickListener starsClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int clickedIndex = -1;
for (int i = 0; i < stars.length; i++) {
if (stars[i].getId() == v.getId()) {
clickedIndex = i;
break;
}
}

if (clickedIndex != -1) {
for (int i = 0; i <= clickedIndex; i++) {
stars[i].setImageResource(drawable_active);
}
for (int i = clickedIndex + 1; i < stars.length; i++) {
stars[i].setImageResource(drawable_deactive);
}
}

alert_BTN_ok.setEnabled(true);
alert_BTN_ok.setText((clickedIndex + 1) + "/5\n" + ok_text);
selectedStar = clickedIndex + 1;
}
};

alert_BTN_star_1.setOnClickListener(starsClickListener);
alert_BTN_star_2.setOnClickListener(starsClickListener);
alert_BTN_star_3.setOnClickListener(starsClickListener);
alert_BTN_star_4.setOnClickListener(starsClickListener);
alert_BTN_star_5.setOnClickListener(starsClickListener);



alert_LBL_title.setText(title);
alert_LBL_content.setText(content);



if (ok_text!=null && !ok_text.equals("")) {
alert_BTN_ok.setText(ok_text);
alert_BTN_ok.setText("?/5\n" + ok_text);
alert_BTN_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveLastAskTime(activity, DONT_ASK_AGAIN_VALUE);

int _openStoreFrom_Stars = openStoreFromXStars;
if (openStoreFromXStars < 1 || openStoreFromXStars > 5) {
_openStoreFrom_Stars = 1;
}
if (selectedStar >= _openStoreFrom_Stars) {
launchMarket(activity);
} else {
Toast.makeText(activity, thanksForFeedback, Toast.LENGTH_SHORT).show();
}
alertDialog.dismiss();
}
});
}
else {
alert_BTN_ok.setVisibility(View.INVISIBLE);
}
alert_BTN_ok.setEnabled(false);

if (later_text!=null && !later_text.equals("")) {
alert_BTN_later.setText(later_text);
alert_BTN_later.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

alertDialog.dismiss();
}
});
}
else {
alert_BTN_later.setVisibility(View.INVISIBLE);
}

if (stop_text!=null && !stop_text.equals("")) {
alert_BTN_stop.setText(stop_text);
alert_BTN_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveLastAskTime(activity, DONT_ASK_AGAIN_VALUE);
alertDialog.dismiss();
}
});
}
else {
alert_BTN_stop.setVisibility(View.INVISIBLE);
}

alertDialog.show();
}

private static void launchMarket(Activity activity) {
Uri uri = Uri.parse("market://details?id=" + activity.getPackageName());
Intent myAppLinkToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
activity.startActivity(myAppLinkToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, " unable to find google play app", Toast.LENGTH_LONG).show();
}
}

private static long getLastAskTime(Activity activity) {
SharedPreferences sharedPreferences = activity.getSharedPreferences(SP_LIBRARY_NAME, Context.MODE_PRIVATE);
long val = sharedPreferences.getLong(SP_KEY_LAST_ASK_TIME, 0);
return val;
}

private static void saveLastAskTime(Activity activity, long time) {
SharedPreferences.Editor editor = activity.getSharedPreferences(SP_LIBRARY_NAME, Context.MODE_PRIVATE).edit();
editor.putLong(SP_KEY_LAST_ASK_TIME, time);
editor.apply();
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
app:srcCompat="@android:drawable/star_off" />

</android.support.design.widget.CoordinatorLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Click on rate FAB!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app'
include ':app', ':smartrate'
1 change: 1 addition & 0 deletions smartrate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
34 changes: 34 additions & 0 deletions smartrate/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28



defaultConfig {
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions smartrate/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package guy4444.smartrate;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("guy4444.smartrate.test", appContext.getPackageName());
}
}
2 changes: 2 additions & 0 deletions smartrate/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="guy4444.smartrate" />
Loading

0 comments on commit c46932f

Please sign in to comment.