Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
applicationId 'ee.ioc.phon.android.speak'
minSdkVersion 24
targetSdkVersion 34
versionCode 1907
versionName '1.9.07'
versionCode 1909
versionName '1.9.09'
vectorDrawables.useSupportLibrary = true
// Keep only en and et resources
resConfigs 'en', 'et'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
Expand All @@ -36,47 +38,45 @@

public class PreferencesRecognitionServiceHttp extends AppCompatActivity {

private static final int ACTIVITY_SELECT_SERVER_URL = 1;
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
Uri serverUri = intent.getData();
if (serverUri == null) {
toast(getString(R.string.errorFailedGetServerUrl));
} else {
long id = Long.parseLong(serverUri.getPathSegments().get(1));
String url = Utils.idToValue(this, Server.Columns.CONTENT_URI, Server.Columns._ID, Server.Columns.URL, id);
if (url != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
PreferenceUtils.putPrefString(prefs, getResources(), R.string.keyHttpServer, url);
}
}
}
});

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.replace(android.R.id.content, new SettingsFragment(mLauncher))
.commit();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case ACTIVITY_SELECT_SERVER_URL:
Uri serverUri = data.getData();
if (serverUri == null) {
toast(getString(R.string.errorFailedGetServerUrl));
} else {
long id = Long.parseLong(serverUri.getPathSegments().get(1));
String url = Utils.idToValue(this, Server.Columns.CONTENT_URI, Server.Columns._ID, Server.Columns.URL, id);
if (url != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
PreferenceUtils.putPrefString(prefs, getResources(), R.string.keyHttpServer, url);
}
}
break;
default:
break;
}
}

private void toast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}


public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

private final ActivityResultLauncher<Intent> mLauncher;

public SettingsFragment(ActivityResultLauncher<Intent> launcher) {
mLauncher = launcher;
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences_server_http, rootKey);
Expand Down Expand Up @@ -110,7 +110,7 @@ public void onResume() {
service.setSummary(sp.getString(getString(R.string.keyHttpServer), getString(R.string.defaultHttpServer)));

service.setOnPreferenceClickListener(preference -> {
getActivity().startActivityForResult(preference.getIntent(), ACTIVITY_SELECT_SERVER_URL);
mLauncher.launch(preference.getIntent());
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
Expand All @@ -34,43 +36,41 @@

public class PreferencesRecognitionServiceWs extends AppCompatActivity {

private static final int ACTIVITY_SELECT_SERVER_URL = 1;
private final ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
Uri serverUri = intent.getData();

if (serverUri == null) {
toast(getString(R.string.errorFailedGetServerUrl));
} else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
PreferenceUtils.putPrefString(prefs, getResources(), R.string.keyWsServer, serverUri.toString());
}
}
});

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.replace(android.R.id.content, new SettingsFragment(mLauncher))
.commit();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case ACTIVITY_SELECT_SERVER_URL:
Uri serverUri = data.getData();
if (serverUri == null) {
toast(getString(R.string.errorFailedGetServerUrl));
} else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
PreferenceUtils.putPrefString(prefs, getResources(), R.string.keyWsServer, serverUri.toString());
}
break;
default:
break;
}
}

private void toast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}

public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

private final ActivityResultLauncher<Intent> mLauncher;

public SettingsFragment(ActivityResultLauncher<Intent> launcher) {
mLauncher = launcher;
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences_server_ws, rootKey);
Expand All @@ -88,7 +88,7 @@ public void onResume() {
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
Preference service = findPreference(getString(R.string.keyWsServer));
service.setOnPreferenceClickListener(preference -> {
getActivity().startActivityForResult(preference.getIntent(), ACTIVITY_SELECT_SERVER_URL);
mLauncher.launch(preference.getIntent());
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package ee.ioc.phon.android.speak.activity;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.ComponentName;
Expand All @@ -41,6 +42,8 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import java.io.FileNotFoundException;
Expand Down Expand Up @@ -114,6 +117,15 @@ public abstract class AbstractRecognizerIntentActivity extends AppCompatActivity

abstract String[] getDetails();

ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
Uri uri = intent.getData();
handleResultByLaunchIntent(intent.getStringExtra(SearchManager.QUERY));
}
});

