forked from pockethub/PocketHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceUtils.java
More file actions
131 lines (120 loc) · 3.27 KB
/
ServiceUtils.java
File metadata and controls
131 lines (120 loc) · 3.27 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
/*
* Copyright 2012 GitHub Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mobile.util;
import static android.content.Context.WINDOW_SERVICE;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
/**
* Helpers for dealing with system services
*/
public class ServiceUtils {
/**
* Get default display
*
* @param context
* @return display
*/
public static Display getDisplay(final Context context) {
return ((WindowManager) context.getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
}
/**
* Get default display
*
* @param view
* @return display
*/
public static Display getDisplay(final View view) {
return getDisplay(view.getContext());
}
/**
* Get default display width
*
* @param context
* @return display
*/
@SuppressWarnings("deprecation")
public static int getDisplayWidth(final Context context) {
return getDisplay(context).getWidth();
}
/**
* Get default display width
*
* @param view
* @return display
*/
public static int getDisplayWidth(final View view) {
return getDisplayWidth(view.getContext());
}
/**
* Get pixels from dps
*
* @param view
* @param dp
* @return pixels
*/
public static float getPixels(final View view, final int dp) {
return getPixels(view.getResources(), dp);
}
/**
* Get pixels from dps
*
* @param resources
* @param dp
* @return pixels
*/
public static float getPixels(final Resources resources, final int dp) {
return TypedValue.applyDimension(COMPLEX_UNIT_DIP, dp,
resources.getDisplayMetrics());
}
/**
* Get pixels from dps
*
* @param view
* @param dp
* @return pixels
*/
public static int getIntPixels(final View view, final int dp) {
return getIntPixels(view.getResources(), dp);
}
/**
* Get pixels from dps
*
* @param context
* @param dp
* @return pixels
*/
public static int getIntPixels(final Context context, final int dp) {
return getIntPixels(context.getResources(), dp);
}
/**
* Get pixels from dps
*
* @param resources
* @param dp
* @return pixels
*/
public static int getIntPixels(final Resources resources, final int dp) {
float pixels = TypedValue.applyDimension(COMPLEX_UNIT_DIP, dp,
resources.getDisplayMetrics());
return (int) Math.floor(pixels + 0.5F);
}
}