Skip to content

Commit

Permalink
Add option to use Android's Download Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
revanmj committed Mar 11, 2018
1 parent cfb02df commit 9235fe4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ Android-WVersionManager
====================

## Objective
- Whole point of this fork is to convert library to an Android Studio project and remove depency on deprecated org.apache.http library (replacing all calls with URLConnection or HttpURLConnection).
Point of this fork is to:
- Convert WVersionManager library to an Android Studio project and remove depency on deprecated org.apache.http library (replacing all calls with URLConnection or HttpURLConnection)
- Add option to download update using Android's Download Manager (.useDownloadManager(true)). Remember, that you have to add "android.permission.REQUEST_INSTALL_PACKAGES" (if you target Oreo or higher) and "android.permission.WRITE_EXTERNAL_STORAGE" permissions to your app's manifest for this to work

Compiled .jar file can be downloaded from [jar folder in this repo](https://github.com/revanmj/Android-WVersionManager/tree/master/jar) or [releases tab]().
Compiled .jar file can be downloaded from [jar folder in this repo](https://github.com/revanmj/Android-WVersionManager/tree/master/jar) or [releases tab](https://github.com/revanmj/Android-WVersionManager/releases).

You can read original readme with all details about the setup here: https://github.com/winsontan520/Android-WVersionManager/blob/master/README.md

Expand Down
Binary file added jar/wversionmanager-1.5.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions library/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 4
versionName "1.4"
versionCode 5
versionName "1.5"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public interface IWVersionManager {
*/
public void setMessage(String message);

/**
* @param value Set if Android's Download Manager should be used (off by default) instead of opening a link in a browser
*/
public void useDownloadManager(boolean value);

/**
* @return message of dialog
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
Expand Down Expand Up @@ -51,6 +54,7 @@ public class WVersionManager implements IWVersionManager {
private AlertDialogButtonListener listener;
private boolean mDialogCancelable = true;
private boolean mIsAskForRate = false;
private boolean mUseDownloadManager = false;
private String mAskForRatePositiveLabel;
private String mAskForRateNegativeLabel;
private int mMode = 100; // default mode
Expand Down Expand Up @@ -228,6 +232,17 @@ public void setMessage(String message) {
this.message = message;
}

/*
* (non-Javadoc)
*
* @see
* com.winsontan520.wversionmanagertest.IWVersionManager#useDownloadManager(boolean)
*/
@Override
public void useDownloadManager(boolean value) {
mUseDownloadManager = value;
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -351,14 +366,33 @@ public void setReminderTimer(int minutes) {
}

private void updateNow(String url) {
if (url != null) {
if (url != null && !mUseDownloadManager) {
try {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "is update url correct?" + e);
}
} else if (url != null) {
Uri downloadUri = Uri.parse(url);

// Use app's name as Download Manager's notification title
ApplicationInfo appInfo = activity.getApplicationInfo();
int stringId = appInfo.labelRes;
String title = stringId == 0 ?
appInfo.nonLocalizedLabel.toString() : activity.getString(appInfo.labelRes);

// Build Download Manager's request
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setTitle(title);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, downloadUri.getLastPathSegment());

// Start downloading
DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}

}
Expand Down

0 comments on commit 9235fe4

Please sign in to comment.