Skip to content

Commit 673cad8

Browse files
committed
check root permission
1 parent fe285c7 commit 673cad8

File tree

10 files changed

+154
-4
lines changed

10 files changed

+154
-4
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ Screenshot Tile for Android 7.0+
55
<a href="https://play.google.com/store/apps/details?id=com.github.ipcjs.screenshottile">
66
<img alt="Android app on Google Play"
77
src="https://developer.android.com/images/brand/en_app_rgb_wo_45.png" />
8+
</a>
9+
<a href="http://www.coolapk.com/apk/com.github.ipcjs.screenshottile" style="width: 127px; height: 43px; background-color: black; color: white; /*display: block;*/ position: absolute; border-radius: 6px; margin: 1px; text-decoration: none; padding: px">
10+
<span style="font-size: 12px; display: block; padding-left: 30px; transform: scale(0.65); width: 135%; left: -17.5%; position: relative;">ANDROID APP ON</span>
11+
<img style="width: 20px;display: block; float: left;" src="http://image.coolapk.com/apk_logo/2016/0108/12202_1452248424_4592.png"/>
12+
<span style="font-size: 20px; display: block; float: left;">CoolApk</span>
813
</a>

app/build.gradle

+30-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ android {
1818
applicationId "com.github.ipcjs.screenshottile"
1919
minSdkVersion 24
2020
targetSdkVersion 25
21-
versionCode 2
22-
versionName "1.0.1"
21+
versionCode 3
22+
versionName "1.0.2"
2323
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2424
}
2525
buildTypes {
@@ -29,7 +29,35 @@ android {
2929
signingConfig signingConfigs.release
3030
debuggable false
3131
}
32+
debug {
33+
applicationIdSuffix '.debug'
34+
versionNameSuffix '.debug'
35+
}
36+
}
37+
// 自动在debug版的app_name后加".debug"(兼容各种版本的as)
38+
applicationVariants.all { variant ->// 遍历所有的variant
39+
if (variant.buildType.name.equals("debug")) {// 该variant的buildType是debug
40+
variant.mergeResources.doLast {// 合并完res后执行:
41+
def encoding = "UTF-8"// 文件编码(不要取系统默认编码,win*的系统默认编码是GBK)
42+
def langDirs = ["values", "values-zh"]// 语言列表
43+
langDirs.each { langDir ->
44+
File valuesFile = file("${variant.mergeResources.outputDir.absolutePath}/${langDir}/${langDir}.xml")
45+
// 合并后的values文件
46+
if (valuesFile.exists()) {// 若存在
47+
String content = valuesFile.getText(encoding)// 读取values
48+
content = content.replaceFirst("<string name=\"app_name\">(.+)</string>", "<string name=\"app_name\">\$1.debug</string>")
49+
// 替换app_name的内容
50+
valuesFile.write(content, encoding)// 写入values
51+
println ">>>替换${valuesFile.absolutePath}中的app_name"
52+
} else {
53+
println ">>>不存在: ${valuesFile.absolutePath}"
54+
}
55+
}
56+
57+
}
58+
}
3259
}
60+
3361
}
3462