protected Uri getAudioUri(String filename) {
// TODO: ask the sample rate directly from the recorder
int sampleRate = PreferenceUtils.getPrefInt(PreferenceManager.getDefaultSharedPreferences(this),
Expand Down Expand Up @@ -528,7 +540,7 @@ private void handleResultByLaunchIntent(final List<String> results) {
Intent searchIntent = new Intent(this, DetailsActivity.class);
searchIntent.putExtra(DetailsActivity.EXTRA_TITLE, getString(R.string.dialogTitleHypotheses));
searchIntent.putExtra(DetailsActivity.EXTRA_STRING_ARRAY, results.toArray(new String[results.size()]));
startActivityForResult(searchIntent, ACTIVITY_REQUEST_CODE_DETAILS);
mStartForResult.launch(searchIntent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.app.LoaderManager;
Expand Down Expand Up @@ -63,9 +65,6 @@
*/
public class AppListActivity extends AbstractContentActivity {

private static final int ACTIVITY_SELECT_GRAMMAR_URL = 1;
private static final int ACTIVITY_SELECT_SERVER_URL = 2;

private static final Uri CONTENT_URI = App.Columns.CONTENT_URI;

private static final String[] COLUMNS = new String[]{
Expand All @@ -78,6 +77,33 @@ public class AppListActivity extends AbstractContentActivity {

private long mCurrentAppId;

ActivityResultLauncher<Intent> mStartForResultGrammar = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
Uri grammarUri = intent.getData();
if (grammarUri == null) {
toast(getString(R.string.errorFailedGetGrammarUrl));
} else {
updateApp(mCurrentAppId, App.Columns.GRAMMAR, grammarUri);
}
}
});


ActivityResultLauncher<Intent> mStartForResultServer = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
Uri serverUri = intent.getData();
if (serverUri == null) {
toast(getString(R.string.errorFailedGetServerUrl));
} else {
updateApp(mCurrentAppId, App.Columns.SERVER, serverUri);
}
}
});

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -94,7 +120,7 @@ private boolean menuAction(int itemId, Cursor cursor) {
switch (itemId) {
case R.id.cmAppAssignGrammar:
Intent pickSpeakerIntent = new Intent(AppListActivity.this, GrammarListActivity.class);
startActivityForResult(pickSpeakerIntent, ACTIVITY_SELECT_GRAMMAR_URL);
mStartForResultGrammar.launch(pickSpeakerIntent);
return true;
case R.id.cmAppRemoveGrammar:
Utils.getYesNoDialog(
Expand All @@ -105,7 +131,7 @@ private boolean menuAction(int itemId, Cursor cursor) {
return true;
case R.id.cmAppAssignServer:
Intent intentServer = new Intent(AppListActivity.this, ServerListActivity.class);
startActivityForResult(intentServer, ACTIVITY_SELECT_SERVER_URL);
mStartForResultServer.launch(intentServer);
return true;
case R.id.cmAppRemoveServer:
Utils.getYesNoDialog(
Expand All @@ -126,35 +152,6 @@ private boolean menuAction(int itemId, Cursor cursor) {
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case ACTIVITY_SELECT_GRAMMAR_URL:
Uri grammarUri = data.getData();
if (grammarUri == null) {
toast(getString(R.string.errorFailedGetGrammarUrl));
} else {
updateApp(mCurrentAppId, App.Columns.GRAMMAR, grammarUri);
}
break;
case ACTIVITY_SELECT_SERVER_URL:
Uri serverUri = data.getData();
if (serverUri == null) {
toast(getString(R.string.errorFailedGetServerUrl));
} else {
updateApp(mCurrentAppId, App.Columns.SERVER, serverUri);
}
break;
default:
break;
}
}


private void updateApp(long key, String columnName, Uri uri) {
long id = Long.parseLong(uri.getPathSegments().get(1));
ContentValues values = new ContentValues();
Expand Down
Loading