3131import androidx .annotation .RequiresApi ;
3232import androidx .core .app .NotificationCompat ;
3333
34+ import org .apache .commons .io .FileUtils ;
35+
36+ import java .io .File ;
37+ import java .nio .charset .StandardCharsets ;
38+ import java .util .ArrayList ;
39+ import java .util .List ;
3440import java .util .Random ;
3541
42+ import mobile .Mobile ;
43+
3644/**
3745 * 保活服务.
3846 *
3947 * @author <a href="https://88250.b3log.org">Liang Ding</a>
40- * @version 1.0.1.2, Feb 7, 2024
48+ * @version 1.0.2.0, Jan 14, 2025
4149 * @since 1.0.0
4250 */
4351public class KeepLiveService extends Service {
@@ -61,23 +69,6 @@ public void onCreate() {
6169 }
6270 }
6371
64- private final String [] words = new String []{
65- "We are programmed to receive" ,
66- "Then the piper will lead us to reason" ,
67- "You're not the only one" ,
68- "Sometimes I need some time all alone" ,
69- "We still can find a way" ,
70- "You gotta make it your own way" ,
71- "Everybody needs somebody" ,
72- "原谅我这一生不羁放纵爱自由" ,
73- "我要再次找那旧日的足迹" ,
74- "心中一股冲劲勇闯,抛开那现实没有顾虑" ,
75- "愿望是努力走向那一方" ,
76- "其实怕被忘记至放大来演吧" ,
77- "荣耀的背后刻着一道孤独" ,
78- "动机也只有一种名字那叫做欲望" ,
79- };
80-
8172 private Random random = new Random ();
8273
8374 @ RequiresApi (api = Build .VERSION_CODES .O )
@@ -99,14 +90,61 @@ private void startMyOwnForeground() {
9990 final NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
10091 manager .createNotificationChannel (chan );
10192 final NotificationCompat .Builder notificationBuilder = new NotificationCompat .Builder (this , NOTIFICATION_CHANNEL_ID );
93+ final String [] texts = getNotificationTexts ();
10294 final Notification notification = notificationBuilder .setOngoing (true ).
10395 setSmallIcon (R .drawable .icon ).
104- setContentTitle (words [random .nextInt (words .length )]).
96+ setContentTitle (texts [random .nextInt (texts .length )]).
10597 setPriority (NotificationManager .IMPORTANCE_MIN ).
10698 setCategory (Notification .CATEGORY_SERVICE ).
10799 setContentIntent (resultPendingIntent ).
108100 build ();
109101 startForeground (2 , notification );
110102 }
103+
104+ private String [] getNotificationTexts () {
105+ final String workspacePath = Mobile .getCurrentWorkspacePath ();
106+ final String notificationTxtPath = workspacePath + "/data/assets/android-notification-texts.txt" ;
107+ try {
108+ final File notificationTxtFile = new File (notificationTxtPath );
109+ if (!notificationTxtFile .exists ()) {
110+ return words ;
111+ }
112+
113+ final List <String > tmp = FileUtils .readLines (notificationTxtFile , StandardCharsets .UTF_8 );
114+ final List <String > lines = new ArrayList <>();
115+ for (final String line : tmp ) {
116+ if (line .trim ().isEmpty ()) {
117+ continue ;
118+ }
119+ lines .add (line );
120+ }
121+ if (lines .isEmpty ()) {
122+ return words ;
123+ }
124+
125+ final String [] ret = new String [lines .size ()];
126+ return lines .toArray (ret );
127+ } catch (final Exception e ) {
128+ Utils .LogError ("boot" , "check version failed" , e );
129+ return words ;
130+ }
131+ }
132+
133+ private final String [] words = new String []{
134+ "We are programmed to receive" ,
135+ "Then the piper will lead us to reason" ,
136+ "You're not the only one" ,
137+ "Sometimes I need some time all alone" ,
138+ "We still can find a way" ,
139+ "You gotta make it your own way" ,
140+ "Everybody needs somebody" ,
141+ "原谅我这一生不羁放纵爱自由" ,
142+ "我要再次找那旧日的足迹" ,
143+ "心中一股冲劲勇闯,抛开那现实没有顾虑" ,
144+ "愿望是努力走向那一方" ,
145+ "其实怕被忘记至放大来演吧" ,
146+ "荣耀的背后刻着一道孤独" ,
147+ "动机也只有一种名字那叫做欲望" ,
148+ };
111149}
112150
0 commit comments