File indexing completed on 2024-12-22 04:41:38
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com> 0003 * SPDX-FileCopyrightText: 2020 Sylvia van Os <sylvia@hackerchick.me> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 package org.kde.kdeconnect.Plugins.BigscreenPlugin; 0009 0010 import android.Manifest; 0011 import android.app.Activity; 0012 import android.content.Intent; 0013 import android.os.Bundle; 0014 import android.speech.RecognizerIntent; 0015 import android.speech.SpeechRecognizer; 0016 import android.view.View; 0017 0018 import androidx.appcompat.app.AppCompatActivity; 0019 0020 import org.kde.kdeconnect.KdeConnect; 0021 import org.kde.kdeconnect.UserInterface.MainActivity; 0022 import org.kde.kdeconnect.UserInterface.PermissionsAlertDialogFragment; 0023 import org.kde.kdeconnect_tp.R; 0024 import org.kde.kdeconnect_tp.databinding.ActivityBigscreenBinding; 0025 0026 import java.util.ArrayList; 0027 import java.util.Objects; 0028 0029 public class BigscreenActivity extends AppCompatActivity { 0030 0031 private static final int REQUEST_SPEECH = 100; 0032 0033 @Override 0034 protected void onCreate(Bundle savedInstanceState) { 0035 super.onCreate(savedInstanceState); 0036 0037 final ActivityBigscreenBinding binding = ActivityBigscreenBinding.inflate(getLayoutInflater()); 0038 setContentView(binding.getRoot()); 0039 0040 setSupportActionBar(binding.toolbarLayout.toolbar); 0041 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); 0042 getSupportActionBar().setDisplayShowHomeEnabled(true); 0043 0044 final String deviceId = getIntent().getStringExtra("deviceId"); 0045 0046 if (!SpeechRecognizer.isRecognitionAvailable(this)) { 0047 binding.micButton.setEnabled(false); 0048 binding.micButton.setVisibility(View.INVISIBLE); 0049 } 0050 0051 BigscreenPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, BigscreenPlugin.class); 0052 if (plugin == null) { 0053 finish(); 0054 return; 0055 } 0056 0057 binding.leftButton.setOnClickListener(v -> plugin.sendLeft()); 0058 binding.rightButton.setOnClickListener(v -> plugin.sendRight()); 0059 binding.upButton.setOnClickListener(v -> plugin.sendUp()); 0060 binding.downButton.setOnClickListener(v -> plugin.sendDown()); 0061 binding.selectButton.setOnClickListener(v -> plugin.sendSelect()); 0062 binding.homeButton.setOnClickListener(v -> plugin.sendHome()); 0063 binding.micButton.setOnClickListener(v -> { 0064 if (plugin.hasMicPermission()) { 0065 activateSTT(); 0066 } else { 0067 new PermissionsAlertDialogFragment.Builder() 0068 .setTitle(plugin.getDisplayName()) 0069 .setMessage(R.string.bigscreen_optional_permission_explanation) 0070 .setPermissions(new String[]{Manifest.permission.RECORD_AUDIO}) 0071 .setRequestCode(MainActivity.RESULT_NEEDS_RELOAD) 0072 .create().show(getSupportFragmentManager(), null); 0073 } 0074 }); 0075 } 0076 0077 public void activateSTT() { 0078 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 0079 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 0080 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, R.string.bigscreen_speech_extra_prompt); 0081 startActivityForResult(intent, REQUEST_SPEECH); 0082 } 0083 0084 @Override 0085 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 0086 super.onActivityResult(requestCode, resultCode, data); 0087 if (requestCode == REQUEST_SPEECH) { 0088 if (resultCode == Activity.RESULT_OK && data != null) { 0089 ArrayList<String> result = data 0090 .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 0091 if (result.get(0) != null) { 0092 final String deviceId = getIntent().getStringExtra("deviceId"); 0093 BigscreenPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, BigscreenPlugin.class); 0094 if (plugin == null) { 0095 finish(); 0096 return; 0097 } 0098 plugin.sendSTT(result.get(0)); 0099 } 0100 } 0101 } 0102 } 0103 0104 @Override 0105 public boolean onSupportNavigateUp() { 0106 super.onBackPressed(); 0107 return true; 0108 } 0109 } 0110