-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebUtils.java
63 lines (53 loc) · 2.23 KB
/
WebUtils.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.jeffreynyauke.myapplication.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.MenuItem;
/**
* Created by Jeffrey Nyauke on 1/31/2017.
*/
public class WebUtils {
public static boolean isSameDomain(String url, String url1) {
return getRootDomainUrl(url.toLowerCase()).equals(getRootDomainUrl(url1.toLowerCase()));
}
private static String getRootDomainUrl(String url) {
String[] domainKeys = url.split("/")[2].split("\\.");
int length = domainKeys.length;
int dummy = domainKeys[0].equals("www") ? 1 : 0;
if (length - dummy == 2)
return domainKeys[length - 2] + "." + domainKeys[length - 1];
else {
if (domainKeys[length - 1].length() == 2) {
return domainKeys[length - 3] + "." + domainKeys[length - 2] + "." + domainKeys[length - 1];
} else {
return domainKeys[length - 2] + "." + domainKeys[length - 1];
}
}
}
public static void tintMenuIcon(Context context, MenuItem item, int color) {
Drawable drawable = item.getIcon();
if (drawable != null) {
// If we don't mutate the drawable, then all drawable's with this id will have a color
// filter applied to it.
drawable.mutate();
drawable.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
}
}
public static void bookmarkUrl(Context context, String url) {
SharedPreferences pref = context.getSharedPreferences("androidhive", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
// if url is already bookmarked, unbookmark it
if (pref.getBoolean(url, false)) {
editor.putBoolean(url, false);
} else {
editor.putBoolean(url, true);
}
editor.commit();
}
public static boolean isBookmarked(Context context, String url) {
SharedPreferences pref = context.getSharedPreferences("androidhive", 0);
return pref.getBoolean(url, false);
}
}