Skip to content

Commit

Permalink
Merge branch 'ahrm:development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisFeldbusch authored Feb 6, 2024
2 parents d9be085 + 1de2f65 commit f83548c
Show file tree
Hide file tree
Showing 28 changed files with 2,035 additions and 338 deletions.
12 changes: 12 additions & 0 deletions android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
<application android:name="org.qtproject.qt.android.bindings.QtApplication" android:hardwareAccelerated="true" android:label="-- %%INSERT_APP_NAME%% --" android:requestLegacyExternalStorage="true" android:allowNativeHeapPointerTagging="false" android:allowBackup="true" android:fullBackupOnly="false" android:icon="@drawable/icon">
<service
android:name="info.sioyek.sioyek.TextToSpeechService"
android:foregroundServiceType="mediaPlayback"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>

<activity android:name="info.sioyek.sioyek.SioyekActivity" android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density" android:label="-- %%INSERT_APP_NAME%% --" android:launchMode="singleInstance" android:screenOrientation="unspecified" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
11 changes: 11 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ apply plugin: 'com.android.application'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'
implementation 'androidx.media:media:1.6.0'
implementation "androidx.media3:media3-exoplayer:1.0.1"
implementation "androidx.media3:media3-ui:1.0.1"
implementation "androidx.media3:media3-common:1.0.1"
implementation 'androidx.media3:media3-session:1.0.1'

}

android {
Expand Down
3 changes: 3 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ org.gradle.parallel=true
# build with the same inputs. However, over time, the cache size will
# grow. Uncomment the following line to enable it.
#org.gradle.caching=true

android.useAndroidX=true
android.enableJetifier=true
187 changes: 182 additions & 5 deletions android/src/info/sioyek/sioyek/SioyekActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,90 @@
import android.util.Log;
import android.content.ContentResolver;
import android.webkit.MimeTypeMap;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.media3.session.MediaController;
import androidx.media3.session.SessionToken;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;

import android.view.Menu;
import android.view.MenuItem;



public class SioyekActivity extends QtActivity{
public static native void setFileUrlReceived(String url);
public static native void qDebug(String msg);
public static native void onTts(int begin, int end);
public static native void onTtsStateChange(String newState);
public static native void onExternalTtsStateChange(String newState);
public static native String getRestOnPause();
public static native void onResumeState(boolean isPlaying, boolean readingRest, int offset);

public static boolean isIntentPending;
public static boolean isInitialized;
public static boolean isPaused = true;

private static SioyekActivity instance = null;

private MediaController mediaController = null;
private SessionToken ttsSessionToken = null;

private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int begin = intent.getIntExtra("begin", 0);
int end = intent.getIntExtra("end", 0);
onTts(begin, end);
}
};

private BroadcastReceiver stateMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra("state");
onTtsStateChange(state);
}
};

private BroadcastReceiver externalStateMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra("state");
onExternalTtsStateChange(state);
//onTtsStateChange(state);
}
};

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);


getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


Intent intent = getIntent();

if (intent != null){
Expand Down Expand Up @@ -93,6 +159,56 @@ public void onCreate(Bundle savedInstanceState){

}

@Override
public void onStart(){
super.onStart();
ttsSessionToken = new SessionToken(getApplicationContext(), new ComponentName(getApplicationContext(), TextToSpeechService.class));
ListenableFuture<MediaController> controllerFuture = new MediaController.Builder(getApplicationContext(), ttsSessionToken).buildAsync();
controllerFuture.addListener(() -> {
try{
mediaController = controllerFuture.get();
}
catch(Exception e){
qDebug("sioyek: could not get media controller");
}
}, MoreExecutors.directExecutor());
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, new IntentFilter("sioyek_tts"));
LocalBroadcastManager.getInstance(this).registerReceiver(stateMessageReceiver, new IntentFilter("sioyek_tts_state"));
LocalBroadcastManager.getInstance(this).registerReceiver(externalStateMessageReceiver, new IntentFilter("sioyek_external_tts_state"));
}

@Override
public void onStop(){

super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(stateMessageReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(externalStateMessageReceiver);
}

@Override
public void onResume(){

isPaused = false;
Intent intent = new Intent(getApplicationContext(), TextToSpeechService.class);
intent.putExtra("resume", true);
startService(intent);

super.onResume();
}

@Override
public void onPause(){
isPaused = true;
String rest = getRestOnPause();

if (rest.length() > 0){
setTtsRestOfDocument(rest);
}

super.onPause();
}

@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
Expand Down Expand Up @@ -164,9 +280,7 @@ public static String getRealPathFromUri(Context context, Uri contentUri) throws
return cursor.getString(column_index);
}
catch(Exception e){
qDebug("sioyek: something happened");
String newFileName= getFile(context, contentUri).getPath();
qDebug("sioyek: new file name = " + newFileName);
return getFile(context, contentUri).getPath();
}
finally {
Expand Down Expand Up @@ -305,7 +419,6 @@ private void processIntent(){
Uri intentUri = intent.getData();
if (intentUri == null){
intentUri = Uri.parse(intent.getStringExtra("sharedData"));
qDebug("sioyek: got message with sharedData=" + intent.getStringExtra("sharedData"));
}
//String realPath = getRealPathFromUri(getApplicationContext(), intentUri);
//Uri newUri = Uri.fromFile(new File(realPath));
Expand All @@ -315,7 +428,6 @@ private void processIntent(){
try{
realPath = getRealPathFromUri(this, intentUri);
//Toast.makeText(this, "trying to open " + realPath, Toast.LENGTH_LONG).show();
qDebug("sioyek: trying to open " + realPath );
setFileUrlReceived(realPath);
}
catch(IOException e){
Expand All @@ -324,4 +436,69 @@ private void processIntent(){
}
return;
}

public void ttsPause(){
runOnUiThread(new Runnable() {
@Override
public void run() {
mediaController.pause();
}
});

}

public void ttsStop(){
runOnUiThread(new Runnable() {
@Override
public void run() {
mediaController.stop();
}
});
}

public void ttsSetRate(float rate){
runOnUiThread(new Runnable() {
@Override
public void run() {
setTtsRate(rate);
}
});
}

public void ttsSetRestOfDocument(String text){
runOnUiThread(new Runnable() {
@Override
public void run() {
setTtsRestOfDocument(text);
}
});
}


public void ttsSay(String text){
Intent intent = new Intent(getApplicationContext(), TextToSpeechService.class);
intent.putExtra("text", text);
startService(intent);

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
mediaController.play();
}
}, 100);
}

public void setTtsRate(float rate){
Intent intent = new Intent(getApplicationContext(), TextToSpeechService.class);
intent.putExtra("rate", rate);
startService(intent);
}

public void setTtsRestOfDocument(String rest){
Intent intent = new Intent(getApplicationContext(), TextToSpeechService.class);
intent.putExtra("rest", rest);
startService(intent);
}

}
Loading

0 comments on commit f83548c

Please sign in to comment.