Android Open Source - eyebrows-sync Async Crypt






From Project

Back to project page eyebrows-sync.

License

The source code is released under:

Copyright (c) 2014 Jon Petraglia of Qweex All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "...

If you think the Android project eyebrows-sync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qweex.eyebrowssync;
//from   w  ww.ja  v  a 2  s.  c om
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import com.qweex.utils.Crypt;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

class AsyncCrypt extends AsyncTask<String, Void, Exception> {
    Dialog dialog;
    enum Task {ENCRYPTING, DECRYPTING, CHANGING}
    Task t;
    Context c;
    Handler callback;

    public AsyncCrypt(Context c, Task t, Handler callback) throws IllegalArgumentException {
        this.t = t;
        this.c = c;
        this.callback = callback;
        if(callback==null || c==null)
            throw new IllegalArgumentException();
    }

    @Override
    protected void onPreExecute() {
        ProgressBar herp = new ProgressBar(c);
        if(SavedJobs.getAll().getCount()>0)
            dialog = new AlertDialog.Builder(c)
                    .setTitle(t.toString())
                    .setView(herp)
                    .setCancelable(false)
                    .show();
    }

    @Override
    protected void onPostExecute(Exception error) {
        if(dialog!=null)
            dialog.dismiss();
        Message m = new Message();
        Bundle b = new Bundle();
        b.putSerializable("error", error);
        callback.sendMessage(m);
    }

    // arg[] = {password[, old_password]}
    @Override
    protected Exception doInBackground(String... strings) {
        try {
            if(t==Task.CHANGING) {
                doTask(Task.DECRYPTING, strings[1]);
                doTask(Task.ENCRYPTING, strings[0]);
            } else {
                doTask(t, strings[0]);
            }
            return null;
        } catch(Exception e) { return e; }
    }

    private void doTask(Task task, String password) throws IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException {
        UserConfig.masterKey = Crypt.getKeyFromPassword(password);
        Cursor c = SavedJobs.getAll();
        if(c.getCount()==0)
            return;
        c.moveToFirst();

        while(!c.isAfterLast()) {
            String auth = c.getString(c.getColumnIndex("auth"));
            if(task==Task.ENCRYPTING)
                auth = Crypt.encrypt(auth, UserConfig.masterKey);
            else if(task==Task.DECRYPTING)
                auth = Crypt.decrypt(auth, UserConfig.masterKey);
            else
                throw new RuntimeException("Task was CHANGING inside of doTask");
            Bundle b = new Bundle();
            //b.putString("name", c.getString(c.getColumnIndex("name")));
            b.putString("auth", auth);
            SavedJobs.update(this.c, c.getString(c.getColumnIndex("name")), b);
            c.moveToNext();
        }
    }
}




Java Source Code List

com.qweex.NumberPickerDialogPreference.java
com.qweex.eyebrows.EyebrowsError.java
com.qweex.eyebrows.did_not_write.JSONDownloader.java
com.qweex.eyebrowssync.AboutActivity.java
com.qweex.eyebrowssync.AsyncCrypt.java
com.qweex.eyebrowssync.AttachedRelativeLayout.java
com.qweex.eyebrowssync.EditJob.java
com.qweex.eyebrowssync.FileModifiedHelper.java
com.qweex.eyebrowssync.NotificationSupervisor.java
com.qweex.eyebrowssync.SavedJobs.java
com.qweex.eyebrowssync.StartActivity.java
com.qweex.eyebrowssync.StatusWindow.java
com.qweex.eyebrowssync.Syncer.java
com.qweex.eyebrowssync.UserConfig.java
com.qweex.eyebrowssync.JobList.Base.java
com.qweex.eyebrowssync.JobList.v11.java
com.qweex.eyebrowssync.JobList.v3.java
com.qweex.utils.Crypt.java
com.qweex.utils.DirectoryChooserDialog.java