Search in sources :

Example 1 with MessageActivity

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);
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) AsyncTask(android.os.AsyncTask) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) View(android.view.View) TextView(android.widget.TextView) Button(android.widget.Button) MessageActivity(ar.rulosoft.mimanganu.MessageActivity) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) ProgressBar(android.widget.ProgressBar) SuppressLint(android.annotation.SuppressLint)

Example 2 with MessageActivity

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);
}
Also used : AlertDialog(android.app.AlertDialog) OkHttpClient(okhttp3.OkHttpClient) DialogInterface(android.content.DialogInterface) AsyncTask(android.os.AsyncTask) AppCompatActivity(android.support.v7.app.AppCompatActivity) JSONException(org.json.JSONException) View(android.view.View) TextView(android.widget.TextView) JSONException(org.json.JSONException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) Button(android.widget.Button) MessageActivity(ar.rulosoft.mimanganu.MessageActivity) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) ProgressBar(android.widget.ProgressBar) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 AsyncTask (android.os.AsyncTask)2 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 Button (android.widget.Button)2 ProgressBar (android.widget.ProgressBar)2 TextView (android.widget.TextView)2 MessageActivity (ar.rulosoft.mimanganu.MessageActivity)2 AppCompatActivity (android.support.v7.app.AppCompatActivity)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 OkHttpClient (okhttp3.OkHttpClient)1 Response (okhttp3.Response)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1