Skip to content

Commit 8a6087d

Browse files
committed
add autostart feature
1 parent 383f76d commit 8a6087d

File tree

3 files changed

+167
-10
lines changed

3 files changed

+167
-10
lines changed

app/src/main/java/io/github/ratul/topactivity/ui/MainActivity.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static com.android.volley.Request.Method.GET;
2323
import static java.lang.Integer.parseInt;
2424
import static io.github.ratul.topactivity.App.showToast;
25+
import static io.github.ratul.topactivity.utils.AutostartUtil.isAutoStartPermissionAvailable;
26+
import static io.github.ratul.topactivity.utils.AutostartUtil.requestAutoStartPermission;
2527

2628
import android.app.AppOpsManager;
2729
import android.content.BroadcastReceiver;
@@ -30,7 +32,6 @@
3032
import android.content.Intent;
3133
import android.content.IntentFilter;
3234
import android.content.ServiceConnection;
33-
import android.graphics.Insets;
3435
import android.net.Uri;
3536
import android.os.Build;
3637
import android.os.Bundle;
@@ -45,14 +46,19 @@
4546
import android.view.WindowMetrics;
4647
import android.widget.Button;
4748
import android.widget.EditText;
49+
import android.widget.LinearLayout;
4850
import android.widget.TextView;
4951

52+
import androidx.activity.EdgeToEdge;
5053
import androidx.activity.result.ActivityResultLauncher;
5154
import androidx.activity.result.contract.ActivityResultContracts;
5255
import androidx.appcompat.app.AlertDialog;
5356
import androidx.appcompat.app.AppCompatActivity;
5457
import androidx.appcompat.widget.SwitchCompat;
5558
import androidx.core.content.ContextCompat;
59+
import androidx.core.graphics.Insets;
60+
import androidx.core.view.ViewCompat;
61+
import androidx.core.view.WindowInsetsCompat;
5662

5763
import com.android.volley.toolbox.JsonObjectRequest;
5864
import com.android.volley.toolbox.Volley;
@@ -64,6 +70,7 @@
6470
import io.github.ratul.topactivity.receivers.NotificationReceiver;
6571
import io.github.ratul.topactivity.services.AccessibilityMonitoringService;
6672
import io.github.ratul.topactivity.services.PackageMonitoringService;
73+
import io.github.ratul.topactivity.utils.AutostartUtil;
6774
import io.github.ratul.topactivity.utils.DatabaseUtil;
6875
import io.github.ratul.topactivity.utils.WindowUtil;
6976

