-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathScreenUtils.java
358 lines (325 loc) · 10.9 KB
/
ScreenUtils.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package com.dds.common.utils;
import static android.Manifest.permission.WRITE_SETTINGS;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import com.dds.common.lifecycle.Utils;
public final class ScreenUtils {
private ScreenUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return the width of screen, in pixel.
*
* @return the width of screen, in pixel
*/
public static int getScreenWidth() {
WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wm.getDefaultDisplay().getRealSize(point);
} else {
wm.getDefaultDisplay().getSize(point);
}
return point.x;
}
/**
* Return the height of screen, in pixel.
*
* @return the height of screen, in pixel
*/
public static int getScreenHeight() {
WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wm.getDefaultDisplay().getRealSize(point);
} else {
wm.getDefaultDisplay().getSize(point);
}
return point.y;
}
/**
* Return the application's width of screen, in pixel.
*
* @return the application's width of screen, in pixel
*/
public static int getAppScreenWidth() {
WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.x;
}
/**
* Return the application's height of screen, in pixel.
*
* @return the application's height of screen, in pixel
*/
public static int getAppScreenHeight() {
WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.y;
}
/**
* Return the density of screen.
*
* @return the density of screen
*/
public static float getScreenDensity() {
return Resources.getSystem().getDisplayMetrics().density;
}
/**
* Return the screen density expressed as dots-per-inch.
*
* @return the screen density expressed as dots-per-inch
*/
public static int getScreenDensityDpi() {
return Resources.getSystem().getDisplayMetrics().densityDpi;
}
/**
* Return the exact physical pixels per inch of the screen in the Y dimension.
*
* @return the exact physical pixels per inch of the screen in the Y dimension
*/
public static float getScreenXDpi() {
return Resources.getSystem().getDisplayMetrics().xdpi;
}
/**
* Return the exact physical pixels per inch of the screen in the Y dimension.
*
* @return the exact physical pixels per inch of the screen in the Y dimension
*/
public static float getScreenYDpi() {
return Resources.getSystem().getDisplayMetrics().ydpi;
}
/**
* Return the distance between the given View's X (start point of View's width) and the screen width.
*
* @return the distance between the given View's X (start point of View's width) and the screen width.
*/
public int calculateDistanceByX(View view) {
int[] point = new int[2];
view.getLocationOnScreen(point);
return getScreenWidth() - point[0];
}
/**
* Return the distance between the given View's Y (start point of View's height) and the screen height.
*
* @return the distance between the given View's Y (start point of View's height) and the screen height.
*/
public int calculateDistanceByY(View view) {
int[] point = new int[2];
view.getLocationOnScreen(point);
return getScreenHeight() - point[1];
}
/**
* Return the X coordinate of the given View on the screen.
*
* @return X coordinate of the given View on the screen.
*/
public int getViewX(View view) {
int[] point = new int[2];
view.getLocationOnScreen(point);
return point[0];
}
/**
* Return the Y coordinate of the given View on the screen.
*
* @return Y coordinate of the given View on the screen.
*/
public int getViewY(View view) {
int[] point = new int[2];
view.getLocationOnScreen(point);
return point[1];
}
/**
* Set full screen.
*
* @param activity The activity.
*/
public static void setFullScreen(@NonNull final Activity activity) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/**
* Set non full screen.
*
* @param activity The activity.
*/
public static void setNonFullScreen(@NonNull final Activity activity) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/**
* Toggle full screen.
*
* @param activity The activity.
*/
public static void toggleFullScreen(@NonNull final Activity activity) {
boolean isFullScreen = isFullScreen(activity);
Window window = activity.getWindow();
if (isFullScreen) {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
/**
* Return whether screen is full.
*
* @param activity The activity.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isFullScreen(@NonNull final Activity activity) {
int fullScreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
return (activity.getWindow().getAttributes().flags & fullScreenFlag) == fullScreenFlag;
}
/**
* Set the screen to landscape.
*
* @param activity The activity.
*/
@SuppressLint("SourceLockedOrientationActivity")
public static void setLandscape(@NonNull final Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/**
* Set the screen to portrait.
*
* @param activity The activity.
*/
@SuppressLint("SourceLockedOrientationActivity")
public static void setPortrait(@NonNull final Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/**
* Return whether screen is landscape.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLandscape() {
return Utils.getApp().getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
}
/**
* Return whether screen is portrait.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isPortrait() {
return Utils.getApp().getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT;
}
/**
* Return the rotation of screen.
*
* @param activity The activity.
* @return the rotation of screen
*/
public static int getScreenRotation(@NonNull final Activity activity) {
switch (activity.getWindowManager().getDefaultDisplay().getRotation()) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
default:
return 0;
}
}
/**
* Return the bitmap of screen.
*
* @param activity The activity.
* @return the bitmap of screen
*/
public static Bitmap screenShot(@NonNull final Activity activity) {
return screenShot(activity, false);
}
/**
* Return the bitmap of screen.
*
* @param activity The activity.
* @param isDeleteStatusBar True to delete status bar, false otherwise.
* @return the bitmap of screen
*/
public static Bitmap screenShot(@NonNull final Activity activity, boolean isDeleteStatusBar) {
View decorView = activity.getWindow().getDecorView();
Bitmap bmp = ImageUtils.view2Bitmap(decorView);
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
if (isDeleteStatusBar) {
int statusBarHeight = BarUtils.getStatusBarHeight();
return Bitmap.createBitmap(
bmp,
0,
statusBarHeight,
dm.widthPixels,
dm.heightPixels - statusBarHeight
);
} else {
return Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
}
}
/**
* Return whether screen is locked.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isScreenLock() {
KeyguardManager km =
(KeyguardManager) Utils.getApp().getSystemService(Context.KEYGUARD_SERVICE);
if (km == null) return false;
return km.inKeyguardRestrictedInputMode();
}
/**
* Set the duration of sleep.
* <p>Must hold {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
*
* @param duration The duration.
*/
@RequiresPermission(WRITE_SETTINGS)
public static void setSleepDuration(final int duration) {
Settings.System.putInt(
Utils.getApp().getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT,
duration
);
}
/**
* Return the duration of sleep.
*
* @return the duration of sleep.
*/
public static int getSleepDuration() {
try {
return Settings.System.getInt(
Utils.getApp().getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT
);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return -123;
}
}
}