Skip to content

Commit cd99315

Browse files
committed
add Float Window UI
1 parent d78b05e commit cd99315

File tree

10 files changed

+198
-4
lines changed

10 files changed

+198
-4
lines changed

app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
android:minSdkVersion="21"
88
android:targetSdkVersion="24" />
99

10+
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
11+
1012
<application
1113
android:name="com.fadisu.cpurun.MyApplication"
1214
android:allowBackup="true"

app/src/main/java/com/fadisu/cpurun/fragment/CpuStatusFragment.java

+126-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.content.ServiceConnection;
7+
import android.graphics.PixelFormat;
8+
import android.net.Uri;
9+
import android.os.Build;
710
import android.os.Bundle;
811
import android.os.Handler;
912
import android.os.IBinder;
1013
import android.os.Message;
1114
import android.os.RemoteException;
15+
import android.provider.Settings;
1216
import android.support.v4.app.Fragment;
1317
import android.util.Log;
18+
import android.view.Gravity;
1419
import android.view.LayoutInflater;
20+
import android.view.MotionEvent;
1521
import android.view.View;
1622
import android.view.ViewGroup;
23+
import android.view.WindowManager;
24+
import android.widget.Button;
1725
import android.widget.ListView;
1826
import android.widget.TextView;
1927

@@ -22,21 +30,26 @@
2230
import com.fadisu.cpurun.service.CpuMsgService;
2331
import com.fadisu.cpurun.service.ICpuMsgCallBack;
2432
import com.fadisu.cpurun.service.ICpuMsgService;
33+
import com.fadisu.cpurun.util.ProcCpuInfo;
2534

2635
import java.lang.ref.WeakReference;
36+
import java.lang.reflect.Method;
2737
import java.util.ArrayList;
2838
import java.util.List;
2939

3040

