JCUtils.java
4.51 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package videoplayer_lib;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import java.util.Formatter;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ContextThemeWrapper;
import android.text.TextUtils;
import java.util.Formatter;
import java.util.Locale;
/**
* Created by Nathen
* On 2016/02/21 12:25
*/
public class JCUtils {
public static String stringForTime(int timeMs) {
if (timeMs <= 0 || timeMs >= 24 * 60 * 60 * 1000) {
return "00:00";
}
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
StringBuilder stringBuilder = new StringBuilder();
Formatter mFormatter = new Formatter(stringBuilder, Locale.getDefault());
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
/**
* This method requires the caller to hold the permission ACCESS_NETWORK_STATE.
*
* @param context a application context
* @return if wifi is connected,return true
*/
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* Get activity from context object
*
* @param context something
* @return object of Activity or null if it is not Activity
*/
public static Activity scanForActivity(Context context) {
if (context == null) return null;
if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof ContextWrapper) {
return scanForActivity(((ContextWrapper) context).getBaseContext());
}
return null;
}
/**
* Get AppCompatActivity from context
*
* @param context
* @return AppCompatActivity if it's not null
*/
public static AppCompatActivity getAppCompActivity(Context context) {
if (context == null) return null;
if (context instanceof AppCompatActivity) {
return (AppCompatActivity) context;
} else if (context instanceof ContextThemeWrapper) {
return getAppCompActivity(((ContextThemeWrapper) context).getBaseContext());
}
return null;
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static void saveProgress(Context context, String url, int progress) {
if (!JCVideoPlayer.SAVE_PROGRESS) return;
SharedPreferences spn = context.getSharedPreferences("JCVD_PROGRESS",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = spn.edit();
editor.putInt(url, progress);
editor.apply();
}
public static int getSavedProgress(Context context, String url) {
if (!JCVideoPlayer.SAVE_PROGRESS) return 0;
SharedPreferences spn;
spn = context.getSharedPreferences("JCVD_PROGRESS",
Context.MODE_PRIVATE);
return spn.getInt(url, 0);
}
/**
* if url == null, clear all progress
*
* @param context
* @param url if url!=null clear this url progress
*/
public static void clearSavedProgress(Context context, String url) {
if (TextUtils.isEmpty(url)) {
SharedPreferences spn = context.getSharedPreferences("JCVD_PROGRESS",
Context.MODE_PRIVATE);
spn.edit().clear().apply();
} else {
SharedPreferences spn = context.getSharedPreferences("JCVD_PROGRESS",
Context.MODE_PRIVATE);
spn.edit().putInt(url, 0).apply();
}
}
}