Utils class: compression string gzip

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Compression {
    public static byte[] compress(String paramString) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(paramString.length());
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(paramString.getBytes());
        gzipOutputStream.close();
        byte[] bytes = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return bytes;
    }

    public static String decompress(byte[] paramArrayOfByte) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, 32);
        StringBuilder stringBuilder = new StringBuilder();
        byte[] bytes = new byte[32];
        for (; ; ) {
            int i = gzipInputStream.read(bytes);
            if (i == -1) {
                break;
            }
            stringBuilder.append(new String(bytes, 0, i));
        }
        gzipInputStream.close();
        byteArrayInputStream.close();
        return stringBuilder.toString();
    }
}

 

Reading plain text file in java and android asset

//read lines from file

ArrayList<String> lines = new ArrayList<String>();
try {
    BufferedReader input = new BufferedReader(new FileReader(f));

    String line;
    do {
        if ((line = input.readLine()) != null)
            lines.add(line);
    } while (line != null);
} catch (IOException e) {
    e.printStackTrace();
}
return lines;

//read lines from file asset

StringBuilder sb = new StringBuilder();
try {
    BufferedReader input = new BufferedReader(
            new InputStreamReader(assets.open(fileName)));

    String line;
    do {
        if ((line = input.readLine()) != null)
            sb.append(line);
    } while (line != null);
} catch (IOException e) {
    e.printStackTrace();
}
return sb.toString();

Change time in genymotion emulator – simple java app

I made app in executable jar java file for setting time in genymotion emulator.

Here is simple java code and compiled app for setting time in genymotion app. Below code I showed how the app looks like and also added ready jar app to download.

Code:

public class SetTimeInGenyMotion {
private JTextField textField1;
private JButton getTimeInProperButton;
private JButton setTimeInProperButton;
private JButton getTimeAndSetButton;
private JPanel panel1;
public static final String FORMAT = "YYYYMMDD.HHmmss";

public static void main(String[] args) {
new SetTimeInGenyMotion().doStuff();
}

private void doStuff() {

getTimeInProperButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.setText(getTime());
}
});

setTimeInProperButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String time = textField1.getText();
setTime(time);
}
});

getTimeAndSetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String time = getTime();
setTime(time);
textField1.setText(time);
}
});

showWindow();
}

private void showWindow() {
JFrame frame = new JFrame("SetTimeInGenyMotion");
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(680, 0));
frame.pack();
frame.setVisible(true);
}

private void setTime(String time) {
try {
String command = "adb shell date -s " + time;

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getErrorStream(), "UTF-8"));
String l = "";

while ((l = br.readLine()) != null) {
System.out.println(l);
}
br.close();

} catch (IOException e1) {
e1.printStackTrace();
}
}

private String getTime() {
//adb shell date -s YYYYMMDD.HHmmss
//http://stackoverflow.com/questions/7094772/how-to-set-android-device-date-time-programmatically

String format = "YYYYMMdd.HHmmss";
Calendar instance = Calendar.getInstance();
String time = getDate(instance.getTimeInMillis(), format);
return time;
}

public static String getDate(long milliSeconds, String dateFormat) {
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}

Pic:
Zrzut ekranu 2015-06-03 11.58.59

Download:
download