31-
public class CpuStatusFragment extends Fragment implements CustomAdapter.LayoutView {
41+
public class CpuStatusFragment extends Fragment implements CustomAdapter.LayoutView, View.OnClickListener {
3242

3343
public static final int UPDATE_UI = 0;
3444

45+
public static final String CPU_INFO = ProcCpuInfo.getProcessor();
46+
3547
private Context mContext;
3648
private List<String> result;
3749
private CustomAdapter<String> mCustomAdapter;
3850

3951
private View mView;
52+
private Button mFloatBtn;
4053
private ListView mListView;
4154

4255
private Handler mHandler = null;
@@ -52,7 +65,6 @@ public void updateCpuUsage(List<String> list) throws RemoteException {
5265
}
5366
};
5467

55-
;
5668
private ServiceConnection mConnection = new ServiceConnection() {
5769

5870
@Override
@@ -80,7 +92,7 @@ public void onAttach(Context context) {
8092

8193
@Override
8294
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);
8496
initViews();
8597
initValues();
8698
initListeners();
@@ -89,6 +101,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
89101

90102
@Override
91103
public void onResume() {
104+
if (!isCanDrawOverlays(mContext)) {
105+
requestAlertWindowPermission();
106+
}
92107
bindService();
93108
super.onResume();
94109
}
@@ -101,9 +116,12 @@ public void onStop() {
101116

102117
private void initViews() {
103118
mListView = (ListView) mView.findViewById(R.id.listview);
119+
mFloatBtn = (Button) mView.findViewById(R.id.btn_float_window);
104120
}
105121

106122
private void initValues() {
123+
mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
124+
107125
mHandler = new MyHandler(this);
108126
result = new ArrayList<>();
109127
result.add("ServiceConnection .....");
@@ -113,6 +131,7 @@ private void initValues() {
113131
}
114132

115133
private void initListeners() {
134+
mFloatBtn.setOnClickListener(this);
116135
mCustomAdapter.setLayoutView(this);
117136
}
118137

@@ -148,6 +167,7 @@ private void unBindService() {
148167
Log.d("CpuMsgService", "unBindService");
149168
}
150169

170+
151171
private static class MyHandler extends Handler {
152172

153173
WeakReference<CpuStatusFragment> mActivity;
@@ -162,6 +182,15 @@ public void handleMessage(Message msg) {
162182
switch (msg.what) {
163183
case UPDATE_UI:
164184
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+
}
165194
break;
166195
default:
167196
break;
@@ -172,4 +201,98 @@ public void handleMessage(Message msg) {
172201
class ViewHolder {
173202
TextView tv_info;
174203
}
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+
175298
}

app/src/main/java/com/fadisu/cpurun/service/CpuMsgService.java

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fadisu.cpurun.R;
1212
import com.fadisu.cpurun.util.CpuUtils;
1313
import com.fadisu.cpurun.util.ProcCpuStatUtil;
14+
import com.fadisu.cpurun.util.ThermalInfoUtil;
1415

1516
import java.util.ArrayList;
1617
import java.util.List;
@@ -77,6 +78,7 @@ public void run() {
7778
while (isRun) {
7879
List<String> result = new ArrayList<>();
7980
result.add(mContext.getString(R.string.cpu_usage) + ProcCpuStatUtil.getCpuUsage());
81+
result.add(ThermalInfoUtil.getCpuTemparature());
8082
result.addAll(CpuUtils.getCpuCurFreq(mContext));
8183
if (null != mICpuMsgService) {
8284
try {

app/src/main/java/com/fadisu/cpurun/util/ProcCpuStatUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static String getCpuUsage() {
6565
CpuRateInfo mCpuRateInfo1 = getCpuRateInfo();
6666

6767
try {
68-
Thread.sleep(2000);
68+
Thread.sleep(1000);
6969
} catch (InterruptedException e) {
7070
//e.printStackTrace();
7171
}

app/src/main/java/com/fadisu/cpurun/util/ThermalInfoUtil.java

+33
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,37 @@ public boolean accept(File file) {
7373
return result;
7474
}
7575

76+
public static String getCpuTemparature() {
77+
String result = null;
78+
79+
BufferedReader br = null;
80+
String line = null;
81+
String type = null;
82+
String temp = null;
83+
try {
84+
br = new BufferedReader(new FileReader("/sys/class/thermal/thermal_zone1/type"));
85+
line = br.readLine();
86+
87+
if (line != null) {
88+
type = line;
89+
}
90+
91+
br = new BufferedReader(new FileReader("/sys/class/thermal/thermal_zone1/temp"));
92+
line = br.readLine();
93+
if (line != null) {
94+
long temperature = Long.parseLong(line);
95+
if (temperature < 0) {
96+
temp = "Unknow";
97+
} else {
98+
temp = (float) (temperature / 1000.0) + "°C";
99+
}
100+
101+
result = type + " : " + temp;
102+
}
103+
} catch (IOException e) {
104+
e.printStackTrace();
105+
}
106+
107+
return result;
108+
}
76109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<TextView
7+
android:id="@+id/tv_info"
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:text="Text"/>
11+
12+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<Button
8+
android:id="@+id/btn_float_window"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:text="@string/title_float_window_open"/>
12+
13+
<ListView
14+
android:id="@+id/listview"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"></ListView>
17+
</LinearLayout>

app/src/main/res/values-zh/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<string name="title_cpu_scene_freq">CPU场景频率</string>
1717
<string name="title_cpu_voltage">CPU 电压</string>
1818
<string name="title_temperature">温度</string>
19+
<string name="title_float_window_open">打开悬浮窗</string>
20+
<string name="title_float_window_close">关闭悬浮窗</string>
1921

2022
<string name="cpu_core_number">CPU 核数: </string>
2123
<string name="cpu_bits">CPU 位数: </string>

app/src/main/res/values/colors.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
<color name="title_selected_bg">#13375b</color>
1515
<!-- 标题分隔线颜色 -->
1616
<color name="title_divider_bg">#2c495d</color>
17+
<color name="float_bg">#80000000</color>
1718
</resources>

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<string name="title_cpu_scene_freq">CPU Scene Of Frequency</string>
1717
<string name="title_cpu_voltage">CPU Voltage</string>
1818
<string name="title_temperature">Temperature</string>
19+
<string name="title_float_window_open">Open Float Window</string>
20+
<string name="title_float_window_close">Close Float Window</string>
1921

2022
<string name="cpu_core_number">CPU core number: </string>
2123
<string name="cpu_bits">CPU bit: </string>

0 commit comments

Comments
 (0)