Skip to content

Commit

Permalink
Hopefully fix two most common crashes introduced in 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kethsar committed Apr 8, 2018
1 parent 1d5f82e commit fd63cce
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ gradlew.bat
local.properties
build
gradle
app/app.iml
app/app.iml
release/
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ android {
applicationId "io.r_a_d.radio"
minSdkVersion 16
targetSdkVersion 27
versionName '1.2.1'
versionName '1.2.1.1'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionCode 7
versionCode 8
}
buildTypes {
release {
Expand Down
93 changes: 73 additions & 20 deletions app/src/main/java/io/r_a_d/radio/ActivityMain.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.r_a_d.radio;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Rect;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.design.widget.TabLayout;
import android.support.v4.content.res.ResourcesCompat;
Expand Down Expand Up @@ -39,6 +42,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -57,17 +61,19 @@ public class ActivityMain extends AppCompatActivity implements ViewPager.OnPageC
private boolean firstSearchClick = true;
private boolean sendBluetoothMeta = false;
private boolean newsSet = false;
private boolean m_bound = false;
private ViewPager viewPager;
private JSONScraperTask jsonTask = new JSONScraperTask(this, 0);
private DJImageTask djimageTask = new DJImageTask(this);
private String current_dj_image;
public JSONObject current_ui_json;
private JSONObject current_ui_json;
private ScheduledExecutorService scheduledTaskExecutor;
private HashMap<String, Integer> songTimes;
private Requestor mRequestor;
private View searchFooter;
private AudioManager am;
private MediaSessionCompat mMediaSession;
private RadioService m_service;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -110,6 +116,9 @@ public void run() {
mMediaSession = new MediaSessionCompat(this, "RadioMediaSession");
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setActive(true);

if (PlayerState.isServiceStarted())
bindToService();
}

private void maybeUpdateBluetooth() {
Expand Down Expand Up @@ -174,9 +183,11 @@ protected void onDestroy() {
super.onDestroy();

scheduledTaskExecutor.shutdownNow();
unbindFromService();

mMediaSession.release();
}

@Override
protected void onResume() {
super.onResume();
Expand All @@ -192,7 +203,9 @@ public void onPageSelected(int position) {
if(curView != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(curView.getWindowToken(), 0);
if (imm != null) {
imm.hideSoftInputFromWindow(curView.getWindowToken(), 0);
}
}

int pageID = R.id.now_playing_page;
Expand All @@ -216,14 +229,10 @@ public void onPageSelected(int position) {
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
return;
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

@Override
public void onPageScrollStateChanged(int state) {
return;
}
public void onPageScrollStateChanged(int state) { }

public void openThread(View v) {
try {
Expand Down Expand Up @@ -472,10 +481,12 @@ private void performSearch(Integer pageNumber){
searchMsg.setText("Searching...");
if (curView != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(curView.getWindowToken(), 0);
if (imm != null) {
imm.hideSoftInputFromWindow(curView.getWindowToken(), 0);
}
}

String searchURL = String.format(SEARCH_API, query, pageNumber);
String searchURL = String.format(Locale.getDefault(), SEARCH_API, query, pageNumber);
new JSONScraperTask(this, 2).execute(searchURL);
}
}
Expand Down Expand Up @@ -870,42 +881,84 @@ public void closeSideDrawer() {
}

private void playPlayerService() {
Intent i = new Intent(this, RadioService.class);
i.putExtra("action", RadioService.ACTION_PLAY);
startService(i);
if (PlayerState.isServiceStarted() && m_bound) {
m_service.beginPlaying();
}
else {
Intent i = new Intent(this, RadioService.class);
i.putExtra("action", RadioService.ACTION_PLAY);
startService(i);
}
}

private void pausePlayerService() {
Intent i = new Intent(this, RadioService.class);
i.putExtra("action", RadioService.ACTION_PAUSE);
startService(i);
if (PlayerState.isServiceStarted() && m_bound) {
m_service.stopPlaying();
}
else {
Intent i = new Intent(this, RadioService.class);
i.putExtra("action", RadioService.ACTION_PAUSE);
startService(i);
}
}

private void updateNotificationTags() {
Intent i = new Intent(this, RadioService.class);
i.putExtra("action", RadioService.ACTION_UPDATE_TAGS);
startService(i);
if (PlayerState.isServiceStarted() && m_bound && m_service.isForeground())
m_service.updateTags();
}

private void setClipboard(View v) {
String text = ((TextView) v).getText().toString();

android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
clipboard.setPrimaryClip(clip);
if (clipboard != null)
clipboard.setPrimaryClip(clip);
}

public void togglePlayPause(View v) {
if(isDrawerVisible(findViewById(android.R.id.content))) return;

ImageButton img = v.findViewById(R.id.play_pause);

if(!PlayerState.isPlaying()){
img.setImageResource(R.drawable.pause_small);
playPlayerService();
sendBluetoothMeta = true;

if(!m_bound)
bindToService();
} else {
img.setImageResource(R.drawable.arrow_small);
pausePlayerService();
sendBluetoothMeta = false;

if (m_bound)
unbindFromService();
}
}

private void bindToService() {
Intent i = new Intent(this, RadioService.class);
bindService(i, m_connection, BIND_AUTO_CREATE);
}

private void unbindFromService() {
unbindService(m_connection);
m_bound = false;
}

private ServiceConnection m_connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
RadioService.RadioBinder binder = (RadioService.RadioBinder) service;
m_service = binder.getService();
m_bound = true;
}

@Override
public void onServiceDisconnected(ComponentName name) {
m_bound = false;
}
};
}
3 changes: 3 additions & 0 deletions app/src/main/java/io/r_a_d/radio/PlayerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

public final class PlayerState {
private static boolean m_playing = false;
private static boolean m_serivceStarted = false;
private static String m_artist = "";
private static String m_title = "";

public static boolean isPlaying() { return m_playing; }
public static void setPlayingStatus(boolean status) { m_playing = status; }
public static boolean isServiceStarted() { return m_serivceStarted; }
public static void setServiceStatus(boolean status) { m_serivceStarted = status; }
public static String getArtist() { return m_artist; }
public static String getTitle() { return m_title; }

Expand Down
Loading

0 comments on commit fd63cce

Please sign in to comment.