Skip to content

Commit

Permalink
feat: add tts, intent filter and handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
momcilovicluka committed Feb 11, 2024
1 parent ca81218 commit ddd1f62
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 21 deletions.
15 changes: 1 addition & 14 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -33,6 +34,11 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>

<provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.provider.CalendarContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.webkit.URLUtil;
Expand Down Expand Up @@ -57,6 +58,7 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand All @@ -74,6 +76,7 @@ public class AnimeDetailsActivity extends AppCompatActivity {
OkHttpClient client = new OkHttpClient();
ObjectMapper objectMapper = new ObjectMapper();
MusicVideoAdapter musicVideoAdapter;
private TextToSpeech textToSpeech;
private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 1;
private static final int WRITE_CALENDAR_PERMISSION_REQUEST_CODE = 2;

Expand All @@ -86,6 +89,12 @@ protected void onCreate(Bundle savedInstanceState) {
favoritesManager = new FavoritesManager(this);
anime = (Anime) getIntent().getSerializableExtra("anime");

textToSpeech = new TextToSpeech(getApplicationContext(), status -> {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.US);
}
});

ImageView imageView = findViewById(R.id.anime_image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -146,6 +155,16 @@ public void onLoadCleared(@Nullable Drawable placeholder) {
});

TextView descriptionTextView = findViewById(R.id.anime_description);
descriptionTextView.setOnClickListener(v -> {
if (textToSpeech.isSpeaking()) {
textToSpeech.stop();
} else {
String description = descriptionTextView.getText().toString();
textToSpeech.setLanguage(Locale.US);
textToSpeech.speak(description, TextToSpeech.QUEUE_FLUSH, null, null);
}
});

TextView scoreTextView = findViewById(R.id.anime_score);
scoreTextView.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -180,6 +199,15 @@ public void onClick(View v) {
TextView durationTextView = findViewById(R.id.anime_duration);
TextView popularityTextView = findViewById(R.id.anime_popularity);
TextView titleNativeTextView = findViewById(R.id.anime_title_native);
titleNativeTextView.setOnClickListener(v -> {
if (textToSpeech.isSpeaking()) {
textToSpeech.stop();
} else {
String titleNative = titleNativeTextView.getText().toString();
textToSpeech.setLanguage(Locale.JAPANESE);
textToSpeech.speak(titleNative, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
TextView seasonTextView = findViewById(R.id.anime_season);
TextView statusTextView = findViewById(R.id.anime_status);
TextView typeTextView = findViewById(R.id.anime_type);
Expand Down Expand Up @@ -276,6 +304,11 @@ private void fetchMusicVideos(int id) {
handler.post(() -> {
Toast.makeText(this, "Failed to fetch music videos", Toast.LENGTH_SHORT).show();
});
} catch (Exception e) {
e.printStackTrace();
handler.post(() -> {
Toast.makeText(this, "Failed to fetch music videos", Toast.LENGTH_SHORT).show();
});
}
});
}
Expand Down Expand Up @@ -382,4 +415,13 @@ private int getBroadcastDay(String broadcastDay) {
return -1; // Invalid broadcast day
}
}

@Override
protected void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
32 changes: 26 additions & 6 deletions app/src/main/java/com/luka/anidroid/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.luka.anidroid.activity;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
Expand All @@ -30,16 +32,12 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the state of your activity or any data you want to retain
// For example, you can save the selected item id of the BottomNavigationView
outState.putInt("selectedItemId", bottomNavigationView.getSelectedItemId());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore the saved state of your activity or any data
// For example, you can restore the selected item id of the BottomNavigationView
int selectedItemId = savedInstanceState.getInt("selectedItemId");
bottomNavigationView.setSelectedItemId(selectedItemId);
}
Expand All @@ -55,12 +53,22 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

// Set HomeFragment as default fragment
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, favoritesFragment).commit();

bottomNavigationView = findViewById(R.id.bottom_navigation);

// Set home item as selected
bottomNavigationView.setSelectedItemId(R.id.navigation_home);
bottomNavigationView.setSelectedItemId(R.id.navigation_favorites);

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}

bottomNavigationView.setOnItemSelectedListener(item -> {
Fragment selectedFragment = null;
Expand Down Expand Up @@ -114,6 +122,18 @@ public void onSensorChanged(SensorEvent event) {
}
}

void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, searchFragment).commit();
bottomNavigationView.setSelectedItemId(R.id.action_search);
EditText searchEditText = findViewById(R.id.search_view);

searchFragment.searchAnime(sharedText);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ private void fetchAnimeData(List<Anime> animeList, int page) {
jsonArray = new JSONArray(data.toString());
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (NullPointerException e) {
return;
}

for (int i = 0; i < jsonArray.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean onQueryTextChange(String newText) {
return view;
}

private void searchAnime(String query) {
public void searchAnime(String query) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());

Expand Down Expand Up @@ -111,6 +111,8 @@ private void searchAnime(String query) {
jsonArray = new JSONArray(data.toString());
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (NullPointerException e) {
return;
}

for (int i = 0; i < jsonArray.length(); i++) {
Expand Down

0 comments on commit ddd1f62

Please sign in to comment.