use of ar.rulosoft.mimanganu.MessageActivity in project MiMangaNu by raulhaag.
the class AppUpdateUtil method generateUpdateDialog.
@SuppressLint("StaticFieldLeak")
public static void generateUpdateDialog(final Context context) {
if (NetworkUtilsAndReceiver.isConnectedNonDestructive(context))
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
final Triple<String, String, String> info = getCurrentVersion();
((AppCompatActivity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.dialog_update, null);
final TextView desc = rootView.findViewById(R.id.descrption);
final ProgressBar progressBar = rootView.findViewById(R.id.progress);
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
desc.setText(info.getThird());
dialogBuilder.setTitle(context.getString(R.string.new_version) + " " + info.getFirst());
dialogBuilder.setView(rootView);
dialogBuilder.setPositiveButton(context.getString(R.string.download), null);
dialogBuilder.setNegativeButton(context.getString(R.string.close), null);
AlertDialog dialog = dialogBuilder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
final Button cancel = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
if (context instanceof MessageActivity) {
((MessageActivity) context).onBackPressed();
}
}
});
final Button accept = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
AppCompatActivity activity = (AppCompatActivity) context;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((AlertDialog) dialog).setCancelable(false);
String init_download_text = context.getString(R.string.downloading) + " 0%";
desc.setText(init_download_text);
accept.setEnabled(false);
cancel.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
}
});
download(activity, info.getSecond(), progressBar, desc, dialog);
} catch (Exception e) {
Log.e(TAG, "Error while starting download");
e.printStackTrace();
}
}
});
}
});
dialog.show();
}
});
} catch (Exception e) {
Log.e(TAG, "Error while searching for new update");
e.printStackTrace();
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of ar.rulosoft.mimanganu.MessageActivity in project MiMangaNu by raulhaag.
the class UpdateUtil method generateUpdateDialog.
@SuppressLint("StaticFieldLeak")
public static void generateUpdateDialog(final Context context) {
if (NetworkUtilsAndReceiver.isConnectedNonDestructive(context))
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
final OkHttpClient client = Navigator.getInstance().getHttpClient().newBuilder().connectTimeout(3, TimeUnit.SECONDS).readTimeout(3, TimeUnit.SECONDS).build();
Response response = client.newCall(new Request.Builder().url(LATEST_RELEASE_URL).build()).execute();
final JSONObject object = new JSONObject(response.body().string());
String version_name = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
if (!version_name.equals(object.getString("tag_name"))) {
// Test <-----
((AppCompatActivity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.dialog_update, null);
final TextView desc = (TextView) rootView.findViewById(R.id.descrption);
final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.progress);
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
try {
desc.setText(object.getString("body"));
dialogBuilder.setTitle(context.getString(R.string.new_version) + " " + object.getString("tag_name"));
} catch (JSONException e) {
Log.e(TAG, "Error reading source");
}
dialogBuilder.setView(rootView);
dialogBuilder.setPositiveButton(context.getString(R.string.download), null);
dialogBuilder.setNegativeButton(context.getString(R.string.close), null);
AlertDialog dialog = dialogBuilder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
final Button cancel = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
if (context instanceof MessageActivity) {
((MessageActivity) context).onBackPressed();
}
}
});
final Button accept = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
AppCompatActivity activity = (AppCompatActivity) context;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((AlertDialog) dialog).setCancelable(false);
String init_download_text = context.getString(R.string.downloading) + " 0%";
desc.setText(init_download_text);
accept.setEnabled(false);
cancel.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
}
});
download(activity, object.getJSONArray("assets").getJSONObject(0).getString("browser_download_url"), progressBar, desc, dialog);
} catch (Exception e) {
Log.e(TAG, "Error while starting download");
e.printStackTrace();
}
}
});
}
});
dialog.show();
}
});
} else {
Log.i(TAG, "App is up to date!!!!");
}
} catch (Exception e) {
Log.e(TAG, "Error while searching for new update");
e.printStackTrace();
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations