|
| 1 | +package com.dvinfosys.widgets.Utils; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.ContextWrapper; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.net.ConnectivityManager; |
| 8 | +import android.net.NetworkInfo; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.text.TextUtils; |
| 11 | +import android.view.ContextThemeWrapper; |
| 12 | + |
| 13 | +import com.dvinfosys.widgets.VideoPlayer.VPVideoPlayer; |
| 14 | + |
| 15 | +import java.util.Formatter; |
| 16 | +import java.util.Locale; |
| 17 | + |
| 18 | +public class VPUtils { |
| 19 | + public static String stringForTime(int timeMs) { |
| 20 | + if (timeMs <= 0 || timeMs >= 24 * 60 * 60 * 1000) { |
| 21 | + return "00:00"; |
| 22 | + } |
| 23 | + int totalSeconds = timeMs / 1000; |
| 24 | + int seconds = totalSeconds % 60; |
| 25 | + int minutes = (totalSeconds / 60) % 60; |
| 26 | + int hours = totalSeconds / 3600; |
| 27 | + StringBuilder stringBuilder = new StringBuilder(); |
| 28 | + Formatter mFormatter = new Formatter(stringBuilder, Locale.getDefault()); |
| 29 | + if (hours > 0) { |
| 30 | + return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString(); |
| 31 | + } else { |
| 32 | + return mFormatter.format("%02d:%02d", minutes, seconds).toString(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * This method requires the caller to hold the permission ACCESS_NETWORK_STATE. |
| 38 | + * |
| 39 | + * @param context context |
| 40 | + * @return if wifi is connected,return true |
| 41 | + */ |
| 42 | + public static boolean isWifiConnected(Context context) { |
| 43 | + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 44 | + NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); |
| 45 | + return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Get activity from context object |
| 50 | + * |
| 51 | + * @param context context |
| 52 | + * @return object of Activity or null if it is not Activity |
| 53 | + */ |
| 54 | + public static Activity scanForActivity(Context context) { |
| 55 | + if (context == null) return null; |
| 56 | + |
| 57 | + if (context instanceof Activity) { |
| 58 | + return (Activity) context; |
| 59 | + } else if (context instanceof ContextWrapper) { |
| 60 | + return scanForActivity(((ContextWrapper) context).getBaseContext()); |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get AppCompatActivity from context |
| 68 | + * |
| 69 | + * @param context context |
| 70 | + * @return AppCompatActivity if it's not null |
| 71 | + */ |
| 72 | + public static AppCompatActivity getAppCompActivity(Context context) { |
| 73 | + if (context == null) return null; |
| 74 | + if (context instanceof AppCompatActivity) { |
| 75 | + return (AppCompatActivity) context; |
| 76 | + } else if (context instanceof ContextThemeWrapper) { |
| 77 | + return getAppCompActivity(((ContextThemeWrapper) context).getBaseContext()); |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + public static int dip2px(Context context, float dpValue) { |
| 83 | + final float scale = context.getResources().getDisplayMetrics().density; |
| 84 | + return (int) (dpValue * scale + 0.5f); |
| 85 | + } |
| 86 | + |
| 87 | + public static void saveProgress(Context context, String url, int progress) { |
| 88 | + if (!VPVideoPlayer.SAVE_PROGRESS) return; |
| 89 | + SharedPreferences spn = context.getSharedPreferences("JCVD_PROGRESS", |
| 90 | + Context.MODE_PRIVATE); |
| 91 | + SharedPreferences.Editor editor = spn.edit(); |
| 92 | + editor.putInt(url, progress); |
| 93 | + editor.apply(); |
| 94 | + } |
| 95 | + |
| 96 | + public static int getSavedProgress(Context context, String url) { |
| 97 | + if (!VPVideoPlayer.SAVE_PROGRESS) return 0; |
| 98 | + SharedPreferences spn; |
| 99 | + spn = context.getSharedPreferences("JCVD_PROGRESS", |
| 100 | + Context.MODE_PRIVATE); |
| 101 | + return spn.getInt(url, 0); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * if url == null, clear all progress |
| 106 | + * |
| 107 | + * @param context context |
| 108 | + * @param url if url!=null clear this url progress |
| 109 | + */ |
| 110 | + public static void clearSavedProgress(Context context, String url) { |
| 111 | + if (TextUtils.isEmpty(url)) { |
| 112 | + SharedPreferences spn = context.getSharedPreferences("JCVD_PROGRESS", |
| 113 | + Context.MODE_PRIVATE); |
| 114 | + spn.edit().clear().apply(); |
| 115 | + } else { |
| 116 | + SharedPreferences spn = context.getSharedPreferences("JCVD_PROGRESS", |
| 117 | + Context.MODE_PRIVATE); |
| 118 | + spn.edit().putInt(url, 0).apply(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments