WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d34c5a5

Browse files
committed
通知常駐機能を追加
1 parent 0ede63f commit d34c5a5

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1010
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
1111
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
12+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
1213
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1314
<uses-permission android:name="android.permission.INTERNET" />
1415
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
@@ -220,6 +221,10 @@
220221
android:name=".data.service.PermissionIntentService"
221222
android:exported="false" />
222223

224+
<service
225+
android:name=".data.service.AlwaysNotiService"
226+
android:exported="false" />
227+
223228
<receiver
224229
android:name=".data.receiver.PackageReceiver"
225230
android:exported="false">
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.saradabar.cpadcustomizetool.data.service;
2+
3+
import android.app.Notification;
4+
import android.app.Service;
5+
import android.content.Intent;
6+
import android.os.IBinder;
7+
import android.widget.RemoteViews;
8+
9+
import com.saradabar.cpadcustomizetool.R;
10+
11+
public class AlwaysNotiService extends Service {
12+
13+
private static final int NOTIFICATION_ID = 1000;
14+
15+
/** @noinspection SameParameterValue*/
16+
private void startForeground(int id) {
17+
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
18+
Notification.Builder builder = new Notification.Builder(getApplicationContext());
19+
builder.setContent(views);
20+
builder.setContentText("");
21+
builder.setContentTitle("");
22+
builder.setTicker("");
23+
builder.setShowWhen(false);
24+
builder.setSmallIcon(R.drawable.ic_stat_transparent);
25+
builder.setPriority(Notification.PRIORITY_MIN);
26+
builder.setAutoCancel(false);
27+
Notification noti = builder.build();
28+
noti.flags |= Notification.FLAG_NO_CLEAR;
29+
startForeground(id, noti);
30+
}
31+
32+
@Override
33+
public int onStartCommand(Intent intent, int flags, int startId) {
34+
startForeground(NOTIFICATION_ID);
35+
return START_STICKY;
36+
}
37+
38+
@Override
39+
public void onDestroy() {
40+
stopForeground(true);
41+
}
42+
43+
@Override
44+
public IBinder onBind(Intent intent) {
45+
return null;
46+
}
47+
}

app/src/main/java/com/saradabar/cpadcustomizetool/view/flagment/AppSettingsFragment.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.widget.Toast;
3030

3131
import androidx.appcompat.app.AlertDialog;
32+
import androidx.core.content.ContextCompat;
3233
import androidx.preference.Preference;
3334
import androidx.preference.PreferenceCategory;
3435
import androidx.preference.PreferenceFragmentCompat;
@@ -37,6 +38,7 @@
3738
import com.rosan.dhizuku.shared.DhizukuVariables;
3839
import com.saradabar.cpadcustomizetool.BuildConfig;
3940
import com.saradabar.cpadcustomizetool.R;
41+
import com.saradabar.cpadcustomizetool.data.service.AlwaysNotiService;
4042
import com.saradabar.cpadcustomizetool.util.Common;
4143
import com.saradabar.cpadcustomizetool.util.Constants;
4244
import com.saradabar.cpadcustomizetool.util.Preferences;
@@ -52,6 +54,7 @@ public class AppSettingsFragment extends PreferenceFragmentCompat {
5254
PreferenceCategory catDebugRestriction;
5355

5456
SwitchPreferenceCompat swUpdateCheck,
57+
swNotiAlways,
5558
swUseDcha,
5659
swDebugRestriction;
5760

@@ -72,6 +75,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
7275
addPreferencesFromResource(R.xml.pre_app);
7376

7477
swUpdateCheck = findPreference("pre_app_update_check");
78+
swNotiAlways = findPreference("pre_app_noti_always");
7579
swUseDcha = findPreference("pre_app_use_dcha");
7680
preCrashLog = findPreference("pre_app_crash_log");
7781
preDelCrashLog = findPreference("pre_app_del_crash_log");
@@ -87,6 +91,15 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
8791
return true;
8892
});
8993

94+
swNotiAlways.setOnPreferenceChangeListener((preference, newValue) -> {
95+
Intent notiService = new Intent(requireContext(), AlwaysNotiService.class);
96+
if ((boolean) newValue)
97+
ContextCompat.startForegroundService(requireContext(), notiService);
98+
else
99+
requireContext().stopService(notiService);
100+
return true;
101+
});
102+
90103
swUseDcha.setOnPreferenceChangeListener((preference, newValue) -> {
91104
Preferences.save(requireActivity(), Constants.KEY_FLAG_APP_SETTING_DCHA, (boolean) newValue);
92105
return true;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<color xmlns:android="http://schemas.android.com/apk/res/android" android:color="#00000000"/>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:background="#00000000"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"/>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@
127127

128128
<!-- アプリ設定 -->
129129
<string name="pre_app_category_start_settings">起動設定</string>
130+
<string name="pre_app_category_noti">通知設定</string>
130131
<string name="pre_app_category_settings">アプリ設定</string>
131132
<string name="pre_app_category_update">インストール設定</string>
132133
<string name="pre_app_category_log">アプリログ</string>
133134
<string name="pre_app_title_update_check">アップデートチェックを無効</string>
135+
<string name="pre_app_title_noti_always">通知を常駐</string>
134136
<string name="pre_app_title_dcha">内部設定の変更に DchaService を使用</string>
135137
<string name="pre_app_title_usb">デバイス起動時の USB デバッグ</string>
136138
<string name="pre_app_title_update">インストールモード</string>

app/src/main/res/xml/pre_app.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
android:title="@string/pre_app_title_delete_crash_history" />
5454
</PreferenceCategory>
5555

56+
<PreferenceCategory
57+
android:title="@string/pre_app_category_noti">
58+
59+
<SwitchPreferenceCompat
60+
android:key="pre_app_noti_always"
61+
android:summary="バックグラウンドでの自動タスクキルを防ぎます。"
62+
android:title="@string/pre_app_title_noti_always" />
63+
</PreferenceCategory>
64+
5665
<PreferenceCategory
5766
android:key="pre_app_category_debug"
5867
android:title="デバッグ"

0 commit comments

Comments
 (0)