4
4
import android .content .Context ;
5
5
import android .content .Intent ;
6
6
import android .content .ServiceConnection ;
7
+ import android .graphics .PixelFormat ;
8
+ import android .net .Uri ;
9
+ import android .os .Build ;
7
10
import android .os .Bundle ;
8
11
import android .os .Handler ;
9
12
import android .os .IBinder ;
10
13
import android .os .Message ;
11
14
import android .os .RemoteException ;
15
+ import android .provider .Settings ;
12
16
import android .support .v4 .app .Fragment ;
13
17
import android .util .Log ;
18
+ import android .view .Gravity ;
14
19
import android .view .LayoutInflater ;
20
+ import android .view .MotionEvent ;
15
21
import android .view .View ;
16
22
import android .view .ViewGroup ;
23
+ import android .view .WindowManager ;
24
+ import android .widget .Button ;
17
25
import android .widget .ListView ;
18
26
import android .widget .TextView ;
19
27
22
30
import com .fadisu .cpurun .service .CpuMsgService ;
23
31
import com .fadisu .cpurun .service .ICpuMsgCallBack ;
24
32
import com .fadisu .cpurun .service .ICpuMsgService ;
33
+ import com .fadisu .cpurun .util .ProcCpuInfo ;
25
34
26
35
import java .lang .ref .WeakReference ;
36
+ import java .lang .reflect .Method ;
27
37
import java .util .ArrayList ;
28
38
import java .util .List ;
29
39
30
40
31
- public class CpuStatusFragment extends Fragment implements CustomAdapter .LayoutView {
41
+ public class CpuStatusFragment extends Fragment implements CustomAdapter .LayoutView , View . OnClickListener {
32
42
33
43
public static final int UPDATE_UI = 0 ;
34
44
45
+ public static final String CPU_INFO = ProcCpuInfo .getProcessor ();
46
+
35
47
private Context mContext ;
36
48
private List <String > result ;
37
49
private CustomAdapter <String > mCustomAdapter ;
38
50
39
51
private View mView ;
52
+ private Button mFloatBtn ;
40
53
private ListView mListView ;
41
54
42
55
private Handler mHandler = null ;
@@ -52,7 +65,6 @@ public void updateCpuUsage(List<String> list) throws RemoteException {
52
65
}
53
66
};
54
67
55
- ;
56
68
private ServiceConnection mConnection = new ServiceConnection () {
57
69
58
70
@ Override
@@ -80,7 +92,7 @@ public void onAttach(Context context) {
80
92
81
93
@ Override
82
94
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
83
- mView = inflater .inflate (R .layout .fragment_base , container , false );
95
+ mView = inflater .inflate (R .layout .fragment_cpu_status , container , false );
84
96
initViews ();
85
97
initValues ();
86
98
initListeners ();
@@ -89,6 +101,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
89
101
90
102
@ Override
91
103
public void onResume () {
104
+ if (!isCanDrawOverlays (mContext )) {
105
+ requestAlertWindowPermission ();
106
+ }
92
107
bindService ();
93
108
super .onResume ();
94
109
}
@@ -101,9 +116,12 @@ public void onStop() {
101
116
102
117
private void initViews () {
103
118
mListView = (ListView ) mView .findViewById (R .id .listview );
119
+ mFloatBtn = (Button ) mView .findViewById (R .id .btn_float_window );
104
120
}
105
121
106
122
private void initValues () {
123
+ mWm = (WindowManager ) mContext .getSystemService (Context .WINDOW_SERVICE );
124
+
107
125
mHandler = new MyHandler (this );
108
126
result = new ArrayList <>();
109
127
result .add ("ServiceConnection ....." );
@@ -113,6 +131,7 @@ private void initValues() {
113
131
}
114
132
115
133
private void initListeners () {
134
+ mFloatBtn .setOnClickListener (this );
116
135
mCustomAdapter .setLayoutView (this );
117
136
}
118
137
@@ -148,6 +167,7 @@ private void unBindService() {
148
167
Log .d ("CpuMsgService" , "unBindService" );
149
168
}
150
169
170
+
151
171
private static class MyHandler extends Handler {
152
172
153
173
WeakReference <CpuStatusFragment > mActivity ;
@@ -162,6 +182,15 @@ public void handleMessage(Message msg) {
162
182
switch (msg .what ) {
163
183
case UPDATE_UI :
164
184
activity .mCustomAdapter .updateData ((ArrayList <String >) activity .result );
185
+
186
+ if (null != activity .mResultTv ) {
187
+ String info = "" ;
188
+ for (String value : activity .result ) {
189
+ info = info + value + "\n " ;
190
+ }
191
+ info = info + CPU_INFO ;
192
+ activity .mResultTv .setText (info );
193
+ }
165
194
break ;
166
195
default :
167
196
break ;
@@ -172,4 +201,98 @@ public void handleMessage(Message msg) {
172
201
class ViewHolder {
173
202
TextView tv_info ;
174
203
}
204
+
205
+ private TextView mResultTv ;
206
+ private WindowManager mWm ;
207
+ private WindowManager .LayoutParams mParams ;
208
+
209
+ @ Override
210
+ public void onClick (View view ) {
211
+ switch (view .getId ()) {
212
+ case R .id .btn_float_window :
213
+ String info = mFloatBtn .getText ().toString ();
214
+ if (info .equals (getString (R .string .title_float_window_open ))) {
215
+ showFloatWindow ();
216
+ mFloatBtn .setText (getString (R .string .title_float_window_close ));
217
+ } else if (info .equals (getString (R .string .title_float_window_close ))) {
218
+ hideFloatWindow ();
219
+ mFloatBtn .setText (getString (R .string .title_float_window_open ));
220
+ }
221
+ break ;
222
+ }
223
+ }
224
+
225
+ private void showFloatWindow () {
226
+ mResultTv = new TextView (mContext );
227
+ mResultTv .setBackgroundColor (mContext .getColor (R .color .float_bg ));
228
+ mResultTv .setTextColor (mContext .getColor (R .color .white ));
229
+ mResultTv .setOnTouchListener (new View .OnTouchListener () {
230
+ int lastX = 0 ;
231
+ int lastY = 0 ;
232
+ int paramX = 0 ;
233
+ int paramY = 0 ;
234
+
235
+ @ Override
236
+ public boolean onTouch (View view , MotionEvent motionEvent ) {
237
+ switch (motionEvent .getAction ()) {
238
+ case MotionEvent .ACTION_DOWN :
239
+ lastX = (int ) motionEvent .getRawX ();
240
+ lastY = (int ) motionEvent .getRawY ();
241
+ paramX = mParams .x ;
242
+ paramY = mParams .y ;
243
+ break ;
244
+ case MotionEvent .ACTION_MOVE :
245
+ int dx = (int ) motionEvent .getRawX () - lastX ;
246
+ int dy = (int ) motionEvent .getRawY () - lastY ;
247
+ mParams .x = paramX + dx ;
248
+ mParams .y = paramY + dy ;
249
+ // update float window
250
+ mWm .updateViewLayout (mResultTv , mParams );
251
+ break ;
252
+ }
253
+ return true ;
254
+ }
255
+ });
256
+
257
+ mParams = new WindowManager .LayoutParams ();
258
+ mParams .width = WindowManager .LayoutParams .WRAP_CONTENT ;
259
+ mParams .height = WindowManager .LayoutParams .WRAP_CONTENT ;
260
+ // 悬浮窗的核心
261
+ mParams .type = WindowManager .LayoutParams .TYPE_SYSTEM_ALERT ;
262
+ // 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
263
+ mParams .flags = WindowManager .LayoutParams .FLAG_NOT_TOUCH_MODAL
264
+ | WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE ;
265
+ mParams .format = PixelFormat .TRANSPARENT ;
266
+
267
+ mWm .addView (mResultTv , mParams );
268
+ }
269
+
270
+ private void hideFloatWindow () {
271
+ mWm .removeView (mResultTv );
272
+ }
273
+
274
+ private static final int REQUEST_CODE = 1 ;
275
+
276
+ //判断权限
277
+ private boolean isCanDrawOverlays (Context context ) {
278
+ Boolean result = true ;
279
+ if (Build .VERSION .SDK_INT >= 23 ) {
280
+ try {
281
+ Class clazz = Settings .class ;
282
+ Method canDrawOverlays = clazz .getDeclaredMethod ("canDrawOverlays" , Context .class );
283
+ result = (Boolean ) canDrawOverlays .invoke (null , context );
284
+ } catch (Exception e ) {
285
+ e .printStackTrace ();
286
+ }
287
+ }
288
+ return result ;
289
+ }
290
+
291
+ //申请权限
292
+ private void requestAlertWindowPermission () {
293
+ Intent intent = new Intent (Settings .ACTION_MANAGE_OVERLAY_PERMISSION );
294
+ intent .setData (Uri .parse ("package:" + mContext .getPackageName ()));
295
+ startActivityForResult (intent , REQUEST_CODE );
296
+ }
297
+
175
298
}
0 commit comments