Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / util / UiUtil.java
1 package com.folioreader.util;
2
3 import android.app.Activity;
4 import android.content.ClipData;
5 import android.content.ClipboardManager;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.res.ColorStateList;
9 import android.content.res.Resources;
10 import android.content.res.TypedArray;
11 import android.graphics.PorterDuff;
12 import android.graphics.Typeface;
13 import android.graphics.drawable.ColorDrawable;
14 import android.graphics.drawable.Drawable;
15 import android.graphics.drawable.GradientDrawable;
16 import android.graphics.drawable.StateListDrawable;
17 import android.support.v4.content.ContextCompat;
18 import android.text.TextUtils;
19 import android.util.AttributeSet;
20 import android.util.DisplayMetrics;
21 import android.util.Log;
22 import android.util.StateSet;
23 import android.view.View;
24 import android.view.WindowManager;
25 import android.widget.Button;
26 import android.widget.TextView;
27
28 import com.folioreader.R;
29 import com.folioreader.view.UnderlinedTextView;
30
31 import java.lang.ref.SoftReference;
32 import java.util.Hashtable;
33
34 /**
35  * Created by mahavir on 3/30/16.
36  */
37 public class UiUtil {
38     public static void setCustomFont(View view, Context ctx, AttributeSet attrs,
39                                      int[] attributeSet, int fontId) {
40         TypedArray a = ctx.obtainStyledAttributes(attrs, attributeSet);
41         String customFont = a.getString(fontId);
42         setCustomFont(view, ctx, customFont);
43         a.recycle();
44     }
45
46     public static boolean setCustomFont(View view, Context ctx, String asset) {
47         if (TextUtils.isEmpty(asset))
48             return false;
49         Typeface tf = null;
50         try {
51             tf = getFont(ctx, asset);
52             if (view instanceof TextView) {
53                 ((TextView) view).setTypeface(tf);
54             } else {
55                 ((Button) view).setTypeface(tf);
56             }
57         } catch (Exception e) {
58             Log.e("AppUtil", "Could not get typface  " + asset);
59             return false;
60         }
61
62         return true;
63     }
64
65     private static final Hashtable<String, SoftReference<Typeface>> fontCache = new Hashtable<String, SoftReference<Typeface>>();
66
67     public static Typeface getFont(Context c, String name) {
68         synchronized (fontCache) {
69             if (fontCache.get(name) != null) {
70                 SoftReference<Typeface> ref = fontCache.get(name);
71                 if (ref.get() != null) {
72                     return ref.get();
73                 }
74             }
75
76             Typeface typeface = Typeface.createFromAsset(c.getAssets(), name);
77             fontCache.put(name, new SoftReference<Typeface>(typeface));
78
79             return typeface;
80         }
81     }
82
83     public static ColorStateList getColorList(Context context, int selectedColor, int unselectedColor) {
84         int[][] states = new int[][]{
85                 new int[]{android.R.attr.state_selected},
86                 new int[]{}
87         };
88         int[] colors = new int[]{
89                 ContextCompat.getColor(context, selectedColor),
90                 ContextCompat.getColor(context, unselectedColor)
91         };
92         ColorStateList list = new ColorStateList(states, colors);
93         return list;
94     }
95
96     public static void keepScreenAwake(boolean enable, Context context) {
97         if (enable) {
98             ((Activity) context)
99                     .getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
100         } else {
101             ((Activity) context)
102                     .getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
103         }
104     }
105
106     public static void setBackColorToTextView(UnderlinedTextView textView, String type) {
107         Context context = textView.getContext();
108         if (type.equals("yellow")) {
109             setUnderLineColor(textView, context, R.color.yellow, R.color.yellow);
110         } else if (type.equals("green")) {
111             setUnderLineColor(textView, context, R.color.green, R.color.green);
112         } else if (type.equals("blue")) {
113             setUnderLineColor(textView, context, R.color.blue, R.color.blue);
114         } else if (type.equals("pink")) {
115             setUnderLineColor(textView, context, R.color.pink, R.color.pink);
116         } else if (type.equals("underline")) {
117             setUnderLineColor(textView, context, android.R.color.transparent, android.R.color.holo_red_dark);
118             textView.setUnderlineWidth(2.0f);
119         }
120     }
121
122
123     private static void setUnderLineColor(UnderlinedTextView underlinedTextView, Context context, int background,int underlinecolor) {
124         underlinedTextView.setBackgroundColor(ContextCompat.getColor(context,
125                 background));
126         underlinedTextView.setUnderLineColor(ContextCompat.getColor(context,
127                 underlinecolor));
128     }
129
130     public static float convertDpToPixel(float dp, Context context) {
131         Resources resources = context.getResources();
132         DisplayMetrics metrics = resources.getDisplayMetrics();
133         float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
134         return px;
135     }
136
137     public static void copyToClipboard(Context context, String text) {
138         ClipboardManager clipboard =
139                 (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
140         ClipData clip = ClipData.newPlainText("copy", text);
141         clipboard.setPrimaryClip(clip);
142     }
143
144     public static void share(Context context, String text) {
145         Intent sendIntent = new Intent();
146         sendIntent.setAction(Intent.ACTION_SEND);
147         sendIntent.putExtra(Intent.EXTRA_TEXT, text);
148         sendIntent.setType("text/plain");
149         context.startActivity(Intent.createChooser(sendIntent,
150                 context.getResources().getText(R.string.send_to)));
151     }
152
153
154     public static void setColorToImage(Context context, int color, Drawable drawable) {
155         drawable.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
156     }
157
158
159     public static StateListDrawable convertColorIntoStateDrawable(Context context, int colorSelected, int colorNormal) {
160         StateListDrawable stateListDrawable = new StateListDrawable();
161         stateListDrawable.addState(new int[]{android.R.attr.state_selected}, new ColorDrawable(ContextCompat.getColor(context, colorSelected)));
162         stateListDrawable.addState(StateSet.WILD_CARD, new ColorDrawable(ContextCompat.getColor(context, colorNormal)));
163         return stateListDrawable;
164     }
165
166     public static GradientDrawable getShapeDrawable(Context context, int color) {
167         GradientDrawable gradientDrawable = new GradientDrawable();
168         gradientDrawable.setShape(GradientDrawable.RECTANGLE);
169         gradientDrawable.setStroke(pxToDp(2), ContextCompat.getColor(context, color));
170         gradientDrawable.setColor(ContextCompat.getColor(context, color));
171         gradientDrawable.setCornerRadius(pxToDp(3));
172         return gradientDrawable;
173     }
174
175     public static int pxToDp(int px) {
176         return (int) (px / Resources.getSystem().getDisplayMetrics().density);
177     }
178 }