Util.java 1.5 KB
package it.nexus.android.interview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Base64;
import android.view.View;

import java.util.*;

import java.io.*;

/**
 * Created by kel666 on 12/01/17.
 */
public abstract class Util {
    public static void showAlert(Context context, String title, String msg, DialogInterface.OnClickListener onClose) {
        AlertDialog.Builder builder=new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(msg)
                .setIcon(android.R.drawable.ic_dialog_alert);
        if (onClose!=null)
            builder=builder.setNeutralButton(R.string.close, onClose);
        builder.show();
    }

    /** Read the object from Base64 string. */
    public static Object fromString( String s ) throws IOException,
            ClassNotFoundException {
        byte [] data = Base64.decode(s, Base64.DEFAULT);
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(  data ) );
        Object o  = ois.readObject();
        ois.close();
        return o;
    }

    /** Write the object to a Base64 string. */
    public static String toString( Serializable o ) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream( baos );
        oos.writeObject( o );
        oos.close();
        return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
    }
}