Example usage for android.app ProgressDialog setCancelable

List of usage examples for android.app ProgressDialog setCancelable

Introduction

In this page you can find the example usage for android.app ProgressDialog setCancelable.

Prototype

public void setCancelable(boolean flag) 

Source Link

Document

Sets whether this dialog is cancelable with the KeyEvent#KEYCODE_BACK BACK key.

Usage

From source file:com.juick.android.MessageMenu.java

public void translateToRussian(final String body, final Utils.Function<Void, String[]> callback) {
    final ProgressDialog mDialog = new ProgressDialog(activity);
    mDialog.setMessage("Google Translate...");
    mDialog.setCancelable(true);
    mDialog.setCanceledOnTouchOutside(true);
    final ArrayList<HttpGet> actions = new ArrayList<HttpGet>();

    final String[] split = body.split("\\.");
    final String[] results = new String[split.length];
    final int[] pieces = new int[] { 0 };
    for (int i = 0; i < split.length; i++) {
        final String s = split[i];
        if (s.trim().length() == 0) {
            results[i] = split[i];/*from w  w w .j a  v a2s . c  o  m*/
            synchronized (pieces) {
                pieces[0]++;
                if (pieces[0] == results.length) {
                    mDialog.hide();
                    callback.apply(results);
                }
            }
        } else {
            Uri.Builder builder = new Uri.Builder().scheme("http").authority("translate.google.com")
                    .path("translate_a/t");
            builder = builder.appendQueryParameter("client", "at");
            builder = builder.appendQueryParameter("v", "2.0");
            builder = builder.appendQueryParameter("sl", "auto");
            builder = builder.appendQueryParameter("tl", "ru");
            builder = builder.appendQueryParameter("hl", "en_US");
            builder = builder.appendQueryParameter("ie", "UTF-8");
            builder = builder.appendQueryParameter("oe", "UTF-8");
            builder = builder.appendQueryParameter("inputm", "2");
            builder = builder.appendQueryParameter("source", "edit");
            builder = builder.appendQueryParameter("text", s);
            final HttpClient client = new DefaultHttpClient();
            // AndroidHttpClient.newInstance("AndroidTranslate/2.4.2 2.3.6 (gzip)", activity);
            final HttpGet verb = new HttpGet(builder.build().toString());
            actions.add(verb);
            verb.setHeader("Accept-Charset", "UTF-8");
            final int finalI = i;
            final Thread thread = new Thread("google translate") {
                @Override
                public void run() {
                    final StringBuilder out = new StringBuilder("");
                    try {
                        final String retval = client.execute(verb, new BasicResponseHandler());
                        out.setLength(0);
                        out.append(retval);
                    } catch (IOException e) {
                        if (s.length() > 0)
                            out.append("[error: " + e.toString() + "]");
                        Log.e("com.juickadvanced", "Error calling google translate", e);
                    } finally {
                        client.getConnectionManager().shutdown();
                        synchronized (pieces) {
                            pieces[0]++;
                            results[finalI] = out.toString();
                            if (pieces[0] == results.length) {
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mDialog.hide();
                                        callback.apply(results);
                                    }
                                });
                            }
                        }
                    }
                }

            };
            thread.start();
        }

    }

    mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            for (HttpGet action : actions) {
                action.abort();
            }
            synchronized (pieces) {
                pieces[0] = -10000;
            }
            mDialog.hide();
        }
    });
    mDialog.show();
}

From source file:com.andrewshu.android.reddit.submit.SubmitLinkActivity.java

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;//from   w w w . jav  a 2 s. com
    ProgressDialog pdialog;
    switch (id) {
    case Constants.DIALOG_LOGIN:
        dialog = new LoginDialog(this, mSettings, true) {
            @Override
            public void onLoginChosen(String user, String password) {
                removeDialog(Constants.DIALOG_LOGIN);
                new MyLoginTask(user, password).execute();
            }
        };
        break;

    // "Please wait"
    case Constants.DIALOG_LOGGING_IN:
        pdialog = new ProgressDialog(new ContextThemeWrapper(this, mSettings.getDialogTheme()));
        pdialog.setMessage("Logging in...");
        pdialog.setIndeterminate(true);
        pdialog.setCancelable(true);
        dialog = pdialog;
        break;
    case Constants.DIALOG_SUBMITTING:
        pdialog = new ProgressDialog(new ContextThemeWrapper(this, mSettings.getDialogTheme()));
        pdialog.setMessage("Submitting...");
        pdialog.setIndeterminate(true);
        pdialog.setCancelable(true);
        dialog = pdialog;
        break;
    default:
        break;
    }
    return dialog;
}

