Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use context instead of activity #129

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions library/src/main/java/com/rampo/updatechecker/ASyncCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class ASyncCheck extends AsyncTask<String, Integer, Integer> {
ASyncCheckResult mResultInterface;
String mVersionDownloadable;

ASyncCheck(Store store, ASyncCheckResult resultInterface, Context activity) {
ASyncCheck(Store store, ASyncCheckResult resultInterface, Context context) {
this.mStore = store;
this.mResultInterface = resultInterface;
this.mContext = activity;
this.mContext = context;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions library/src/main/java/com/rampo/updatechecker/Comparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.rampo.updatechecker;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

public class Comparator {
Expand All @@ -24,10 +25,10 @@ public class Comparator {
*
* @param versionDownloadable String to compare to the version installed of the app.
*/
public static boolean isVersionDownloadableNewer(Activity mActivity, String versionDownloadable) {
public static boolean isVersionDownloadableNewer(Context context, String versionDownloadable) {
String versionInstalled = null;
try {
versionInstalled = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0).versionName;
versionInstalled = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException ignored) {
}
if (versionInstalled.equals(versionDownloadable)) { // If it is equal, no new version downloadable
Expand Down
25 changes: 13 additions & 12 deletions library/src/main/java/com/rampo/updatechecker/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.rampo.updatechecker;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;

Expand All @@ -41,7 +42,7 @@ public class UpdateChecker implements ASyncCheckResult, UpdateCheckerResult {
static int DEFAULT_NOTICE_ICON_RES_ID = 0;
static Notice DEFAULT_NOTICE = Notice.DIALOG;

static Activity mActivity;
static Context mRootContext;
static Store mStore;
static int mSuccessfulChecksRequired;
static Notice mNotice;
Expand All @@ -50,8 +51,8 @@ public class UpdateChecker implements ASyncCheckResult, UpdateCheckerResult {
static ASyncCheckResult mCheckResultCallback;
static boolean mCustomImplementation;

public UpdateChecker(Activity activity) {
mActivity = activity;
public UpdateChecker(Context context) {
mRootContext = context;
mStore = DEFAULT_STORE;
mSuccessfulChecksRequired = DEFAULT_SUCCESSFUL_CHECKS_REQUIRED;
mNotice = DEFAULT_NOTICE;
Expand All @@ -61,8 +62,8 @@ public UpdateChecker(Activity activity) {
mCustomImplementation = false;
}

public UpdateChecker(Activity activity, UpdateCheckerResult updateCheckerResult) {
mActivity = activity;
public UpdateChecker(Context context, UpdateCheckerResult updateCheckerResult) {
mRootContext = context;
mStore = DEFAULT_STORE;
mSuccessfulChecksRequired = DEFAULT_SUCCESSFUL_CHECKS_REQUIRED;
mNotice = DEFAULT_NOTICE;
Expand Down Expand Up @@ -125,7 +126,7 @@ public static void setNoticeIcon(int noticeIconResId) {
* Start the process
*/
public static void start() {
ASyncCheck asynctask = new ASyncCheck(mStore, mCheckResultCallback, mActivity);
ASyncCheck asynctask = new ASyncCheck(mStore, mCheckResultCallback, mRootContext);
asynctask.execute();
}

Expand All @@ -136,7 +137,7 @@ public static void start() {
*/
@Override
public void versionDownloadableFound(String versionDownloadable) {
if (Comparator.isVersionDownloadableNewer(mActivity, versionDownloadable)) {
if (Comparator.isVersionDownloadableNewer(mRootContext, versionDownloadable)) {
if (hasToShowNotice(versionDownloadable) && !hasUserTappedToNotShowNoticeAgain(versionDownloadable)) {
mLibraryResultCallaback.foundUpdateAndShowIt(versionDownloadable);
} else {
Expand Down Expand Up @@ -249,7 +250,7 @@ public void returnStoreError() {
* @see com.rampo.updatechecker.notice.Dialog#userHasTappedToNotShowNoticeAgain(android.content.Context, String)
*/
private boolean hasUserTappedToNotShowNoticeAgain(String versionDownloadable) {
SharedPreferences prefs = mActivity.getSharedPreferences(PREFS_FILENAME, 0);
SharedPreferences prefs = mRootContext.getSharedPreferences(PREFS_FILENAME, 0);
String prefKey = DONT_SHOW_AGAIN_PREF_KEY + versionDownloadable;
return prefs.getBoolean(prefKey, false);
}
Expand All @@ -258,7 +259,7 @@ private boolean hasUserTappedToNotShowNoticeAgain(String versionDownloadable) {
* Show the Notice only if it's the first time or the number of the checks made is a multiple of the argument of setSuccessfulChecksRequired(int) method. (If you don't call setSuccessfulChecksRequired(int) the default is 5).
*/
private boolean hasToShowNotice(String versionDownloadable) {
SharedPreferences prefs = mActivity.getSharedPreferences(PREFS_FILENAME, 0);
SharedPreferences prefs = mRootContext.getSharedPreferences(PREFS_FILENAME, 0);
String prefKey = SUCCESSFUL_CHEKS_PREF_KEY + versionDownloadable;
int mChecksMade = prefs.getInt(prefKey, 0);
if (mChecksMade % mSuccessfulChecksRequired == 0 || mChecksMade == 0) {
Expand All @@ -275,7 +276,7 @@ private boolean hasToShowNotice(String versionDownloadable) {
*/
private void saveNumberOfChecksForUpdatedVersion(String versionDownloadable, int mChecksMade) {
mChecksMade++;
SharedPreferences prefs = mActivity.getSharedPreferences(PREFS_FILENAME, 0);
SharedPreferences prefs = mRootContext.getSharedPreferences(PREFS_FILENAME, 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(SUCCESSFUL_CHEKS_PREF_KEY + versionDownloadable, mChecksMade);
editor.commit();
Expand All @@ -286,14 +287,14 @@ private void saveNumberOfChecksForUpdatedVersion(String versionDownloadable, int
* Show Dialog
*/
public void showDialog(String versionDownloadable) {
Dialog.show(mActivity, mStore, versionDownloadable, mNoticeIconResId);
Dialog.show(mRootContext, mStore, versionDownloadable, mNoticeIconResId);
}

/**
* Show Notification
*/
public static void showNotification() {
Notification.show(mActivity, mStore, mNoticeIconResId);
Notification.show(mRootContext, mStore, mNoticeIconResId);
}

/**
Expand Down