3563
dependencies {

app/src/main/AndroidManifest.xml

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
<activity
1212
android:name=".NoDisplayActivity"
1313
android:theme="@android:style/Theme.NoDisplay"/>
14+
<activity
15+
android:name=".RootDialogActivity"
16+
android:excludeFromRecents="true"
17+
android:theme="@style/AppTheme.Transparent">
18+
<!--<intent-filter>-->
19+
<!--<action android:name="android.intent.action.MAIN"/>-->
20+
<!--<category android:name="android.intent.category.LAUNCHER"/>-->
21+
<!--</intent-filter>-->
22+
</activity>
23+
1424
<service
1525
android:name=".ScreenshotTileService"
1626
android:icon="@drawable/ic_stat_name"

app/src/main/java/com/github/ipcjs/screenshottile/NoDisplayActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected void onCreate(Bundle savedInstanceState) {
1111
super.onCreate(savedInstanceState);
1212
p("NoDisplayActivity.onCreate");
1313
// Utils.runCmd("input keyevent 120", true);
14-
Utils.runOneCmdByRootNoWait("input keyevent 120");
14+
Utils.runOneCmdByRoot("input keyevent 120", false);
1515
finish();
1616
}
1717

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.ipcjs.screenshottile;
2+
3+
import android.app.Activity;
4+
import android.app.AlertDialog;
5+
import android.app.Dialog;
6+
import android.app.DialogFragment;
7+
import android.content.DialogInterface;
8+
import android.content.Intent;
9+
import android.net.Uri;
10+
import android.os.Bundle;
11+
12+
import static com.github.ipcjs.screenshottile.Utils.hasRoot;
13+
import static com.github.ipcjs.screenshottile.Utils.p;
14+
15+
/**
16+
* Created by ipcjs on 02/07.
17+
*/
18+
19+
public class RootDialogActivity extends Activity {
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
if (savedInstanceState == null) {
24+
RootDialogFragment fragment = RootDialogFragment.newInstance();
25+
// fragment.setCancelable(false);
26+
fragment.show(getFragmentManager(), RootDialogFragment.class.getName());
27+
}
28+
}
29+
30+
public static class RootDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
31+
public static RootDialogFragment newInstance() {
32+
return new RootDialogFragment();
33+
}
34+
35+
@Override
36+
public Dialog onCreateDialog(Bundle savedInstanceState) {
37+
return new AlertDialog.Builder(getActivity(), getTheme())
38+
.setTitle(R.string.dialog_obtain_root)
39+
.setMessage(getString(R.string.dialog_obtain_root_message, getString(R.string.app_name)))
40+
.setPositiveButton(R.string.dialog_reacquire, this)
41+
.setNeutralButton(R.string.dialog_i_know, this)
42+
.setNegativeButton(R.string.dialog_uninstall, this)
43+
.create();
44+
}
45+
46+
@Override
47+
public void onClick(DialogInterface dialog, int which) {
48+
switch (which) {
49+
case Dialog.BUTTON_POSITIVE:
50+
if (!hasRoot()) {
51+
startActivity(new Intent(getActivity(), RootDialogActivity.class));
52+
}
53+
break;
54+
case Dialog.BUTTON_NEUTRAL:
55+
break;
56+
case Dialog.BUTTON_NEGATIVE:
57+
startActivity(new Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.parse("package:" + getActivity().getPackageName())));
58+
break;
59+
}
60+
getActivity().finish();
61+
}
62+
63+
@Override
64+
public void onDismiss(DialogInterface dialog) {
65+
super.onDismiss(dialog);
66+
p("RootDialogFragment.onDismiss");
67+
}
68+
69+
@Override
70+
public void onCancel(DialogInterface dialog) {
71+
super.onCancel(dialog);
72+
getActivity().finish();
73+
}
74+
}
75+
}

app/src/main/java/com/github/ipcjs/screenshottile/ScreenshotTileService.java

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class ScreenshotTileService extends TileService {
1212
public void onTileAdded() {
1313
super.onTileAdded();
1414
p("onTileAdded");
15+
// if (!hasRoot()) {
16+
startActivityAndCollapse(new Intent(this, RootDialogActivity.class));
17+
// }
1518
}
1619

1720
@Override

app/src/main/java/com/github/ipcjs/screenshottile/Utils.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,24 @@ public static void p(String format, Object... args) {
3030
p(String.format(format, args));
3131
}
3232

33-
public static void runOneCmdByRootNoWait(String cmd) {
33+
public static int runOneCmdByRoot(String cmd, boolean isWait) {
34+
int returnCode = -1;
3435
try {
3536
Process process = Runtime.getRuntime().exec(new String[]{"su", "-c", cmd});
37+
if (isWait) {
38+
returnCode = process.waitFor();
39+
}
3640
p("rumCmd: %s", cmd);
3741
} catch (IOException e) {
3842
e.printStackTrace();
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
3945
}
46+
return returnCode;
47+
}
48+
49+
public static boolean hasRoot() {
50+
return runOneCmdByRoot("echo", true) == 0;
4051
}
4152

4253
public static int runCmd(String cmd, boolean isRoot, boolean isWait) {

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

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
<resources>
33
<string name="app_name">截屏瓷贴</string>
44
<string name="tile_label">截屏</string>
5+
<string name="dialog_obtain_root">获取Root权限失败</string>
6+
<string name="dialog_obtain_root_message">%s 需要Root权限用来发送截屏的模拟按键</string>
7+
<string name="dialog_reacquire">重新获取</string>
8+
<string name="dialog_i_know">我知道了</string>
9+
<string name="dialog_uninstall">卸载</string>
510
</resources>

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<resources>
22
<string name="app_name">Screenshot Tile</string>
33
<string name="tile_label">Screenshot</string>
4+
<string name="dialog_obtain_root">Failed to get root permission</string>
5+
<string name="dialog_obtain_root_message">%s requires root permission to send screenshots of the analog keys</string>
6+
<string name="dialog_reacquire">Reacquire</string>
7+
<string name="dialog_i_know">I Know</string>
8+
<string name="dialog_uninstall">Uninstall</string>
49
</resources>

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

+8
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@
55
<!-- Customize your theme here. -->
66
</style>
77

8+
<style name="AppTheme.Transparent">
9+
<item name="android:windowIsTranslucent">true</item>
10+
<item name="android:windowBackground">@android:color/transparent</item>
11+
<item name="android:windowNoTitle">true</item>
12+
<item name="android:windowContentOverlay">@null</item>
13+
<item name="android:windowAnimationStyle">@null</item>
14+
</style>
15+
816
</resources>

0 commit comments

Comments
 (0)