Skip to content

Commit

Permalink
In the broadcast receiver, implemented handling of FAILED_CODE, inter…
Browse files Browse the repository at this point in the history
…action with cache and storage of data
  • Loading branch information
JaHeRoth committed Jul 24, 2017
1 parent 979c2d5 commit c3e0f18
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
public final class SharedConstants {
public final static String CODE_BUNDLE_KEY = "result_code";
public final static String READ_BUNDLE_KEY = "output_text";
public static final String CACHE_BUNDLE_KEY = "cache_key";
public static final String DESTINATION_BUNDLE_KEY = "destination_filename";
/** Reading succeeded */
public final static int SUCCESS_CODE = 0;
/** Remote reading returned nothing new */
public final static int IDENTICAL_CODE = 1;
/** Reading failed */
public final static int FAILED_CODE = 2;
public final static int FAILED_CODE = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ class AsyncReader extends AsyncTask<Void, Void, String> {
private final String broadcastName;
private final LocationType locationType;
private final String contentLocation;
private final String cacheKey;
private final String destination;

AsyncReader(Context appContext, String broadcastName, LocationType locationType, String contentLocation) {
AsyncReader(Context appContext, String broadcastName, LocationType locationType, String contentLocation, String cacheKey, String destination) {
this.appContext = appContext;
this.broadcastName = broadcastName;
this.locationType = locationType;
this.contentLocation = contentLocation;
this.cacheKey = cacheKey;
this.destination = destination;
}

@Override
Expand All @@ -55,6 +59,8 @@ protected void onPostExecute(String epg) {
Intent intent = new Intent(broadcastName);
intent.putExtra(SharedConstants.CODE_BUNDLE_KEY, epg.isEmpty() ? SharedConstants.FAILED_CODE : SharedConstants.SUCCESS_CODE);
intent.putExtra(SharedConstants.READ_BUNDLE_KEY, epg);
intent.putExtra(SharedConstants.CACHE_BUNDLE_KEY, cacheKey);
intent.putExtra(SharedConstants.DESTINATION_BUNDLE_KEY, destination);
LocalBroadcastManager.getInstance(appContext).sendBroadcast(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -23,20 +22,24 @@
import rothschild.henning.jacob.epg.domain.EPGEvent;
import rothschild.henning.jacob.noriginmedia.R;
import rothschild.henning.jacob.noriginmedia.SharedConstants;
import rothschild.henning.jacob.noriginmedia.model.CacheHandler;
import rothschild.henning.jacob.noriginmedia.model.EPGDataCreator;
import rothschild.henning.jacob.noriginmedia.model.StorageWriter;

public class EPGFragment extends Fragment {

// TODO: Update epg-view regularly, to represent that the time is constantly changing (right now the visual current-time-indicator is stuck until restart)

private static final String TAG = EPGFragment.class.getSimpleName() + ".";
private static final String TAG = EPGFragment.class.getSimpleName();
private static final String EPG_KEY = "epg";
private static final String EPG_FILE_LOCAL = "epg.txt";
// 10.0.3.2 is localhost's IP address in Genymotion emulator (10.0.2.2 in Android emulator)
private static final String EPG_FILE_REMOTE = "http://10.0.3.2:1337/epg"; // localhost
private static final String EPG_BROADCAST_LOCAL = "EPG_BROADCAST_LOCAL";
private static final String EPG_BROADCAST_REMOTE = "EPG_BROADCAST_REMOTE";
private final static String EPG_INPUT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssXXX";

private CacheHandler cache = new CacheHandler();
private EPG epg;
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -85,27 +88,35 @@ public void onResetButtonClicked() {
}

private void fetchEPGData() {
fetchData(EPG_BROADCAST_LOCAL, LocationType.LOCAL, EPG_FILE_LOCAL);
fetchData(EPG_BROADCAST_REMOTE, LocationType.REMOTE, EPG_FILE_REMOTE);
fetchData(EPG_BROADCAST_LOCAL, LocationType.LOCAL, EPG_FILE_LOCAL, EPG_KEY, null);
fetchData(EPG_BROADCAST_REMOTE, LocationType.REMOTE, EPG_FILE_REMOTE, EPG_KEY, EPG_FILE_LOCAL);
}

private void fetchData(String broadcastKey, LocationType locationType, String contentLocation) {
private void fetchData(String broadcastKey, LocationType locationType, String contentLocation, String cacheKey, String destination) {
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter(broadcastKey));
new AsyncReader(getActivity().getApplicationContext(), broadcastKey, locationType, contentLocation).execute();
new AsyncReader(getActivity().getApplicationContext(), broadcastKey, locationType, contentLocation, cacheKey, destination).execute();
}

private void handleReceivedBroadcast(Intent intent) {
Log.d(TAG + "..ReceivedBroadcast", String.valueOf(intent.getIntExtra(SharedConstants.CODE_BUNDLE_KEY, SharedConstants.FAILED_CODE)));
Log.d(TAG + "..ReceivedBroadcast", intent.getStringExtra(SharedConstants.READ_BUNDLE_KEY));
// TODO: Check for identical, handle fail, write to storage, store in variable-cache
try {
setAndRedrawEPGData(EPGDataCreator.fromJSONString(intent.getStringExtra(SharedConstants.READ_BUNDLE_KEY), new SimpleDateFormat(EPG_INPUT_DATE_FORMAT, Locale.UK)));
int resultCode = intent.getIntExtra(SharedConstants.CODE_BUNDLE_KEY, SharedConstants.FAILED_CODE);
String cacheKey = intent.getStringExtra(SharedConstants.CACHE_BUNDLE_KEY);
String read = intent.getStringExtra(SharedConstants.READ_BUNDLE_KEY);
String destinationFilename = intent.getStringExtra(SharedConstants.DESTINATION_BUNDLE_KEY);
handleReceivedBroadcastValues(resultCode, cacheKey, read, destinationFilename);
} catch (Exception e) {
e.printStackTrace();
// I'm not a big fan of these general exception catchers...
}
}

private void handleReceivedBroadcastValues(int resultCode, String cacheKey, String read, String destinationFilename) throws Exception {
if (resultCode == SharedConstants.SUCCESS_CODE && cache.ifNewWillCacheAndTell(cacheKey, read)) {
StorageWriter.writeIfFilenameNotNull(getActivity().getApplicationContext(), destinationFilename, read);
setAndRedrawEPGData(EPGDataCreator.fromJSONString(read, new SimpleDateFormat(EPG_INPUT_DATE_FORMAT, Locale.UK)));
}
}

private void setAndRedrawEPGData(EPGData data) {
epg.setEPGData(data);
epg.recalculateAndRedraw(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package rothschild.henning.jacob.noriginmedia.model;

import java.util.HashMap;

/**
* Created by Jacob H. Rothschild on 24.07.2017.
*/

public class CacheHandler {

private HashMap<String, Object> cache = new HashMap<>();

/**
* Puts key-value pair in cache, if it is NOT identical to something stored there already
* @return Whether this key-value pair is NOT stored in cache
*/
public boolean ifNewWillCacheAndTell(String key, Object value) {
boolean isNew = isNew(key, value);
if (isNew) putInCache(key, value);
return isNew;
}

/** @return Whether value is NOT stored in our cache (and thus tells us something new) */
public boolean isNew(String key, Object value) {
Object cached = cache.get(key);
return cached == null || !value.equals(cached);
}

/** Puts key-value pair in cache */
public void putInCache(String key, Object value) {
cache.put(key, value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package rothschild.henning.jacob.noriginmedia.model;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.IOException;
import java.io.OutputStreamWriter;

/**
* Created by Jacob H. Rothschild on 24.07.2017.
*/

public class StorageWriter {

public static void writeIfFilenameNotNull(Context appContext, @Nullable String filename, String content) throws IOException {
if (filename != null) write(appContext, filename, content);
}

public static void write(Context appContext, @NonNull String filename, String content) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(appContext.openFileOutput(filename, Context.MODE_PRIVATE));
outputStreamWriter.write(content);
outputStreamWriter.close();
}

}

0 comments on commit c3e0f18

Please sign in to comment.