MainActivity.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package me.yoqi.app.wxredpacket.activities;
  2. import android.accessibilityservice.AccessibilityServiceInfo;
  3. import android.annotation.TargetApi;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.preference.PreferenceManager;
  10. import android.provider.Settings;
  11. import android.view.View;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.view.accessibility.AccessibilityManager;
  15. import android.widget.ImageView;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import com.tencent.bugly.Bugly;
  19. import java.util.List;
  20. import me.yoqi.app.wxredpacket.R;
  21. import me.yoqi.app.wxredpacket.utils.ConnectivityUtil;
  22. import me.yoqi.app.wxredpacket.utils.UpdateTask;
  23. public class MainActivity extends Activity implements AccessibilityManager.AccessibilityStateChangeListener {
  24. //开关切换按钮
  25. private TextView pluginStatusText;
  26. private ImageView pluginStatusIcon;
  27. private AccessibilityManager accessibilityManager;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. Bugly.init(getApplicationContext(), "9410cbd743", false);
  32. setContentView(R.layout.activity_main);
  33. pluginStatusText = findViewById(R.id.layout_control_accessibility_text);
  34. pluginStatusIcon = findViewById(R.id.layout_control_accessibility_icon);
  35. handleMaterialStatusBar();
  36. explicitlyLoadPreferences();
  37. //监听AccessibilityService 变化
  38. accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
  39. accessibilityManager.addAccessibilityStateChangeListener(this);
  40. updateServiceStatus();
  41. }
  42. private void explicitlyLoadPreferences() {
  43. PreferenceManager.setDefaultValues(this, R.xml.general_preferences, false);
  44. }
  45. /**
  46. * 适配MIUI沉浸状态栏
  47. */
  48. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  49. private void handleMaterialStatusBar() {
  50. // Not supported in APK level lower than 21
  51. if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
  52. Window window = this.getWindow();
  53. window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  54. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  55. window.setStatusBarColor(0xffE46C62);
  56. }
  57. @Override
  58. protected void onPause() {
  59. super.onPause();
  60. }
  61. @Override
  62. protected void onResume() {
  63. super.onResume();
  64. updateServiceStatus();
  65. // Check for update when WIFI is connected or on first time.
  66. if (ConnectivityUtil.isWifi(this) || UpdateTask.count == 0)
  67. new UpdateTask(this, false).update();
  68. }
  69. @Override
  70. protected void onDestroy() {
  71. //移除监听服务
  72. accessibilityManager.removeAccessibilityStateChangeListener(this);
  73. super.onDestroy();
  74. }
  75. public void openAccessibility(View view) {
  76. try {
  77. Toast.makeText(this, getString(R.string.turn_on_toast) + pluginStatusText.getText(), Toast.LENGTH_SHORT).show();
  78. Intent accessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
  79. startActivity(accessibleIntent);
  80. } catch (Exception e) {
  81. Toast.makeText(this, getString(R.string.turn_on_error_toast), Toast.LENGTH_LONG).show();
  82. e.printStackTrace();
  83. }
  84. }
  85. public void openGitHub(View view) {
  86. Intent webViewIntent = new Intent(this, WebViewActivity.class);
  87. webViewIntent.putExtra("title", getString(R.string.webview_github_title));
  88. webViewIntent.putExtra("url", "https://github.com/geeeeeeeeek/WeChatLuckyMoney");
  89. startActivity(webViewIntent);
  90. }
  91. public void openUber(View view) {
  92. Intent webViewIntent = new Intent(this, WebViewActivity.class);
  93. webViewIntent.putExtra("title", getString(R.string.webview_uber_title));
  94. String[] couponList = new String[]{"https://dc.tt/oTLtXH2BHsD", "https://dc.tt/ozFJHDnfLky"};
  95. int index = (int) (Math.random() * 2);
  96. webViewIntent.putExtra("url", couponList[index]);
  97. startActivity(webViewIntent);
  98. }
  99. public void openSettings(View view) {
  100. Intent settingsIntent = new Intent(this, SettingsActivity.class);
  101. settingsIntent.putExtra("title", getString(R.string.preference));
  102. settingsIntent.putExtra("frag_id", "GeneralSettingsFragment");
  103. startActivity(settingsIntent);
  104. }
  105. @Override
  106. public void onAccessibilityStateChanged(boolean enabled) {
  107. updateServiceStatus();
  108. }
  109. /**
  110. * 更新当前 HongbaoService 显示状态
  111. */
  112. private void updateServiceStatus() {
  113. if (isServiceEnabled()) {
  114. pluginStatusText.setText(R.string.service_off);
  115. pluginStatusIcon.setBackgroundResource(R.mipmap.ic_stop);
  116. } else {
  117. pluginStatusText.setText(R.string.service_on);
  118. pluginStatusIcon.setBackgroundResource(R.mipmap.ic_start);
  119. }
  120. }
  121. /**
  122. * 获取 HongbaoService 是否启用状态
  123. *
  124. * @return
  125. */
  126. private boolean isServiceEnabled() {
  127. List<AccessibilityServiceInfo> accessibilityServices =
  128. accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
  129. for (AccessibilityServiceInfo info : accessibilityServices) {
  130. if (info.getId().equals(getPackageName() + "/.services.HongbaoService")) {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. }