-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
plugins { | ||
id 'com.android.library' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.appcompat:appcompat:1.3.1' | ||
implementation 'com.google.android.material:material:1.4.0' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | ||
} |
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 com.lib.appUpdater; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
import androidx.test.ext.junit.runners.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.getInstrumentation().getTargetContext(); | ||
assertEquals("com.lib.appUpdater.test", appContext.getPackageName()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.lib.appUpdater"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
|
||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package com.lib.appUpdater; | ||
|
||
import android.app.ProgressDialog; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.os.AsyncTask; | ||
import android.os.Build; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.util.Log; | ||
import android.webkit.URLUtil; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.content.FileProvider; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.Objects; | ||
|
||
|
||
public class AppUpdater extends AppCompatActivity { | ||
|
||
Context context; | ||
|
||
public AppUpdater(Context context) { | ||
this.context = context; | ||
} | ||
|
||
public void updateAppWithUrl(String url, String fileName){ | ||
|
||
if(URLUtil.isValidUrl(url)){ | ||
new DownloadUpdatedVersion().execute(url, fileName); | ||
} | ||
} | ||
|
||
private class DownloadUpdatedVersion extends AsyncTask<String, Integer, Boolean> { | ||
|
||
|
||
ProgressDialog progressDialog; | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
super.onPreExecute(); | ||
progressDialog = new ProgressDialog(context); | ||
progressDialog.setCancelable(false); | ||
progressDialog.setMessage("Updating..."); | ||
|
||
progressDialog.setCanceledOnTouchOutside(false); | ||
progressDialog.show(); | ||
|
||
} | ||
|
||
@Override | ||
protected Boolean doInBackground(String... values) { | ||
String downloadUrl = values[0]; | ||
String fileName = values[1]; | ||
Boolean flag = false; | ||
int count; | ||
|
||
|
||
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/"; | ||
File outputFile = new File(path+""+fileName); | ||
|
||
while (outputFile.exists()) { | ||
boolean deleted = outputFile.delete(); | ||
|
||
} | ||
|
||
File directory = new File(path); | ||
if (!directory.exists()) { | ||
directory.mkdirs(); | ||
} | ||
|
||
try { | ||
URL url = new URL(downloadUrl); | ||
URLConnection connection = url.openConnection(); | ||
connection.connect(); | ||
|
||
Integer totalSize = connection.getContentLength(); | ||
|
||
InputStream input = new BufferedInputStream(url.openStream()); | ||
|
||
OutputStream output = new FileOutputStream(outputFile); | ||
|
||
byte data[] = new byte[1024]; | ||
|
||
long total = 0; | ||
|
||
while ((count = input.read(data)) != -1) { | ||
total += count; | ||
|
||
publishProgress(Integer.valueOf("" + (int) ((total * 100) / totalSize))); | ||
|
||
output.write(data, 0, count); | ||
} | ||
|
||
output.flush(); | ||
|
||
output.close(); | ||
input.close(); | ||
openNewVersion(outputFile.getPath()); | ||
flag = true; | ||
} catch (Exception e) { | ||
Log.e("Error: ", e.getMessage()); | ||
flag = false; | ||
} | ||
|
||
|
||
|
||
return flag; | ||
} | ||
|
||
private void openNewVersion(String path) { | ||
Intent intent = new Intent(Intent.ACTION_VIEW); | ||
intent.setDataAndType( | ||
getUriFromFile(path), | ||
"application/vnd.android.package-archive" | ||
); | ||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | ||
context.startActivity(intent); | ||
} | ||
|
||
private Uri getUriFromFile(String path) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { | ||
return Uri.fromFile(new File(path)); | ||
} else { | ||
|
||
return FileProvider.getUriForFile( | ||
context, | ||
context.getPackageName() + ".provider", | ||
new File(path) | ||
); | ||
} | ||
} | ||
|
||
protected void onProgressUpdate(Integer... progress) { | ||
|
||
String msg = ""; | ||
Integer updatedProgress = progress[0]; | ||
if (updatedProgress != null) { | ||
progressDialog.setProgress(updatedProgress); | ||
if (updatedProgress > 99){ | ||
msg = "Finishing... "; | ||
} else{ | ||
msg = "Downloading... "+updatedProgress; | ||
} | ||
} | ||
|
||
progressDialog.setMessage(msg); | ||
|
||
progressDialog.setProgress(progress[0]); | ||
} | ||
|
||
|
||
@Override | ||
protected void onPostExecute(Boolean result) { | ||
|
||
progressDialog.dismiss(); | ||
if (result != null && result) { | ||
Toast.makeText(context, "Please install the updated application.", Toast.LENGTH_SHORT).show(); | ||
} else { | ||
Toast.makeText(context, "Please try again later", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} |