Merge pull request #16 from EladKeyshawn/master

Rate app dialog implemented
This commit is contained in:
Rashiq Ahmad 2016-04-02 20:01:43 +03:00
commit e467d6996f
2 changed files with 124 additions and 3 deletions

View File

@ -20,6 +20,7 @@
package org.kiwix.kiwixmobile; package org.kiwix.kiwixmobile;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -80,12 +81,14 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.prefs.Preferences;
import org.json.JSONArray; import org.json.JSONArray;
import org.kiwix.kiwixmobile.settings.Constants; import org.kiwix.kiwixmobile.settings.Constants;
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity; import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
import org.kiwix.kiwixmobile.utils.KiwixTextToSpeech; import org.kiwix.kiwixmobile.utils.KiwixTextToSpeech;
import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.LanguageUtils;
import org.kiwix.kiwixmobile.utils.files.FileUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils;
import org.kiwix.kiwixmobile.utils.files.RateAppCounter;
import org.kiwix.kiwixmobile.views.AnimatedProgressBar; import org.kiwix.kiwixmobile.views.AnimatedProgressBar;
import org.kiwix.kiwixmobile.views.CompatFindActionModeCallback; import org.kiwix.kiwixmobile.views.CompatFindActionModeCallback;
import org.kiwix.kiwixmobile.views.KiwixWebView; import org.kiwix.kiwixmobile.views.KiwixWebView;
@ -173,7 +176,8 @@ public class KiwixMobileActivity extends AppCompatActivity
private ActionMode mActionMode = null; private ActionMode mActionMode = null;
private KiwixWebView tempForUndo; private KiwixWebView tempForUndo;
private LinearLayout snackbarLayout; private LinearLayout snackbarLayout;
private RateAppCounter visitCounterPref;
private int tempVisitCount;
@Override @Override
public void onActionModeStarted(ActionMode mode) { public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) { if (mActionMode == null) {
@ -221,6 +225,17 @@ public class KiwixMobileActivity extends AppCompatActivity
toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
visitCounterPref = new RateAppCounter(this);
tempVisitCount = visitCounterPref.getCount();
++tempVisitCount;
visitCounterPref.setCount(tempVisitCount);
if(tempVisitCount >= 5 && !visitCounterPref.getNoThanksState()){
showRateDialog(this,visitCounterPref.getEditor());
}
bookmarks = new ArrayList<>(); bookmarks = new ArrayList<>();
requestClearHistoryAfterLoad = false; requestClearHistoryAfterLoad = false;
requestWebReloadOnFinished = 0; requestWebReloadOnFinished = 0;
@ -235,9 +250,8 @@ public class KiwixMobileActivity extends AppCompatActivity
exitFullscreenButton = (ImageButton) findViewById(R.id.FullscreenControlButton); exitFullscreenButton = (ImageButton) findViewById(R.id.FullscreenControlButton);
tempForUndo = tempForUndo =
new KiwixWebView(getApplicationContext()); /** initializing temporary tab value **/ new KiwixWebView(getApplicationContext()); /** initializing temporary tab value **/
snackbarLayout = snackbarLayout =
(LinearLayout) findViewById(R.id.linearlayout_main); /** Coordinator layout definition**/ (LinearLayout) findViewById(R.id.linearlayout_main); /** Linear layout definition**/
RelativeLayout newTabButton = (RelativeLayout) findViewById(R.id.new_tab_button); RelativeLayout newTabButton = (RelativeLayout) findViewById(R.id.new_tab_button);
newTabButton.setOnClickListener(new View.OnClickListener() { newTabButton.setOnClickListener(new View.OnClickListener() {
@ -299,6 +313,73 @@ public class KiwixMobileActivity extends AppCompatActivity
updateTitle(ZimContentProvider.getZimFileTitle()); updateTitle(ZimContentProvider.getZimFileTitle());
} }
public void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
alertDialog.setTitle("Please Rate");
alertDialog.setMessage("If you enjoy using "
+ getString(R.string.app_name)
+ ", please take a moment to rate it. Thanks for your support!");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Rate!",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
visitCounterPref.setNoThanksState(true);
goToRateApp();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No, thanks",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
visitCounterPref.setNoThanksState(true);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Remind me later",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
tempVisitCount = 0;
visitCounterPref.setCount(tempVisitCount);
}
});
alertDialog.show();
}
private void goToRateApp() {
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
}
}
private void updateTitle(String zimFileTitle) { private void updateTitle(String zimFileTitle) {
if (zimFileTitle == null || zimFileTitle.trim().isEmpty()) { if (zimFileTitle == null || zimFileTitle.trim().isEmpty()) {
getSupportActionBar().setTitle(getString(R.string.app_name)); getSupportActionBar().setTitle(getString(R.string.app_name));
@ -430,6 +511,7 @@ public class KiwixMobileActivity extends AppCompatActivity
@Override @Override
public void onClick(View v) { public void onClick(View v) {
restoreTabAtIndex(tempForUndo.getUrl(), index); restoreTabAtIndex(tempForUndo.getUrl(), index);
selectTab(index);
} }
}); });
undoSnackbar.setActionTextColor(getResources().getColor(R.color.white_undo)); undoSnackbar.setActionTextColor(getResources().getColor(R.color.white_undo));

View File

@ -0,0 +1,39 @@
package org.kiwix.kiwixmobile.utils.files;
import android.content.Context;
import android.content.SharedPreferences;
public class RateAppCounter {
String MASTER_NAME = "visitCounter";
String NOTHANKS_CLICKED = "clickedNoThanks";
SharedPreferences visitCounter;
public RateAppCounter(Context context) {
visitCounter = context.getSharedPreferences(MASTER_NAME, 0);
visitCounter = context.getSharedPreferences(NOTHANKS_CLICKED,0);
}
public boolean getNoThanksState(){
return visitCounter.getBoolean(NOTHANKS_CLICKED, false);
}
public void setNoThanksState(boolean val){
SharedPreferences.Editor CounterEditor = visitCounter.edit();
CounterEditor.putBoolean(NOTHANKS_CLICKED, val);
CounterEditor.commit();
}
public void setCount(int count) {
SharedPreferences.Editor CounterEditor = visitCounter.edit();
CounterEditor.putInt("count", count);
CounterEditor.commit();
}
public SharedPreferences.Editor getEditor(){
return visitCounter.edit();
}
public int getCount() {
return visitCounter.getInt("count", 0);
}
}