Util.java
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
}
}