Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / Tools.java
1 /* Tools.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp;
17
18 import android.view.View;
19 import android.view.ViewGroup;
20 import android.widget.AdapterView;
21
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.Calendar;
25 import java.util.Date;
26
27 import javax.crypto.Cipher;
28 import javax.crypto.spec.SecretKeySpec;
29
30 /**
31  * Created by pk910 on 25.01.2016.
32  */
33 public class Tools {
34
35     public static final String md5(final String s) {
36         final String MD5 = "MD5";
37         try {
38             // Create MD5 Hash
39             MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
40             digest.update(s.getBytes());
41             byte messageDigest[] = digest.digest();
42
43             // Create Hex String
44             StringBuilder hexString = new StringBuilder();
45             for (byte aMessageDigest : messageDigest) {
46                 String h = Integer.toHexString(0xFF & aMessageDigest);
47                 while (h.length() < 2)
48                     h = "0" + h;
49                 hexString.append(h);
50             }
51             return hexString.toString();
52
53         } catch (NoSuchAlgorithmException e) {
54             e.printStackTrace();
55         }
56         return "";
57     }
58
59     public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
60         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
61         Cipher cipher = Cipher.getInstance("AES");
62         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
63         byte[] encrypted = cipher.doFinal(clear);
64         return encrypted;
65     }
66
67     public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
68         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
69         Cipher cipher = Cipher.getInstance("AES");
70         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
71         byte[] decrypted = cipher.doFinal(encrypted);
72         return decrypted;
73     }
74
75     public static void removeAllChildViews(ViewGroup viewGroup) {
76         for (int i = 0; i < viewGroup.getChildCount(); i++) {
77             View child = viewGroup.getChildAt(i);
78             if (child instanceof ViewGroup) {
79                 if (child instanceof AdapterView) {
80                     viewGroup.removeView(child);
81                     return;
82                 }
83                 removeAllChildViews(((ViewGroup) child));
84             } else {
85                 viewGroup.removeView(child);
86             }
87         }
88     }
89
90     public static String getWeekdayString(long date) {
91         return getWeekdayString(date, true);
92     }
93
94     public static String getWeekdayString(long date, boolean fullString) {
95         return getWeekdayString(new Date(date * 1000), fullString);
96     }
97
98     public static String getWeekdayString(Date date) {
99         return getWeekdayString(date, true);
100     }
101
102     public static String getWeekdayString(Date date, boolean fullString) {
103         int weekdayResIds[];
104         if(fullString)
105             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 };
106         else
107             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 };
108         Calendar cal = Calendar.getInstance();
109         cal.setTime(date);
110         int dow = cal.get(Calendar.DAY_OF_WEEK);
111         int weekdayResId = weekdayResIds[dow-1];
112         return CampusAppContext.getInstance().getResString(weekdayResId);
113     }
114
115 }