@@ -108,7 +115,14 @@ public void onReceive(Context context, Intent intent) {
108115
@Override
109116
protected void onCreate(Bundle savedInstanceState) {
110117
super.onCreate(savedInstanceState);
118+
EdgeToEdge.enable(this);
111119
setContentView(R.layout.activity_main);
120+
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
121+
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
122+
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
123+
return insets;
124+
});
125+
112126
checkForUpdate(true);
113127
startAccessibilityService();
114128
DatabaseUtil.setDisplayWidth(getScreenWidth());
@@ -124,6 +138,10 @@ protected void onCreate(Bundle savedInstanceState) {
124138
Button downloadAccessibility = findViewById(R.id.download_accessibility);
125139
Button configureWidth = findViewById(R.id.configure_width);
126140

141+
LinearLayout autostartLayout = findViewById(R.id.autostart_layout);
142+
TextView autostartDivider = findViewById(R.id.autostart_divider);
143+
Button allowAutostart = findViewById(R.id.allow_autostart);
144+
127145
updateReceiver = new UpdateSwitchReceiver();
128146
ContextCompat.registerReceiver(this, updateReceiver,
129147
new IntentFilter(ACTION_STATE_CHANGED), ContextCompat.RECEIVER_EXPORTED);
@@ -176,6 +194,15 @@ protected void onCreate(Bundle savedInstanceState) {
176194

177195
configureWidth.setOnClickListener(v -> configureWidth());
178196

197+
if (isAutoStartPermissionAvailable(this)) {
198+
autostartLayout.setVisibility(View.VISIBLE);
199+
autostartDivider.setVisibility(View.VISIBLE);
200+
allowAutostart.setOnClickListener(v -> requestAutoStartPermission(this));
201+
} else {
202+
autostartLayout.setVisibility(View.GONE);
203+
autostartDivider.setVisibility(View.GONE);
204+
}
205+
179206
if (handleQsTileIntent(getIntent())) {
180207
moveTaskToBack(true);
181208
}
@@ -224,7 +251,8 @@ protected void onDestroy() {
224251
private int getScreenWidth() {
225252
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
226253
WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
227-
Insets insets = windowMetrics.getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
254+
android.graphics.Insets insets = windowMetrics.getWindowInsets()
255+
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
228256
return windowMetrics.getBounds().width() - insets.left - insets.right;
229257
} else {
230258
DisplayMetrics displayMetrics = new DisplayMetrics();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.github.ratul.topactivity.utils;
2+
3+
import static io.github.ratul.topactivity.App.showToast;
4+
import static io.github.ratul.topactivity.utils.NullSafety.isNullOrEmpty;
5+
6+
import android.content.ComponentName;
7+
import android.content.Context;
8+
import android.content.Intent;
9+
import android.content.pm.PackageManager;
10+
import android.os.Build;
11+
12+
public class AutostartUtil {
13+
private static final ComponentName[] AUTOSTART_COMPONENTS = {
14+
// Xiaomi
15+
new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"),
16+
// LeTV
17+
new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"),
18+
// Huawei, Honor
19+
new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity"),
20+
new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity"),
21+
// Oppo
22+
new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"),
23+
new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity"),
24+
new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity"),
25+
// Vivo
26+
new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"),
27+
new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager"),
28+
new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"),
29+
// Samsung
30+
new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity"),
31+
// HTC
32+
new ComponentName("com.htc.pitroad", "com.htc.pitroad.landingpage.activity.LandingPageActivity"),
33+
// Asus
34+
new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.MainActivity"),
35+
};
36+
37+
public static boolean isAutoStartPermissionAvailable(Context context) {
38+
PackageManager packageManager = context.getPackageManager();
39+
40+
for (ComponentName component : AUTOSTART_COMPONENTS) {
41+
Intent intent = new Intent()
42+
.setComponent(component)
43+
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
44+
45+
if (packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
46+
return true;
47+
}
48+
}
49+
50+
return !isNullOrEmpty(Build.BRAND) && Build.BRAND.equalsIgnoreCase("LG");
51+
}
52+
53+
public static void requestAutoStartPermission(Context context) {
54+
PackageManager packageManager = context.getPackageManager();
55+
56+
for (ComponentName component : AUTOSTART_COMPONENTS) {
57+
Intent intent = new Intent()
58+
.setComponent(component)
59+
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60+
61+
if (packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
62+
context.startActivity(intent);
63+
return;
64+
}
65+
}
66+
67+
if (!isNullOrEmpty(Build.BRAND) && Build.BRAND.equalsIgnoreCase("LG")) {
68+
LG(context);
69+
}
70+
}
71+
72+
private static void LG(Context context) {
73+
try {
74+
Intent intent = new Intent(Intent.ACTION_MAIN)
75+
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
76+
.putExtra("packageName", context.getPackageName())
77+
.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity"));
78+
context.startActivity(intent);
79+
} catch (Throwable ignored) {
80+
showToast(context, "Failed to open Autostart");
81+
}
82+
}
83+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
android:id="@+id/main"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:gravity="center_horizontal"
65
android:orientation="vertical">
76

87
<LinearLayout
@@ -18,7 +17,7 @@
1817
android:layout_weight="1"
1918
android:text="Show Floating Window"
2019
android:textColor="?editTextColor"
21-
android:textSize="16sp" />
20+
android:textSize="@dimen/title_size" />
2221

2322
<androidx.appcompat.widget.SwitchCompat
2423
android:id="@+id/show_window"
@@ -52,13 +51,13 @@
5251
android:layout_marginBottom="4dp"
5352
android:text="Use Notification"
5453
android:textColor="?editTextColor"
55-
android:textSize="16sp" />
54+
android:textSize="@dimen/title_size" />
5655

5756
<TextView
5857
android:layout_width="match_parent"
5958
android:layout_height="wrap_content"
6059
android:text="Show a notification to observe activity info"
61-
android:textSize="12sp" />
60+
android:textSize="@dimen/subtitle_size" />
6261
</LinearLayout>
6362

6463
<androidx.appcompat.widget.SwitchCompat
@@ -93,13 +92,13 @@
9392
android:layout_marginBottom="4dp"
9493
android:text="Use Accessibility Service"
9594
android:textColor="?editTextColor"
96-
android:textSize="16sp" />
95+
android:textSize="@dimen/title_size" />
9796

9897
<TextView
9998
android:layout_width="match_parent"
10099
android:layout_height="wrap_content"
101100
android:text="Improve package monitoring significantly"
102-
android:textSize="12sp" />
101+
android:textSize="@dimen/subtitle_size" />
103102
</LinearLayout>
104103

105104
<androidx.appcompat.widget.SwitchCompat
@@ -117,6 +116,53 @@
117116
android:visibility="@integer/show_global_link" />
118117
</LinearLayout>
119118

119+
<TextView
120+
android:id="@+id/autostart_divider"
121+
android:layout_width="match_parent"
122+
android:layout_height="1dp"
123+
android:background="?actionBarDivider"
124+
android:visibility="@integer/use_accessibility" />
125+
126+
<LinearLayout
127+
android:id="@+id/autostart_layout"
128+
android:layout_width="match_parent"
129+
android:layout_height="wrap_content"
130+
android:layout_margin="8dp"
131+
android:gravity="center"
132+
android:minHeight="48dp"
133+
android:padding="8dp"
134+
android:visibility="@integer/use_accessibility">
135+
136+
<LinearLayout
137+
android:layout_width="0dp"
138+
android:layout_height="wrap_content"
139+
android:layout_marginEnd="8dp"
140+
android:layout_weight="1"
141+
android:orientation="vertical">
142+
143+
<TextView
144+
android:layout_width="match_parent"
145+
android:layout_height="wrap_content"
146+
android:layout_marginBottom="4dp"
147+
android:text="Use Autostart"
148+
android:textColor="?editTextColor"
149+
android:textSize="@dimen/title_size" />
150+
151+
<TextView
152+
android:layout_width="match_parent"
153+
android:layout_height="wrap_content"
154+
android:text="Fix accessibility service problems"
155+
android:textSize="@dimen/subtitle_size" />
156+
</LinearLayout>
157+
158+
<Button
159+
android:id="@+id/allow_autostart"
160+
style="@style/Button"
161+
android:layout_width="wrap_content"
162+
android:layout_height="wrap_content"
163+
android:text="Enable" />
164+
</LinearLayout>
165+
120166
<TextView
121167
android:layout_width="match_parent"
122168
android:layout_height="1dp"
@@ -143,13 +189,13 @@
143189
android:layout_marginBottom="4dp"
144190
android:text="Set Window Size"
145191
android:textColor="?editTextColor"
146-
android:textSize="16sp" />
192+
android:textSize="@dimen/title_size" />
147193

148194
<TextView
149195
android:layout_width="match_parent"
150196
android:layout_height="wrap_content"
151197
android:text="Change the width of popup window"
152-
android:textSize="12sp" />
198+
android:textSize="@dimen/subtitle_size" />
153199
</LinearLayout>
154200

155201
<Button

0 commit comments

Comments
 (0)