| | |
| | | package com.auto.lyric; |
| | | |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import android.content.Intent; |
| | | import android.net.Uri; |
| | | import android.os.Build; |
| | | import android.provider.Settings; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import android.os.Bundle; |
| | | import com.auto.lyric.base.activities.BaseActivity; |
| | | import com.auto.lyric.databinding.ActivityMainBinding; |
| | | import com.auto.lyric.service.AutoInputService; |
| | | import com.auto.lyric.vm.MainViewModel; |
| | | |
| | | public class MainActivity extends AppCompatActivity { |
| | | public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> { |
| | | |
| | | Intent service = new Intent(getApplicationContext(), AutoInputService.class); |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.activity_main); |
| | | public void initViews() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | protected void onResume() { |
| | | super.onResume(); |
| | | |
| | | //申请权限 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| | | //开启悬浮窗 |
| | | if(!Settings.canDrawOverlays(getApplicationContext())) { |
| | | //启动Activity让用户授权 |
| | | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
| | | intent.setData(Uri.parse("package:" + getPackageName())); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else if(!isAccessibilitySettingsOn()){//辅助权限 |
| | | Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else{//开启监听服务 |
| | | startService(service); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public boolean isAccessibilitySettingsOn() { |
| | | int accessibilityEnabled = 0; |
| | | final String service = getPackageName() + "/" + AutoInputService.class.getCanonicalName(); |
| | | try { |
| | | accessibilityEnabled = Settings.Secure.getInt( |
| | | this.getApplicationContext().getContentResolver(), |
| | | android.provider.Settings.Secure.ACCESSIBILITY_ENABLED); |
| | | Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled); |
| | | } catch (Settings.SettingNotFoundException e) { |
| | | Log.e(TAG, "Error finding setting, default accessibility to not found: " |
| | | + e.getMessage()); |
| | | } |
| | | TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':'); |
| | | |
| | | if (accessibilityEnabled == 1) { |
| | | Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------"); |
| | | String settingValue = Settings.Secure.getString( |
| | | getApplicationContext().getContentResolver(), |
| | | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); |
| | | if (settingValue != null) { |
| | | mStringColonSplitter.setString(settingValue); |
| | | while (mStringColonSplitter.hasNext()) { |
| | | String accessibilityService = mStringColonSplitter.next(); |
| | | |
| | | Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service); |
| | | if (accessibilityService.equalsIgnoreCase(service)) { |
| | | Log.v(TAG, "We've found the correct setting - accessibility is switched on!"); |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | Log.v(TAG, "***ACCESSIBILITY IS DISABLED***"); |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | } |