Skip to content

Commit

Permalink
Added update manager
Browse files Browse the repository at this point in the history
  • Loading branch information
HirbodBehnam committed Jun 16, 2019
1 parent dd5d766 commit 2948b06
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.hirbod.randomnumbergenerator"
minSdkVersion 14
targetSdkVersion 28
versionCode 20
versionName "1.3.1"
versionCode 22
versionName "1.3.2"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
Expand All @@ -17,6 +20,10 @@
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.Random;

public class MainActivity extends Activity {
Expand Down Expand Up @@ -127,6 +134,13 @@ public void onClick(View v) {
SetClipboard(MainActivity.this,((EditText) findViewById(R.id.editText)).getText().toString());
}
});
//Updater{
try{
int version = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
new CheckUpdates(this, version).execute();
}catch (PackageManager.NameNotFoundException ex){
ex.printStackTrace();
}
}
public static void SetClipboard(Context context,String text){
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
Expand Down Expand Up @@ -236,4 +250,61 @@ private void changeFarsi(){
((CheckBox) findViewById(R.id.checkBox)).setText("کپی خودکار");
}
public static boolean isInteger(float str) { return str % 1 == 0; }
private static class CheckUpdates extends AsyncTask<Void,Void,Integer>{
private WeakReference<MainActivity> activityReference;
private int currentVersion;
// only retain a weak reference to the activity
CheckUpdates(MainActivity context, int currentVersion) {
activityReference = new WeakReference<>(context);
this.currentVersion = currentVersion;
}
@Override
protected Integer doInBackground(Void... voids) {
//https://alvinalexander.com/blog/post/java/java-how-read-from-url-string-text
int webVersion = Integer.MIN_VALUE;
try
{
URL url = new URL("https://raw.githubusercontent.com/HirbodBehnam/RandomNumberGenerator/master/app/build.gradle");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
if(line.trim().startsWith("versionCode")){
webVersion = Integer.parseInt(line.trim().split(" ")[1]);
break;
}
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
return -1;
}
return webVersion > currentVersion ? webVersion : -1;
}

@Override
protected void onPostExecute(Integer nextVersion) {
super.onPostExecute(nextVersion);
if(nextVersion == -1)
return;
//https://stackoverflow.com/a/46166223/4213397
final MainActivity activity = activityReference.get();
if (activity == null || activity.isFinishing())
return;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
new AlertDialog.Builder(activity)
.setMessage(preferences.getInt("Lang",0) == 1 ? ("یک آپدیت برنامه به ورژن ساخت " + nextVersion + " موجود است.") : ("A new update to build version " + nextVersion + " is available. Do you want to update?"))
.setTitle(preferences.getInt("Lang",0) == 1 ? "آپدیت برنامه" : "Update Available")
.setPositiveButton(preferences.getInt("Lang",0) == 1 ? "آپدیت" :"Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://cafebazaar.ir/app/com.hirbod.randomnumbergenerator/"));
activity.startActivity(browserIntent);
}
})
.setNegativeButton(preferences.getInt("Lang",0) == 1 ? "بعدا" :"Later", null)
.show();
}
}
}

0 comments on commit 2948b06

Please # to comment.