Automation/app/src/main/java/com/jens/automation2/ActivityManageActionPlaySound.java

111 lines
3.7 KiB
Java
Raw Normal View History

2021-03-28 20:33:44 +02:00
package com.jens.automation2;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
2021-03-29 16:36:21 +02:00
import android.widget.EditText;
2021-03-28 20:33:44 +02:00
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
2021-03-29 16:36:21 +02:00
import java.io.File;
2021-03-28 20:33:44 +02:00
public class ActivityManageActionPlaySound extends Activity
{
final static int PICKFILE_RESULT_CODE = 4711;
CheckBox chkPlaySoundAlwaysPlay;
2021-03-29 16:36:21 +02:00
EditText etSelectedSoundFile;
2021-03-28 20:33:44 +02:00
Button bSelectSoundFile, bSavePlaySound;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_play_sound);
chkPlaySoundAlwaysPlay = (CheckBox)findViewById(R.id.chkPlaySoundAlwaysPlay);
2021-03-29 16:36:21 +02:00
etSelectedSoundFile = (EditText)findViewById(R.id.etSelectedSoundFile);
2021-03-28 20:33:44 +02:00
bSelectSoundFile = (Button)findViewById(R.id.bSelectSoundFile);
bSavePlaySound = (Button)findViewById(R.id.bSavePlaySound);
boolean edit = getIntent().getBooleanExtra("edit", false);
if(edit)
{
boolean param1 = getIntent().getBooleanExtra("actionParameter1", false);
String param2 = getIntent().getStringExtra("actionParameter2");
chkPlaySoundAlwaysPlay.setChecked(param1);
2021-03-29 16:36:21 +02:00
etSelectedSoundFile.setText(param2);
2021-03-28 20:33:44 +02:00
}
bSelectSoundFile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Need to check for storage permissions
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
chooseFile = Intent.createChooser(chooseFile, getResources().getString(R.string.selectSoundFile));
startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
}
});
bSavePlaySound.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
savePlaySoundSettings();
}
});
}
void savePlaySoundSettings()
{
2021-03-29 16:36:21 +02:00
if(etSelectedSoundFile.getText().toString() == null || etSelectedSoundFile.getText().toString().length() == 0)
2021-03-28 20:33:44 +02:00
{
Toast.makeText(ActivityManageActionPlaySound.this, getResources().getString(R.string.selectSoundFile), Toast.LENGTH_LONG).show();
return;
}
2021-03-29 16:36:21 +02:00
else
{
File soundFile = new File(etSelectedSoundFile.getText().toString());
if(!soundFile.exists())
{
Toast.makeText(ActivityManageActionPlaySound.this, getResources().getString(R.string.fileDoesNotExist), Toast.LENGTH_LONG).show();
return;
}
}
2021-03-28 20:33:44 +02:00
Intent returnData = new Intent();
returnData.putExtra("actionParameter1", chkPlaySoundAlwaysPlay.isChecked());
2021-03-29 16:36:21 +02:00
returnData.putExtra("actionParameter2", etSelectedSoundFile.getText().toString());
2021-03-28 20:33:44 +02:00
setResult(RESULT_OK, returnData);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
if(requestCode == PICKFILE_RESULT_CODE)
{
Uri fileUri = data.getData();
2021-03-29 16:36:21 +02:00
String filePath = CompensateCrappyAndroidPaths.getPath(ActivityManageActionPlaySound.this, fileUri);
etSelectedSoundFile.setText(filePath);
2021-03-28 20:33:44 +02:00
}
}
}
}