From source file:com.arantius.tivocommander.SeasonPass.java

public void reorderApply(View unusedView) {
    Utils.log("SeasonPass::reorderApply() " + Boolean.toString(mInReorderMode));

    boolean noChange = true;
    ArrayList<String> subIds = new ArrayList<String>();
    for (int i = 0; i < mSubscriptionIds.size(); i++) {
        if (mSubscriptionIds.get(i) != mSubscriptionIdsBeforeReorder.get(i)) {
            noChange = false;/*from  w  w  w.  j av a  2  s  . c  o  m*/
        }
        subIds.add(mSubscriptionData.get(i).path("subscriptionId").asText());
    }

    final ProgressDialog d = new ProgressDialog(this);
    final MindRpcResponseListener onReorderComplete = new MindRpcResponseListener() {
        public void onResponse(MindRpcResponse response) {
            if (d.isShowing()) {
                d.dismiss();
            }

            // Flip the buttons.
            findViewById(R.id.reorder_enable).setVisibility(View.VISIBLE);
            findViewById(R.id.reorder_apply).setVisibility(View.GONE);
            // Turn off the drag handles.
            mInReorderMode = false;
            mListAdapter.notifyDataSetChanged();
        }
    };

    if (noChange) {
        // If there was no change, switch the UI back immediately.
        onReorderComplete.onResponse(null);
    } else {
        // Otherwise show a dialog while we do the RPC.
        d.setIndeterminate(true);
        d.setTitle("Saving ...");
        d.setMessage("Saving new season pass order.  " + "Patience please, this takes a while.");
        d.setCancelable(false);
        d.show();

        SubscriptionsReprioritize req = new SubscriptionsReprioritize(subIds);
        MindRpc.addRequest(req, onReorderComplete);
    }
}

From source file:id.ridon.keude.AppDetailsData.java

private ProgressDialog getProgressDialog(String file) {
    if (progressDialog == null) {
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage(getString(R.string.download_server) + ":\n " + file);
        pd.setCancelable(true);
        pd.setCanceledOnTouchOutside(false);

        // The indeterminate-ness will get overridden on the first progress event we receive.
        pd.setIndeterminate(true);/*from   w  ww  .  ja  v a2 s. c  o m*/

        pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Log.d(TAG, "User clicked 'cancel' on download, attempting to interrupt download thread.");
                if (downloadHandler != null) {
                    downloadHandler.cancel();
                    cleanUpFinishedDownload();
                } else {
                    Log.e(TAG, "Tried to cancel, but the downloadHandler doesn't exist.");
                }
                progressDialog = null;
                Toast.makeText(AppDetails.this, getString(R.string.download_cancelled), Toast.LENGTH_LONG)
                        .show();
            }
        });
        pd.setButton(DialogInterface.BUTTON_NEUTRAL, getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        pd.cancel();
                    }
                });
        progressDialog = pd;
    }
    return progressDialog;
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

