alpha 0.0.2
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / Tools.java
1 package de.dhbwloe.campusapp;
2
3 import android.view.View;
4 import android.view.ViewGroup;
5 import android.widget.AdapterView;
6
7 import java.security.MessageDigest;
8 import java.security.NoSuchAlgorithmException;
9 import java.util.Calendar;
10 import java.util.Date;
11
12 import javax.crypto.Cipher;
13 import javax.crypto.spec.SecretKeySpec;
14
15 /**
16  * Created by pk910 on 25.01.2016.
17  */
18 public class Tools {
19
20     public static final String md5(final String s) {
21         final String MD5 = "MD5";
22         try {
23             // Create MD5 Hash
24             MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
25             digest.update(s.getBytes());
26             byte messageDigest[] = digest.digest();
27
28             // Create Hex String
29             StringBuilder hexString = new StringBuilder();
30             for (byte aMessageDigest : messageDigest) {
31                 String h = Integer.toHexString(0xFF & aMessageDigest);
32                 while (h.length() < 2)
33                     h = "0" + h;
34                 hexString.append(h);
35             }
36             return hexString.toString();
37
38         } catch (NoSuchAlgorithmException e) {
39             e.printStackTrace();
40         }
41         return "";
42     }
43
44     public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
45         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
46         Cipher cipher = Cipher.getInstance("AES");
47         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
48         byte[] encrypted = cipher.doFinal(clear);
49         return encrypted;
50     }
51
52     public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
53         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
54         Cipher cipher = Cipher.getInstance("AES");
55         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
56         byte[] decrypted = cipher.doFinal(encrypted);
57         return decrypted;
58     }
59
60     public static void removeAllChildViews(ViewGroup viewGroup) {
61         for (int i = 0; i < viewGroup.getChildCount(); i++) {
62             View child = viewGroup.getChildAt(i);
63             if (child instanceof ViewGroup) {
64                 if (child instanceof AdapterView) {
65                     viewGroup.removeView(child);
66                     return;
67                 }
68                 removeAllChildViews(((ViewGroup) child));
69             } else {
70                 viewGroup.removeView(child);
71             }
72         }
73     }
74
75     public static String getWeekdayString(long date) {
76         return getWeekdayString(date, true);
77     }
78
79     public static String getWeekdayString(long date, boolean fullString) {
80         return getWeekdayString(new Date(date * 1000), fullString);
81     }
82
83     public static String getWeekdayString(Date date) {
84         return getWeekdayString(date, true);
85     }
86
87     public static String getWeekdayString(Date date, boolean fullString) {
88         int weekdayResIds[];
89         if(fullString)
90             weekdayResIds = new int[] { R.string.week_sunday, R.string.week_monday, R.string.week_tuesday, R.string.week_wednesday, R.string.week_thursday, R.string.week_friday, R.string.week_saturday };
91         else
92             weekdayResIds = new int[] { R.string.week_sunday_short, R.string.week_monday_short, R.string.week_tuesday_short, R.string.week_wednesday_short, R.string.week_thursday_short, R.string.week_friday_short, R.string.week_saturday_short };
93         Calendar cal = Calendar.getInstance();
94         cal.setTime(date);
95         int dow = cal.get(Calendar.DAY_OF_WEEK);
96         int weekdayResId = weekdayResIds[dow-1];
97         return CampusAppContext.getInstance().getResString(weekdayResId);
98     }
99
100 }