123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package me.yoqi.app.wxredpacket.activities;
- import android.accessibilityservice.AccessibilityServiceInfo;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.provider.Settings;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.accessibility.AccessibilityManager;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.tencent.bugly.Bugly;
- import java.util.List;
- import me.yoqi.app.wxredpacket.R;
- import me.yoqi.app.wxredpacket.utils.ConnectivityUtil;
- import me.yoqi.app.wxredpacket.utils.UpdateTask;
- public class MainActivity extends Activity implements AccessibilityManager.AccessibilityStateChangeListener {
- //开关切换按钮
- private TextView pluginStatusText;
- private ImageView pluginStatusIcon;
- private AccessibilityManager accessibilityManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Bugly.init(getApplicationContext(), "9410cbd743", false);
- setContentView(R.layout.activity_main);
- pluginStatusText = findViewById(R.id.layout_control_accessibility_text);
- pluginStatusIcon = findViewById(R.id.layout_control_accessibility_icon);
- handleMaterialStatusBar();
- explicitlyLoadPreferences();
- //监听AccessibilityService 变化
- accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
- accessibilityManager.addAccessibilityStateChangeListener(this);
- updateServiceStatus();
- }
- private void explicitlyLoadPreferences() {
- PreferenceManager.setDefaultValues(this, R.xml.general_preferences, false);
- }
- /**
- * 适配MIUI沉浸状态栏
- */
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- private void handleMaterialStatusBar() {
- // Not supported in APK level lower than 21
- if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
- Window window = this.getWindow();
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- window.setStatusBarColor(0xffE46C62);
- }
- @Override
- protected void onPause() {
- super.onPause();
- }
- @Override
- protected void onResume() {
- super.onResume();
- updateServiceStatus();
- // Check for update when WIFI is connected or on first time.
- if (ConnectivityUtil.isWifi(this) || UpdateTask.count == 0)
- new UpdateTask(this, false).update();
- }
- @Override
- protected void onDestroy() {
- //移除监听服务
- accessibilityManager.removeAccessibilityStateChangeListener(this);
- super.onDestroy();
- }
- public void openAccessibility(View view) {
- try {
- Toast.makeText(this, getString(R.string.turn_on_toast) + pluginStatusText.getText(), Toast.LENGTH_SHORT).show();
- Intent accessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
- startActivity(accessibleIntent);
- } catch (Exception e) {
- Toast.makeText(this, getString(R.string.turn_on_error_toast), Toast.LENGTH_LONG).show();
- e.printStackTrace();
- }
- }
- public void openGitHub(View view) {
- Intent webViewIntent = new Intent(this, WebViewActivity.class);
- webViewIntent.putExtra("title", getString(R.string.webview_github_title));
- webViewIntent.putExtra("url", "https://github.com/geeeeeeeeek/WeChatLuckyMoney");
- startActivity(webViewIntent);
- }
- public void openUber(View view) {
- Intent webViewIntent = new Intent(this, WebViewActivity.class);
- webViewIntent.putExtra("title", getString(R.string.webview_uber_title));
- String[] couponList = new String[]{"https://dc.tt/oTLtXH2BHsD", "https://dc.tt/ozFJHDnfLky"};
- int index = (int) (Math.random() * 2);
- webViewIntent.putExtra("url", couponList[index]);
- startActivity(webViewIntent);
- }
- public void openSettings(View view) {
- Intent settingsIntent = new Intent(this, SettingsActivity.class);
- settingsIntent.putExtra("title", getString(R.string.preference));
- settingsIntent.putExtra("frag_id", "GeneralSettingsFragment");
- startActivity(settingsIntent);
- }
- @Override
- public void onAccessibilityStateChanged(boolean enabled) {
- updateServiceStatus();
- }
- /**
- * 更新当前 HongbaoService 显示状态
- */
- private void updateServiceStatus() {
- if (isServiceEnabled()) {
- pluginStatusText.setText(R.string.service_off);
- pluginStatusIcon.setBackgroundResource(R.mipmap.ic_stop);
- } else {
- pluginStatusText.setText(R.string.service_on);
- pluginStatusIcon.setBackgroundResource(R.mipmap.ic_start);
- }
- }
- /**
- * 获取 HongbaoService 是否启用状态
- *
- * @return
- */
- private boolean isServiceEnabled() {
- List<AccessibilityServiceInfo> accessibilityServices =
- accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
- for (AccessibilityServiceInfo info : accessibilityServices) {
- if (info.getId().equals(getPackageName() + "/.services.HongbaoService")) {
- return true;
- }
- }
- return false;
- }
- }
|