Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
hoping to fix a rare(*) crash with the image picker -> solution: use …
Browse files Browse the repository at this point in the history
…consistent context application-wide | (*) unable to replicate + no crash log yet - too bad
  • Loading branch information
y20k committed Dec 17, 2015
1 parent ed3a2f6 commit e623f63
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 73 deletions.
13 changes: 8 additions & 5 deletions app/src/main/java/org/y20k/transistor/MainActivityFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class MainActivityFragment extends Fragment {


/* Main class variables */
private Application mApplication;
private Activity mActivity;
private Collection mCollection;
private CollectionAdapter mCollectionAdapter = null;
Expand All @@ -98,8 +100,9 @@ public MainActivityFragment() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// store context
// get activity and application contexts
mActivity = getActivity();
mApplication = mActivity.getApplication();

// set list state null
mListState = null;
Expand Down Expand Up @@ -377,7 +380,7 @@ public void onReceive(Context context, Intent intent) {
}
};
IntentFilter playbackStoppedIntentFilter = new IntentFilter(ACTION_PLAYBACK_STOPPED);
LocalBroadcastManager.getInstance(mActivity).registerReceiver(playbackStoppedReceiver, playbackStoppedIntentFilter);
LocalBroadcastManager.getInstance(mApplication).registerReceiver(playbackStoppedReceiver, playbackStoppedIntentFilter);

// broadcast receiver: player service stopped playback
BroadcastReceiver playbackStartedReceiver = new BroadcastReceiver() {
Expand All @@ -387,7 +390,7 @@ public void onReceive(Context context, Intent intent) {
}
};
IntentFilter playbackStartedIntentFilter = new IntentFilter(ACTION_PLAYBACK_STARTED);
LocalBroadcastManager.getInstance(mActivity).registerReceiver(playbackStartedReceiver, playbackStartedIntentFilter);
LocalBroadcastManager.getInstance(mApplication).registerReceiver(playbackStartedReceiver, playbackStartedIntentFilter);

// broadcast receiver: station added, deleted, or changed
BroadcastReceiver collectionChangedReceiver = new BroadcastReceiver() {
Expand All @@ -397,7 +400,7 @@ public void onReceive(Context context, Intent intent) {
}
};
IntentFilter collectionChangedIntentFilter = new IntentFilter(ACTION_COLLECTION_CHANGED);
LocalBroadcastManager.getInstance(mActivity).registerReceiver(collectionChangedReceiver, collectionChangedIntentFilter);
LocalBroadcastManager.getInstance(mApplication).registerReceiver(collectionChangedReceiver, collectionChangedIntentFilter);

// broadcast receiver: listen for request to change station image
BroadcastReceiver imageChangeRequestReceiver = new BroadcastReceiver() {
Expand All @@ -410,7 +413,7 @@ public void onReceive(Context context, Intent intent) {
}
};
IntentFilter imageChangeRequesIntentFilter = new IntentFilter(ACTION_IMAGE_CHANGE_REQUESTED);
LocalBroadcastManager.getInstance(mActivity).registerReceiver(imageChangeRequestReceiver, imageChangeRequesIntentFilter);
LocalBroadcastManager.getInstance(mApplication).registerReceiver(imageChangeRequestReceiver, imageChangeRequesIntentFilter);

}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/y20k/transistor/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ private void startPlayback() {
// send local broadcast (needed by MainActivityFragment)
Intent i = new Intent();
i.setAction(ACTION_PLAYBACK_STARTED);
LocalBroadcastManager.getInstance(this).sendBroadcast(i);
LocalBroadcastManager.getInstance(this.getApplication()).sendBroadcast(i);
}