public void commentPost(final Activity activity, final String postId, final NewObjectListener listener) {

    if (!mFacebook.isSessionValid()) {
        login(activity, new SimpleRequestListener() {

            public void onComplete() {
                commentPost(activity, postId, listener);
            }//from   w w w . j  ava2s  .  com

            public void onFail(Throwable thr) {
                listener.onFail(thr);
            }

            public void onCancel() {
                listener.onCancel();
            }
        });
    } else {

        final FbTextDialog dialog = new FbTextDialog(activity);
        dialog.setDialogListener(new DialogListener() {

            public void onComplete(Bundle values) {

                final ProgressDialog spinner = new ProgressDialog(activity);
                spinner.setMessage(spinner.getContext().getString(R.string.loading));
                spinner.setCancelable(false);
                spinner.show();

                facebookRequest(activity, postId + "/comments", values, "POST", true, new RequestAdapter() {

                    public void onFail(Throwable thr, Object state) {
                        listener.onFail(thr);
                        spinner.dismiss();
                        dialog.dismiss();
                    }

                    public void onComplete(JSONObject jsonResponse, Object state) {
                        listener.onComplete(jsonResponse.optString("id"));
                        spinner.dismiss();
                        dialog.dismiss();
                    }

                    public void onCancel() {
                        listener.onCancel();
                        spinner.dismiss();
                        dialog.dismiss();
                    }
                });
            }

            public void onFacebookError(FacebookError e) {
                listener.onFail(e);
            }

            public void onError(DialogError e) {
                listener.onFail(e);
            }

            public void onCancel() {
                listener.onCancel();
            }
        });

        dialog.show();
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

public void publish(final Activity activity, final FbSimplePost post, final String pageId,
        final NewObjectListener listener) {

    if (!mFacebook.isSessionValid()) {

        login(activity, new SimpleRequestListener() {

            public void onFail(Throwable thr) {
                listener.onFail(thr);/*  w  ww .j a v a 2  s.  c  o  m*/
            }

            public void onCancel() {
                listener.onCancel();
            }

            public void onComplete() {
                publish(activity, post, pageId, listener);
            }
        });
    } else {

        final FbTextDialog dialog = new FbTextDialog(activity);
        dialog.setTitle(R.string.dialog_post_to_wall_title);
        dialog.setPlaceHolder(R.string.dialog_post_to_wall_placeholder);

        dialog.setDialogListener(new DialogListener() {

            public void onComplete(Bundle values) {

                final ProgressDialog spinner = new ProgressDialog(activity);
                spinner.setMessage(spinner.getContext().getString(R.string.loading));
                spinner.setCancelable(false);
                spinner.show();

                Bundle params = paramsForPost(post);
                params.putAll(values);

                facebookRequest(activity, pageId + "/feed", params, "POST", true, new RequestAdapter() {

                    public void onFail(Throwable thr, Object state) {
                        listener.onFail(thr);
                        spinner.dismiss();
                        dialog.dismiss();
                    }

                    public void onComplete(JSONObject jsonResponse, Object state) {
                        listener.onComplete(jsonResponse.optString("id"));
                        spinner.dismiss();
                        dialog.dismiss();
                    }

                    public void onCancel() {
                        listener.onCancel();
                        spinner.dismiss();
                        dialog.dismiss();
                    }
                });
            }

            public void onFacebookError(FacebookError e) {
                listener.onFail(e);
            }

            public void onError(DialogError e) {
                listener.onFail(e);
            }

            public void onCancel() {
                listener.onCancel();
            }
        });

        dialog.show();
    }
}

From source file:com.arantius.tivocommander.SeasonPass.java

public void reorderEnable(View unusedView) {
    Utils.log("SeasonPass::reorderEnable() " + Boolean.toString(mInReorderMode));

    final ArrayList<String> subscriptionIds = new ArrayList<String>();
    final ArrayList<Integer> slots = new ArrayList<Integer>();
    int i = 0;// w w  w . j a v a 2 s. c  o  m
    while (i < mSubscriptionData.size()) {
        if (mSubscriptionStatus.get(i) == SubscriptionStatus.MISSING) {
            String subscriptionId = mSubscriptionIds.get(i);
            subscriptionIds.add(subscriptionId);
            slots.add(i);
            mSubscriptionStatus.set(i, SubscriptionStatus.LOADING);
        }
        i++;
    }

    final ProgressDialog d = new ProgressDialog(this);
    final MindRpcResponseListener onAllPassesLoaded = new MindRpcResponseListener() {
        public void onResponse(MindRpcResponse response) {
            if (response != null) {
                mDetailCallback.onResponse(response);
            }
            d.dismiss();

            // Save the state before ordering.
            mSubscriptionIdsBeforeReorder.clear();
            mSubscriptionIdsBeforeReorder.addAll(mSubscriptionIds);
            // Flip the buttons.
            findViewById(R.id.reorder_enable).setVisibility(View.GONE);
            findViewById(R.id.reorder_apply).setVisibility(View.VISIBLE);
            // Show the drag handles.
            mInReorderMode = true;
            mListAdapter.notifyDataSetChanged();
        }
    };

    if (subscriptionIds.size() == 0) {
        // No subscriptions need loading? Proceed immediately.
        onAllPassesLoaded.onResponse(null);
    } else {
        // Otherwise, show dialog and start loading.
        d.setIndeterminate(true);
        d.setTitle("Preparing ...");
        d.setMessage("Loading all season pass data.");
        d.setCancelable(false);
        d.show();

        final SubscriptionSearch req = new SubscriptionSearch(subscriptionIds);
        mRequestSlotMap.put(req.getRpcId(), slots);
        MindRpc.addRequest(req, onAllPassesLoaded);
    }
}

From source file:com.xperia64.rompatcher.MainActivity.java

public void patch(final boolean c, final boolean d, final boolean r, final String ed) {
    final ProgressDialog myPd_ring = ProgressDialog.show(MainActivity.this,
            getResources().getString(R.string.wait), getResources().getString(R.string.wait_desc), true);
    myPd_ring.setCancelable(false);
    new Thread(new Runnable() {
        public void run() {
            if (new File(Globals.patchToApply).exists() && new File(Globals.fileToPatch).exists()
                    && !Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".ecm")) {
                String msg = getResources().getString(R.string.success);
                if (!new File(Globals.fileToPatch).canWrite()) {
                    Globals.msg = msg = "Can not write to output file. If you are on KitKat or Lollipop, move the file to your internal storage.";
                    return;
                }// w  ww  .  j ava2  s .c  o m
                if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ups")) {
                    int e = upsPatchRom(Globals.fileToPatch, Globals.patchToApply, Globals.fileToPatch + ".new",
                            r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_UPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta3")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".vcdiff")) {
                    RandomAccessFile f = null;
                    try {
                        f = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    StringBuilder s = new StringBuilder();
                    try {
                        if (f.length() >= 9) {
                            for (int i = 0; i < 8; i++) {
                                s.append((char) f.readByte());
                                f.seek(i + 1);
                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        f.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    // Header of xdelta patch determines version
                    if (s.toString().equals("%XDELTA%") || s.toString().equals("%XDZ000%")
                            || s.toString().equals("%XDZ001%") || s.toString().equals("%XDZ002%")
                            || s.toString().equals("%XDZ003%") || s.toString().equals("%XDZ004%")) {
                        int e = xdelta1PatchRom(Globals.fileToPatch, Globals.patchToApply,
                                Globals.fileToPatch + ".new");
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_XDELTA1);
                        }
                    } else {
                        int e = xdelta3PatchRom(Globals.fileToPatch, Globals.patchToApply,
                                Globals.fileToPatch + ".new");
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_XDELTA3);
                        }
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bps")) {
                    int e = bpsPatchRom(Globals.fileToPatch, Globals.patchToApply, Globals.fileToPatch + ".new",
                            r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_BPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dps")) {
                    int e = dpsPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_DPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bsdiff")) {
                    int e = bsdiffPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_BSDIFF);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".aps")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // Wow.
                    byte[] gbaSig = { 0x41, 0x50, 0x53, 0x31, 0x00 };
                    byte[] n64Sig = { 0x41, 0x50, 0x53, 0x31, 0x30 };
                    byte[] realSig = new byte[5];
                    RandomAccessFile raf = null;
                    System.out.println("APS Patch");
                    try {
                        raf = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    try {
                        raf.read(realSig);
                        raf.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    if (Arrays.equals(realSig, gbaSig)) {
                        System.out.println("GBA APS");
                        APSGBAPatcher aa = new APSGBAPatcher();
                        aa.crcTableInit();
                        int e = 0;
                        try {
                            e = aa.ApplyPatch(Globals.patchToApply, Globals.fileToPatch, r);
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                            e = -5;
                        }
                        System.out.println("e: " + e);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_APSGBA);
                        }
                    } else if (Arrays.equals(realSig, n64Sig)) {
                        System.out.println("N64 APS");
                        int e = apsN64PatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_APSN64);
                        }
                    } else {
                        msg = parseError(-131, -10000);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ppf")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e = ppfPatchRom(Globals.fileToPatch, Globals.patchToApply, r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_PPF);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".patch")) {
                    int e = xdelta1PatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_XDELTA1);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".asm")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e;
                    if (Globals.asar)
                        e = asarPatchRom(Globals.fileToPatch, Globals.patchToApply, r ? 1 : 0);
                    else
                        e = asmPatchRom(Globals.fileToPatch, Globals.patchToApply);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_ASM);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dldi")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e = dldiPatchRom(Globals.fileToPatch, Globals.patchToApply);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_DLDI);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xpc")) {
                    int e = xpcPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_XPC);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".nmp")) {

                    String drm = MainActivity.this.getPackageName();
                    System.out.println("Drm is: " + drm);
                    if (drm.equals("com.xperia64.rompatcher.donation")) {
                        if (c) {
                            File f = new File(Globals.fileToPatch);
                            File f2 = new File(Globals.fileToPatch + ".bak");
                            try {
                                Files.copy(f, f2);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        NitroROMFilesystem fs;
                        try {
                            fs = new NitroROMFilesystem(Globals.fileToPatch);
                            ROM.load(fs);
                            RealPatch.patch(Globals.patchToApply, new Object());
                            ROM.close();
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            //e1.printStackTrace();
                            Globals.msg = msg = String.format(getResources().getString(R.string.nmpDefault),
                                    e1.getStackTrace()[0].getFileName(), e1.getStackTrace()[0].getLineNumber());
                        }
                        if (c && d && !TextUtils.isEmpty(ed)) {
                            File f = new File(Globals.fileToPatch);
                            File f3 = new File(Globals.fileToPatch + ".bak");
                            File f2 = new File(
                                    Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/') + 1)
                                            + ed);
                            f.renameTo(f2);
                            f3.renameTo(f);
                        }
                    } else {
                        Globals.msg = msg = getResources().getString(R.string.drmwarning);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                                b.setTitle(getResources().getString(R.string.drmwarning));
                                b.setIcon(
                                        ResourcesCompat.getDrawable(getResources(), R.drawable.icon_pro, null));
                                b.setMessage(getResources().getString(R.string.drmwarning_desc));
                                b.setCancelable(false);
                                b.setNegativeButton(getResources().getString(android.R.string.cancel),
                                        new OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                            }
                                        });
                                b.setPositiveButton(getResources().getString(R.string.nagInfo),
                                        new OnClickListener() {

                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                                try {
                                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                                                            "market://details?id=com.xperia64.rompatcher.donation")));
                                                } catch (android.content.ActivityNotFoundException anfe) {
                                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                                                            "http://play.google.com/store/apps/details?id=com.xperia64.rompatcher.donation")));
                                                }
                                            }
                                        });
                                b.create().show();
                            }
                        });
                    }

                } else {
                    RandomAccessFile f = null;
                    try {
                        f = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    StringBuilder s = new StringBuilder();
                    try {
                        if (f.length() >= 6) {
                            for (int i = 0; i < 5; i++) {
                                s.append((char) f.readByte());
                                f.seek(i + 1);
                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        f.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    int e;
                    // Two variants of IPS, the normal PATCH type, then this weird one called IPS32 with messily hacked in 32 bit offsets
                    if (s.toString().equals("IPS32")) {
                        e = ips32PatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_IPS);
                        }
                    } else {
                        e = ipsPatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_IPS);
                        }
                    }

                }
                if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ups")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta3")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".vcdiff")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".patch")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bps")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bsdiff")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dps")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xpc")) {
                    File oldrom = new File(Globals.fileToPatch);
                    File bkrom = new File(Globals.fileToPatch + ".bak");
                    oldrom.renameTo(bkrom);
                    File newrom = new File(Globals.fileToPatch + ".new");
                    newrom.renameTo(oldrom);
                }
                if (!c) {
                    File f = new File(Globals.fileToPatch + ".bak");
                    if (f.exists()) {
                        f.delete();
                    }
                } else {
                    if (d) {
                        File one = new File(Globals.fileToPatch + ".bak");
                        File two = new File(Globals.fileToPatch);
                        File three = new File(
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/') + 1)
                                        + ed);
                        two.renameTo(three);
                        File four = new File(Globals.fileToPatch);
                        one.renameTo(four);
                    }
                }
                Globals.msg = msg;
            } else if (Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".ecm")) {
                int e = 0;
                String msg = getResources().getString(R.string.success);
                if (c) {

                    if (d) {
                        e = ecmPatchRom(Globals.fileToPatch,
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/')) + ed, 1);
                    } else {
                        //new File(Globals.fileToPatch).renameTo(new File(Globals.fileToPatch+".bak"));
                        e = ecmPatchRom(Globals.fileToPatch,
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('.')), 1);
                    }
                } else {
                    e = ecmPatchRom(Globals.fileToPatch, "", 0);
                }
                if (e != 0) {
                    msg = parseError(e, Globals.TYPE_ECM);
                }
                Globals.msg = msg;
            } else {
                Globals.msg = getResources().getString(R.string.fnf);
            }

        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (Globals.msg.equals("")) {
                    Thread.sleep(25);
                }
                ;
                myPd_ring.dismiss();
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast t = Toast.makeText(staticThis, Globals.msg, Toast.LENGTH_SHORT);
                        t.show();
                        Globals.msg = "";
                    }
                });
                if (Globals.msg.equals(getResources().getString(R.string.success))) // Don't annoy people who did something wrong even further
                {
                    final SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(MainActivity.this);
                    int x = prefs.getInt("purchaseNag", 5);
                    if (x != -1) {
                        if ((isPackageInstalled("com.xperia64.timidityae", MainActivity.this)
                                || isPackageInstalled("com.xperia64.rompatcher.donation", MainActivity.this))) {
                            prefs.edit().putInt("purchaseNag", -1);
                        } else {
                            if (x >= 5) {

                                prefs.edit().putInt("purchaseNag", 0).commit();
                                /*runOnUiThread(new Runnable() {
                                    public void run() {
                                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                                b.setTitle("Like ROM Patcher?");
                                b.setIcon(getResources().getDrawable(R.drawable.icon_pro));
                                b.setMessage(getResources().getString(R.string.nagMsg));
                                b.setCancelable(false);
                                b.setNegativeButton(getResources().getString(android.R.string.cancel), new OnClickListener(){@Override public void onClick(DialogInterface arg0, int arg1) {}});
                                b.setPositiveButton(getResources().getString(R.string.nagInfo), new OnClickListener(){
                                        
                                  @Override
                                  public void onClick(DialogInterface arg0, int arg1) {
                                     try  {
                                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.xperia64.rompatcher.donation")));
                                     } catch (android.content.ActivityNotFoundException anfe) {
                                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.xperia64.rompatcher.donation")));
                                     }
                                  }
                                   });
                                      b.create().show();
                                       }
                                   }); // end of UIThread*/
                            } else {
                                prefs.edit().putInt("purchaseNag", x + 1).commit();
                            }
                        }
                    }
                }

            } catch (Exception e) {
            }
        }
    }).start();
}

