33import android .app .NotificationChannel ;
44import android .app .NotificationManager ;
55import android .content .ComponentName ;
6+ import android .content .Context ;
67import android .content .DialogInterface ;
78import android .content .Intent ;
89import android .content .SharedPreferences ;
1920import android .widget .Button ;
2021import android .widget .CompoundButton ;
2122import android .widget .EditText ;
22- import android .widget .Spinner ;
2323import android .widget .Switch ;
2424import android .widget .TextView ;
2525import android .widget .Toast ;
3131
3232import java .util .List ;
3333
34- //TODO 增加应用前台判断
35- public class MainActivity extends AppCompatActivity implements CompoundButton .OnCheckedChangeListener {
34+ public class MainActivity extends AppCompatActivity implements Switch .OnCheckedChangeListener {
3635 static SharedPreferences preferences ;
37- static Intent service ;
38- boolean isEnabled ;
36+ boolean isNotiListenerEnabled ;
37+ boolean isAccessEnabled ;
3938 Switch swh ;
39+ Switch hide ;
4040 EditText input ;
4141 TextView DeviceID ;
4242 Button priority ;
@@ -53,12 +53,27 @@ protected void onCreate(Bundle savedInstanceState) {
5353 setTheme (preferences .getInt ("style" , R .style .base_AppTheme_teal ));
5454 setContentView (R .layout .activity_main );
5555 swh = findViewById (R .id .switch1 );
56+ swh .setOnCheckedChangeListener (this );
5657
57- priority = findViewById (R .id .priority );
58- priority . setOnClickListener (new Button . OnClickListener () {
58+ hide = findViewById (R .id .switch2 );
59+ hide . setOnCheckedChangeListener (new Switch . OnCheckedChangeListener () {
5960 @ Override
60- public void onClick (View v ) {
61- startActivity (new Intent (getApplicationContext (), Application .class ));
61+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
62+ Intent intent = new Intent (Settings .ACTION_ACCESSIBILITY_SETTINGS );
63+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
64+ if (isChecked ) {
65+ if (isAccessEnabled ) {
66+ preferences .edit ().putBoolean ("hide" , true ).apply ();
67+ } else {
68+ startActivity (intent );
69+ preferences .edit ().putBoolean ("hide" , false ).apply ();
70+ hide .setChecked (false );
71+ }
72+ } else {
73+ if (isAccessEnabled )
74+ startActivity (intent );
75+ preferences .edit ().putBoolean ("hide" , false ).apply ();
76+ }
6277 }
6378 });
6479
@@ -77,6 +92,14 @@ public void onClick(View v) {
7792 }
7893 });
7994
95+ priority = findViewById (R .id .priority );
96+ priority .setOnClickListener (new Button .OnClickListener () {
97+ @ Override
98+ public void onClick (View v ) {
99+ startActivity (new Intent (getApplicationContext (), Application .class ));
100+ }
101+ });
102+
80103 colors = findViewById (R .id .colors );
81104 colors .setOnClickListener (new View .OnClickListener () {
82105 @ Override
@@ -146,7 +169,7 @@ public void onClick(DialogInterface dialog, int which) {
146169 normalDialog .show ();
147170 }
148171 });
149- swh . setOnCheckedChangeListener ( this );
172+
150173 FirebaseInstanceId .getInstance ().getInstanceId ()
151174 .addOnCompleteListener (new OnCompleteListener <InstanceIdResult >() {
152175 @ Override
@@ -158,7 +181,6 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
158181 DeviceID .setText (task .getResult ().getToken ());
159182 }
160183 });
161- startService (new Intent (this , FCMReceiver .class ));
162184 input .setText (preferences .getString ("ID" , "" ));
163185
164186 List <ApplicationInfo > packageInfo = this .getPackageManager ().getInstalledApplications (0 );
@@ -181,12 +203,19 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
181203 protected void onStart () {
182204 super .onStart ();
183205 if (isNotificationListenersEnabled ()) {
184- isEnabled = true ;
206+ isNotiListenerEnabled = true ;
185207 swh .setChecked (true );
186208 } else {
187- isEnabled = false ;
209+ isNotiListenerEnabled = false ;
188210 swh .setChecked (false );
189211 }
212+ if (isAccessibilitySettingsOn (this )) {
213+ isAccessEnabled = true ;
214+ hide .setChecked (true );
215+ } else {
216+ isAccessEnabled = false ;
217+ hide .setChecked (false );
218+ }
190219 preferences .edit ().putString ("ID" , input .getText ().toString ()).apply ();
191220 }
192221
@@ -196,10 +225,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
196225 intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
197226 if (isChecked ) {
198227 if (!input .getText ().toString ().trim ().isEmpty ()) {
199- if (isEnabled ) {
228+ if (isNotiListenerEnabled ) {
200229 preferences .edit ().putString ("ID" , input .getText ().toString ()).apply ();
201230 input .setEnabled (false );
202- startService (service = new Intent (this , GetNotification .class ));
203231 } else {
204232 startActivity (intent );
205233 input .setEnabled (true );
@@ -212,10 +240,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
212240 swh .setChecked (false );
213241 }
214242 } else {
215- if (isEnabled )
243+ if (isNotiListenerEnabled )
216244 startActivity (intent );
217245 input .setEnabled (true );
218- stopService (service );
219246 }
220247 }
221248
@@ -235,4 +262,34 @@ public boolean isNotificationListenersEnabled() {
235262 }
236263 return false ;
237264 }
265+
266+
267+ private boolean isAccessibilitySettingsOn (Context mContext ) {
268+ int accessibilityEnabled ;
269+ final String service = getPackageName () + "/" + ForegroundMonitor .class .getCanonicalName ();
270+ try {
271+ accessibilityEnabled = Settings .Secure .getInt (
272+ mContext .getApplicationContext ().getContentResolver (),
273+ android .provider .Settings .Secure .ACCESSIBILITY_ENABLED );
274+ } catch (Settings .SettingNotFoundException e ) {
275+ return false ;
276+ }
277+ TextUtils .SimpleStringSplitter mStringColonSplitter = new TextUtils .SimpleStringSplitter (':' );
278+
279+ if (accessibilityEnabled == 1 ) {
280+ String settingValue = Settings .Secure .getString (
281+ mContext .getApplicationContext ().getContentResolver (),
282+ Settings .Secure .ENABLED_ACCESSIBILITY_SERVICES );
283+ if (settingValue != null ) {
284+ mStringColonSplitter .setString (settingValue );
285+ while (mStringColonSplitter .hasNext ()) {
286+ String accessibilityService = mStringColonSplitter .next ();
287+ if (accessibilityService .equalsIgnoreCase (service )) {
288+ return true ;
289+ }
290+ }
291+ }
292+ }
293+ return false ;
294+ }
238295}
0 commit comments