Expand All @@ -364,7 +364,7 @@ private void stopPlayback() {
// send local broadcast (needed by PlayerActivityFragment and MainActivityFragment)
Intent i = new Intent();
i.setAction(ACTION_PLAYBACK_STOPPED);
LocalBroadcastManager.getInstance(this).sendBroadcast(i);
LocalBroadcastManager.getInstance(this.getApplication()).sendBroadcast(i);

// retrieve notification system service and cancel notification
NotificationManager notificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.y20k.transistor.helpers;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class CollectionAdapter extends BaseAdapter {
/* Main class variables */
private final LinkedList<String> mStationNames;
private final LinkedList<Bitmap> mStationImages;
private final Context mContext;
private final Activity mActivity;
private Collection mCollection;
private CollectionChangedListener mCollectionChangedListener;
private boolean mPlayback;
Expand All @@ -58,14 +59,14 @@ public interface CollectionChangedListener {


/* Constructor */
public CollectionAdapter(Context context, LinkedList<String> stationNames, LinkedList<Bitmap> stationImage) {
mContext = context;
public CollectionAdapter(Activity activity, LinkedList<String> stationNames, LinkedList<Bitmap> stationImage) {
mActivity = activity;
mStationNames = stationNames;
mStationImages = stationImage;
mCollection = null;
mCollectionChangedListener = null;

loadPlaybackState(context);
loadPlaybackState(mActivity);
}


Expand Down Expand Up @@ -94,7 +95,7 @@ public View getView(final int position, View convertView, ViewGroup parent) {

// create new view if no convertView available
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_collection, parent, false);

holder = new ViewHolder();
Expand Down Expand Up @@ -128,7 +129,7 @@ public View getView(final int position, View convertView, ViewGroup parent) {
@Override
public void onClick(View view) {
StationContextMenu menu = new StationContextMenu();
menu.initialize(mContext, mCollection, view, position);
menu.initialize(mActivity, mCollection, view, position);

// listen for changes invoked by StationContextMenu
menu.setStationChangedListener(new StationContextMenu.StationChangedListener() {
Expand All @@ -152,7 +153,7 @@ public void stationChanged() {
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
loadPlaybackState(mContext);
loadPlaybackState(mActivity);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package org.y20k.transistor.helpers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -30,20 +30,20 @@
public class DialogAddStation {

/* Main class variables */
private final Context mContext;
private final Activity mActivity;


/* Constructor */
public DialogAddStation(Context context) {
mContext = context;
public DialogAddStation(Activity activity) {
mActivity = activity;
}


/* Construct and show dialog */
public void show() {
// prepare dialog builder
LayoutInflater inflater = LayoutInflater.from(mContext);
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
LayoutInflater inflater = LayoutInflater.from(mActivity);
final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

// get input field
View view = inflater.inflate(R.layout.dialog_add_station, null);
Expand All @@ -61,7 +61,7 @@ public void onClick(DialogInterface dialog, int id) {
final String input = inputField.getText().toString();

// download and add new station
StationDownloader stationDownloader = new StationDownloader(input, mContext);
StationDownloader stationDownloader = new StationDownloader(input, mActivity);
stationDownloader.execute();
}
}
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/org/y20k/transistor/helpers/DialogDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package org.y20k.transistor.helpers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
Expand All @@ -35,7 +35,7 @@ public class DialogDelete {


/* Main class variables */
private final Context mContext;
private final Activity mActivity;
private final Collection mCollection;
private final int mStationID;
private StationDeletedListener mStationDeletedListener;
Expand All @@ -48,8 +48,8 @@ public interface StationDeletedListener {


/* Constructor */
public DialogDelete(Context context, Collection collection, int stationID) {
mContext = context;
public DialogDelete(Activity activity, Collection collection, int stationID) {
mActivity = activity;
mStationID = stationID;
mCollection = collection;
mStationDeletedListener = null;
Expand All @@ -58,7 +58,7 @@ public DialogDelete(Context context, Collection collection, int stationID) {

/* Construct and show dialog */
public void show() {
AlertDialog.Builder deleteDialog = new AlertDialog.Builder(mContext);
AlertDialog.Builder deleteDialog = new AlertDialog.Builder(mActivity);

// add message to dialog
deleteDialog.setMessage(R.string.dialog_delete_station_message);
Expand All @@ -70,12 +70,12 @@ public void onClick(DialogInterface arg0, int arg1) {
boolean success = mCollection.delete(mStationID);
if (success) {
// notify the user
Toast.makeText(mContext, R.string.toastalert_delete_successful, Toast.LENGTH_LONG).show();
Toast.makeText(mActivity, R.string.toastalert_delete_successful, Toast.LENGTH_LONG).show();

// send local broadcast
Intent i = new Intent();
i.setAction(ACTION_COLLECTION_CHANGED);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(i);
LocalBroadcastManager.getInstance(mActivity.getApplication()).sendBroadcast(i);

// notify MainActivityFragment
if (mStationDeletedListener != null) {
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/org/y20k/transistor/helpers/DialogError.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package org.y20k.transistor.helpers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -30,15 +30,15 @@
class DialogError {

/* Main class variables */
private final Context mContext;
private final Activity mActivity;
private final String mErrorTitle;
private final String mErrorMessage;
private final String mErrorDetails;


/* Constructor */
public DialogError(Context context, String errorTitle, String errorMessage, String errorDetails) {
mContext = context;
public DialogError(Activity activity, String errorTitle, String errorMessage, String errorDetails) {
mActivity = activity;
mErrorTitle = errorTitle;
mErrorMessage = errorMessage;
mErrorDetails = errorDetails;
Expand All @@ -48,8 +48,8 @@ public DialogError(Context context, String errorTitle, String errorMessage, Stri
/* Construct and show dialog */
public void show() {
// prepare dialog builder
LayoutInflater inflater = LayoutInflater.from(mContext);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
LayoutInflater inflater = LayoutInflater.from(mActivity);
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

// get views
View view = inflater.inflate(R.layout.dialog_error, null);
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/org/y20k/transistor/helpers/DialogRename.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package org.y20k.transistor.helpers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
Expand All @@ -38,7 +38,7 @@ public class DialogRename {


/* Main class variables */
private final Context mContext;
private final Activity mActivity;
private final Collection mCollection;
private final int mStationID;
private String mStationName;
Expand All @@ -52,8 +52,8 @@ public interface StationRenamedListener {


/* Constructor */
public DialogRename(Context context, Collection collection, String stationName, int stationID) {
mContext = context;
public DialogRename(Activity activity, Collection collection, String stationName, int stationID) {
mActivity = activity;
mStationName = stationName;
mStationID = stationID;
mCollection = collection;
Expand All @@ -64,8 +64,8 @@ public DialogRename(Context context, Collection collection, String stationName,
/* Construct and show dialog */
public void show() {
// prepare dialog builder
LayoutInflater inflater = LayoutInflater.from(mContext);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
LayoutInflater inflater = LayoutInflater.from(mActivity);
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

// get input field
View view = inflater.inflate(R.layout.dialog_rename_station, null);
Expand All @@ -83,20 +83,20 @@ public void onClick(DialogInterface arg0, int arg1) {
boolean success = mCollection.rename(mStationID, mStationName);
if (!success) {
// notify the user
Toast.makeText(mContext, R.string.toastalert_rename_unsuccessful, Toast.LENGTH_LONG).show();
Toast.makeText(mActivity, R.string.toastalert_rename_unsuccessful, Toast.LENGTH_LONG).show();
} else {
// notify MainActivityFragment
if (mStationRenamedListener != null) {
mStationRenamedListener.stationRenamed();
}

// check for playback
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mActivity);
boolean playback = settings.getBoolean(PLAYBACK, false);

if (playback) {
// put up changed notification
NotificationHelper notificationHelper = new NotificationHelper(mContext);
NotificationHelper notificationHelper = new NotificationHelper(mActivity);
notificationHelper.setStationName(mStationName);
notificationHelper.createNotification();
}
Expand Down
Loading

0 comments on commit e623f63

Please sign in to comment.