Skip to content
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
106 changes: 90 additions & 16 deletions library/src/main/java/com/rampo/updatechecker/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.rampo.updatechecker.notice.Dialog;
import com.rampo.updatechecker.notice.Notice;
Expand All @@ -31,24 +32,27 @@
*/
public class UpdateChecker implements ASyncCheckResult, UpdateCheckerResult {

public static final String ROOT_PLAY_STORE_DEVICE = "market://details?id=";
public static final String PREFS_FILENAME = "updateChecker";
public static final String DONT_SHOW_AGAIN_PREF_KEY = "dontShow";
public static final String ROOT_PLAY_STORE_DEVICE = "market://details?id=";
public static final String PREFS_FILENAME = "updateChecker";
public static final String DONT_SHOW_AGAIN_PREF_KEY = "dontShow";
private static final String SUCCESSFUL_CHEKS_PREF_KEY = "nLaunches";

static Store DEFAULT_STORE = Store.GOOGLE_PLAY;
static int DEFAULT_SUCCESSFUL_CHECKS_REQUIRED = 5;
static int DEFAULT_NOTICE_ICON_RES_ID = 0;
static Notice DEFAULT_NOTICE = Notice.DIALOG;
static Store DEFAULT_STORE = Store.GOOGLE_PLAY;
static int DEFAULT_SUCCESSFUL_CHECKS_REQUIRED = 5;
static int DEFAULT_NOTICE_ICON_RES_ID = 0;
static Notice DEFAULT_NOTICE = Notice.DIALOG;

static Activity mActivity;
static Store mStore;
static int mSuccessfulChecksRequired;
static Notice mNotice;
static int mNoticeIconResId;
static Activity mActivity;
static Store mStore;
static int mSuccessfulChecksRequired;
static Notice mNotice;
static int mNoticeIconResId;
static UpdateCheckerResult mLibraryResultCallaback;
static ASyncCheckResult mCheckResultCallback;
static boolean mCustomImplementation;
static ASyncCheckResult mCheckResultCallback;
static boolean mCustomImplementation;
static CharSequence mNotificationPositiveButtonText;
static CharSequence mNotificationHeaderText;
static CharSequence mNotificationContentText;

public UpdateChecker(Activity activity) {
mActivity = activity;
Expand All @@ -59,6 +63,9 @@ public UpdateChecker(Activity activity) {
mCheckResultCallback = this;
mLibraryResultCallaback = this;
mCustomImplementation = false;
mNotificationHeaderText = null;
mNotificationContentText = null;
mNotificationPositiveButtonText = null;
}

public UpdateChecker(Activity activity, UpdateCheckerResult updateCheckerResult) {
Expand All @@ -70,6 +77,9 @@ public UpdateChecker(Activity activity, UpdateCheckerResult updateCheckerResult)
mCheckResultCallback = this;
mLibraryResultCallaback = updateCheckerResult;
mCustomImplementation = true;
mNotificationHeaderText = null;
mNotificationContentText = null;
mNotificationPositiveButtonText = null;
}

/**
Expand Down Expand Up @@ -121,6 +131,68 @@ public static void setNoticeIcon(int noticeIconResId) {
}
}

/**
* Set the header text of the dialog. If you don't call this, the dialog will have the default text
*
* @param notificationHeaderText text for the header in the dialog.
* @see com.rampo.updatechecker.notice.Dialog
*/
public static void setNotificationHeaderText(final CharSequence notificationHeaderText) {
if (notificationHeaderText == null || TextUtils.getTrimmedLength(notificationHeaderText) == 0) {
throw new IllegalStateException("The can't set an empty string or a null to the dialog header text");
}

if (mCustomImplementation) {
throw new IllegalStateException("You can't set the dialog header text when you choose a custom "
+ "implementation.\nThe Dialog is controlled manually by you with the "
+ "callbacks.");
}

mNotificationHeaderText = notificationHeaderText;
}

/**
* Set the content text of the dialog. If you don't call this, the dialog will have the default text
*
* @param notificationContentText text for the content text in the dialog.
* @see com.rampo.updatechecker.notice.Dialog
*/
public static void setNotificationContentText(final CharSequence notificationContentText) {
if (notificationContentText == null || TextUtils.getTrimmedLength(notificationContentText) == 0) {
throw new IllegalStateException("The can't set an empty string or a null to the dialog content text");
}

if (mCustomImplementation) {
throw new IllegalStateException("You can't set the dialog content text when you choose a custom "
+ "implementation.\nThe Dialog is controlled manually by you with the "
+ "callbacks.");
}

mNotificationContentText = notificationContentText;
}

/**
* Set the positive button text of the dialog. If you don't call this, the dialog will have the default text
*
* @param notificationPositiveButtonText text for the positive button in the dialog.
* @see com.rampo.updatechecker.notice.Dialog
*/
public static void setNotificationPositiveButtonText(final CharSequence notificationPositiveButtonText) {
if (notificationPositiveButtonText == null || TextUtils.getTrimmedLength(notificationPositiveButtonText) == 0) {
throw new IllegalStateException("The can't set an empty string or a null to the dialog positive button");
}

if (mCustomImplementation) {
throw new IllegalStateException("You can't set the dialog positive button text when you choose a custom "
+ "implementation.\nThe Dialog is controlled manually by you with the "
+ "callbacks.");
}

mNotificationPositiveButtonText = notificationPositiveButtonText;


}

/**
* Start the process
*/
Expand Down Expand Up @@ -286,7 +358,8 @@ private void saveNumberOfChecksForUpdatedVersion(String versionDownloadable, int
* Show Dialog
*/
public void showDialog(String versionDownloadable) {
Dialog.show(mActivity, mStore, versionDownloadable, mNoticeIconResId);
Dialog.show(mActivity, mStore, versionDownloadable, mNoticeIconResId, mNotificationHeaderText,
mNotificationContentText, mNotificationPositiveButtonText);
}

/**
Expand Down Expand Up @@ -358,7 +431,8 @@ public static void checkForNotification(int notificationIconResId, FragmentActiv
* @param notificationIconResId R.drawable.* resource to set to Notification icon.
*/
@Deprecated
public static void checkForNotification(int notificationIconResId, FragmentActivity fragmentActivity, int successfulChecksRequired) {
public static void checkForNotification(int notificationIconResId, FragmentActivity fragmentActivity,
int successfulChecksRequired) {
}

/**
Expand Down
74 changes: 48 additions & 26 deletions library/src/main/java/com/rampo/updatechecker/notice/Dialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.WindowManager;

Expand All @@ -38,7 +39,14 @@
*/
public class Dialog {

public static void show(final Context context, final Store store, final String versionDownloadable, final int dialogIconResId) {
public static void show(final Context context, final Store store, final String versionDownloadable,
final int dialogIconResId,
@Nullable
final CharSequence mDialogHeaderText,
@Nullable
final CharSequence mDialogContentText,
@Nullable
final CharSequence mDialogConfirmButtonText) {
try {
String storeName = null;
if (store == Store.GOOGLE_PLAY) {
Expand All @@ -49,33 +57,46 @@ public static void show(final Context context, final Store store, final String v
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
String appName = null;
try {
appName = (String) context.getPackageManager().getApplicationLabel(context.getPackageManager().getApplicationInfo(context.getPackageName(), 0));
appName = (String) context.getPackageManager()
.getApplicationLabel(context.getPackageManager()
.getApplicationInfo(context.getPackageName(), 0));
} catch (PackageManager.NameNotFoundException ignored) {
}
alertDialogBuilder.setTitle(context.getResources().getString(R.string.newUpdateAvailable));
alertDialogBuilder.setMessage(context.getResources().getString(R.string.downloadFor, appName, storeName))
.setCancelable(true)
.setPositiveButton(context.getString(R.string.dialogPositiveButton), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
goToMarket(context);
dialog.cancel();
}
})
.setNeutralButton(context.getString(R.string.dialogNeutralButton), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton(context.getString(R.string.dialogNegativeButton), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
userHasTappedToNotShowNoticeAgain(context, versionDownloadable);
dialog.cancel();
}
// If alternative text set manually, then take this (mDialogHeaderText != null)
alertDialogBuilder.setTitle(mDialogHeaderText != null ? mDialogHeaderText :
activity.getResources().getString(R.string.newUpdateAvailable));
// If alternative text set manually, then take this (mDialogContentText != null)
alertDialogBuilder.setMessage(mDialogContentText != null ? mDialogContentText : context.getResources()
.getString(R.string.downloadFor,
appName,
storeName))
.setCancelable(true)
.setPositiveButton(mDialogConfirmButtonText != null ? mDialogConfirmButtonText :
context.getString(R.string.dialogPositiveButton),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
goToMarket(context);
dialog.cancel();
}
})
.setNeutralButton(context.getString(R.string.dialogNeutralButton),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton(context.getString(R.string.dialogNegativeButton),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
userHasTappedToNotShowNoticeAgain(context,
versionDownloadable);
dialog.cancel();
}

});
});
if (dialogIconResId != 0) {
alertDialogBuilder.setIcon(dialogIconResId);
}
Expand All @@ -101,7 +122,8 @@ private static void userHasTappedToNotShowNoticeAgain(Context mContext, String m
}

private static void goToMarket(Context mContext) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(UpdateChecker.ROOT_PLAY_STORE_DEVICE + mContext.getPackageName())));
mContext.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(UpdateChecker.ROOT_PLAY_STORE_DEVICE + mContext.getPackageName())));

}
}