Skip to content

Commit 68d1fde

Browse files
committed
优化 Demo 工程的演示效果
1 parent e332556 commit 68d1fde

File tree

6 files changed

+378
-10
lines changed

6 files changed

+378
-10
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:theme="@style/AppTheme">
99

1010
<activity
11-
android:name="com.hjq.device.compat.demo.MainActivity"
11+
android:name=".MainActivity"
1212
android:exported="true">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
@@ -18,6 +18,9 @@
1818
</intent-filter>
1919
</activity>
2020

21+
<activity
22+
android:name=".SystemPropertyActivity" />
23+
2124
</application>
2225

2326
</manifest>

app/src/main/java/com/hjq/device/compat/demo/MainActivity.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Intent;
44
import android.net.Uri;
5+
import android.os.Build;
56
import android.os.Bundle;
67
import android.provider.Settings;
78
import android.support.v7.app.AppCompatActivity;
@@ -13,23 +14,26 @@
1314
import com.hjq.device.compat.DeviceBrand;
1415
import com.hjq.device.compat.DeviceOs;
1516

16-
public class MainActivity extends AppCompatActivity implements OnClickListener {
17+
public final class MainActivity extends AppCompatActivity implements OnClickListener {
1718

1819
@Override
1920
protected void onCreate(Bundle savedInstanceState) {
2021
super.onCreate(savedInstanceState);
2122
setContentView(R.layout.main_activity);
2223

23-
TextView messageView = findViewById(R.id.btn_main_messages);
24+
TextView messageView = findViewById(R.id.tv_main_messages);
2425
StringBuilder stringBuilder = new StringBuilder()
25-
.append("\nBrand:" + DeviceBrand.getBrandName())
26-
.append("\nOs:" + DeviceOs.getOsName())
27-
.append("\nOriginalOsVersionName:" + DeviceOs.getOsVersionName())
28-
.append("\nOsVersionName:" + DeviceOs.getOsVersionName())
29-
.append("\nOsBigVersionCode:" + DeviceOs.getOsBigVersionCode());
26+
.append("\nBrandName: " + DeviceBrand.getBrandName())
27+
.append("\nOsName: " + DeviceOs.getOsName())
28+
.append("\nOriginalOsVersionName: " + DeviceOs.getOsVersionName())
29+
.append("\nOsVersionName: " + DeviceOs.getOsVersionName())
30+
.append("\nOsBigVersionCode: " + DeviceOs.getOsBigVersionCode())
31+
.append("\nAndroidVersion: Android " + Build.VERSION.RELEASE)
32+
.append("\nAndroidApiLevel: " + Build.VERSION.SDK_INT);
3033
messageView.setText(stringBuilder);
3134

3235
findViewById(R.id.btn_main_about_device).setOnClickListener(this);
36+
findViewById(R.id.btn_main_get_system_property).setOnClickListener(this);
3337
findViewById(R.id.btn_main_android_settings).setOnClickListener(this);
3438

3539
TitleBar titleBar = findViewById(R.id.tb_main_bar);
@@ -49,6 +53,9 @@ public void onClick(View view) {
4953
if (viewId == R.id.btn_main_about_device) {
5054
// 跳转到关于手机界面
5155
startActivity(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));
56+
} else if (viewId == R.id.btn_main_get_system_property) {
57+
// 跳转到获取系统属性界面
58+
startActivity(new Intent(this, SystemPropertyActivity.class));
5259
} else if (viewId == R.id.btn_main_android_settings) {
5360
// 跳转到 Android 设置界面
5461
startActivity(new Intent(Settings.ACTION_SETTINGS));
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
package com.hjq.device.compat.demo;
2+
3+
import java.io.BufferedReader;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.List;
8+
9+
public final class ShellUtils {
10+
11+
private static final String LINE_SEP = System.getProperty("line.separator");
12+
13+
private ShellUtils() {
14+
throw new UnsupportedOperationException("u can't instantiate me...");
15+
}
16+
17+
/**
18+
* Execute the command.
19+
*
20+
* @param command The command.
21+
* @param isRooted True to use root, false otherwise.
22+
* @return the single {@link CommandResult} instance
23+
*/
24+
public static CommandResult execCmd(final String command, final boolean isRooted) {
25+
return execCmd(new String[]{command}, isRooted, true);
26+
}
27+
28+
/**
29+
* Execute the command.
30+
*
31+
* @param command The command.
32+
* @param envp The environment variable settings.
33+
* @param isRooted True to use root, false otherwise.
34+
* @return the single {@link CommandResult} instance
35+
*/
36+
public static CommandResult execCmd(final String command, final List<String> envp, final boolean isRooted) {
37+
return execCmd(new String[]{command},
38+
envp == null ? null : envp.toArray(new String[]{}),
39+
isRooted,
40+
true);
41+
}
42+
43+
/**
44+
* Execute the command.
45+
*
46+
* @param commands The commands.
47+
* @param isRooted True to use root, false otherwise.
48+
* @return the single {@link CommandResult} instance
49+
*/
50+
public static CommandResult execCmd(final List<String> commands, final boolean isRooted) {
51+
return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRooted, true);
52+
}
53+
54+
/**
55+
* Execute the command.
56+
*
57+
* @param commands The commands.
58+
* @param envp The environment variable settings.
59+
* @param isRooted True to use root, false otherwise.
60+
* @return the single {@link CommandResult} instance
61+
*/
62+
public static CommandResult execCmd(final List<String> commands,
63+
final List<String> envp,
64+
final boolean isRooted) {
65+
return execCmd(commands == null ? null : commands.toArray(new String[]{}),
66+
envp == null ? null : envp.toArray(new String[]{}),
67+
isRooted,
68+
true);
69+
}
70+
71+
/**
72+
* Execute the command.
73+
*
74+
* @param commands The commands.
75+
* @param isRooted True to use root, false otherwise.
76+
* @return the single {@link CommandResult} instance
77+
*/
78+
public static CommandResult execCmd(final String[] commands, final boolean isRooted) {
79+
return execCmd(commands, isRooted, true);
80+
}
81+
82+
/**
83+
* Execute the command.
84+
*
85+
* @param command The command.
86+
* @param isRooted True to use root, false otherwise.
87+
* @param isNeedResultMsg True to return the message of result, false otherwise.
88+
* @return the single {@link CommandResult} instance
89+
*/
90+
public static CommandResult execCmd(final String command,
91+
final boolean isRooted,
92+
final boolean isNeedResultMsg) {
93+
return execCmd(new String[]{command}, isRooted, isNeedResultMsg);
94+
}
95+
96+
/**
97+
* Execute the command.
98+
*
99+
* @param command The command.
100+
* @param envp The environment variable settings.
101+
* @param isRooted True to use root, false otherwise.
102+
* @param isNeedResultMsg True to return the message of result, false otherwise.
103+
* @return the single {@link CommandResult} instance
104+
*/
105+
public static CommandResult execCmd(final String command,
106+
final List<String> envp,
107+
final boolean isRooted,
108+
final boolean isNeedResultMsg) {
109+
return execCmd(new String[]{command}, envp == null ? null : envp.toArray(new String[]{}),
110+
isRooted,
111+
isNeedResultMsg);
112+
}
113+
114+
/**
115+
* Execute the command.
116+
*
117+
* @param command The command.
118+
* @param envp The environment variable settings array.
119+
* @param isRooted True to use root, false otherwise.
120+
* @param isNeedResultMsg True to return the message of result, false otherwise.
121+
* @return the single {@link CommandResult} instance
122+
*/
123+
public static CommandResult execCmd(final String command,
124+
final String[] envp,
125+
final boolean isRooted,
126+
final boolean isNeedResultMsg) {
127+
return execCmd(new String[]{command}, envp, isRooted, isNeedResultMsg);
128+
}
129+
130+
/**
131+
* Execute the command.
132+
*
133+
* @param commands The commands.
134+
* @param isRooted True to use root, false otherwise.
135+
* @param isNeedResultMsg True to return the message of result, false otherwise.
136+
* @return the single {@link CommandResult} instance
137+
*/
138+
public static CommandResult execCmd(final List<String> commands,
139+
final boolean isRooted,
140+
final boolean isNeedResultMsg) {
141+
return execCmd(commands == null ? null : commands.toArray(new String[]{}),
142+
isRooted,
143+
isNeedResultMsg);
144+
}
145+
146+
/**
147+
* Execute the command.
148+
*
149+
* @param commands The commands.
150+
* @param isRooted True to use root, false otherwise.
151+
* @param isNeedResultMsg True to return the message of result, false otherwise.
152+
* @return the single {@link CommandResult} instance
153+
*/
154+
public static CommandResult execCmd(final String[] commands,
155+
final boolean isRooted,
156+
final boolean isNeedResultMsg) {
157+
return execCmd(commands, null, isRooted, isNeedResultMsg);
158+
}
159+
160+
/**
161+
* Execute the command.
162+
*
163+
* @param commands The commands.
164+
* @param envp Array of strings, each element of which
165+
* has environment variable settings in the format
166+
* <i>name</i>=<i>value</i>, or
167+
* <tt>null</tt> if the subprocess should inherit
168+
* the environment of the current process.
169+
* @param isRooted True to use root, false otherwise.
170+
* @param isNeedResultMsg True to return the message of result, false otherwise.
171+
* @return the single {@link CommandResult} instance
172+
*/
173+
public static CommandResult execCmd(final String[] commands,
174+
final String[] envp,
175+
final boolean isRooted,
176+
final boolean isNeedResultMsg) {
177+
int result = -1;
178+
if (commands == null || commands.length == 0) {
179+
return new CommandResult(result, "", "");
180+
}
181+
Process process = null;
182+
BufferedReader successResult = null;
183+
BufferedReader errorResult = null;
184+
StringBuilder successMsg = null;
185+
StringBuilder errorMsg = null;
186+
DataOutputStream os = null;
187+
try {
188+
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh", envp, null);
189+
os = new DataOutputStream(process.getOutputStream());
190+
for (String command : commands) {
191+
if (command == null) continue;
192+
os.write(command.getBytes());
193+
os.writeBytes(LINE_SEP);
194+
os.flush();
195+
}
196+
os.writeBytes("exit" + LINE_SEP);
197+
os.flush();
198+
result = process.waitFor();
199+
if (isNeedResultMsg) {
200+
successMsg = new StringBuilder();
201+
errorMsg = new StringBuilder();
202+
successResult = new BufferedReader(
203+
new InputStreamReader(process.getInputStream(), "UTF-8")
204+
);
205+
errorResult = new BufferedReader(
206+
new InputStreamReader(process.getErrorStream(), "UTF-8")
207+
);
208+
String line;
209+
if ((line = successResult.readLine()) != null) {
210+
successMsg.append(line);
211+
while ((line = successResult.readLine()) != null) {
212+
successMsg.append(LINE_SEP).append(line);
213+
}
214+
}
215+
if ((line = errorResult.readLine()) != null) {
216+
errorMsg.append(line);
217+
while ((line = errorResult.readLine()) != null) {
218+
errorMsg.append(LINE_SEP).append(line);
219+
}
220+
}
221+
}
222+
} catch (Exception e) {
223+
e.printStackTrace();
224+
} finally {
225+
try {
226+
if (os != null) {
227+
os.close();
228+
}
229+
} catch (IOException e) {
230+
e.printStackTrace();
231+
}
232+
try {
233+
if (successResult != null) {
234+
successResult.close();
235+
}
236+
} catch (IOException e) {
237+
e.printStackTrace();
238+
}
239+
try {
240+
if (errorResult != null) {
241+
errorResult.close();
242+
}
243+
} catch (IOException e) {
244+
e.printStackTrace();
245+
}
246+
if (process != null) {
247+
process.destroy();
248+
}
249+
}
250+
return new CommandResult(
251+
result,
252+
successMsg == null ? "" : successMsg.toString(),
253+
errorMsg == null ? "" : errorMsg.toString()
254+
);
255+
}
256+
257+
/**
258+
* The result of command.
259+
*/
260+
public static class CommandResult {
261+
public int result;
262+
public String successMsg;
263+
public String errorMsg;
264+
265+
public CommandResult(final int result, final String successMsg, final String errorMsg) {
266+
this.result = result;
267+
this.successMsg = successMsg;
268+
this.errorMsg = errorMsg;
269+
}
270+
271+
@Override
272+
public String toString() {
273+
return "result: " + result + "\n" +
274+
"successMsg: " + successMsg + "\n" +
275+
"errorMsg: " + errorMsg;
276+
}
277+
}
278+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hjq.device.compat.demo;
2+
3+
import android.content.Intent;
4+
import android.net.Uri;
5+
import android.os.Bundle;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.util.Log;
8+
import android.widget.TextView;
9+
import com.hjq.bar.OnTitleBarListener;
10+
import com.hjq.bar.TitleBar;
11+
import com.hjq.device.compat.demo.ShellUtils.CommandResult;
12+
13+
public final class SystemPropertyActivity extends AppCompatActivity {
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.system_property_activity);
19+
20+
TextView messageView = findViewById(R.id.tv_system_property_messages);
21+
22+
TitleBar titleBar = findViewById(R.id.tb_system_property_bar);
23+
titleBar.setOnTitleBarListener(new OnTitleBarListener() {
24+
@Override
25+
public void onTitleClick(TitleBar titleBar) {
26+
Intent intent = new Intent(Intent.ACTION_VIEW);
27+
intent.setData(Uri.parse(titleBar.getTitle().toString()));
28+
startActivity(intent);
29+
}
30+
});
31+
32+
new Thread(() -> {
33+
CommandResult systemPropertyInfo = ShellUtils.execCmd("getprop", false, true);
34+
Log.i("DeviceCompat", systemPropertyInfo.successMsg);
35+
runOnUiThread(() -> messageView.setText(systemPropertyInfo.successMsg));
36+
}).start();
37+
}
38+
}

0 commit comments

Comments
 (0)