From source file:org.odk.collect.android.activities.GoogleDriveActivity.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case PROGRESS_DIALOG:
        Collect.getInstance().getActivityLogger().logAction(this, "onCreateDialog.PROGRESS_DIALOG", "show");

        ProgressDialog progressDialog = new ProgressDialog(this);
        DialogInterface.OnClickListener loadingButtonListener = new DialogInterface.OnClickListener() {
            @Override/*from  w  w w. j  a  v  a2 s .c o  m*/
            public void onClick(DialogInterface dialog, int which) {
                Collect.getInstance().getActivityLogger().logAction(this, "onCreateDialog.PROGRESS_DIALOG",
                        "cancel");
                dialog.dismiss();
                getFileTask.cancel(true);
                getFileTask.setGoogleDriveFormDownloadListener(null);
            }
        };
        progressDialog.setTitle(getString(R.string.downloading_data));
        progressDialog.setMessage(alertMsg);
        progressDialog.setIndeterminate(true);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setButton(getString(R.string.cancel), loadingButtonListener);
        return progressDialog;
    case GOOGLE_USER_DIALOG:
        AlertDialog.Builder gudBuilder = new AlertDialog.Builder(this);

        gudBuilder.setTitle(getString(R.string.no_google_account));
        gudBuilder.setMessage(getString(R.string.google_set_account));
        gudBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        gudBuilder.setCancelable(false);
        return gudBuilder.create();
    }
    return null;
}