1 package com.folioreader.util;
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;
28 import com.folioreader.R;
29 import com.folioreader.view.UnderlinedTextView;
31 import java.lang.ref.SoftReference;
32 import java.util.Hashtable;
35 * Created by mahavir on 3/30/16.
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);
46 public static boolean setCustomFont(View view, Context ctx, String asset) {
47 if (TextUtils.isEmpty(asset))
51 tf = getFont(ctx, asset);
52 if (view instanceof TextView) {
53 ((TextView) view).setTypeface(tf);
55 ((Button) view).setTypeface(tf);
57 } catch (Exception e) {
58 Log.e("AppUtil", "Could not get typface " + asset);
65 private static final Hashtable<String, SoftReference<Typeface>> fontCache = new Hashtable<String, SoftReference<Typeface>>();
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) {
76 Typeface typeface = Typeface.createFromAsset(c.getAssets(), name);
77 fontCache.put(name, new SoftReference<Typeface>(typeface));
83 public static ColorStateList getColorList(Context context, int selectedColor, int unselectedColor) {
84 int[][] states = new int[][]{
85 new int[]{android.R.attr.state_selected},
88 int[] colors = new int[]{
89 ContextCompat.getColor(context, selectedColor),
90 ContextCompat.getColor(context, unselectedColor)
92 ColorStateList list = new ColorStateList(states, colors);
96 public static void keepScreenAwake(boolean enable, Context context) {
99 .getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
102 .getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
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);
123 private static void setUnderLineColor(UnderlinedTextView underlinedTextView, Context context, int background,int underlinecolor) {
124 underlinedTextView.setBackgroundColor(ContextCompat.getColor(context,
126 underlinedTextView.setUnderLineColor(ContextCompat.getColor(context,
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);
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);
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)));
154 public static void setColorToImage(Context context, int color, Drawable drawable) {
155 drawable.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
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;
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;
175 public static int pxToDp(int px) {
176 return (int) (px / Resources.getSystem().getDisplayMetrics().density);